Building Cheapest ROS2 Robot using ESP32 — Part 2— Testing & Calibration

RoboFoundry
9 min readOct 4, 2023

--

In Part1 of this article series, we built the mechanical and electronic hardware for the 2WD robot and completed the wiring of the electronics components. In this article, we will take the next steps to upload the various tests based on Linorobot2_hardware stack to test the motors, encoders and IMU sensor by running various tests.

One of the most important parts of configuring the linorobot2_hardware is to configure myrobot_config.h with correct values for various parameters. Some of these parameters can be measured and some of the parameters will need to be discovered by running some tests.

Here is the list of parameters we need to configure for the robot to work properly and where we can get these from:

Motor Driver and IMU parameters:

#define LINO_BASE DIFFERENTIAL_DRIVE // this defines our robot as 2WD diff drive

#define USE_GENERIC_2_IN_MOTOR_DRIVER // this defines we are using L298N motor driver

#define USE_MPU6050_IMU //this defines we are using MPU6050 IMU
#define SDA_PIN 21 // this is the ESP32 pin connected to SDA pin on IMU
#define SCL_PIN 22 // this is the ESP32 pin connected to SCL pin on IMU

Motor parameters:

#define MOTOR_MAX_RPM 160       // this comes from DFRobot product wiki page
#define MAX_RPM_RATIO 0.9 // we are setting max RPM to be 90% of 160
#define MOTOR_OPERATING_VOLTAGE 6 // self explanatory
#define MOTOR_POWER_MAX_VOLTAGE 6 // self explanatory
#define MOTOR_POWER_MEASURED_VOLTAGE 5.4 // actual voltage measured from battery
#define COUNTS_PER_REV1 960 // from DFRobot product wiki page
#define COUNTS_PER_REV2 960 // from DFRobot product wiki page
#define COUNTS_PER_REV3 240 // this doesn't matter as robot is 2WD
#define COUNTS_PER_REV4 240 // this doesn't matter as robot is 2WD
#define WHEEL_DIAMETER 0.152 // measured using calipers - in meters
#define LR_WHEELS_DISTANCE 0.271 // measured using calipers - in meters
#define PWM_BITS 8 // value for ESP32
#define PWM_FREQUENCY 30000 // value for ESP32

That’s pretty much it, the remaining values in myrobot_config.h are pin numbers for motor PWM pins and encoder pins which are from wiring diagrams in Part 1 article.

Common Setup steps for running tests

  1. Make sure your robot is propped up and front wheels are not touching the ground
  2. ESP32 is plugged into your laptop/computer using a USB to micro-USB cable. Also, make sure you have followed the steps in this article to ensure you have added a Udev rules file for ESP32 so you can flash to ESP32 memory, without it you’ll have to change the write permission everytime it is connected.
  3. Battery power for motors is turned on and L298N is showing leds ON
  4. Make sure you have installed the command line version of PlatformIO following these instructions

Test #1 — Confirming Motors can spin

This is first test and simplest one at that. All we are trying to make sure is we can spin the motors. This test will confirm that ESP32 is able to correctly address each motor, spin it in forward and backward direction and stop.

