Robot does not move in straight line

I recently purchased a Pololu 3pi robot to use as part of a senior engineering project. The code below is what I programmed onto the robot after I inserted the batteries, installed the IDE, etc.

#include <pololu/3pi.h>
#include <stdio.h>

int main()
{
	print("Hello");
	delay(5000);
	while(1)
	{
		red_led(0);
		green_led(1);

		delay_ms(100);

		red_led(1);
		green_led(0);
		
		delay_ms(100);

		set_m1_speed(50);
		set_m2_speed(50);
	}

	return 0;
}

As you can see, it is a very simple program that gives the same speed to both motors. However, after I successfully programmed the robot and set it on the ground, I noticed that it would always veer slightly to the left. Any insight into why this is happening would really help me.

Thanks.

Hello.

The simple explanation is that no two motors are completely identical; give them the same voltage and you will notice that they move at slightly different speeds. Even if you have well matched motors, driving in a completely straight line is very difficult to do without feedback. Usually you would use encoders for this, which are sensors that monitor wheel rotation to let you know how fast your right and left wheels are turning at any given moment. You would then constantly adjust the speed of the motors to keep them in sync. Another solution is to use feedback from the ground, such as following a straight line, for example.

Often the simplest solution, however, is to use trial and error to characterize how your motors are behaving differently so that you can compensate for the problem. Essentially, you would tell your slower motor to move at speed 50 and your faster motor to perhaps move at speed 47 in order to get the 3pi to move in a straight line. Does this make sense?

- Ben

I recently purchased a Pololu 3pi robot to use as part of a senior engineering project. The code below is what I programmed onto the robot after I inserted the batteries, installed the IDE, etc.

code:

int main() {
	initialize();
	while(1)
	{
		set_motors(40, 40);
	}
}

As you can see, it is a very simple program that gives the same speed to both motors. However, after I successfully programmed the robot and set it on the ground, I noticed that it would always veer slightly to the left. Any insight into why this is happening would really help me.

Hello.

The original poster asked the same question, and I answered it (see the post above yours). Do you have any questions about the answer?

- Ben

Hello!

No, I just want to check if the method set_motors() is for the same answer,

thx!

The function set_motors() just internally calls set_m1_speed() and set_m2_speed(). The answer is still that you should not expect two motors to turn at exactly the same speed when supplied with the same voltage. Typically, you will need to use closed-loop feedback if you want to drive in a straight line, but you might be able to get close by using trial and error to determine what speeds result in moving in a straighter line (e.g. set_motors(41, 40)).

- Ben