Go to Post "If you can't hurdle, you can't win." "Remember to practice your obnoxious amateurism." . - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-03-2016, 15:06
isaacaddis's Avatar
isaacaddis isaacaddis is offline
Registered User
FRC #0687
 
Join Date: Jan 2016
Location: america
Posts: 19
isaacaddis is an unknown quantity at this point
Java Iterative-Robot: Toggle Buttons?

Hey CheifDelphi,

I was wondering if anyone knows how to use toggle buttons in an Iterative-Robot in Java.

Here is what I want the code to do:

Apply a command to the robot when a button is pressed until a button is pressed.

Is there a way to create commands in an Iterative Robot?

I apologize in advance for inconveniencing you guys
  #2   Spotlight this post!  
Unread 01-03-2016, 15:28
Fauge7 Fauge7 is offline
Head programmer
FRC #3019 (firebird robotics)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Scottsdale
Posts: 195
Fauge7 is a name known to allFauge7 is a name known to allFauge7 is a name known to allFauge7 is a name known to allFauge7 is a name known to allFauge7 is a name known to all
Re: Java Iterative-Robot: Toggle Buttons?

suggestion #1: dont use iterative, Java is an object oriented programming for what it is meant for, objects!
suggestion #2: post your code so we might be able to help
suggestion #3:look at the code below


here is some example code for suggestion number 1

Code:
boolean buttonToggle = false;
if (button.isPressed()) {
  buttonToggle = !buttonToggle;
}

//code for using it:

if (buttonToggle) {
motor.setSpeed(someSpeed);
}

Last edited by Fauge7 : 01-03-2016 at 15:31.
  #3   Spotlight this post!  
Unread 01-03-2016, 16:09
BL0X3R BL0X3R is offline
Registered User
AKA: Nick
FRC #4488 (ShockWave)
Team Role: Programmer
 
Join Date: Mar 2013
Rookie Year: 2013
Location: Hillsboro
Posts: 43
BL0X3R is on a distinguished road
Re: Java Iterative-Robot: Toggle Buttons?

Quote:
Originally Posted by Fauge7 View Post
suggestion #1: dont use iterative, Java is an object oriented programming for what it is meant for, objects!
I hope you aren't suggesting that this team completely rewrites their robot code to work in a new style a few weeks before the competition. "Don't use iterative" is a pretty overgeneralized statement to make: Plenty of teams use it, some to great success.
The main difference between Iterative and Command based besides structure and style is that Iterative is continuous and periodic, while Command based is built around being asynchronous. While it is also true that the Command based model does use more of the features of object oriented programming, that does not necessarily always make it better in this environment.

Quote:
Originally Posted by Fauge7 View Post
suggestion #2: post your code so we might be able to help
This. Even if you can only post the section that you are having trouble with, any amount of context can help us find a solution.

Quote:
Originally Posted by Fauge7 View Post
Code:
boolean buttonToggle = false;
if (button.isPressed()) {
  buttonToggle = !buttonToggle;
}

//code for using it:

if (buttonToggle) {
motor.setSpeed(someSpeed);
}
This would work for a command based robot, but because of the periodic nature of Iterative robot, this code gets run about 50 times a second. Therefore, if the driver holds the button for more than 20ms, the toggle variable will flip more than once.
One solution is to only flip the variable on the rising edge of the button press - to check if the button is pressed, but was not pressed earlier. Example below.

Code:
boolean buttonToggle = false;
boolean buttonLast = false;

public void runToggle(boolean buttonValue){
     if(buttonValue && !buttonLast)
          buttonToggle = !buttonToggle;     

     buttonLast = buttonValue;

     if(buttonToggle)
          motor.setSpeed(someSpeed)
}
__________________
Carson Finalists (4488, 67, 225, 5659)
2x District Winner + Gracious Professionalism & Engineering Excellence, DCMP Finalist and Entrepreneurship
Galileo Finalists (1153, 4488, 1318, 1218)
2x District Winner + Chairman's & Industrial Design, DCMP Finalist and Quality Award
http://www.chiefdelphi.com/forums/image.php?type=sigpic&userid=74752&dateline=139804  7731 Oregon Rookie All Star, Curie Rookie All Star
  #4   Spotlight this post!  
Unread 01-03-2016, 16:11
KrazyCarl92's Avatar
KrazyCarl92 KrazyCarl92 is offline
Registered User
AKA: Carl Springli
FRC #5811 (The BONDS)(EWCP)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2010
Location: Dayton, OH
Posts: 519
KrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond repute
Re: Java Iterative-Robot: Toggle Buttons?

Quote:
Originally Posted by Fauge7 View Post
Code:
boolean buttonToggle = false;
if (button.isPressed()) {
  buttonToggle = !buttonToggle;
}

//code for using it:

if (buttonToggle) {
motor.setSpeed(someSpeed);
}
I do not believe this will work. What happens when your buttonToggle variable is false? You will need to add more to truly achieve toggle functionality.

Code:
boolean buttonToggle = false;
if (button.isPressed()) {
  buttonToggle = !buttonToggle;
}

//code for using it:

if (buttonToggle) {
motor.setSpeed(someSpeed);
} else {
motor.setSpeed(0);
}
Another reason this may experience an issue is that your code will start from the top of TeleopPeriodic every 20 ms or so. So if you hold down the button for 100 ms, your buttonToggle command (and therefore your motor output) will rapidly switch back and forth between true and false 5 times before letting go of the button. If you hold down the button for 80 ms? Switching back and forth 4 times and now you end up right where you started before you hit the button.

Edit: Follow BL0X3R's suggestion to achieve true toggling functionality.
__________________
[2016-present] FRC 5811 - BONDS Robotics
[2010-2015] FRC 0020 - The Rocketeers

Last edited by KrazyCarl92 : 01-03-2016 at 16:13. Reason: Added note.
  #5   Spotlight this post!  
Unread 01-03-2016, 16:26
rrossbach rrossbach is offline
Registered User
AKA: Ron R
FRC #2607 (RoboVikings)
Team Role: Mentor
 
Join Date: Nov 2008
Rookie Year: 2008
Location: Warrington PA
Posts: 90
rrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to behold
Re: Java Iterative-Robot: Toggle Buttons?

Our team developed a short extension of the Joystick class implementing a couple of useful methods for cases like this.

One of those is a "getToggleButton(int buttonNumber)" that you can use just like getRawButton() in Joystick, the difference being that the return value of the method starts off false and alternates between true and false with each press of the button.

The code is up in our github repo, here's a link to the specific class (robovikingStick)

Hope that helps!

- Ron
Team #2607 controls mentor
__________________

FIRST Mid-Atlantic Volunteer
FRC Team #2607 Mentor
  #6   Spotlight this post!  
Unread 01-03-2016, 16:37
lethc's Avatar
lethc lethc is offline
#gkccurse
AKA: Becker Lethcoe
FRC #1806 (S.W.A.T.)
Team Role: Alumni
 
Join Date: Nov 2012
Rookie Year: 2013
Location: Smithville, MO
Posts: 119
lethc will become famous soon enough
Re: Java Iterative-Robot: Toggle Buttons?

We use 254's Latch class.

Github Link

Their code has been a huge resource for our team.
__________________
2016: Greater Kansas City Regional Finalists, Oklahoma Regional Winners, Tesla Semifinalists, IRI Quarterfinalists
2015: Greater Kansas City Regional Finalists, Oklahoma Regional Winners, Tesla Quarterfinalists, IRI Winners
2014: Central Illinois Regional Quarterfinalists, Greater Kansas City Regional Finalists, Newton Semifinalists
2013: Greater Kansas City Regional Winners, Oklahoma Regional Winners, Galileo Quarterfinalists
  #7   Spotlight this post!  
Unread 01-03-2016, 23:33
Fauge7 Fauge7 is offline
Head programmer
FRC #3019 (firebird robotics)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Scottsdale
Posts: 195
Fauge7 is a name known to allFauge7 is a name known to allFauge7 is a name known to allFauge7 is a name known to allFauge7 is a name known to allFauge7 is a name known to all
Re: Java Iterative-Robot: Toggle Buttons?

Quote:
Originally Posted by BL0X3R View Post
I hope you aren't suggesting that this team completely rewrites their robot code to work in a new style a few weeks before the competition. "Don't use iterative" is a pretty overgeneralized statement to make: Plenty of teams use it, some to great success.
The main difference between Iterative and Command based besides structure and style is that Iterative is continuous and periodic, while Command based is built around being asynchronous. While it is also true that the Command based model does use more of the features of object oriented programming, that does not necessarily always make it better in this environment.
actually command based is also periodic, each subsystem runs the command about every 20ms...but as for a code rewrite it wouldn't necessarily be wise but it wouldn't be impossible.


Quote:
Originally Posted by BL0X3R View Post
This would work for a command based robot, but because of the periodic nature of Iterative robot, this code gets run about 50 times a second. Therefore, if the driver holds the button for more than 20ms, the toggle variable will flip more than once.
One solution is to only flip the variable on the rising edge of the button press - to check if the button is pressed, but was not pressed earlier. Example below.
well, the toggle can work both ways on down and up. IF it runs for 20ms loops and the user holds the button down for 100ms that is 5 loops it runs through before the code reacts, so I would suggest making it on the downstroke instead of the upstroke
Closed Thread


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 06:42.

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