Go to Post However, as a philosopher, while you need math, you can probably talk your way out of it. - sciguy125 [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 18-01-2013, 22:50
jfitz0807 jfitz0807 is offline
Registered User
FRC #2877 (Ligerbots)
Team Role: Parent
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Newton, MA
Posts: 67
jfitz0807 is an unknown quantity at this point
Can't get a command to run

I created a Drive1 command class as follows:

public class Drive1 extends Command {
public Drive1() {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
requires(Robot.driveTrain);

// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
}
private Timer timer;
private boolean running;

// Called just before this Command runs the first time
protected void initialize() {
timer.start();
timer.reset();
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
if (running != true) {
running = true;
timer.reset() ;
}
Robot.driveTrain.runMotor1( 0.5 );
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return timer.get() > 3.0e6;
}
// Called once after isFinished returns true
protected void end() {
Robot.driveTrain.runMotor1( 0 );
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
end();
}

I would expect this command to run the motor connected to jaguar1 for 3 seconds at half speed. It's not doing anything.

I put numerous System.out.println calls throughout my code to try to see what's happening. Maybe some of this is my lack of java proficiency, but I'm having trouble understanding how this is behaving.

In my Oi constructor, I have the following:

joystickButton2 = new JoystickButton(joystick1, 2);
joystickButton2.whenPressed(new Drive1());

I expected this to construct a new Drive1() command when button2 on joystick1 is pressed. My printlns indicate that the constructor of Drive1 is being called in Disabled mode. I would not have expected this constructor to be called until the button was pressed.

Any thoughts?

The next thing I did was to construct a Drive1() command in the disabledInit() method. I expected this to construct a Drive1 command. In any case, how does this newly constructed command become runnable? Do I have to do anything else to make it's execute() method get called on every iteration?
Reply With Quote
  #2   Spotlight this post!  
Unread 19-01-2013, 12:11
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 592
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: Can't get a command to run

I noticed that you never initialize the running variable in your code, that might cause some problems. Commands have a built-in timeout mechanism for doing tasks like the one you're trying to do. So you can run a motor for half speed for 3 seconds like this:

Code:
public class  Drive1 extends Command {
    public Drive1() {

        requires(Robot.driveTrain);
        setTimeout(3);          // 3 second timeout for the command
    }

    protected void initialize() {
        Robot.driveTrain.runMotor(0.5);    // start the motor running 1/2 speed
    }

    protected void execute() {              // motor is already running, so nothing here
    }

    protected boolean isFinished() {
        return isTimedOut();                  // wait for 3 seconds to run out
    }

    protected void end() {
        Robot.driveTrain.runMotor1(0.0);  // stop the motor on the timeout
    }

    protected void interrupted() {
        end();                                      // this is here in case the command is interrupted
    }
}
All the RobotBuilder comments are removed for clarity - keep them in if you intend to keep using RobotBuilder.

This code:
Code:
joystickButton2 = new JoystickButton(joystick1, 2);
joystickButton2.whenPressed(new Drive1());
will create the Drive1 command when THIS code is executed, but it won't be SCHEDULED until the button is pressed. And this is the correct way of doing it. Otherwise, the command would be created every time the button is pressed and that would be not make the memory allocator happy.

Brad
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
Reply With Quote
  #3   Spotlight this post!  
Unread 19-01-2013, 12:44
jfitz0807 jfitz0807 is offline
Registered User
FRC #2877 (Ligerbots)
Team Role: Parent
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Newton, MA
Posts: 67
jfitz0807 is an unknown quantity at this point
Re: Can't get a command to run

Thanks for the response. I apologize, but I guess I'm still missing something. I put a println in the initialize() method, but it's not getting called. I was expecting initialize() to be called when a command is constructed. It appears that this is not the case.

What do I have to do to get the initialize() method to be called?
Reply With Quote
  #4   Spotlight this post!  
Unread 19-01-2013, 15:35
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 592
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: Can't get a command to run

The initialize() method is called when the command is scheduled. So in your case, when the button is pressed, the command should be scheduled and the initialize() method will be called once. Then the command will loop between the isFinished() and exectute().

To get the scheduler to run it needs to be called repeatedly. The idea is to put a Scheduler.run() into the AutonomousPeriodic() and TeleopPeriodic() methods. This causes it to run every 20ms (once for each driver station data update). Each call to run() will do these things:
1. look for buttons that are pressed or released and schedule commands dependent on them
2. for each of the newly scheduled commands: call the initialize() method
3. for each of the running commands: call the execute() method, then the isFinished() method. These are just run one after another for each command.

So it's important to not do any delays or long loops in any of the commands methods or it will cause the other commands to not run or have an unpredictable schedule.

So, the bottom line, make sure you have something like this:
Code:
    public void autonomousPeriodic() {
        Scheduler.getInstance().run();
    }

    public void teleopPeriodic() {
        Scheduler.getInstance().run();
    }
If you use RobotBuilder to generate the program code, it will automatically insert this code for you. Otherwise you need to be sure to do it yourself.

Brad
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
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:47.

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