Go to Post I've never been one for the "you did your best" and other "everyone's a winner" sayings, they don't push you, they don't get you to analyze what went wrong and figure out where to improve. - Aren_Hill [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 26-01-2012, 21:37
neal's Avatar
neal neal is offline
Neal
FRC #1777 (Viking Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2009
Location: United States
Posts: 56
neal is an unknown quantity at this point
A good time based way to program

Whenever my mentors ask me if I can program a motor to run for x seconds, I always feel it's not the best way to do that.

For example, press a button and the motor will run for x seconds and then stop.

All I can think of to do that is simply using Timer() class, but then I feel using multiple timers, if else's for them is not just the best way to do it.

Is there any other better way to do this or is using Timer() fine?

All your thoughts are much appreciated!
Reply With Quote
  #2   Spotlight this post!  
Unread 26-01-2012, 23:05
ianonavy ianonavy is offline
Programming Mentor/Alumnus
AKA: Ian Adam Naval
FRC #3120 (RoboKnights)
Team Role: Mentor
 
Join Date: Dec 2010
Rookie Year: 2008
Location: Sherman Oaks
Posts: 32
ianonavy is an unknown quantity at this point
Re: A good time based way to program

Code:
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.SpeedController;

...

/**
 * Sets a motor to a certain power value for a certain number of seconds
 * @param controller The speed controller of the motor to power.
 * @param power The desired power for the motor.
 * @param time The time to run in seconds.
 */
public void setForTime(SpeedController controller, double power, double time) {
    controller.set(power);
    Timer.delay(time);
    controller.set(0);
}
Would something like this work? Warning: that code is not tested.
Reply With Quote
  #3   Spotlight this post!  
Unread 26-01-2012, 23:14
neal's Avatar
neal neal is offline
Neal
FRC #1777 (Viking Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2009
Location: United States
Posts: 56
neal is an unknown quantity at this point
Re: A good time based way to program

Quote:
Originally Posted by ianonavy View Post
Code:
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.SpeedController;

...

/**
 * Sets a motor to a certain power value for a certain number of seconds
 * @param controller The speed controller of the motor to power.
 * @param power The desired power for the motor.
 * @param time The time to run in seconds.
 */
public void setForTime(SpeedController controller, double power, double time) {
    controller.set(power);
    Timer.delay(time);
    controller.set(0);
}
Would something like this work? Warning: that code is not tested.
I can't test that either until Saturday, but wouldn't that delay the whole main operatorControl() while loop?

If setForTime() is called from drive() and when Timer.delay() will be called, would the whole program stop for that much time or just setForTime()?

This is what the javadoc says:
Quote:
Pause the thread for a specified time. Pause the execution of the thread for a specified period of time given in seconds. Motors will continue to run at their last assigned values, and sensors will continue to update. Only the task containing the wait will pause until the wait time is expired.
"Only the task containing the wait will pause.." seems like that only setForTime() will be paused. So if that's the case, does everything else work normally?

Last edited by neal : 26-01-2012 at 23:21.
Reply With Quote
  #4   Spotlight this post!  
Unread 26-01-2012, 23:25
Lalaland1125 Lalaland1125 is offline
Registered User
AKA: Ethan Steinberg
FRC #2429
Team Role: Programmer
 
Join Date: Jan 2012
Rookie Year: 2011
Location: La Canada
Posts: 29
Lalaland1125 is an unknown quantity at this point
Re: A good time based way to program

How about you create a priority queue of sorts?

(Note that the following is a fake priority queue, it is O(n), while a sorted queue should have a much lower runtime cost)

Code:
class TimerQueue
{

   static class Pair
   {
        Runnable runnable;
        long timeWhenToRunTask;
    }

   Vector<Pair> myTasks;
   
   public void schedule(Runnable task, long timeTillTask)
   {
       myTasks.addElement( new Pair(task,timeTillTask + System.currentTimeInMillisecs() ));
   }

   public void update()
   {
        for (int i = 0; i < myTasks.size(); i++)
        {
            Pair p = myTasks.get(i);
            if (p.timeWhenToRunTask < System.currentTimeInMillisecs() )
            {   
                p.task.run();
                myTasks.remove(i); 
                i--;
            }
        }
   }
}
Warning: Unstested code, use at own risk

Then you could simply schedule tasks and they will run after the certain time has passed, assuming you call update every "tick".

I know threading can do the same things, but I do not trust neither myself nor the cRIO with threading.

Last edited by Lalaland1125 : 26-01-2012 at 23:42.
Reply With Quote
  #5   Spotlight this post!  
Unread 26-01-2012, 23:38
ianonavy ianonavy is offline
Programming Mentor/Alumnus
AKA: Ian Adam Naval
FRC #3120 (RoboKnights)
Team Role: Mentor
 
Join Date: Dec 2010
Rookie Year: 2008
Location: Sherman Oaks
Posts: 32
ianonavy is an unknown quantity at this point
Re: A good time based way to program

Quote:
Originally Posted by neal View Post
I can't test that either until Saturday, but wouldn't that delay the whole main operatorControl() while loop?
Oh, I see. You're using the SimpleRobot class. I recommend that you really look into the command-based robot implementation. It makes everything a lot more object-oriented, and it's really nice. There should be a sample project called GearsBot if you've updated your NetBeans FRC plugins for this year's competition.

If you are going to stick with that style, you could try something like this

Code:
// Buttons
        final int MOVE_ARM = 1;
        
        // Constants
        final int ARM_PWM = 1;
        final double ARM_SPEED = 1.0;
        final int ARM_TIME = 1.0; // seconds
        
        // Should be instance fields
        Timer armTimer = new Timer();
        Jaguar arm = new Jaguar(ARM_PWM);
        Joystick joystick = new Joystick(1);
        
        armTimer.reset();
        armTimer.stop();
        while (isOperatorControl() && isEnabled()) {
            if (joystick.rawButton(MOVE_ARM)) {
                armTimer.start();
            }
            if (armTimer.get() / 1e6 < ARM_TIME) {
                arm.set(ARM_SPEED);
            } else {
                arm.set(0);
                armTimer.stop(); 
                armTimer.reset();
            }
        }
I don't know that it's the best way of approaching it. There's probably some better way of putting them into separate functions. Essentially, you're going to have separate Timer objects for each motor. You could extend the Jaguar class and make something like a TimedJaguar.

Undocumented and untested quick example:

Code:
public class TimedJaguar extends Jaguar{
    Timer timer;

    public TimedJaguar() {
        timer = new Timer();
        timer.reset();
    }

    public void setForTime(double power, double time) {
        // Implement here.
    }
}
Reply With Quote
  #6   Spotlight this post!  
Unread 27-01-2012, 03:42
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,187
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: A good time based way to program

Look at the updated documentation for 2012 WPILib. I think the command based robot is exactly what you are looking for.
Reply With Quote
  #7   Spotlight this post!  
Unread 27-01-2012, 13:25
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 590
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: A good time based way to program

Quote:
Originally Posted by Tom Bottiglieri View Post
Look at the updated documentation for 2012 WPILib. I think the command based robot is exactly what you are looking for.
I agree, and you can specify timeouts for commands. Look at the WPILib Cookbook and find the section on using timeouts. The stuff you're looking for is built in. It will only do the delay for a single command and everything else will keep running.

You might also check out the videos at http://youtube.com/user/bradamiller. It explains how to write the GearsBot sample program step by step.

Brad
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
Reply With Quote
  #8   Spotlight this post!  
Unread 27-01-2012, 17:54
neal's Avatar
neal neal is offline
Neal
FRC #1777 (Viking Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2009
Location: United States
Posts: 56
neal is an unknown quantity at this point
Re: A good time based way to program

Thanks all for your suggestions. I'm trying to just stick with SimpleRobot for this year since I don't think we have enough time now to write it for CommandBased one, and to test, troubleshoot it.

Brad, does timeouts also work with SimpleRobot, I checked the cookbook and it only shows examples for the command-based bot.
Reply With Quote
  #9   Spotlight this post!  
Unread 28-01-2012, 03:44
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 590
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: A good time based way to program

Quote:
Originally Posted by neal View Post
Thanks all for your suggestions. I'm trying to just stick with SimpleRobot for this year since I don't think we have enough time now to write it for CommandBased one, and to test, troubleshoot it.

Brad, does timeouts also work with SimpleRobot, I checked the cookbook and it only shows examples for the command-based bot.
Unfortunately it doesn't. You're correct that if you do a Timer.delay() it will stop the thread, and the rest of the program. If you want overlapped stuff happening then, as you suggested, you can use timers. Then you'll need a loop that checks if the time is up on the drive system while continuing to service the other subsystems. For trying to do a lot of overlapping operations I'd prefer to use the IterativeRobot base class. Then the looping is built in.

Brad
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
Reply With Quote
  #10   Spotlight this post!  
Unread 28-01-2012, 19:10
neal's Avatar
neal neal is offline
Neal
FRC #1777 (Viking Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2009
Location: United States
Posts: 56
neal is an unknown quantity at this point
Re: A good time based way to program

Thanks!

We'll probably just use Command Based Robot. Just started writing it again and looking through the cookbook and GearsBot.
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 13:13.

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