Go to Post Wins = Attention Attention = Sponsors Sponsors = Money Money= being able to do more. a vicious cycle...but it is what it is - Protronie [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-01-2009, 11:29
programmr's Avatar
programmr programmr is offline
Registered User
AKA: Face
FRC #0522
Team Role: Programmer
 
Join Date: Jan 2008
Rookie Year: 2006
Location: Staten Island, NY
Posts: 83
programmr is a jewel in the roughprogrammr is a jewel in the roughprogrammr is a jewel in the rough
Send a message via AIM to programmr
Unhappy Programming Joystick Buttons to toggle a motor on and off

Hello there, can someone please help? I want to use a joystick button to toggle on and off a motor. If logitech button 3 is pressed once, turn the motor on with a speed of .5 If the button number 3 is pressed again, turn the motor off. In this case, it would be controlled by joystick connected to usb port 3.
Attached is what i have so far. Any help would be greatly appreciated.
Attached Files
File Type: zip BuiltinDefaultCode.zip (1.32 MB, 68 views)
__________________
"I would love to change the world but no one will give me the source code"
Reply With Quote
  #2   Spotlight this post!  
Unread 23-01-2009, 16:50
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Programming Joystick Buttons to toggle a motor on and off

Joystick stick3(3);//obviously
Jaguar jag1(3);//PWM port 3
bool btn3state=false;


if (stick3.GetRawButton(3))
{
if (btn3state)
{
jag3.Set(0.5);
btn3state=false;
}
else
{
jag3.Set(0.0);
btn3state=true;
}
}
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #3   Spotlight this post!  
Unread 23-01-2009, 20:30
LinuxMercedes LinuxMercedes is offline
Real men use Macs
FRC #1444 (Lightning Lancers)
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: St. Louis, MO
Posts: 22
LinuxMercedes is an unknown quantity at this point
Re: Programming Joystick Buttons to toggle a motor on and off

One bug in your code, byteit101

Quote:
Originally Posted by byteit101 View Post
Joystick stick3(3);//obviously
Jaguar jag1(3);//PWM port 3
bool btn3state=false;


if (stick3.GetRawButton(3))
{
if (!btn3state) //if the button has not been depressed
{
jag3.Set(0.5);
btn3state=true; //the button has been depressed
}
else
{
jag3.Set(0.0);
btn3state=false; //the button was clicked again
}
}
Otherwise, you'd click the button first to set the jag to 0, then the second click would set it to 0.5. Unless I missed something.
__________________
Reply With Quote
  #4   Spotlight this post!  
Unread 24-01-2009, 08:05
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Programming Joystick Buttons to toggle a motor on and off

no, it is in an if else
if it was
Code:
if (btn3state)
{
jag3.Set(0.5);
btn3state=false; 
}
else if (!btn3state)//always execute
{
jag3.Set(0.0);
btn3state=true;
}
it would be buggy, but if "if" is true, "else" is not executed
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #5   Spotlight this post!  
Unread 24-01-2009, 09:10
GGCO's Avatar
GGCO GGCO is offline
Registered User
AKA: Grant
FRC #3357
Team Role: Alumni
 
Join Date: Jan 2008
Rookie Year: 2004
Location: Michigan
Posts: 406
GGCO is a splendid one to beholdGGCO is a splendid one to beholdGGCO is a splendid one to beholdGGCO is a splendid one to beholdGGCO is a splendid one to beholdGGCO is a splendid one to beholdGGCO is a splendid one to beholdGGCO is a splendid one to behold
Send a message via AIM to GGCO
Re: Programming Joystick Buttons to toggle a motor on and off

This is how my team did it.

// this is all in our autonomous loop....
bool buttonUp = leftStick->GetRawButton(1);
bool buttonDown = leftStick->GetRawButton(2);
bool buttonStop= leftStick->GetRawButton(3);
float elevatorSpeed; // this is the speed of our elevator
if (buttonUP){
elevatorSpeed(1.0);
}
else if (buttonDown){
elevatorSpeed(-1.0);
}
else if (buttonStop){
elevatorSpeed(0.0);
}

ElevatorMotor1->Set(elevatorSpeed); // elevator motors one and two are
ElevatorMotor2->Set(-elevatorSpeed);// on both sides of our elevator
__________________
"Great spirits have always encountered violent opposition from mediocre minds" - Albert Einstein
The FIRST Alliance
COMETS Robotics
Website

Reply With Quote
  #6   Spotlight this post!  
Unread 24-01-2009, 10:01
Eric Finn's Avatar
Eric Finn Eric Finn is offline
Registered User
FRC #0166 (Chop Shop)
Team Role: College Student
 
Join Date: May 2006
Rookie Year: 2005
Location: Merrimack, NH
Posts: 101
Eric Finn has a spectacular aura aboutEric Finn has a spectacular aura about
Send a message via AIM to Eric Finn
Re: Programming Joystick Buttons to toggle a motor on and off

Quote:
Originally Posted by LinuxMercedes View Post
One bug in your code, byteit101
...
Otherwise, you'd click the button first to set the jag to 0, then the second click would set it to 0.5. Unless I missed something.
Even with that, it would not work properly, assuming this code is executed often enough.
Code:
Joystick stick3(3);//obviously
Jaguar jag1(3);//PWM port 3
bool btn3state=0;
bool pwm3state=0

//The motor starts at 0, each time we press the button it toggles between 0 and .5.
//The motor will only change once if the button is held down for multiple loops.
if(stick3.GetRawButton(3) && !btn3state) //if the button is pressed, but not held...
{
	btn3state = 1; //button is now being held
	if(!pwm3state) //if the motor is not moving
	{
		jag3.Set(0.5);
		pwm3state=1; //the motor is moving
	}
	else //if the motor is moving
		{
		jag3.Set(0.0);
		pwm3state=0; //the motor is not moving
	}
}
else if(!stick3.GetRawButton(3) && btn3state) //if we think the button is held, but it's not being pressed...
	btn3state = 0; //button is no longer held
With this, you have to release the joystick button in order to toggle the motor again. Without this, the motor will toggle many times, even with just a short tap of the button.
Remember, if that code is in a while loop, the variables should be declared *before* the loop. If the code is in a function, they should be static.
__________________
It always takes longer than you expect, even when you take into account Hofstadter's Law.
--Hofstadter's Law

Reply With Quote
  #7   Spotlight this post!  
Unread 25-01-2009, 17:47
WatersongX WatersongX is offline
Registered User
AKA: Matthew Keen
FRC #1111 (Powerhawks)
Team Role: Programmer
 
Join Date: Feb 2008
Rookie Year: 2006
Location: Edgewater, MD
Posts: 7
WatersongX is an unknown quantity at this point
Send a message via AIM to WatersongX
Re: Programming Joystick Buttons to toggle a motor on and off

May I reccomend a simpler system?

Assume the following variables:
Jaguar speedcontroller1
Joystick usb3
has been set up and initialized correctly
Code:
// with variable declarations (outside of main loop, so they don't reset every time)
bool togglestate = false;
bool lastu3b3state = false;

// somewhere in code (inside main loop, runs every time)
if ( lastu3b3state == true && usb3.GetRawButton( 3 ) == false ) togglestate = !togglestate;
speedcontroller1.Set( togglestate?0.5f:0.0f );

// at the end of the main loop, or at least after the button is done being used
lastu3b3state = usb3.GetRawButton( 3 );
Reply With Quote
Reply


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
joystick and extra buttons TEAM1949 Programming 3 18-02-2008 13:22
Toggle Motor On & Off Help..... programmr Programming 1 16-02-2008 11:15
Programming Extra Joystick Buttons Inverted Programming 2 08-02-2007 11:45
Programming motors with joystick buttons TMHStitans Programming 5 21-03-2005 21:07
joystick buttons and a quick question h0x4r Programming 2 17-02-2005 00:18


All times are GMT -5. The time now is 14:08.

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