Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   A good time based way to program (http://www.chiefdelphi.com/forums/showthread.php?t=101240)

neal 26-01-2012 21:37

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!

ianonavy 26-01-2012 23:05

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.

neal 26-01-2012 23:14

Re: A good time based way to program
 
Quote:

Originally Posted by ianonavy (Post 1114368)
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?

Lalaland1125 26-01-2012 23:25

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.

ianonavy 26-01-2012 23:38

Re: A good time based way to program
 
Quote:

Originally Posted by neal (Post 1114379)
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.
    }
}


Tom Bottiglieri 27-01-2012 03:42

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.

BradAMiller 27-01-2012 13:25

Re: A good time based way to program
 
Quote:

Originally Posted by Tom Bottiglieri (Post 1114456)
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

neal 27-01-2012 17:54

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.

BradAMiller 28-01-2012 03:44

Re: A good time based way to program
 
Quote:

Originally Posted by neal (Post 1114859)
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

neal 28-01-2012 19:10

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.


All times are GMT -5. The time now is 11:21.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi