Motor Driver (Grove I2C)
Grove - I2C Motor Driver V1.3
The package for the Grove I2C Motor Driver V1.3 from Seeed Studio is created with catkin create pkg PKG_NAME [--catkin-deps [DEP [DEP ...]]]:
1 2 3 4 5 6 7 | |
The package depends on the two ROS client libraries rospy and roscpp.
To control the two motors the package will use the geometry_msgs/Twist message.
The interface to the motor driver is done with the Python library from DexterInd
which is a rewerite of the Seeed Studio Arduino library.
This library requires the following two python libraries
These libraries should be installed with pip3, Python's package manager:
1 2 | |
Note
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
Connect the I2C port of the motor driver to the I2C port 1 of the Raspberry Pi 4 B. Two things matter for this connection: the voltage of the two signals and a common ground.
- Signal voltage: The motor driver's I2C bus works with 5 V (Seeed's specifications). The Raspberry Pi's GPIO pins work with 3.3 V and aren't 5 V tolerant (Raspberry Pi's documentation of the 40-pin GPIO header). So the data line SDA and the clock line SCL go through a bidirectional I2C level shifter, for example Adafruit's BSS138 level converter. The level shifter connects the 3.3 V signals on its low side with the 5 V signals on its high side.
- Common ground: Each board measures the voltages on SDA and SCL against its own ground. On DiffBot, the two boards have separate supplies: a power bank for the Raspberry Pi and four AA batteries for the motor driver (Power Supply). So the grounds of the Raspberry Pi, the level shifter and the motor driver have to be connected.
Switch off both supplies before you connect the wires: shut down the Raspberry Pi and unplug its power bank, and take the batteries out of the motor driver's holder. With Adafruit's converter as the example, each row is one wire. The pins of the Raspberry Pi are those of its 40-pin header (I2C pinout), and the level shifter's pins are named as printed on its board:
| From | To |
|---|---|
| Raspberry Pi pin 1, 3.3 V | Level shifter LV |
| Motor driver's I2C connector, VCC | Level shifter HV |
| Raspberry Pi pin 6, GND | Level shifter GND, on the LV side |
Level shifter GND, on the HV side |
Motor driver's I2C connector, GND |
| Raspberry Pi pin 3, SDA | Level shifter A1 |
Level shifter B1 |
Motor driver's I2C connector, SDA |
| Raspberry Pi pin 5, SCL | Level shifter A2 |
Level shifter B2 |
Motor driver's I2C connector, SCL |
The level shifter connects A1 with B1 and A2 with B2 itself. Don't connect these pins with a wire.
Each side of the level shifter gets its reference voltage from the board on that side: 3.3 V from the Raspberry Pi, and 5 V from the motor driver.
The motor driver makes these 5 V from its batteries, with its own regulator.
Leave the jumper J4 on the motor driver board in place: it connects the regulator to the VCC pin of the I2C connector, and Seeed asks for the jumper when 5 V should be supplied to the I2C bus (Seeed's hardware overview).
Connect this VCC pin only to the level shifter's HV, never to the Raspberry Pi, which has its own supply.
The GND pin of the I2C connector is connected to the board's ground, with or without the jumper.
Keep the motor driver powered whenever the Raspberry Pi has power. Without its batteries, the motor driver would get current from the Raspberry Pi's 3.3 V side through the level shifter and the SDA and SCL wires. These wires end at the board's microcontroller, an ATmega8 (Seeed's schematic), and each of its pins has a protection diode to its supply line (ATmega8 datasheet, I/O Ports). The level shifter keeps the two sides apart only when the Raspberry Pi's side is off (Philips application note AN97055, section 2.3.4). So change the batteries in this order:
- Stop the motors and shut down the Raspberry Pi.
- Unplug the Raspberry Pi's power bank. After a shutdown, the Raspberry Pi keeps power on its header while the power bank is plugged in (Raspberry Pi's bootloader documentation,
POWER_OFF_ON_HALT). - Replace all four batteries.
- Plug the power bank in again, which starts the Raspberry Pi.
This wiring follows the specifications of the two boards. It isn't tested on a DiffBot yet.
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:
1 2 3 4 5 6 7 8 9 10 | |
Test Motor Driver
Test the motor driver by running one of the python files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
Troubleshooting
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 9 10 11 12 13 14 | |
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:
1 2 3 4 5 6 7 8 9 10 | |
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:
1 2 3 4 5 6 7 8 9 10 | |
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.
https://en.wikipedia.org/wiki/Differential_wheeled_robot
http://wiki.ros.org/differential_drive
After the node has been implemented, we need to build the workspace with catkin build:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
As the final note of the build output suggests, we have to source the setup.bash files in the devel space.
1 | |
To make the ranger node executable we have to modify the ranger.py file:
1 | |
Comments
Comments are GitHub Discussions, shown with giscus. Loading them connects to giscus.app (hosted by Vercel, USA) and GitHub (USA); you need a GitHub account to comment.