smbus SMBus (System Management Bus) is a subset from the I2C protocol
These libraries should be installed with pip3, Python's package manager:
12
pip3 install RPi.GPIOpip3 install smbus
Note that this will install these packages system wide. This is ok because they are installed on the Raspberry Pi which is dedicated to
operate for this purpose. For a development environment it is best practice to use a python virtual environment like
venv and install the packages inside it.
Connection
Afterwards the I2C port of the motor should be connected to the I2C port 1 of the Raspberry Pi 4 B.
Don't forget to remove the jumper on the motor driver board which would provide power to the Pi.
However, it is also not required to connect VCC and GND of the I2C connector.
Only the SDA (data) and SCL (clock) wires are required.
Make sure to set the address with the dip switches on the motor driver to 0x0f because this is the default address used
in the library files.
To test the physical I2C connection use i2cdetect described in Hardware Interfaces:
The output should list 0f in the address table:
If you get errors like the following, make sure the I2C cables from the motor driver to the
Raspberry Pi are connected (see Hardware Interfaces for more infos)
and use the RESET button on the motor driver.
1 2 3 4 5 6 7 8 91011121314
fjp@ubuntu:~/git/2wd-robot/ros/src/control$ sudo python grove_i2c_motor_driver.py
Traceback (most recent call last): File "grove_i2c_motor_driver.py", line 68, in <module> m.MotorSpeedSetAB(100,100) File "grove_i2c_motor_driver.py", line 57, in MotorSpeedSetAB bus.write_i2c_block_data(self.I2CMotorDriverAdd, self.MotorSpeedSet, [MotorSpeedA,MotorSpeedB])IOError: [Errno 110] Connection timed outfjp@ubuntu:~/git/2wd-robot/ros/src/control$ sudo python grove_i2c_motor_driver.py
Traceback (most recent call last): File "grove_i2c_motor_driver.py", line 68, in <module> m.MotorSpeedSetAB(100,100) File "grove_i2c_motor_driver.py", line 57, in MotorSpeedSetAB bus.write_i2c_block_data(self.I2CMotorDriverAdd, self.MotorSpeedSet, [MotorSpeedA,MotorSpeedB])IOError: [Errno 121] Remote I/O error
Try pressing the RESET button and release it right before executing one of the scripts.
You should also be able to detect the motor driver with i2cdetect -y 1:
As you can see the address of the motor driver is detected at 0x0f.
In case of the following output, where every address of the I2C bus seems to be taken,
it is most likely that the SDA (data) and SCL (clock) signal cables are switched:
To solve this make sure the SDA and SCL cables are connected to the right pins on the Raspberry Pi.
See Hardware Interfaces for more infos.
Another solution is to restart the Raspberry Pi while making sure that the motor driver is powerd on by connecting it to the battery pack.
ROS Node for Motor Driver
To use the available library of the Grove I2C motor driver in ROS we need to create a wrapper node, called motor_driver.
The node subscribes to the topic /2wd_robot/cmd_vel which is of type Twist message
from the geometry_msgs header.
To send commands to the motor the . These topics can be published with nodes from the navigation stack or with rostopic pub for test purposes.