Troubles with command class


#1

We have been struggling with our command classes since we updated to VSCode. We are binding buttons to commands on the OI class, but they don’t seem to execute the command. Here is the code for OI and Command class. Thank you in advance!

package frc.robot;

import frc.robot.commands.*;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.Button;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

/**
 * This class is the glue that binds the controls on the physical operator
 * interface to the commands and command groups that allow control 
of the robot.
*/


public class OI 
{

    public Joystick driverController = new Joystick(RobotMap.DRIVER_USB_PORT);

public Button D1 = new JoystickButton(driverController, 1);
public Button D2 = new JoystickButton(driverController, 2);
public Button D3 = new JoystickButton(driverController, 3);
public Button D4 = new JoystickButton(driverController, 4);
public Button D5 = new JoystickButton(driverController, 5);
public Button D6 = new JoystickButton(driverController, 6);
public Button D7 = new JoystickButton(driverController, 7);
public Button D8 = new JoystickButton(driverController, 8);
public Button D9 = new JoystickButton(driverController, 9);
public Button D10 = new JoystickButton(driverController, 10);

public OI()
{
	D1.whileHeld(new elevatorUPtoTop());
	// D2.whileHeld(new elevatorDownTObottom());
	D2.whenPressed(new ShootTime(2));

	SmartDashboard.putData("Elevator", new elevatorUPtoTop());
	SmartDashboard.putData("Print Text", new PrintText());
	SmartDashboard.putData("Shoot", new ShootTime(2));
}
}

#2

Subsystem Elevator:

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package frc.robot.subsystems;

import frc.robot.RobotMap;
import com.ctre.phoenix.motorcontrol.can.TalonSRX;
import com.ctre.phoenix.motorcontrol.ControlMode;

import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.Encoder;

public class elevator extends Subsystem {
  // Put methods for controlling this subsystem
  // here. Call these from Commands.

  // Talon for elevator
  TalonSRX elevatorTalon = new 
TalonSRX(RobotMap.ELEVATOR_CAN_ID);

  // Encoder of the elevator
  Encoder ElevatorEncoder;

  @Override
  public void initDefaultCommand() {
    // Set the default command for a subsystem here.
    // setDefaultCommand(new MySpecialCommand());
    ElevatorEncoder = new Encoder(0, 1, false, 
Encoder.EncodingType.k4X);
    ElevatorEncoder.reset();
      }

      public void stop() {
        elevatorTalon.set(ControlMode.PercentOutput, 0);
      }

   public void elevator_up(double speed) {
   elevatorTalon.set(ControlMode.PercentOutput, speed);
  }

  public void elevator_down(double speed) {
    elevatorTalon.set(ControlMode.PercentOutput, -speed);
  }

  public double getEncoder(){
return ElevatorEncoder.get();
  }

  public void resetEncoder(){ 
    ElevatorEncoder.reset();
      }

}

#3

Command class

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package frc.robot.commands;

import frc.robot.Robot;
import edu.wpi.first.wpilibj.command.Command;

public class elevatorUPtoTop extends Command {

 public elevatorUPtoTop() {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
requires(Robot.m_elevator);
  }

 // Called just before this Command runs the first time
 @Override
  protected void initialize() {
  }

  // Called repeatedly when this Command is scheduled to run
  @Override
  protected void execute() {
System.out.println("execute!");
Robot.m_elevator.elevator_up(0.5);
  }

      // Make this return true when this Command no longer needs to run 
execute()
 @Override
  protected boolean isFinished() {
    System.out.println("isFinished!");
      return isTimedOut();
 }

  // Called once after isFinished returns true
  @Override
  protected void end() {
    Robot.m_elevator.stop();
  }

  // Called when another command which requires one or more of the same
  // subsystems is scheduled to run
   @Override
  protected void interrupted() {
    end();
  }
}

#4

First thing, definitely confirm your CAN IDs (I assume you probably have).

Second, there is a stray execute()call in your command just above the isFinished() method. Not sure if that is part of the comment, or a separate line, but if its a separate line, your code probably doesn’t compile. Are you certain your build/uploads to the RIO are working?

I don’t see anything else with a quick look over. I’ll keep looking though.


#5

Like gixxy, I do not see anything obvious that would stop it from running. There are a couple things to check. BTW, gixxy, I think the stray execute() is a word wrap of the comment.

First, on the driver station, make sure your joystick is actually at the port defined by the DRIVER_USB_PORT constant.

Second, check the lights on the Talon as you attempt to drive the elevator up. It could be that it is trying but there is something mechanical stopping it from moving.

I also have a question, are you seeing any of the System.out prints from the command? BTW, I would suggest getting familiar with using the debugger from VSCode. It is generally more productive than sprinkling System.out around.

Finally, as far as I can tell, you are not setting a timeout for the command, so isTimedOut() will always return false. This is okay in this case, but if there is some code setting the timeout that we do not see here, it could explain the behavior if it was being set to 0 or close to 0. I think eventually you will want to add a limit switch to the elevator and use that for isFinished.

Ok, really finally… setting up the encoder in initializedDefaultCommand is rather strange. I think you should do it in a constructor instead.

I hope this helps,
Steve


#6

Hey gixxy,

First thing, definitely confirm your CAN IDs (I assume you probably have).

The encoder/engine works perfectly. In class robot, we added:

public void teleopPeriodic() {
if (m_oi.D3.get()==true) m_elevator.elevator_up(0.5);
else m_elevator.elevator_up(0);
}

Motor/Encoder are working fine.


#7

Hey Waz,

Second, check the lights on the Talon as you attempt to drive the elevator up. It could be that it is trying but there is something mechanical stopping it from moving.

Solid Orange… No change in color. Motor is free load atm, so no possible mechanical error, and it runs with the previously added code.


#8

No System.out prints from the Command… It is like the OI does not call the class.
We have macbook ios. We don’t know how to run the debugger from VSCode, so we depend on System.out prints and the SmartDashBoard.


#9

Done :slight_smile:


#10

poteus,

Can you provide a link to your code in GitHub or elsewhere? I suspect the problem is in something we are not seeing. For example, in teleopPeriodic you now have something like:

Scheduler.getInstance().run();

If you do not, that would cause what you are seeing.

Steve


#11

OMG…
On the nose! I was missing run().

Thank you!! Get a cold one on us :slight_smile:


#12

Good to hear. Glad I could help.

Steve