Thanks again NewtonCrosby.
Based on the presentation you suggested and your comments, I tried to assign “button8” to run a command called “AutoRotate”. When I press button8, it calls AutoRotate, but doesn’t call the execute. Any ideas?
package org.usfirst.frc4097.DeepSpace;
import org.usfirst.frc4097.DeepSpace.commands.*;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import org.usfirst.frc4097.DeepSpace.subsystems.*;
/**
* 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 joystick1;
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
public OI() {
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
joystick1 = new Joystick(0);
// SmartDashboard Buttons
SmartDashboard.putData("Autonomous Command", new AutonomousCommand());
SmartDashboard.putData("OperatorDrive", new OperatorDrive());
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
JoystickButton button8 = new JoystickButton(joystick1, 8);
button8.whenPressed(new AutoRotate());
}
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=FUNCTIONS
public Joystick getJoystick1() {
return joystick1;
}
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=FUNCTIONS
}
Here’s the code for AutoRotate:
/----------------------------------------------------------------------------/
/* 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 org.usfirst.frc4097.DeepSpace.commands;
import org.usfirst.frc4097.DeepSpace.Robot;
import org.usfirst.frc4097.DeepSpace.subsystems.DriveTrain;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
public class AutoRotate extends Command {
double startAngle;
public AutoRotate() {
// Use requires() here to declare subsystem dependencies
requires(Robot.driveTrain);
// eg. requires(chassis);
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
startAngle = Robot.driveTrain.getAngle();
SmartDashboard.putString(“AutoRotate”, “hello”);
SmartDashboard.putNumber(“StartAngle”,this.startAngle);
}
// Called repeatedly when this Command is scheduled to run
@Override
protected void execute() {
Robot.driveTrain.tankDrive(.4, -.4);
SmartDashboard.putString(“Executing”,“hello”);
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
boolean done;
if (Math.abs(this.startAngle - Robot.driveTrain.getAngle()) >= 90){
done = true;
}
else {
done = false;
}
return done;
}
// Called once after isFinished returns true
@Override
protected void end() {
Robot.driveTrain.stop();
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
@Override
protected void interrupted() {
}
}
Finally, here’s our DriveTrain code:
// RobotBuilder Version: 2.0
//
// This file was generated by RobotBuilder. It contains sections of
// code that are automatically generated and assigned by robotbuilder.
// These sections will be updated in the future when you export to
// Java from RobotBuilder. Do not put any code or make any change in
// the blocks indicating autogenerated code or it will be lost on an
// update. Deleting the comments indicating the section will prevent
// it from being updated in the future.
package org.usfirst.frc4097.DeepSpace.subsystems;
import org.usfirst.frc4097.DeepSpace.commands.*;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.PIDOutput;
import edu.wpi.first.wpilibj.PIDSource;
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.VictorSP;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.interfaces.Gyro;
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
import com.kauailabs.navx.frc.*;
import edu.wpi.first.wpilibj.SPI;
/**
*
*/
public class DriveTrain extends Subsystem {
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTANTS
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
private VictorSP speedController1;
private VictorSP speedController2;
private DifferentialDrive differentialDrive1;
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
double Kp = 0.03;
AHRS gyro = new AHRS(SPI.Port.kMXP);
public DriveTrain() {
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
speedController1 = new VictorSP(3);
addChild("Speed Controller 1",speedController1);
speedController1.setInverted(false);
speedController2 = new VictorSP(1);
addChild("Speed Controller 2",speedController2);
speedController2.setInverted(false);
differentialDrive1 = new DifferentialDrive(speedController1, speedController2);
addChild("Differential Drive 1",differentialDrive1);
differentialDrive1.setSafetyEnabled(true);
differentialDrive1.setExpiration(0.1);
differentialDrive1.setMaxOutput(1.0);
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
gyro.reset();
}
@Override
public void initDefaultCommand() {
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DEFAULT_COMMAND
setDefaultCommand(new OperatorDrive());
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DEFAULT_COMMAND
// Set the default command for a subsystem here.
// setDefaultCommand(new MySpecialCommand());
}
@Override
public void periodic() {
// Put code here to be run every loop
}
public void arcadeDrive(double xSpeed, double zRotation) {
differentialDrive1.arcadeDrive(xSpeed, zRotation);
}
public void driveStraight(double moveSpeed){
//used double movespeed as parameter
//double currentAngle = this.getAngle();
//arcadeDrive(moveSpeed, -currentAngle*Kp);
//the above, when called with a conditional, caused it to make a snap sound and turn slightly
//differentialDrive1.arcadeDrive(moveSpeed, 0);
differentialDrive1.tankDrive(moveSpeed, moveSpeed);
}
public void curvatureDrive(double xSpeed, double zRotation) {
differentialDrive1.curvatureDrive(xSpeed, zRotation, true);
}
public void tankDrive(double leftSpeed, double rightSpeed){
tankDrive(leftSpeed, rightSpeed);
}
public double getAngle(){
double z = gyro.getAngle();
if (z>=360){
z=z-360;
//makes sure that angle output is always between 0 and 360
}
else if (z<=-360){
z=z+360;
}
return z;
}
public void stop() {
tankDrive(0,0);
}
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CMDPIDGETTERS
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CMDPIDGETTERS
// Put methods for controlling this subsystem
// here. Call these from Commands.
}
Thanks for your help!
Dave