Go to Post The people that have posted in this thread are intelligent and I know that when they talk, I need to listen. - rtfgnow [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
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 10-02-2015, 08:48
nguobadia nguobadia is offline
Registered User
FRC #5608
 
Join Date: Feb 2015
Location: United States
Posts: 1
nguobadia is an unknown quantity at this point
Toggling Buttons

Hi from team 5608! So I'm trying to figure out how to toggle the buttons on our joysticks so that we can turn on and off our solenoids. I keep trying to do it, but they keep conflicting with each other so the pistons either twitch or nothing happens.
So just some background: we have 4 solenoids and 2 joysticks, one joystick for every two solenoids, however Button 3 (raw button) on both joysticks are used to activate their respective solenoids.
Here's our code (I'll give you just one of them since the code would be almost identical for the other one):

/**Toggles the right joystick button 3.*/
public void rightToggle(){

if (right.getRawButton(3)){
if(!isPressedPiston1){
switchSol1 = !switchSol1;

}else {
isPressedPiston1 = !isPressedPiston1;

}
if (switchSol1){
sol1.set(true);
sol2.set(true);

}else {
sol1.set(false);
sol2.set(false);

}

}

}
Reply With Quote
  #2   Spotlight this post!  
Unread 10-02-2015, 09:19
cstelter cstelter is offline
Programming Mentor
AKA: Craig Stelter
FRC #3018 (Nordic Storm)
Team Role: Mentor
 
Join Date: Apr 2012
Rookie Year: 2012
Location: Mankato, MN
Posts: 77
cstelter will become famous soon enough
Re: Toggling Buttons

Quote:
Originally Posted by nguobadia View Post
Hi from team 5608! So I'm trying to figure out how to toggle the buttons on our joysticks so that we can turn on and off our solenoids. I keep trying to do it, but they keep conflicting with each other so the pistons either twitch or nothing happens.
So just some background: we have 4 solenoids and 2 joysticks, one joystick for every two solenoids, however Button 3 (raw button) on both joysticks are used to activate their respective solenoids.
Here's our code (I'll give you just one of them since the code would be almost identical for the other one):

Code:
/**Toggles the right joystick button 3.*/
public void rightToggle(){
		
	if (right.getRawButton(3)){
		if(!isPressedPiston1){
			switchSol1 = !switchSol1;
				
		}else {
			isPressedPiston1 = !isPressedPiston1;
				
		}
		if (switchSol1){
			sol1.set(true);
			sol2.set(true);
				
		}else {
			sol1.set(false);
			sol2.set(false);
				
		}
			
	}
}
In the future, when pasting code wrap the code with [ code] and [ /code] tokens so that it will come out as in the quote above rather than your original email. It's much easier for folks to read code with indentation.

So your code seems to maintain a local boolean isPressedPiston1 for how you last set the solenoid. Then only when you switch from off to on, you set a secondary boolean switchSol1 to actually effect the switch. But you don't set isPressedPiston1 to true in this case so it stays false, even though you've changed the piston state-- unsure if this is what you intended.

I think we need to see the context(s) that you call this rightToggle() function. Since you are checking the state of the button, I would presume you are calling it multiple times from teleopPeriodic() in which case when it is not pressed you will simply be flip-flopping isPressedPiston1. Then when you press the button, the previous state of isPressedPiston1 will be unknown-- fully dependent on the last value it was flip-floped from. It may or may not work. On the other hand if you are only calling this after you know that button is pushed, then there should be no need to check button in this function, or maintain isPressedPiston1.

Please provide the calling context as with just the one function it is difficult to guess what may be wrong.
Reply With Quote
  #3   Spotlight this post!  
Unread 10-02-2015, 09:33
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,654
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Toggling Buttons

I'm unsure what you meant this to do, but it looks like you're trying to implement a 4-state machine with one button. If so, this will not achieve it - once isPressedPiston1 is set to true, it will never toggle again.

In any case, assuming that switchSol1 and isPressedPiston1 are persistent variables after isPressedPiston1 is set true, every time this routine is entered and button 3 is pressed, switchSol1 will toggle. The problem is likely that this routine is called rather quickly, in a loop. If so, this would cause the state of switchSol1 (and therefore sol1 and sol2) to twitch. Depending on your programming model, you can either use a whenPressed() callback to just call this one time when the button is pressed, or include a timeout. That is, when the state is toggled, set a time to wait (perhaps 300 milliseconds to start), and exit this routine immediately if it is called and you have not reached the timeout yet.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
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


All times are GMT -5. The time now is 12:35.

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