This guide will help you set up and run the ROS2 Python node for the YAHBOOM G1 Tank with Raspberry Pi 4.
Ubuntu 22 for Raspberry Pi: Installed and set up on your Raspberry Pi 4. Ubuntu installation guide.
ROS2 Humble-desktop: Installed and sourced following the official ROS2 installation guide.
Colcon: Required for building and testing ROS packages. Install with:
sudo apt install python3-colcon-common-extensions
For more information on installing Colcon, please refer to the official Colcon installation guide.
RPI-GPIO: Library to control Raspberry Pi GPIO channels. Install using:
sudo apt install python3-rpi.gpio
Setting Permissions: If you encounter a "RuntimeError: No access to /dev/mem", adjust permissions for GPIO access on your Raspberry Pi:
sudo chmod 777 /dev/mem
sudo chmod 777 /dev/gpiomem
After adjusting, reboot your Raspberry Pi.
Clone the Repository: Navigate to your home directory, then run:
mkdir ~/ros2_ws/
cd ~/ros2_ws
git clone https://github.com/jhiggason/yahboomg1tank.git .
Build the ROS2 Package:
Compile the project using colcon
:
colcon build
Source the Workspace: After building, source the workspace:
source ~/ros2_ws/install/setup.bash
Running the Node: To activate and run your ROS2 node, execute:
ros2 run tank_control_pkg tank_control
Upon successfully setting up and running the node, the YAHBOOM G1 Tank should now respond to the twist messages provided by the turtlesim
teleop or any other control method you've implemented.
Running the Turtlesim Node To activate and run your turtlesim node, execute:
ros2 run turtlesim turtle_teleop_key --ros-args --remap /turtle1/cmd_vel:=/cmd_vel
Use your arrow keys on your keyboard to move the tank around.
For any issues or to contribute, engage with this GitHub repository.
The TankControl
class, found in tank_control.py, is designed to control a tank-like robot using ROS2 and Raspberry Pi GPIO pins. It subscribes to ROS2 topics to receive motion commands and controls the robot's movement accordingly.
Initialization: The class initializes upon instantiation, setting up GPIO pins for the left and right motors and configuring PWM (Pulse Width Modulation) for speed control.
ROS2 Subscriptions: It subscribes to the /cmd_vel
topic to receive Twist
messages, containing linear and angular velocities for the tank's movement.
Motor Speed and Direction Control: The class processes incoming Twist
messages to calculate the speeds for the left and right wheels. It applies a correction factor to the linear velocity and amplifies the angular velocity. The speeds are normalized and used to control the motor direction and speed through GPIO pins.
Safety Features: An inactivity timer is implemented to stop the motors if no command is received for a short duration (currently 0.1 seconds), ensuring safety.
GPIO Pins Configuration: self.left_motor_pins
and self.right_motor_pins
define the GPIO pins for the motors.
Message Processing: The subscription_callback
method processes Twist
messages, applying a correction factor and amplifying the angular velocity to determine motor speeds.
Inactivity Timer: The timer_callback
method checks for command inactivity, stopping the motors if no command is received within 0.1 seconds.
Motor Pins: Modify self.left_motor_pins
and self.right_motor_pins
to match your GPIO configuration.
Subscription Topic: Change the topic name in create_subscription
if needed.
Control Logic: Adjust the subscription_callback
method for different tank motion logic.
Inactivity Duration: Alter the inactivity duration in timer_callback
for different safety requirements.
Careful consideration should be given when modifying GPIO configurations to prevent damage to the Raspberry Pi or connected components.
For more examples, please refer to the Documentation
In this example we are going to use an Xbox Series X/S controller, connected to bluetooth on the raspberry pi, to send twist MSGs to the /cmd_vel topic. This will control our tank.
*Setting Up Xbox Controller on Ubuntu 22 for ROS 2
Disable Secure Boot: If you are doing this on a system other than raspberry pi-please disable secure boot.
Del
, F2
, F12
, or Esc
.Update Xbox Controller:
Install Xbox Driver for Ubuntu:
sudo apt update
sudo apt install dkms git
xpadneo
from its repository:
git clone https://github.com/atar-axis/xpadneo.git
cd xpadneo
sudo ./install.sh
Install Joystick Tools:
sudo apt update
sudo apt install jstest-gtk joystick
Pair Xbox Controller via Bluetooth:
sudo bluetoothctl
agent on
default-agent
scan on
pair XX:XX:XX:XX:XX:XX
trust XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:XX
scan off
exit
Install ros-humble-teleop-twist-joy
:
sudo apt update
sudo apt install ros-humble-teleop-twist-joy
Adjust xbox joy settings:
cd /opt/ros/humble/share/teleop_twist_joy/config/
sudo nano xbox.config.yaml
Edit the file and change the values to 0 and 1.
enable_button: 0 # Button A
enable_turbo_button: 1 # Button B
Then Ctrl+O then CTRL+X
Run Teleop Twist joy
ros2 launch teleop_twist_joy teleop-launch.py joy_config:='xbox'
Now you should be able to control your tank with the xbox controller after pressing the "A" button on the controller and moving the left stick.
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Distributed under the MIT License. See LICENSE for more information.
Your Name - Jeff Higgason - jeffh@opensar.net
Project Link: https://github.com/jhiggason/YahBoomG1Tank