Go to Post I see people show up to a competition with their masterpiece, and proud of it. They are inspired by what they have accomplished. To make it onto the field is a high-it is the culmination of the thousands of hours of dedication compromises and commitment. They feel on top of the world. That's FIRST. - fox46 [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 09-02-2012, 08:22
touchdownjesus4's Avatar
touchdownjesus4 touchdownjesus4 is offline
Registered User
AKA: Tyler Vonderhaar
FRC #4028 (Eagle Robotics)
Team Role: Programmer
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Cincinnati, OH
Posts: 64
touchdownjesus4 is on a distinguished road
Set command to run for a certain amount of time

I need a motor to run for a short duration when I click a button. I already have it working to run continuously when I hit the button but im not sure how to get it to only run for like half a second or something.

I'm sure this is a simple fix I'm just a rookie programmer so could anyone walk me through this?

FYI, I'm using the command based template from WPI.
__________________
2012 Boilermaker Regional Champions (With 1756 & 1501)
Team Captain, Programmer, Lead Driver
Reply With Quote
  #2   Spotlight this post!  
Unread 09-02-2012, 09:16
Brian Selle's Avatar
Brian Selle Brian Selle is offline
Mentor
FRC #3310 (Black Hawk Robotics)
Team Role: Engineer
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Texas
Posts: 170
Brian Selle has a spectacular aura aboutBrian Selle has a spectacular aura aboutBrian Selle has a spectacular aura about
Re: Set command to run for a certain amount of time

Here is a command that uses a timer. When you create an instance of the command you pass in the time (seconds) you want it to run.


package edu.rhhs.frc.commands;

/**
* @author rhhs
*/
public class ConveyorTimed extends CommandBase {

private double m_timeout;

public ConveyorTimed(double timeout) {
m_timeout = timeout;
requires(m_conveyor);
}

protected void initialize() {
setTimeout(m_timeout);
m_conveyor.start();
}

protected void execute() {
}

protected boolean isFinished() {
return isTimedOut();
}

protected void end() {
m_conveyor.stop();
}

protected void interrupted() {
m_conveyor.stop();
}
}
Reply With Quote
  #3   Spotlight this post!  
Unread 09-02-2012, 16:40
Patrick Chiang Patrick Chiang is offline
Programming
FRC #3070 (Team Pronto)
Team Role: Mentor
 
Join Date: Feb 2009
Rookie Year: 2009
Location: Seattle
Posts: 162
Patrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to all
Re: Set command to run for a certain amount of time

If you don't want something big and fancy like ^ that guy suggested , I would use the FPGA time method in the Utility class.

Code:
long millisecondsToRun = 1000; // This should run 1000ms = 1 s.
long initTime = Utility.getFPGATime();
while (Utility.getFPGATime() - initTime <= millisecondsToRun){
    // Place your code here.
}
What this does is:
Utility.getFPGATime() gives you a time in milliseconds, say 123400 ms. Now, you want to run your code for a continuous 2 seconds. You would then set the while loop to run your code UNTIL 2 seconds or 2000 ms has passed. So, the loop would be to run until the FPGA time is equals or greater than 125400 ms. And then, it stops running because the conditional is no longer true.
Reply With Quote
  #4   Spotlight this post!  
Unread 09-02-2012, 16:43
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: Set command to run for a certain amount of time

Quote:
Originally Posted by Patrick Chiang View Post
the loop would be to run until the FPGA time is equals or greater than 125400 ms. And then, it stops running because the conditional is no longer true.
this is called "busy waiting" or "spinning" and it chews up CPU time.

not the best way to do things.

http://en.wikipedia.org/wiki/Busy_waiting

http://citeseerx.ist.psu.edu/viewdoc...=rep1&type=pdf

Reply With Quote
  #5   Spotlight this post!  
Unread 09-02-2012, 16:53
Patrick Chiang Patrick Chiang is offline
Programming
FRC #3070 (Team Pronto)
Team Role: Mentor
 
Join Date: Feb 2009
Rookie Year: 2009
Location: Seattle
Posts: 162
Patrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to all
Re: Set command to run for a certain amount of time

Quote:
Originally Posted by Ether View Post
this is called "busy waiting" or "spinning" and it chews up CPU time.

not the best way to do things.

http://en.wikipedia.org/wiki/Busy_waiting

http://citeseerx.ist.psu.edu/viewdoc...=rep1&type=pdf

