# m3pi -Line following PID trouble at low speed

Hi there, I am working on maze solving with the m3pi robot but sometimes (at low speed around 20%) the robot oscillates in straight line. I just add something to the line following pid function in order to fix it and as it works pretty well I am happy to share it with you.
When you compute the new speed of each wheel :

``````        // Compute new speeds
right = speed+power;
left  = speed-power;``````

you must take in count the actual maximum speed of the robot (MAX) then you will not slow down to much the robot and then make it oscillates :

``````        // Compute new speeds
right = speed+(power*MAX);
left  = speed-(power*MAX);``````

It’s a small contribution but anyway I hope it will help some people .

ps: I am sorry for my English but I am not a native English speaker.

Hi.

Thanks for sharing your findings. What number is MAX? Can you share/publish your code from within the mbed compiler so we can see exactly what you are doing?

- Ryan

MAX is the maximum speed you want for the robot, and also MIN is the minimum speed. For example if you put MAX = 0.8 and MIN = 0.2 the speed of the robot will be between 20% and 80% of full speed.
Here is the line following pid code I use:

``````#include "mbed.h"
#include "m3pi.h"

m3pi m3pi;

// Minimum and maximum motor speeds
#define MAX 1.0
#define MIN 0

// PID terms
#define P_TERM 1
#define I_TERM 0
#define D_TERM 20

int main() {

m3pi.locate(0,1);
m3pi.printf("Line PID");

wait(2.0);

m3pi.sensor_auto_calibrate();

float right;
float left;
float current_pos_of_line = 0.0;
float previous_pos_of_line = 0.0;
float derivative,proportional,integral = 0;
float power;
float speed = MAX;

while (1) {

// Get the position of the line.
current_pos_of_line = m3pi.line_position();
proportional = current_pos_of_line;

// Compute the derivative
derivative = current_pos_of_line - previous_pos_of_line;

// Compute the integral
integral += proportional;

// Remember the last position.
previous_pos_of_line = current_pos_of_line;

// Compute the power
power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ;

// Compute new speeds
right = speed+power*MAX;
left  = speed-power*MAX;

// limit checks
if (right < MIN)
right = MIN;
else if (right > MAX)
right = MAX;

if (left < MIN)
left = MIN;
else if (left > MAX)
left = MAX;

// set speed
m3pi.left_motor(left);
m3pi.right_motor(right);

}
} ``````

I found it here: mbed.org/users/chris/code/m3pi_L … ource.html

Thanks for posting your code. Just a note to anyone trying it, the [ b] [/b] on the relevant line are BBCodes and should be removed if you are using the code.

- Ryan

Sorry about that, I just remove it.