Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

How to make the robot to move in straight line using encoders

i m amateur to programming …
doing project on “path follower robot” in which i have to give path virtually that part is done
robot is also going on path but it is not going straight so it is distracting from path….

my robot is pololu 3pi+.


It is normal for brushed DC motors to have some unit-to-unit variation in speed, so a differential drive robot like the 3pi+ will not keep a straight path unless you account for it properly. You might be able to calibrate for it by empirically determining an offset between the two speeds for your particular combination of motors. Alternatively, you could use the encoders for closed-loop feedback (as your title suggestions) so you control the actual speed of the motors instead of just the voltage to them.

We do not currently have any examples for doing closed-loop speed control like that for the 3pi+, but using PID control for each motor would be my recommendation. This PID speed control tutorial on YouTube might be a good starting point for learning how to do that.


How to make the robot (3pi +) to move in straight line and return back to it’s home position.

And also how to record the values of position and speed into a graph format. It would be great if someone could help me out asap.


I merged your new thread into your old one since the questions are very similar and seem to be about the same topic.

Returning back to the starting position would probably involve keeping track of how many encoder counts you get while moving forward, then either turning around or reversing until the same number of encoder ticks are counted. It sounds like you might be describing behavior of a “dead reckoning” robot, so you might find this blog post about Paul’s dead reckoning robot helpful.

As far as logging the position and speed values, there are many ways you could do something like that, but the 3pi+ does not have any special data logging features built-in. Depending on the amount of data you are trying to gather, you might be able to just store the coordinates of the data you want to graph in arrays, then have the 3pi+ print the data after you connect it to your computer via USB. From there, you could import the data to a spreadsheet and graph it. Another option might be to use some kind of wireless device (e.g. Bluetooth, WiFi, or RF) that you can use to send the data wirelessly to another device like your computer or a separate microcontroller. Alternatively, you could look into adding some kind of data logging device to the 3pi+ that you can use to store the data separately, such as a micro SD card carrier. However, each I/O pin on the 3pi+ is already being used for some other purpose, so you would need to disable features to get something like that to work.