# Slew Rate Questions

I understand the purpose of slew rate, to accelerate and decelerate your motors in a nice smooth fashion so it doesn’t trip the PTC controller. After reading some threads and looking at some code, I have some questions:

1. Is making slew rate into a task the most efficient way?
2. Is the change of motor values something that is different for everyone, similar to that of PID constants.
3. How do people use only one array to keep the requested values of motor speeds for all motors?
4. How do you actually calculate the change? Do you take the difference of the requested motor value and current motor value and multiply it by some constant?
Thank you in advance.

Yes, the slew rate control needs to run in a continuous loop. You could make it a function in a loop, but it’s much easier to make it a separate task.

I don’t know about this question, I believe it depends on the application, e.g. lift / drive / etc.

Create an array with 10 elements and use it instead of

``````
motor[x]

``````

. This works because the

``````
port1 ... port10

``````

names are enumerated,

``````
port1 = 0, port2 = 1, port3 = 2, port 10 = 9

``````
``````
int motorRequestPower;
motorRequestPower[port1] = 40;

``````

Loop through the array, if the power != the requested power, increase/decrease the power.

``````
sgn(variable)

``````

returns the sign (-1 for negative, 1 for positive) of the variable.

``````
int powerDifference;
int powerIncrement = 5; // change this number
while (true) {
for (int i=0; i<10; i++) {
powerDifference = motorRequestPower* - motor*;
if (powerDifference) {
if (abs(powerDifference) > powerIncrement)
motor* += sgn(powerDifference) * powerIncrement;
else
motor* = motorRequestPower*;
}
}
delay(20); // run at 50Hz
}

``````

Code is untested.*****