View Single Post
  #13   Spotlight this post!  
Unread 13-12-2015, 14:22
nighterfighter nighterfighter is offline
1771 Alum, 1771 Mentor
AKA: Matt B
FRC #1771 (1771)
Team Role: Mentor
 
Join Date: Sep 2009
Rookie Year: 2007
Location: Suwanee/Kennesaw, GA
Posts: 835
nighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant future
Re: how to toggle motors in c++ using buttons

Here is a quick and dirty way to do it.

What this code will do: While you are pressing a joystick trigger, the respective motor will spin at full speed. If you are not pressing the trigger, it will drive like normal tank drive.

The first thing you'll see is that I made 2 float variables, starting at 0.0. These will be passed into the TankDrive function.

The next thing is the "if" statement. If you are pressing the trigger on stick OR stick2, you then check to see which stick is being pressed, and assign 1.0 to the respective float values.

If you are NOT pressing the joysticks trigger, then the float values will be determined by the Y axis of the joystick.

Finally, those float values will be passed to the TankDrive method.

Forgive any formatting, I wrote it all in here. Also you might have to make one of the values negative, depending on how your motors are set up.


Code:
while (IsOperatorControl() && IsEnabled())
		{
			float leftValueToUse = 0.0; //2 values that will
                        float rightValueToUse = 0.0; // be passed to TankDrive
			
                       if(stick.GetRawButton(1) || stick2.GetRawButton(1) )
                      {
			if(stick.GetRawButton(1))
                       {
				leftValueToUse = 1.0;
			}
			if(stick2.GetRawButton(1))
                       {
				rightValueToUse = 1.0;
			}
                       
                      }
                    else
                    {
                    leftValueToUse = stick.GetY();
                    rightValueToUse = stick.GetY();
                     
                     }
                    myRobot.TankDrive(leftValueToUse,rightValueToUse);

                        Wait(0.005); //Wait for a motor update time

		}

Also, this isn't the best way to do it. There is a problem with this way. Think about what will happen if you are driving like normal, and then press and hold one of the joystick triggers. What will happen to the motor you are NOT pressing the respective joystick for? How can you fix that?
__________________
1771- Programmer, Captain, Drive Team (2009-2012)
4509- Mentor (2013-2015)
1771- Mentor (2015)