How to control speed and distance in PI controller?

Hello,

I am using an Arduino UNO R3 to control my robot with 2 DC geared motors equipped with Hall encoders. I am trying to implement a PI controller to control the speed and distance travelled by the robot by counting the number of pulses from each motor encoder. But i can't figure out how to use it to control the distance travelled by the robot.

Here is my attempt for a PI speed controller, with the loop executed by a timer interrupt every 20 ms and the average_speed variable is used as the measured/feedback variable in the PI controller.

float setPoint = 0; //global variable definition

void speedPIcontroller() //loop executed every 20 ms by timer interrupt
{
  average_speed = (pulseleft + pulseright) / 2.0; //average pulses from each motor encoder
  distance = average_speed;
  PI_PWM = kp_speed * (setPoint - average_speed) + ki_speed * (setPoint - distance);
}

I have 2 questions:

  1. To control the robot average speed, only a change in the setPoint value is needed, right? So, if the setPoint value is changed to 100, then the PI_PWM output will try to maintain the average speed of the robot to 100 pulses per 20 ms. Am i correct?

  2. How to control the robot displacement? Can i use the same PI controller or do i need to use a separate PID controller? If yes, then which type? PI or PD or PID?

If you have the PI controller working successfully to control the speed do you really also need PI to control distance.

Why not just use the pulse count to measure distance. If you want to stop at a specific point then get your program to implement successively lower speeds as the distance point approaches.

...R