# Dividing Speed on Robot Drive

I am trying to divide the speed of the motors…
by 1/2 when Button 1 of Stick 1 is pressed;
by 3/4 when Button 1 of Stick 2 is pressed; and
by 1/4 when Button 1 of Both sticks are pressed.

Where is the best place to add the divide function.

``````
// Drive
if(J_stick1.GetRawButton(1) == true)
R_myRobot.TankDrive(J_stick1, J_stick2);
else
if(J_stick2.GetRawButton(1) == true)
R_myRobot.TankDrive(J_stick1, J_stick2)*3/4;
else
if(J_stick1.GetRawButton(1) == true && J_stick2.GetRawButton(2) == true)
R_myRobot.TankDrive(J_stick1, J_stick2)/4;
else
R_myRobot.TankDrive(J_stick1, J_stick2);

``````

It needs to be placed in the “IsOperatorControl()” loop where TankDrive() is being called now. call the GetY() method of the joysticks to get the value and multiply it by your scale. I would use decimals like 0.25. For example:

R_myRobot.TankDrive(J_stick1->GetY() * 0.25, J_stick2->GetY() * 0.25);

When you don’t want to scale keep
R_myRobot.TankDrive(J_stick1, J_stick2);

In our example below, you forgot to multiply by 0.5 when button one was pressed.

You don’t need “== true” in your checks for GetRawButton(). It returns a bool

You should have a scaleFactor variable and have the buttons modify it.

``````
bool j1Btn1 = J_stick1->GetRawButton(1);
bool j2Btn1 = J_stick2->GetRawButton(1);
float scaleFactor = 1.0;
if (j1Btn1 && j2Btn1)
{
scaleFactor = 0.25;
}
else if (j1Btn1)
{
scaleFactor = 0.5;
}
else if (j2Btn1)
{
scaleFactor = 0.75;
}

R_myRobot.TankDrive(J_stick1->GetY()*scaleFactor, J_stick2->GetY()*scaleFactor);

``````

Alright thanks, I did not know to use the GetY after the joysticks. For now it has all been working because I had just set the motors up as 2 different declarations and it worked there. So I will be switching it over today because its just much smoother.

Thanks,
Davis

Pardon my ignorance (or misreading of the post), but how do you get away with calling the TankDrive function (method?) with what looks like different data types as parameters? Is this what’s known in C++ speak as “polymorphism”?

R_myRobot.TankDrive(J_stick1, J_stick2);
vs
R_myRobot.TankDrive(J_stick1->GetY() * 0.25, J_stick2->GetY() * 0.25);

Yes, that is polymorphism. Basically the function that is used is determined by the parameter types you pass into the function.

Inside the class it defines each different input parameter arrangement with the same function name, and you automatically select between those different functions by the parameters you provide.

It is a form of polymorphism. Function overloading and templates are what is known as static polymorphism. When speaking with most C++ programmers though, polymorphism generally refers to dynamic polymorphism, implemented using function pointers and virtual functions. Static polymorphism is decided at compile time, and is based upon the visible type of the objects to the compiler. Dynamic polymorphism, however, is actually based upon the run-time type of the object.