I recently have been having problems to figure out how to accelerate and decelerate the drive train during autonomous for v5 pro text. I want to slowly ramp up or ramp down the speed to make sure that it is consistent. I have tried to split the distance I want the go and redeclare the velocity but then there is a pause in-between the lines of code when I redeclare. Can someone show me a template of how to accelerate or decelerate a motor. I am a brand new programmer that knows barely nothing.
Since you are new, start simple. Get the motor to work the way you want it to without worrying about how elegant the code looks.
In pseudocode, probably you want something like:
setMotorSpeed(10);
wait(20, msec);
setMotorSpeed(15);
wait(20,msec);
setMotorSpeed(20);
wait(20,msec);
// ... etc.
Recall from math/physics that “acceleration” is change in velocity over some period of time. The pseudocode above does precisely that, it changes the motor speed over some period of time.
If you want something really simple, here is an example of how to do a pseudo-trapezoidal motion profile. It’s definitely not written very well, but hopefully it can get the idea across. It’s basically just two P controllers with a limit. A slew controller for acceleration and a P controller for deceleration would also work well as @trontech569 mentioned.
While @Bob132 and @trontech569 provide correct answers for longer-term solutions, you probably need to get your feet wet keeping things very simple to start before researching more involved techniques.
Assuming you are using VexCode, there are a lot of choices to make when commanding a motor to move. For this exercise, you’ll probably want to focus on the method signature vex::motor::spin(directionType dir, double velocity, percentUnits units)
So your code would look something like:
motor m = motor(PORT1, vex::ratio18_1, false); // Or whatever your motor settings are
m.spin(directionType::fwd, 10, velocityUnits::pct);
wait(250, msec);
m.spin(directionType::fwd, 15, velocityUnits::pct);
wait(250, msec);
m.spin(directionType::fwd, 20, velocityUnits::pct);
wait(250, msec);
m.spin(directionType::fwd, 25, velocityUnits::pct);
wait(250, msec);
m.spin(directionType::fwd, 30, velocityUnits::pct);
// etc.
My guess is that you only observe the motor spin at 50 percent power for 100 degrees. You’d need to provide some way for time to pass between setting the velocity to 100 and then changing it to 50 percent.
The startRotateFor that you use is what is called “non-blocking”. As soon as that line of code is hit, it goes onto the next line. This is useful in your drivetrain for making all 4 motors move the same distance at the same time. Unfortunately, when that last one is reached, the brain will immediately run the next setVelocity, without the motors running the full 100 degrees.
I just implemented this into my code and there is still a pause when I redeclare the velocity and when I run the code and after it reaches the point of after the redeclare the velocity
These are fantastic solutions to the OP’s likely real problem, which is “How do I get my robot to move accurately”.
What is not apparent (and with no disrespect intended to the OP) is whether the OP is at a stage in their programming development where concepts such as “Control Loops”, PID, Motion Profiling, etc. are relevant. Lets try to help the OP move from crawling to walking before introducing running…
If PID is the most easy and efficient way to do this could someone show me an example of how to do this with PID? I don’t really understand this because I’m a newer programmer
//this is a constant that you will need to tune
double kP = 0.1;
//error is just the difference between target and current pos
//here I am just getting it from the front left motor, but it doesn't really matter which one you use
double error = targetPos - FrontLeft.position(deg);
//accuracy is how close you want the robot to get to the target; if it is too small it will take too much time to reach the target, but if it's too large it won't be accurate
//this is needed because the robot will pretty much never be exactly at the target position
double accuracy = 5;
//run the loop while the robot is not at target position
while (abs(error) > accuracy){
//update error
error = targetPos - FrontLeft.position(deg);
//calculate output power by multiplying error by kP
double pow = error * kP;
//assign the power to the motors
FrontLeft.setVelocity(pow, pct);
FrontRight.setVelocity(pow, pct);
BackLeft.setVelocity(pow, pct);
BackRight.setVelocity(pow, pct);
FrontLeft.spin();
FrontRight.spin();
BackLeft.spin();
BackRight.spin();
}
//stop when done
FrontLeft.stop();
FrontRight.stop();
BackLeft.stop();
BackRight.stop();