Go to Post I believe with every fiber of my being that Engineering is an Art. It is a beautiful, wonderful, delightful art. - Joe Johnson [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 28-02-2014, 22:08
2185Bilal's Avatar
2185Bilal 2185Bilal is offline
Driver, Ld. Programmer, Electrical
AKA: Bilal Majeed
FRC #2185 (Ramazoidz)
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2011
Location: Toronto, Canada
Posts: 110
2185Bilal will become famous soon enough
Java wait Function

What we want to do is to have our solenoids to open and then after like 0.5 seconds have it close automatically.

This is our code so far:
Code:
if (j_shooting.getRawButton(2)){ //FORWARD - BUTTON A
            flag = 1;
        }else if (j_shooting.getRawButton(1)){ //BACKWARD - BUTTON X
            cyl_shoot.set(DoubleSolenoid.Value.kForward);
        }else {
            cyl_shoot.set(DoubleSolenoid.Value.kOff);
        }
        
        if (flag == 1){
            cyl_shoot.set(DoubleSolenoid.Value.kReverse);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            cyl_shoot.set(DoubleSolenoid.Value.kForward);
            flag = 0;
        }
**ignore the indentation, and the kForward and kBackwards, its switched

So my question is, is the code i have right now good and if not what other options is there to have the pistons open, pause for 0.5 seconds, and then close.

thanks
__________________
RAMAZOIDZ

2009 Toronto West Regional - Regional Winners
2009 Waterloo Regional - Regional Winners
Reply With Quote
  #2   Spotlight this post!  
Unread 28-02-2014, 22:47
Domenic Rodriguez's Avatar
Domenic Rodriguez Domenic Rodriguez is offline
Registered User
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Grove City, PA
Posts: 213
Domenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura about
Re: Java wait Function

Thread.sleep() causes the current thread to pause for a time. If you call this from the main thread, the robot will be unresponsive for those 500 ms while the thread sleeps. You could avoid this by moving the solenoid code to a separate thread.

A simpler option that I often use is to record a timestamp at the start and then subtract that timestamp from future readings to get the elapsed time.

Example:
Code:
double startTime;

if (j_shooting.getRawButton(2)) {
    // This will repeatedly be set until you let go of the button
    startTime = System.currentTimeMillis(); 
} else if (j_shooting.getRawButton(1))
    // This will override, but not reset, the timer
    cyl_shoot.set(DoubleSolenoid.Value.kForward);
} else {
    // Check the time to see if the pistons should be...
    if (System.currentTimeMillis() - startTime < 0.5) {
        cyl_shoot.set(DoubleSolenoid.Value.kReverse);  // ...extended, or...
    } else {
        cyl_shoot.set(DoubleSolenoid.Value.kForward);  // ..retracted
    }
}
This way you aren't locking up the rest of your program while you wait for the pistons to retract.
__________________

LuNaTeCs - Learning Under Nurturing Adults Teaching Engineering Concepts and Skills - Small and Mighty!

FRC 316 LuNaTeCs - Student (2011-2014), Lead Programmer (2011-2014), Team Captain (2013-2014), Operator (2013), Drive Coach (2014), Mentor (2015-????)
'11 Philly Regional Finalists, '13 Chestnut Hill Finalists, '13 Lenape Champions, '13 Archimedes Division, '14 Chestnut Hill Champions, '14 Lenape Champions
FTC 7071 EngiNerds - Founding Advisor (2013-2014) | FRC 5420 Velocity - Founding Advisor (2015)
Grove City College Class of '18, Electrical/Computer Engineering (B.S.E.E)

Reply With Quote
  #3   Spotlight this post!  
Unread 28-02-2014, 22:49
cgmv123's Avatar
cgmv123 cgmv123 is offline
FRC RI/FLL Field Manager
AKA: Max Vrany
FRC #1306 (BadgerBOTS)
Team Role: College Student
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Madison, WI
Posts: 2,085
cgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond repute
Re: Java wait Function

Timer.delay() is supposed to leave outputs set where they are as well, but I've had more success using Timer.getFPGATimestamp() in conjunction with a loop.
__________________
BadgerBOTS Robotics|@team1306|Facebook: BadgerBOTS
2016 FIRST Championship Tesla Division | 2016 Wisconsin Regional Engineering Inspiration Award