Make sure you’ve followed 1–5 steps in common setup steps section. Once you are ready follow these steps.

  1. Clone the github repo [git clone https://github.com/robofoundry/linorobot2_hardware_hippo_esp32_fix.git]
  2. Make sure to update myrobot_config.h file with values appropriate for you

Follow the command line steps below as needed:

cd ~
git clone https://github.com/robofoundry/linorobot2_hardware_hippo_esp32_fix.git linorobot2_hardware
cd linorobot2_hardware

~/linorobot2_hardware is the root directory of our robot hardware project.

These are the main directories under this root directory:

├── calibration — contains calibration routines
├── config — contains header files including myrobot_config.h
├── docs
├── firmware — main firmware program that will run the robot final stage
├── test_motors — contains motor test routines
└── test_sensors — contains IMU test routines

The original linorobot2_hardware code only had two tests spin and sample in calibration tests. I have added two more tests — ticks and test.

ticks — allows you to measure/confirm ticks per revolution of any motor you are using for your robot

test — runs the PID test at constant PWM value to maintain constant speed by running feedback loop by measuring current rpm, compute the required PWM to keep speed constant based on kinematics calculations and continues doing that in a loop for 20 secs.

Run these commands to test the motors:

cd ~/linorobot2_hardware
cd test_motors
# upload the test motors program to ESP32, make sure ESP32 is connected to your computer
pio run --target upload -e myrobot

You’ll see the progress percentage as the program is flashed to ESP32 finally showing 100% complete. At this point the program to test motors is flashed on to ESP32 and we need to run the serial monitor to run the commands:

pio device monitor -b 115200

# you'll see output like this

WIFI connected
IP address: 192.168.1.128
MOTOR1 FWD RPM 66.8 0.0 0.0 0.0
MOTOR1 FWD RPM 83.1 0.0 0.0 -32.4
MOTOR1 FWD RPM 83.5 0.0 0.0 -33.7
MOTOR1 FWD RPM 83.3 0.0 0.0 -33.9
MOTOR1 FWD RPM 83.6 0.0 0.0 -33.7
MOTOR1 FWD RPM 83.9 0.0 0.0 -33.7
MOTOR1 FWD RPM 84.3 0.0 0.0 -33.7
MOTOR1 FWD RPM 84.3 0.0 0.0 -33.7
MOTOR1 SPEED 0.67 m/s 4.95 rad/s STOP 0.291 m
MOTOR2 FWD RPM 36.5 57.5 0.0 -33.7
MOTOR2 FWD RPM 0.0 70.7 0.0 -73.2
MOTOR2 FWD RPM 0.0 70.9 0.0 -33.7
MOTOR2 FWD RPM 0.0 70.9 0.0 -33.7
MOTOR2 FWD RPM 0.0 70.7 0.0 -33.6
MOTOR2 FWD RPM 0.0 70.6 0.0 -33.7
MOTOR2 FWD RPM 0.0 70.8 0.0 -33.7
MOTOR2 FWD RPM 0.0 70.8 0.0 -33.4
MOTOR2 SPEED 0.56 m/s 4.16 rad/s STOP 0.160 m

Test #2 — Confirming Counts per Revolution for Motor

Although we are lucky that DFRobot has a pretty decent product wiki page for their FIT0450 encoder motors and they have listed the spec for number of ticks for encoder per revolution to be 960 [120:1 gearbox ration * 8 pulses per round = 960 ticks per revolution of main motor shaft], it is possible that you are working with a motor that does not provide any documentation and you need to figure this out yourself.

I have added a simple test in the calibration routines which can be run by entering ‘ticks’ and <Enter> at serial console, we will see this step by step. But the basic idea is simple — run the motor at slow enough speed and visually count the number of revolutions and let the calibration routine print out the total number of ticks. Then divide total number of ticks by visually counted number of revolutions and you can get fairly close average number of ticks per revolution number. If you run this test, it will be a number pretty close to 960 for DFRobot motor.

How to run the test:

In addition to common setup steps we need one more

  1. Stick a piece of tape on both wheels at a location you can visually track the position of the tape e.g. at top of wheel, that allows you to track the number of revolutions of each wheel when tape crosses the same point while rotating.

Run these commands to test the motors:

cd ~/linorobot2_hardware
cd calibration
# upload the test motors program to ESP32, make sure ESP32 is connected to your computer
pio run --target upload -e myrobot

You’ll see the progress percentage as the program is flashed to ESP32 finally showing 100% complete. At this point the program is flashed on to ESP32 and we need to run the serial monitor to run the commands:

pio device monitor -b 115200

# you'll get prompt like this
Sampling process will spin the motors at its maximum RPM.
Please ensure that the robot is ELEVATED and there are NO OBSTRUCTIONS to the wheels.

Type 'spin' and press enter to spin the motors.
Type 'sample' and press enter to spin the motors with motor summary.
Type 'ticks' and press enter to measure ticks per revolution of the motors.
Type 'test' and press enter to spin the using cmd_vel and observe PID in action.
Press enter to clear command.

# at this point you can choose any of 4 tests but we will enter - ticks<ENTER>
# you'll see output like this
Please ensure that the robot is ELEVATED and there are NO OBSTRUCTIONS to the wheels.
ticks test will run each motor at slow speed one motor at a time.
count the number of revolutions visually and make a note of final tick count for each motor along with number of revolutions observed.
Number of ticks for each motor can be calculated with following formula:
ticks per rev = final tick count/number of revolutions counted
Press enter to continue to tick count test.

FRONT LEFT - M1: ....................
final_tick_count for FRONT LEFT - M1: = 23615.00
=============
FRONT RIGHT - M2: .....................
final_tick_count for FRONT RIGHT - M2: = 20586.00
=============
# the motor will run one at a time for 20 secs and allow you to count revolutions
# at the end of test it will print total tick count for each motor
# do the simple math total_tick_count/number_of_revolution_observed = ticks/rev for motor

Test #3 — Confirming PID control works for motors

Run these commands to test the motors:

cd ~/linorobot2_hardware
cd calibration
# upload the test motors program to ESP32, make sure ESP32 is connected to your computer
pio run --target upload -e myrobot

You’ll see the progress percentage as the program is flashed to ESP32 finally showing 100% complete. At this point the program is flashed on to ESP32 and we need to run the serial monitor to run the commands:

pio device monitor -b 115200

# you'll get prompt like this
Sampling process will spin the motors at its maximum RPM.
Please ensure that the robot is ELEVATED and there are NO OBSTRUCTIONS to the wheels.

Type 'spin' and press enter to spin the motors.
Type 'sample' and press enter to spin the motors with motor summary.
Type 'ticks' and press enter to measure ticks per revolution of the motors.
Type 'test' and press enter to spin the using cmd_vel and observe PID in action.
Press enter to clear command.

# at this point you can choose any of 4 tests but we will enter - test<ENTER>
# it will runs the PID test for each motor for 20 secs
# you'll see output like this for each motor

req_rpm:: 62.82 current_rpm:: 62.77 pwm:: 203
req_rpm:: 62.82 current_rpm:: 62.77 pwm:: 203
req_rpm:: 62.82 current_rpm:: 62.77 pwm:: 203
req_rpm:: 62.82 current_rpm:: 63.38 pwm:: 201
req_rpm:: 62.82 current_rpm:: 62.77 pwm:: 202
req_rpm:: 62.82 current_rpm:: 63.38 pwm:: 201
req_rpm:: 62.82 current_rpm:: 62.77 pwm:: 202
req_rpm:: 62.82 current_rpm:: 62.77 pwm:: 202
req_rpm:: 62.82 current_rpm:: 62.77 pwm:: 202
STOP motor
=============

# as you can see above, the required rpm and current rpm are continuously
# changing and PWM is getting adjusted to keep the speed to required rpm

Test #4 — Confirming IMU data read works correctly

Run these commands to test the motors:

cd ~/linorobot2_hardware
cd test_sensors
# upload the test motors program to ESP32, make sure ESP32 is connected to your computer
pio run --target upload -e myrobot

You’ll see the progress percentage as the program is flashed to ESP32 finally showing 100% complete. At this point the program is flashed on to ESP32 and we need to run the serial monitor to run the commands:

pio device monitor -b 115200

# you'll see output like this as you move the robot around different axes
WIFI connected
IP address: 192.168.1.128
ACC -0.33 -0.00 9.27 GYR 0.00 0.00 0.00 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC -0.36 0.02 9.26 GYR 0.00 0.00 0.00 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC -0.34 -0.02 9.26 GYR 0.00 0.00 0.00 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC -1.30 0.83 9.48 GYR 0.71 -0.09 0.71 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC 1.70 3.16 7.74 GYR 0.05 -0.75 0.05 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC -3.97 3.11 7.40 GYR -0.04 0.89 -0.04 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC 4.57 5.34 6.79 GYR -0.51 0.58 -0.51 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC -1.38 -3.68 9.84 GYR 2.08 -0.88 2.08 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC 7.34 3.82 3.95 GYR -1.13 -0.72 -1.13 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC 4.64 3.65 8.16 GYR -0.19 -2.14 -0.19 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC 2.05 2.00 8.24 GYR 0.94 -3.46 0.94 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m
ACC -1.23 0.76 8.97 GYR -0.04 0.05 -0.04 MAG 0.00 0.00 0.00
BAT 0.00V RANGE 0.00m

That’s it for running different tests on robot. In next article we will fire up ROS2 code for the robot and observe the topics published in Rqt and also watch the live data being published in the RViz2 and run robot in Gazebo simulator.

References

--

--