Skip to content

Arduino code to control a differential drive robot via ROS Twist messages using a NodeMcu or ESP8266

License

Notifications You must be signed in to change notification settings

Reinbert/ros_diffdrive_robot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Control software for differential drive smart car chassis using a NodeMcu, Arduino and the Robot Operating System (ROS)

This software is intended to control a 4 wheel differential drive robot via ROS messages using a NodeMcu or ESP8266. The software will respond to geometry_msgs/Twist messages (precisely Twist.linear.x and Twist.angular.z) and generate PWM signals accordingly. Programmed with Arduino and the ESP8266 Core. If you have questions or improvements create a new issue here.

front view

Parts needed:

  • Smart car chassis with at least 1 motor on each side, like this: Smart car chassis 4WD
  • NodeMCU (I'm using version 1.0, but v3 is out already)
  • L298N motor driver like this: L298N
  • Battery pack or other energy source. (I use 8x 1.2 Volt rechargable batteries)
  • Breadboard jumper wires
  • Linux workstation to run ROS
  • Wifi access point (or use the access point from the NodeMcu)
  • Gamepad (optional)

Software:

Connections:

  • Battery (+) to L298N +12V or VCC
  • Battery (-) to L298N GND
  • L298N GND to NodeMcu GND
  • L298N +5V to NodeMcu Vin
  • Remove jumpers from L298N ENA and ENB
  • L298N ENA to NodeMcu D1
  • L298N IN1 to NodeMcu D2
  • L298N IN2 to NodeMcu D3
  • L298N IN3 to NodeMcu D5
  • L298N IN4 to NodeMcu D6
  • L298N ENB to NodeMcu D7
  • L298N OUT1&2 to left motor (connect in parallel if >1)
  • L298N OUT3&4 to right motor (connect in parallel if >1)

ROS Installation:

  • Install and configure ROS if you haven't done so yet (Installation Tutorial). I'm using Kinetic Kame on Ubuntu 16.04, but feel free to change the following terminal commands to your version.
  • Install rosserial_server via terminal: sudo apt install ros-kinetic-rosserial-server
  • Install teleop_twist_keyboard sudo apt install ros-kinetic-teleop-twist-keyboard
  • Or if you have a gamepad install teleop_twist_joy: sudo apt install ros-kinetic-joy ros-kinetic-teleop-twist-joy

Arduino IDE:

  • Go to "Sketch/Include Library/Manage Libraries...", search for "ESP8266" and install that library.
  • Use the same method to install library "rosserial" or if that doesn't work follow this tutorial.
  • If you want to use your own Wifi remove the line containing #define ACCESS_POINT_SSID "SMARTCAR" and add your own Wifi credentials into setupWiFi()
  • Find out the IP address of the computer you intend to use as the ROS serial server and change IPAddress server(192, 168, 4, 2); accordingly. (You may need to connect to the SMARTCAR Wifi network first if you are using access point mode.)
  • If you are using a different micro controller than the NodeMcu v1.0 find out what its highest possible PWM value is and change 'MAX_PWM' and 'MIN_PWN' accordingly. (Max PWM on Arduino is 255, while on a NodeMcu it's 1023)
  • Flash nodemcu-ros-car.ino onto the NodeMcu

ROS Operation:

  • Launch rosserial socket node: roslaunch rosserial_server socket.launch
  • Launch gamepad node in a new terminal: roslaunch teleop_twist_joy teleop.launch joy_config:=__insert gamepad type__ or
  • Keybord node: rosrun teleop_twist_keyboard teleop_twist_keyboard.py

This should suffice to make the robot car connect to your Wifi and rosserial server and follow twist commands from your gamepad.

Alternative Teleop:

I've written this small ROS node (teleop_twist_gamepad) which allows to control this robot like a car in a racing video game. Pressing Right Trigger will accelerate the car, while pressing Left Trigger will decelerate and make the robot go backwards. Steering is done via the Left Stick. In the default configuration you are required to hold down the deadman button (button A on xbox360) for safety reasons. The node should work with any joystick that is supported by Linux. However, I only tested it with an Xbox360 wireless controller and therefore only provided a config file for that controller. Feel free to create and send me yours.

Installation:

cd ~/catkin_ws/src
git clone https://github.com/Reinbert/teleop_twist_gamepad
cd ..
catkin_make
source devel/setup.bash

Launch:

roslaunch teleop_twist_gamepad teleop.launch

Launch teleop AND a rosserial socket at the same time. With this you don't need to launch a rosserial_server in a second terminal:

roslaunch teleop_twist_gamepad teleop_serial.launch 

How to disable the deadman button: (Only intended for toy cars. USE AT YOUR OWN RISK!)

roslaunch teleop_twist_gamepad teleop.launch enable_deadman:=false 
roslaunch teleop_twist_gamepad teleop_serial.launch enable_deadman:=false

Troubleshooting:

  • Open Serial Monitor in Arduino IDE and check for Wifi and ROS connection output.
  • The LED on the NodeMcu should turn on (only) if its connected to both Wifi and ROS.
  • teleop_twist_joy and teleop_twist_gamepad use an enable (deadman) button which needs to be pressed while using the gamepad. Find out which one in the config.
  • Test gamepad with jstest /dev/input/js0
  • Make sure the teleop node is outputting commands: rostopic echo /cmd_vel
  • If the LED on the L298N isn't lit, then the batteries aren't connected properly or the jumper for the 5V regulator isn't placed.

Images:

The cables and battery packs are a little bit whacky, but it's sufficient for the proof of concept. full view front view detail connections NodeMcu and L298N detail cables to motors

NodeMcu GPIO pins

NodeMcu pinout

Example L298N motor driver

L298N description

About

Arduino code to control a differential drive robot via ROS Twist messages using a NodeMcu or ESP8266

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages