Distance/speed pid controller

Hello,
Im building a car simulator with ac-motors, and in the code there is a pid controller for setting the speed on the motors, and slow them down when they are getting close to the target position. They need to slow down so they dont go past the target point and have to go backwards again. In the code there is PWM that is the speed (0-255) and GAP is the distance between the actual position and the target position. In the code those are now steps, is it possible (easy way) to get a smooth change like; from Gap 150 to Gap 50 the pwm goes from 255 to 50. without steps like this;

    pwm=255;

    if (gap>150) {  pwm=255;
    }
    else if (gap>100) {  pwm=120; 
    }
    else if (gap>90) {  pwm=100; 
    }
    else if (gap>80) {  pwm=80; 
    }
    else if (gap>70) {  pwm=60; 
    } 
    else if (gap>60) {  pwm=40;
    }
    else if (gap>50) {  pwm=30;

The first test of the value of gap is superfluous

After that, if you divide the value of gap by 10 you could use it as an an index to an array of pwm values

The PID controller does not seem to be involved with slowing the car as it approaches the target. You seem to be coding that without the PID, or bypassing it. What does the PID do?

pwm = map(gap, 150, 50, 255, 50);

map() will give you a smooth range of output values but that may not suit the characteristics of the motors

Hi,
Will the initial speed and mass of the simcar always be the same?

If say the gap is between 80 and 90, if the cars speed is higher in some instances then you will need a different PWM change to slow it down.

The code you have presented is not PID, just a proportional reaction code that assumes a constant deceleration for drops in PWM.

SPACEX do this with their re-landing Falcon craft and I'd love to see the complex PID code they must have.

Tom... :grinning: :coffee: :+1: :australia:

Thanks for the reply! The pwm signal goes to a 0-10v converter, that sets the speed on the VFD on the ac motor. lower pwm is lower speed on the motor. Its not a moving car, its a platform that tilts and rolls with the g-forces :slight_smile: so something like this?;

if (gap>150) {  pwm=255;
    }
    else if (pwm = map(gap, 150, 50, 255, 50));

No. Why do you have a second "if" ?

if (gap>150)
  pwm=255;
else
  pwm = map(gap, 150, 50, 255, 50);

So that when the gap is higher then 150, it dont care about the lower values ? sorry, have not worked so much with coding :smiley:

Perfect! thanks! I will try that :smiley:

You seem to have jumped into the deep end of the swimming pool without taking any swimming lessons.

Is it? What if gap < 50 ?

Yeah, its a code shared on a page for car simulators, and you have to adjust it a bit because it need to fit the hardware you built with. Got it to work, but the steps was a bit hard when I have the ramp time on the motors on 0.1sec. Below 50 is the tolerance. Too near to move. Its in the code, but i did not copy it in the post :slight_smile:

Well, I have to upload tonight to test, but your suggestion makes sence to me :rofl:

So you want the speed/pwm to be zero when gap < 50? If so, you need to make sure the code will do that, otherwise it might get stuck at 50 or even go negative.

Here is that part of the code. In this code the tolerance is 25, so i guess the lowest number in the gap calculation you wrote should be 25. Then it goes slower until it gets to 25, and then shuts off. Probably dont need BreakingDistance at all if i use your suggestion with the map function, so i will try to set it lower or 0.

 int Tol=25; 


  int gap;
  int pwm;
  int brakingDistance=50;

  targetPos=constrain(targetPos,potMini+brakingDistance,potMaxi-brakingDistance);

  gap=abs(targetPos-actualPos);

  if (gap<= Tol) {
    motorOff(numMot); 

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.