Go to Post The best [fundraiser] that we did was our various little things this year, such as calanders and other things. The worst is not doing one at all. - Joe Matt [more]
Home
Go Back   Chief Delphi > Technical > Technical Discussion
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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)
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 04:39.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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