This tutorial is for those who want to use this library with your custom differential drive robot. However, the easiest way to use this library is from a SnappyXO robot kit designed and optimized for use with this library. In this tutorial, the robot requirements and the steps to use the path following library are covered.
A sample robot using the SnappyXO robotics kit have been put together in this page. The robot should have the following:
This sample code uses the ArduinoBlue-library. The user draws a path on the ArduinoBlue app and the robot follows the drawn path. The ArduinoBlue library is not necessary to use this library.
The first step is to set the necessary constants and make the measurements. Set these constants on the code:
ENCODER_LEFT_PIN
- Left encoder output pin.ENCODER_RIGHT_PIN
- Right encoder output pin.BLUETOOTH_TX
- Bluetooth module TX pin.BLUETOOTH_RX
- Bluetooth module RX pin.RADIUS
- Wheel radius in mm.LENGTH
- Base length in mm (distance from the left to right wheel).BAUD_RATE
- The serial monitor and the bluetooth module baud rate. Do not use separate baud rates for the serial and bluetooth module as this may cause interference. MOTOR_SPEED_MAX
- Maximum write value that corresponds to the maximum speed of the motor. This will usually be 255, which is the maximum analog write value. See "Required Customizations" section for clarification.PULSES_PER_REV
- The number of pulses per revolution of the wheel. Note: this is not the number of pulses per revolution of the encoder shaft, which may be different.MIN_PULSE_INTERVAL
- This is the minimum time in microseconds before recording each pulse from the encoder. This is only necessary for encoders that have a tendency to "bounce." Keep in mind the Nyquist theorem. For a motor that has a maximum revolution of 140 RPM with PULSES_PER_REV of 20 PPR, you can work out the minimum pulse interval as follows.(140 rev/s) x (180 pulses/rev) = (25200 pulses/min)
(1/2800 min/pulse) x (6E7 microsecond/min) = (2380 microsecond/pulse)
By Nyquist dividing by two,
1190 microsecond/pulse.
Or simply use this formula to compute this parameter,
MIN_PULSE_INTERVAL in microsecond = 1/(maximum wheel velocity in RPM x PULSES_PER_REV x 2) x 6E7
But don't worry about calculating it! Try using a low enough value (like 200 microsecond) and it should work fine in most cases.
Once the constants have been set. Code the following functions to meet the following specifications. The speed parameter goes from 0 to MOTOR_SPEED_MAX. These methods are callbacks and will be called from the library to move the robot.
setLeftForward(int speed)
- Set the left motor to go forward at speed. setLeftReverse(int speed)
- Set the left motor to go reverse at speed. setRightForward(int speed)
- Set the right motor to go forward at speed. setRightReverse(int speed)
- Set the right motor to go reverse at speed.Using a higher baud rate than the typical 9600 bps is recommended. The baud rate must be the same for both the serial and SoftwareSerial to prevent interference. The default baud rate on the bluetooth module is 9600 bps. To change this, follow these steps:
Once the customization methods are written, the customizations need to be verified. Test the robot using the testEncoders()
and testMotors()
functions.
To test the ArduinoBlue functionality, simply run the the ArduinoBlue basic example code with the correct pins and baud rate.
The example code has kp set to 100 and kd set to 25. These values have been tested with two robots and was found to work well. Thanks to the robustness and stability of the pure pursuit algorithm, a large range of kp and kd values was stable. Of course, your robot may require different PID parameters. If you find that your robot is unstable when following the path, try adjusting them. Virtual path following is similar to line following. You can find many articles devoted to tuning line follower robots.
After the verification is complete, follow these steps to have your robot follow a drawn path!
The triangle represents the robot location and heading and the blue dot is the goal point that the robot is trying to follow.