2015 FIRST Championship Carson Division | 2015 Wisconsin Regional Chairman's Award

2013 FIRST Championship Curie Division | 2013 Wisconsin Regional Chairman's Award

2012 FIRST Championship Archimedes Division | 2012 Wisconsin Regional Engineering Inspiration Award, Woodie Flowers Finalist Award (Lead Mentor Ben Senson)

Reply With Quote
  #4   Spotlight this post!  
Unread 01-03-2014, 09:10
Bryan Herbst's Avatar
Bryan Herbst Bryan Herbst is online now
Registered User
AKA: Bryan
FRC #2052 (KnightKrawler)
Team Role: Mentor
 
Join Date: Sep 2007
Rookie Year: 2007
Location: Minneapolis, Minnesota
Posts: 545
Bryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond repute
Re: Java wait Function

Quote:
Originally Posted by cgmv123 View Post
Timer.delay() is supposed to leave outputs set where they are as well, but I've had more success using Timer.getFPGATimestamp() in conjunction with a loop.
This is critical to understand.

Our team had similar requirements surrounding our catapult (do X, wait half a second, then do Y). Originally the programmers implemented this using Timer.delay().

The result was that when the drivers tried to perform the action while moving, but then stopped moving or changed directions during the action, the robot would keep moving.

Though .5 seconds doesn't sound like much, it is plenty of time for your robot to go just a little too far and injure someone, break something, or get you a penalty for going too far outside the arena.

Use Timer.delay() with caution.
__________________
Team 2052- KnightKrawler
Mentor and volunteer
Reply With Quote
  #5   Spotlight this post!  
Unread 01-03-2014, 09:42
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,102
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Java wait Function

Quote:
Originally Posted by Tanis View Post
This is critical to understand.
^This.

Year in and year out I see programmers bumping up against this concept.

I think it's probably fair to say that some (many?) of them receive advice on how to fix the problem, but never really understand why there was a problem and why the fix fixed it.

Does anyone have a link to a clear, simple, well-written tutorial on concurrent processing for FRC that covers threads, state machines, and interrupts?



Last edited by Ether : 01-03-2014 at 13:18.
Reply With Quote
  #6   Spotlight this post!  
Unread 02-03-2014, 20:13
ProgrammerMatt ProgrammerMatt is offline
Programmer-Electrical-Mechanical
FRC #0228 (Gus)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Southington
Posts: 138
ProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really nice
Re: Java wait Function

This is what I have done.

Inside op control:
Code:
if(INSERT SOMETHING HERE) {

new Thread() {
     public void run() {
          //Code to run here:
          Thread.sleep(time in ms);
          //More code
          }.start()
     }
}
What this does is every time the if statement becomes true it creates a thread and runs the code in run() {} then kills the thread. you can use Thread.sleep(); inside this and it will NOT affect your main code only whats in that thread.

This has worked well for me.

Hope this helps!

Matt
__________________
2015-2016 CSA
Software Engineering Student @ Johnson & Wales University
Team 228, Gus Robotics Inc.
Facebook
FLL Mentor for 1107, Edison Eagles!
2015- CT State Champions
2012- WPI Finalist(Thanks 1884 and 549), Spirt, Best Website
2011- WPI Chairman's award winners!
2010- WPI Champions! (thanks 230 & 20), WPI Engineering Inspiration, CT Best Website, CT VEX Champions (VRC228, VRC228b) (21-1-0)
2009- QCC VEX Champions (VRC228) (11-0-0), Innovate Award (VRC228)
Reply With Quote
  #7   Spotlight this post!  
Unread 02-03-2014, 21:19
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
Re: Java wait Function

how does the thread get killed?
Reply With Quote
  #8   Spotlight this post!  
Unread 02-03-2014, 21:27
AlexBrinister AlexBrinister is offline
Registered User
AKA: Alex Brinister
FRC #1768 (RoboChiefs)
Team Role: Alumni
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Bolton, MA
Posts: 93
AlexBrinister will become famous soon enough
Re: Java wait Function

