# 6 Motor Drive: Motor Speed Pairing

Heyo, I was wondering if there was a way for me to set the speed of all the motors in my drive train equal to the speed of the slowest motor. Below is a little code I came up with to show my thought process, but I feel like there is a better way to go about this.
double right1speed = Right1.velocity(percent);

double right2speed = Right2.velocity(percent);

double right3speed = Right3.velocity(percent);

double left1speed = Left1.velocity(percent);

double left2speed = Left2.velocity(percent);

double left3speed = Left3.velocity(percent);

if (right1speed > left1speed){

Right1.setVelocity(left1speed,percent);

}

else if(left1speed > right1speed){

Left1.setVelocity(right1speed,percent);

}

if (right1speed > right2speed && right3speed >right2speed){

Right1.setVelocity(right2speed,percent);

Right3.setVelocity(right2speed,percent);

}

else if (right2speed > right1speed && right3speed > right1speed){

Right2.setVelocity(right1speed,percent);

Right3.setVelocity(right1speed,percent);

}

else if(right1speed > right3speed && right2speed > right3speed){

Right1.setVelocity(right3speed,percent);

Right2.setVelocity(right3speed,percent);

}

else{

Brain.Screen.print(â€śgoodâ€ť);

}

if (left1speed > left2speed && left3speed >left2speed){

Left1.setVelocity(left2speed,percent);

Left3.setVelocity(left2speed,percent);

}

else if (left2speed > left1speed && left3speed > left1speed){

Left2.setVelocity(left1speed,percent);

Left3.setVelocity(left1speed,percent);

}

else if(left1speed > left3speed && left2speed > left3speed){

Left1.setVelocity(left3speed,percent);

Left2.setVelocity(left3speed,percent);

}

else{

Brain.Screen.print(â€śgoodâ€ť);

}

1 Like

I believe what you are looking for is the min function. It takes two parameters and returns the lowest of the two values. You can find more information about it here std::min - cppreference.com.

1 Like

So, would I use something like int finalMotorSpeed = std::min({all my motor speeds});
Motor.setVelocity(finalMotorSpeed, percent);?

Also, the function running this, would I need to set it up as a task so that it can run simultaneously with the rest of the code?

Yes, this should work.

This would not be necessary, you can just put this within your driver control code. But it might be useful if you have it in a separate thread.

However, I notice a problem with doing this. While the robot is still accelerating and has not reached max speed, this code will make it so that all the motors will go at the slow speed of that one motor. This means that your robot will effectively never reach any useful speeds. I would not recommend doing this, but Iâ€™m interested in seeing what you are going to use this for.

1 Like

Yeah, I am thinking about using it as a bound on my PID code, but that would entail that the motors are at their upper limit. How to get them their initially will be a problem, but Iâ€™ll give it some thought. If anyone else has any suggestions I would be very grateful. And thank you so much for your help already!

Actually, I had a thought, could I make it to where it uses a gyro sensor to find the acceleration of the bot, and this only goes into effect when the acceleration is less that some small value?

That would be pretty cool if you managed to make it work. I found that the acceleration values were not too reliable, but itâ€™s definitely worth trying out.

1 Like

It may be possible to use the tracking wheels that the PID algorithm uses to write a function for acceleration from the rotation of the encoders.
Something like Velocity = encoder.velocity(rpm);
PrevVelocity = Velocity;
Acceleration = Velocity - PrevVelocity;

My questions for you would be:
why do you want to do this?
what is your drive train configuration?

2 Likes

6 motor, three chained together on each side, two have a gear on them going to a wheel. This code would mostly be for balancing the left and right sides, as well as keeping the three tied motors at the same rate. Itâ€™s mostly to stop turning and keep values constant.

since your three wheels on each side are chained together, they should have the same speed. Any difference is due to an error.

You can take the average of speed by `leftspeed=(Right1.velocity(rmp) + Right2.velocity(rmp) + Right3.velocity(rmp))/3`. You can use PID to correct the speed difference between left and right, which I think you already mentioned in the post.

2 Likes

Yeah, honestly thatâ€™s probably best. Iâ€™ll keep this in mind for later, if I ever design a chassis with unchained motors. And honestly, this could be a good idea for any two motor lift systems.

If you have 3 motors chained together, this sounds like a perfect use case for motor groupsâ€¦

1 Like

For VexCode Pro I thought motor groups only went for two motors?

The sky is the limit

3 Likes