Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Dividing Speed on Robot Drive (http://www.chiefdelphi.com/forums/showthread.php?t=103271)

DavisC 19-02-2012 13:29

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.

Code:

                        // 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);


SuperBK 19-02-2012 14:58

Re: Dividing Speed on Robot Drive
 
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

mikets 19-02-2012 21:19

Re: Dividing Speed on Robot Drive
 
You should have a scaleFactor variable and have the buttons modify it.
Code:

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);


DavisC 20-02-2012 08:07

Re: Dividing Speed on Robot Drive
 
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

jalburty 20-02-2012 10:34

Re: Dividing Speed on Robot Drive
 
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);

DjScribbles 20-02-2012 11:32

Re: Dividing Speed on Robot Drive
 
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.

rbmj 20-02-2012 12:47

Re: Dividing Speed on Robot Drive
 
Quote:

Originally Posted by DjScribbles (Post 1130874)
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.


All times are GMT -5. The time now is 17:36.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi