Go to Post Ask not what FIRST can do for you but what you can do for FIRST. - Koko Ed [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, 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
  #2   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
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 11:21.

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