How to program autonomous in new command based format

Hello again,
For our team, I tried following the WPILIB documentation for autonomous drive, just making the robot drive forward for competition and it is not moving the robot when autonomous is called. If anyone can show me their team’s autonomous code, that’ll help us a lot!

1 Like

Your auto method will only drive the robot if you if timer.get() returns a value less than 2. You also never reset, nor start the timer, so I don’t think that’s going to work the way you think it does.

I’m not sure why you are doing that there, you probably want that in your AutoCommand, not the Drivetrain subsystem. Here are some sample edits you could make to your AutoCommand to drive the robot for two seconds:

public class AutoCommand extends CommandBase {
  /**
   * Creates a new AutoCommand.
   */
  DrivingSubsystem drivingSubsystem;
  public AutoCommand(DrivingSubsystem subsystem) {
    // Use addRequirements() here to declare subsystem dependencies.
    drivingSubsystem = subsystem;
    addRequirements(drivingSubsystem);
    timer = new Timer();
  }

  // Called when the command is initially scheduled.
  @Override
  public void initialize() {
      timer.reset();
      timer.start();
  }

  // Called every time the scheduler runs while the command is scheduled.
  @Override
  public void execute() {
    drivingSubsystem.arcadeDrive(.5,0);// drive straight at half speed
  }

  // Called once the command ends or is interrupted.
  @Override
  public void end(boolean interrupted) {
  }

  // Returns true when the command should end.
  @Override
  public boolean isFinished() {
    return timer.get() > 2;  // end the command if we have run for at least 2 seconds
  }
}
4 Likes

Ok thank you; however, my code still does not work properly after I implemented this into my AutoCommand file, is there anything in Robot I have to change?

Agreed. While I’ve never seen it written this way, my understanding is that the subsystem should “live in the moment.” All of it’s information should either be stable (e.g. port numbers) or constantly updated/refreshed (e.g. throttle/target settings, sensor feedback, and PID state).

What is it actually doing? What is/isn’t moving, what are the motor controller LEDs indicating, what’s coming up on the dashboard?

The robot doesn’t drive forward using the execute method in AutoCommand during autonomous, my theory is that I didn’t use the command correctly in autonomousInit in the Robot file.

OK, first sanity check: when this execute method is called, send a message back to your dashboard. Be absolutely certain if it’s actually called or not.
(This is really the top rule of troubleshooting software and circuitry; question every assumption and try to find a simple way to verify whether it’s true.)

Okay thank you for your help! I just had a simple error in my Robot file that did not make autonomous run, but now it works.

Maybe you’ve fixed these since the last push to GitHub, but

A strange thing I noticed: you have
DrivingSubsystem drivingSubsystem = new DrivingSubsystem();
in both RobotContainer.java and DrivingSubsystem.java. There should only be one new DrivingSubsystem(), in RobotContainer.

Also, the stuff in initDrive() usually goes in the constructor (that is a method with the same name as the class). As you don’t have a constructor method, the SubSystemBase() initializer will run, as that is the class you extended.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.