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 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?
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 so something like this?;
if (gap>150) { pwm=255;
}
else if (pwm = map(gap, 150, 50, 255, 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
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);