# Using PID to keep same distance from 2 sides

hi i am working on a Robot that solves a maze (with walls), i have two ultrasonic sensors and 2 servo motors so i want the robot to go in the center of the 2 walls with full speed without touching them, my INPUT is the distances from both sides, the SETPOINT is 7.5cm from them, how can i controle the OUTPUT(the two servos) ???

Don't use PID. Just get the two values and get it to drive one motor faster if the distance is less on that side.

Once you have that working, then look at PID.

I tried it but it does not work perfectly at is shoul be, after awhile it lost the middle

Maybe you made a mistake in the program.

Post it, using code tags and describe what went wrong.

``````void mesure(){
dd=ultrasonicDroite.Ranging(CM);
dg=ultrasonicGauche.Ranging(CM);
da=ultrasonicDevant.Ranging(CM);
if(da>10 && dg<20 && dd<20){
if (dd<=dg-1 && dd>=dg-3)r=11;/*here the condition*/
if (dd<dg-3)r=12;
if(dd>=dg+1 && dd<=dg+3)r=21;
if (dd>dg+3) r=22;
if (dd==dg)  r=3;
/// and this the actions
if(r==21){
PoussD();
}
//// and here is my PoussD function

void PoussD(){
//Serial.println(" POUSSD  ");
servoD.writeMicroseconds(1600);
servoG.writeMicroseconds(1300);
delay(10);
servoD.writeMicroseconds(1725);
servoG.writeMicroseconds(1300);
delay(30);
servoD.writeMicroseconds(1500);
servoG.writeMicroseconds(1500);
``````

I didnt type the whole code is too much, just an exemple of the correction part

Where do all those "magic numbers" come from?
They make no sense to me, and clearly, to the robot either, since the program doesn't work.

the numbers are the distances that i get from the sensors (dd: the distance from right, dg: the distance from left)
and based on the values i make an action to ensure that the robot remais in the middle of the two walls

But you only take action in one particular case, when r==21.

And why that particular action? More magic numbers.

A different approach:

loop:
l = left_distance();
r = right_distance();
if (l>r) steer_a_bit_left();
if (l<r) steer_a_bit_right();
if (l == r) steer_straight();
end;

Then you need to adjust the amount of steering (PID, eventually).

I didnt post the code of all cases, that was just a simple exemple, there are many cases and each case with function, the code will be too long to post it.
i read about the PID but i cant use it, the INPUT will be the distance from each side and the OUTPUT is the speed 2 servo motors an d the set SET POINT is 7.5cm from each side, how can put all this together using the PID and its library.
Thank you for your interest

there are many cases and each case with function, the code will be too long to post it.

By now, it should be obvious to you that this is the wrong approach.

You can use PID.

1. Use (r-l) as the process variable
2. Use 0 as the setpoint
3. Output to a function steer(), where steer(0) = straight ahead.

Sorry but i didnt get the whole idea, i have the SET POINT and the INPUT but how can i controle the OUTPUT
what command i shoud give to the servo motors ??
Thank you in advance.

what command i shoud give to the servo motors ??

That is something for you to figure out.

I am working on it but if someone could help me with it wit will be good.
Tank you for your help