My understanding is that the JVM handles it. From here, it seems like letting the JVM handle could introduce race conditions if you access the same data from multiple threads like that...

Alex Brinister
Reply With Quote
  #9   Spotlight this post!  
Unread 02-03-2014, 22:50
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
Re: Java wait Function

ah. looked at it too fast. The thread completes normally when run() returns.
Reply With Quote
  #10   Spotlight this post!  
Unread 03-03-2014, 07:50
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,753
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Java wait Function

Is safety configured for the drive base? I'd hope that the Java implementation would halt the drive motors when wait() or breakpoints delay for more than 100ms.

Greg McKaskle
Reply With Quote
  #11   Spotlight this post!  
Unread 03-03-2014, 08:47
AlexBrinister AlexBrinister is offline
Registered User
AKA: Alex Brinister
FRC #1768 (RoboChiefs)
Team Role: Alumni
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Bolton, MA
Posts: 93
AlexBrinister will become famous soon enough
Re: Java wait Function

I think this is only enforced if you have the Motor Safety Helper enabled. This feature is optional in both the C++ and Java libraries and can be disabled by the programmer. It is generally better to keep it enabled but to extend the timeout period using the SetTimeout() function in whatever motor controller class you choose to use (Talon, Victor, CANJaguar, Jaguar, etc).

Alex Brinister
Reply With Quote
  #12   Spotlight this post!  
Unread 03-03-2014, 21:22
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,753
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Java wait Function

What I intended to ask was whether the Motor Safety Helper was enabled when the robot motors ran for several seconds during the sleep() call.

Greg McKaskle
Reply With Quote
  #13   Spotlight this post!  
Unread 03-03-2014, 22:51
ProgrammerMatt ProgrammerMatt is offline
Programmer-Electrical-Mechanical
FRC #0228 (Gus)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Southington
Posts: 138
ProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really nice
Re: Java wait Function

I have not turned motor safety helper on or off. So what ever it defaults to is what it is set to and it still works for me.
__________________
2015-2016 CSA
Software Engineering Student @ Johnson & Wales University
Team 228, Gus Robotics Inc.
Facebook
FLL Mentor for 1107, Edison Eagles!
2015- CT State Champions
2012- WPI Finalist(Thanks 1884 and 549), Spirt, Best Website
2011- WPI Chairman's award winners!
2010- WPI Champions! (thanks 230 & 20), WPI Engineering Inspiration, CT Best Website, CT VEX Champions (VRC228, VRC228b) (21-1-0)
2009- QCC VEX Champions (VRC228) (11-0-0), Innovate Award (VRC228)
Reply With Quote
  #14   Spotlight this post!  
Unread 04-03-2014, 06:46
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,753
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Java wait Function

Does the default result in zombie robots? I'd hope not. It should take effort to produce a zombie robot.

Also, if you are turning off the safety config, please ensure that everyone running the robot knows how to disable or estop the robot. Personally, I'd only turn it off when the robot is on blocks and I want the motors to keep running while I'm at a breakpoint.

Greg McKaskle
Reply With Quote
  #15   Spotlight this post!  
Unread 04-03-2014, 09:55
Bryan Herbst's Avatar
Bryan Herbst Bryan Herbst is online now
Registered User
AKA: Bryan
FRC #2052 (KnightKrawler)
Team Role: Mentor
 
Join Date: Sep 2007
Rookie Year: 2007
Location: Minneapolis, Minnesota
Posts: 545
Bryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond reputeBryan Herbst has a reputation beyond repute
Re: Java wait Function

Greg,

I haven't done any real testing, but it does not appear that any sort of safety kicks in after 100ms in Java. Our robot definitely moved for more than 100ms with our sleep() calls.

You made me curious, though, so I started poking around. The Jaguar class extends SafePWM, which indeed defaults to disabling safety:

Code:
void initSafePWM() {
    m_safetyHelper = new MotorSafetyHelper(this);
    m_safetyHelper.setExpiration(0.0);
    m_safetyHelper.setSafetyEnabled(false);
}
This sounds rather silly for a class billed as "SafePWM" to me, though it does expose methods for you to enable safety if you wish.
__________________
Team 2052- KnightKrawler
Mentor and volunteer
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:37.

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