C++ Pro - Help with Slew Rate

I’m new to VCS and C++ and i am trying to transition from the old vex programming. Right now I’m trying to make a slew rate void or function since there is no Jpearmen library anymore :(, and I’m getting an error. I figured the error is because it doesn’t know what a string is. Also I’m trying to pass a string into parameters and I’m its not working.
This is the code:

void slew_rate(std::string MotorName, int end_power, int time, int destination ){

int deg = MotorName.rotation(vex::rotationUnits::deg);
while (destination != deg){
    int power = 5;
    MotorName.setVelocity(power,vex::percentUnits::pct);
    MotorName.spin(vex::directionType::fwd);

    while (power != end_power){

        MotorName.setVelocity(power,vex::percentUnits::pct);
        power += 5;
        vex::task::sleep(time);
        } 
 }

}

As you can see, the MotorName would be name of one of the motors that im trying to set the slew rate for but it doesnt recognize them. i don’t want to make a slew rate function for every motor. Please help.

You definitely can’t use a


string

in such fashion.

You probably want to pass a pointer to the desired motor and act on the object pointed to by that pointer.

void doStuff(vex::motor *motorObject) {
  // ...
  motorObject.spin(vex::directionType::fwd);
  // ...
}
void otherFunction() {
  vex::motor someMotor(/* ... */);
  doStuff(&someMotor);
}

More about pointers:
https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm

@Barin Thank you, I will try this

void slew_rate(std::string MotorName, int end_power, int time, int destination ) {
    int deg = MotorName.rotation(vex::rotationUnits::deg);
    while (destination != deg) {
        int power = 5;
        MotorName.setVelocity(power,vex::percentUnits::pct);
        MotorName.spin(vex::directionType::fwd);

        while (power != end_power) {
            MotorName.setVelocity(power,vex::percentUnits::pct);
            power += 5;
            vex::task::sleep(time);
        }
    }
}

I don’t have V5 yet, but there are a few things that are unlikely to work in your code.


while (destination != deg)

destination never changes, and neither does deg, so this will loop forever.

You only have slewing for acceleration, what happens if you want to slow down?

You use


!=

a lot, you should use


>

and


<

instead (in case it overshoots because power is not a multiple of 5)

Your code currently looks like it tries to make the motor go to a destination, is this a drive or slew rate function?

Your power will start at 5, then increase until the max power (good :D)
Then if it isn’t at the destination, it will go down to 5 and increase again (bad :P)

EDIT: I don’t think you need slew rate control on the V5 smart motors, but here’s a sample of what can be used for Cortex systems:


int slewMotorReq[kNumbOfTotalMotors];
task slewMotorsTask() {
    int i, diff;
    while (true) {
        // loop through all motors
        for (i = 0; i < kNumbOfTotalMotors; i++) {
            // diff is the amount that needs to be added to get to the target
            diff = slewMotorReq* - motor*;
            // add the diff, but not more than that motor's slew rate
            motor* += min(abs(diff), 5) * sgn(diff);
        }
        delay(15); // change 5 power every 15ms
    }
}


@DarkMatterMatt Thank you, i will fix that rn

@DarkMatterMatt Oh and for the destination, Im trying to say : dont stop spinning until its equal and when it is equal it will stop.

Yes, but it must be exactly equal in order to stop. The code only checks every few milliseconds, and in that time the motor might have overshot the destination, in which case it still is not equal

@DarkMatterMatt Thanks, ill no longer use the unequal.

Btw, slew rate helps with jolts in the autonomous, it will help it to be more consistent.

Do you have any suggestions oh how to make the power decrease?

If you are making slew rate code:
There’s an example of what I did in my first post, so you could adapt it to V5/C++
Alternatively you can always just copy paste the increasing code and reverse the signs to make it decreasing code. Then have an


if (end_power > 0)

do the increasing code, else do the decreasing code.

If you are making autonomous driving code:
You might want to look into PID controllers, they’re designed to slow the motors down as it approaches the destination. I use a simple library made by @jmmckinney, https://github.com/JMMcKinneyWPI/GyroPIDLibrary

Updated repo.

This is pretty much deprecated btw. I haven’t found the time to do any development on the V5 platform yet, and there’s a lot of features and utilities in there that need to be retooled and replaced to be relevant with the new platform.

Yeah but the old one works fine, the new one seems very similar but return a clue between -1 and +1 (so needs to be multiplied by 127).

And I agree it isn’t useful for V5, but it’s a functional library that could be a starting point for Lukyan. There’s a very good introduction to PIDs here

So the new lib has support for some more precise features, like feed-forward constants and removes some clunky features like my integral band method for preventing integral windup, which has been replaced with a soft cap on the summing component and a sign check on the error and sum, which has seemed to provide a more responsive control loop in more situations. That change is part of the reason why the new output range is between -1.0 and 1.0 (inclusive). The other major reason was due to the potential of integration with libs like okapi or anything more general purpose I may work on down the road. I’d strongly recommend switching to the new implementation if you can.