Eh. I did say it was not big and fancy right?

Well, he could put that in its own thread implementation, but they are first year programmers after all...
Reply With Quote
  #6   Spotlight this post!  
Unread 09-02-2012, 16:57
mwtidd's Avatar
mwtidd mwtidd is offline
Registered User
AKA: mike
FRC #0319 (Big Bad Bob)
Team Role: Mentor
 
Join Date: Feb 2005
Rookie Year: 2003
Location: Boston, MA
Posts: 714
mwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond repute
Re: Set command to run for a certain amount of time

Also if you don't need interrupts it may be helpful to have a timed command class:

This class is designed for commands that don't have interrupts and have static motor speeds.

If you wanted dynamic motor speeds you wouldn't want to override execute.

Code:
public abstract class TimedCommand extends Command{
    
    public TimedCommand(double time){
        super(time);
    }
    
    @Override
    protected void execute() {
        //do nothing
    }

    @Override
    protected boolean isFinished() {
        return isTimedOut();
    }


    @Override
    protected void interrupted() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
Code:
public class RunMotor extends TimedCommand{
    
    private static double time = 1.0;
    
    public RunMotor(){
        super(time);
    }

    @Override
    protected void initialize() {
        motor.set(1);
    }

    @Override
    protected void end() {
        motor.set(0);
    }
}
__________________
"Never let your schooling interfere with your education" -Mark Twain

Last edited by mwtidd : 09-02-2012 at 16:59.
Reply With Quote
  #7   Spotlight this post!  
Unread 09-02-2012, 17:05
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: Set command to run for a certain amount of time

Quote:
Originally Posted by Patrick Chiang View Post
he could put that in its own thread
It still chews up CPU time even if it's in its own thread.

Reply With Quote
  #8   Spotlight this post!  
Unread 09-02-2012, 17:15
mwtidd's Avatar
mwtidd mwtidd is offline
Registered User
AKA: mike
FRC #0319 (Big Bad Bob)
Team Role: Mentor
 
Join Date: Feb 2005
Rookie Year: 2003
Location: Boston, MA
Posts: 714
mwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond reputemwtidd has a reputation beyond repute
Re: Set command to run for a certain amount of time

Or you could try this:

The nice thing about this, for all of your timed commands you only have to define what its doing when its running.

Code:
public abstract class Mechanism extends Subsystem{
    
    public abstract void stop();
    
}
Code:
public abstract class TimedCommand extends Command{
    
    private Mechanism mechanism;
    
    public TimedCommand(Mechanism mechanism, double time){
        super(time);
        this.mechanism = mechanism;
        requires(mechanism);
    }
    
    @Override
    protected void execute() {
        //do nothing
    }

    @Override
    protected boolean isFinished() {
        return isTimedOut();
    }


    @Override
    protected void interrupted() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    protected void end() {
        mechanism.stop();
    }
    
}
Code:
public class RunMotor extends TimedCommand{
    
    private static double time = 1.0;
    
    public RunMotor(){
        super(RobotMap.SHOOTER, time);
    }

    @Override
    protected void initialize() {
        RobotMap.SHOOTER.shoot();
    }
}
__________________
"Never let your schooling interfere with your education" -Mark Twain
Reply With Quote
  #9   Spotlight this post!  
Unread 09-02-2012, 22:21
NS_Radication's Avatar
NS_Radication NS_Radication is offline
Student
AKA: Marco Schoener
FRC #1369 (Minotaur)
Team Role: Programmer
 
Join Date: Jan 2012
Rookie Year: 2009
Location: Tampa
Posts: 88
NS_Radication is an unknown quantity at this point
Re: Set command to run for a certain amount of time

You could try this. This worked for me when I had your EXACT problem.

Code:
public void autonomous()
{
    Timer.start() //starts a timer at 0 seconds
    while(isAutonomous() && isEnabled()) //run when autonomous is Clive and you are enabled
    {
        if(Timer.get() < time) //time is a double in terms of seconds
        {
            driveVariable.drive(speed, curve); //move robot at this speed with/without curve
        }
        Timer.delay(0.005); //does nothing for 5 seconds but helps refresh motors in loop
    }
    Timer.stop() //stops timer
}
Good luck and happy competition!
__________________
Team 1369
Senior
Head Programmer (Java)
Head Electrician
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:50.

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