Hi everyone! I am pretty much a novice in programming and really wanted to incorporate slew rate code in our programming so that the robot doesn’t suddenly stop from a high speed causing a rapid voltage drop and motor damage. Are there any useful links that could help me understand the basic mechanics of slew rate coding? Thanks!
Big shout out to @jpearman All this is is his handy work!
What is slew rate:
Slew rate is a tool to limit the swings up and down of current sent to your motor. This helps the PTC not trip and keep your robot functioning and you happy. So if you say you wan tot send it to 127, the slew rate code says not so fast. I will take you to 127 but over a measured set of steps.
You have two variables to worry about - how big each step is, and how long between steps. Vex motors via the Cortex don’t react all that fast, so I recommend 20ms steps. Then you can play with how big each step is. Too small and you will notice a difference in how your robot speeds up and slows down, too big and you still get trips. If you are over taxing your motor, you still get trips. Slew rate helps, but it can;t eliminate when you are doing more than you are supposed to with these poor little motors.
So how we implement slew rate is via a background task and instead of using motor] directly, we call our new global variable motorReq]meaning motor request. Just remember to turn on the slew rate task in auton and driver control. Doing one and not the other could have disastrous consequences of not moving.
Background reading for you and the code:
Slew rate first time occurrence was in this long great thread: (right to slew rate post, click up top to see all the rest)
https://vexforum.com/t/sprocket-and-chain/14951/1
A second post about measuring current and showing the real effects slew rate has on limiting the spikes which drive the PTC nuts.
https://vexforum.com/t/estimating-motor-current/21703/1
https://vexforum.com/t/estimating-269-motor-current-based-on-h-bridge-models/21876/1
https://vexforum.com/t/estimating-motor-current-part-3/22231/1
That leads us into some further discussion…
https://vexforum.com/t/motor-torque-speed-curves-rev2/21868/1
Hi there! Thanks for this information, it was a really helpful insight into the workings of the code. Would it be possible for you to show a simple example of this code where slew rate is implemented on a motor in user-control? Thanks!
Something like this. Start the task, and use motorReq] instead of motor] in usercontrol.
StartTask(slewRate);
while (true)
{
motorReq[motor1] = VexRT[Ch1];
wait1Msec(20);
}
My library has a toggle for motor slew in its custom motor setup. Here is the function that deals with motor slew.
void
motorSet (int iPort, int iSpeed) {
int iOutput, iError;
if (bAccelCurve[iPort]) {
switch (bTrueSpeed[iPort]) {
case NONE: iError = iSpeed - motor[iPort]; break;
case LINESPEED: iError = trueSpeed(iPort, iSpeed) - motor[iPort]; break;
case JORDAN: iError = jTrueSpeed(iSpeed) - motor[iPort]; break;
}
if (abs(iError) >= 4)
iOutput += sgn(iError) * 3;
else
iOutput = iSpeed;
delay(1);
} else {
switch (bTrueSpeed[iPort]) {
case NONE: iOutput = iSpeed; break;
case LINESPEED: iOutput = trueSpeed(iPort, iSpeed); break;
case JORDAN: iOutput = jTrueSpeed(iSpeed); break;
}
}
iOutput = clipNum(iOutput, 127, -127);
motor[iPort] = bMotorReverse[iPort] ? -iOutput : iOutput;
}
What this is doing, is it uses the value you set as a desired value. It finds the error (iSpeed - motor[iPort]) and multiples the signum of the error by 3. And it += the signum of error * 3 to the motor, making it slowly increase its power until it gets to the power you set it to go to.
It is more or less just a P controller that += sgn(error) * 3
If you need more help with this, feel free to send me a message
Here is the library I created for my team: GitHub - VTOW/SpurFlysLibrary: 21 Library
Thanks everyone! It makes sense now