Adjusting motor speed using sensors

my team is attempting to slow down a motor Using the C++ code and i don’t know what sensor to use or if I even need a sensor to slow down the motor also another issue is that we are using buttons to move this motor so it needs to be based on the position of the motor, can you please help if you can.

Do you want to adjust the speed for testing?

What I am asking for is any sample code that we can use to limit the speed of the motor based on its position

The built in encoders of the V5 also have this control loop (albeit PID and not just P) built in. The function to use it will be different depending on if you’re using VexCode, PROS, or RobotMesh but its probably something along the lines of move_To(Location); This will probably be the easiest method to implement what you want.

There are two main Vex sensors that allow you to measure a motor’s position: encoders and potentiometers.

Encoders allow for continuous rotation. You can spin them to infinity and beyond. The downside is that when you boot up your robot it resets back to 0. So if its on an arm, you have to start the arm in the same position every time you turn on the robot (even when testing).

Most encoders work by watching the light change as the axle spins, and when the encoder notices a change it counts a ‘tick’. Most encoders have two light sensors in them, so they can use the two ‘ticks’ to find which way the axle is spinning.

A good use case is if you want to measure how far the robot has moved (as the drivetrain wheels will rotate around 360 degrees multiple times). It is also useful since V5 motors have an integrated encoder, so you don’t need any external sensors if you want to track an arm or claw.

Potentiometers are just a fancy type of resistor. Due to this, they will be absolute in its reading. This means you don’t have to start the mechanism from the same spot. But its range of motion is limited. I believe from memory that the Vex potentiometers can only measure up to 270 degrees of motion. If
you exceed that range of motion the potentiometer no longer updates the value. It won’t explode sadly (you may break the hard limits though which can be annoying). They are great for arms and claws since those generally don’t move more than 270 degrees. You would not want to use these for your drivetrain though, since your wheels there will move over the 270 degree limit many times over.

If you want code that will slow down the motor as it reaches its target until it stops there (and stays there), a good and popular solution is a proportional loop. A proportional loop is a simplified version of a PID loop, work works well enough for most cases. It works by checking to see how close your sensor value is to your target value, aka an error. As the sensor gets closer to its target, the error will get smaller. As the sensor gets far away, the error gets larger. If we set the motor speed to the value of the error, the same traits apply. As the sensor grows away from its target, the motor will go faster. If the sensor is closer to the target, the motor will slow down. The problem we will notice though, is that the value of error is normally in a different range from the motor move functions. This is where proportional (the namesake) comes into play. By multiplying error by a constant proportional, we can make the error fit into the ranges of the motor move function better.

Pseudo code below (idk what programming environment you are using so you gotta do that part yourself).

double kP = 1.3; //our proportional value, will be different for your program
int target = 1000; //What we want the sensor value to be, say the spot on our lift
bool pLoopActive = true; //make true when you want it to turn on

while(pLoopActive){
 int error =  target - getSensorValueFunction();
 double motorSpeed = error * kP;

 setMotorSpeedFunction();
}

Your proportional value (kP) will probably different. A good way to find what the value of kP should be is to run the proportional loop with kP at like 1. Then if its too slow, make kP larger. If the motor starts missing its target or looping in a circle, make kP smaller.

2 Likes