Replace deprecated API usage in C++ examples

Since there is a new version of GearsBot using the new command-based
API, the old GearsBot is just removed.

PR #1842 is being included to verify this PR is correct.
This commit is contained in:
Tyler Veness
2019-08-27 21:21:05 -07:00
committed by Peter Johnson
parent d6b9c7e148
commit c9f9feff1f
61 changed files with 20 additions and 2220 deletions

View File

@@ -220,16 +220,6 @@
"gradlebase": "java"
,"mainclass": "Main"
},
{
"name": "GearsBot",
"description": "A fully functional example CommandBased program for WPIs GearsBot robot. This code can run on your computer if it supports simulation.",
"tags": [
"Complete Robot"
],
"foldername": "gearsbot",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "GearsBot (New)",
"description": "A fully functional example CommandBased program for WPIs GearsBot robot, ported to the new CommandBased library. This code can run on your computer if it supports simulation.",

View File

@@ -1,29 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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 edu.wpi.first.wpilibj.examples.gearsbot;
import edu.wpi.first.wpilibj.RobotBase;
/**
* Do NOT add any static variables to this class, or any initialization at all.
* Unless you know what you are doing, do not modify this file except to
* change the parameter class to the startRobot call.
*/
public final class Main {
private Main() {
}
/**
* Main initialization function. Do not perform any initialization here.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

View File

@@ -1,72 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.Autonomous;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.CloseClaw;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.OpenClaw;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.Pickup;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.Place;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.PrepareToPickup;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.SetElevatorSetpoint;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.SetWristSetpoint;
/**
* 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 {
private final Joystick m_joystick = new Joystick(0);
/**
* Construct the OI and all of the buttons on it.
*/
public OI() {
// Put Some buttons on the SmartDashboard
SmartDashboard.putData("Elevator Bottom", new SetElevatorSetpoint(0));
SmartDashboard.putData("Elevator Platform", new SetElevatorSetpoint(0.2));
SmartDashboard.putData("Elevator Top", new SetElevatorSetpoint(0.3));
SmartDashboard.putData("Wrist Horizontal", new SetWristSetpoint(0));
SmartDashboard.putData("Raise Wrist", new SetWristSetpoint(-45));
SmartDashboard.putData("Open Claw", new OpenClaw());
SmartDashboard.putData("Close Claw", new CloseClaw());
SmartDashboard.putData("Deliver Soda", new Autonomous());
// Create some buttons
final JoystickButton dpadUp = new JoystickButton(m_joystick, 5);
final JoystickButton dpadRight = new JoystickButton(m_joystick, 6);
final JoystickButton dpadDown = new JoystickButton(m_joystick, 7);
final JoystickButton dpadLeft = new JoystickButton(m_joystick, 8);
final JoystickButton l2 = new JoystickButton(m_joystick, 9);
final JoystickButton r2 = new JoystickButton(m_joystick, 10);
final JoystickButton l1 = new JoystickButton(m_joystick, 11);
final JoystickButton r1 = new JoystickButton(m_joystick, 12);
// Connect the buttons to commands
dpadUp.whenPressed(new SetElevatorSetpoint(0.2));
dpadDown.whenPressed(new SetElevatorSetpoint(-0.2));
dpadRight.whenPressed(new CloseClaw());
dpadLeft.whenPressed(new OpenClaw());
r1.whenPressed(new PrepareToPickup());
r2.whenPressed(new Pickup());
l1.whenPressed(new Place());
l2.whenPressed(new Autonomous());
}
public Joystick getJoystick() {
return m_joystick;
}
}

View File

@@ -1,108 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.Autonomous;
import edu.wpi.first.wpilibj.examples.gearsbot.subsystems.Claw;
import edu.wpi.first.wpilibj.examples.gearsbot.subsystems.DriveTrain;
import edu.wpi.first.wpilibj.examples.gearsbot.subsystems.Elevator;
import edu.wpi.first.wpilibj.examples.gearsbot.subsystems.Wrist;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the TimedRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends TimedRobot {
Command m_autonomousCommand;
public static DriveTrain m_drivetrain;
public static Elevator m_elevator;
public static Wrist m_wrist;
public static Claw m_claw;
public static OI m_oi;
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
@Override
public void robotInit() {
// Initialize all subsystems
m_drivetrain = new DriveTrain();
m_elevator = new Elevator();
m_wrist = new Wrist();
m_claw = new Claw();
m_oi = new OI();
// instantiate the command used for the autonomous period
m_autonomousCommand = new Autonomous();
// Show what command your subsystem is running on the SmartDashboard
SmartDashboard.putData(m_drivetrain);
SmartDashboard.putData(m_elevator);
SmartDashboard.putData(m_wrist);
SmartDashboard.putData(m_claw);
}
@Override
public void autonomousInit() {
m_autonomousCommand.start(); // schedule the autonomous command (example)
}
/**
* This function is called periodically during autonomous.
*/
@Override
public void autonomousPeriodic() {
Scheduler.getInstance().run();
log();
}
@Override
public void teleopInit() {
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
m_autonomousCommand.cancel();
}
/**
* This function is called periodically during teleoperated mode.
*/
@Override
public void teleopPeriodic() {
Scheduler.getInstance().run();
log();
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {
}
/**
* The log method puts interesting information to the SmartDashboard.
*/
private void log() {
m_wrist.log();
m_elevator.log();
m_drivetrain.log();
m_claw.log();
}
}

View File

@@ -1,32 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.CommandGroup;
/**
* The main autonomous command to pickup and deliver the soda to the box.
*/
public class Autonomous extends CommandGroup {
/**
* Create a new autonomous command.
*/
public Autonomous() {
addSequential(new PrepareToPickup());
addSequential(new Pickup());
addSequential(new SetDistanceToBox(0.10));
// addSequential(new DriveStraight(4)); // Use Encoders if ultrasonic is
// broken
addSequential(new Place());
addSequential(new SetDistanceToBox(0.60));
// addSequential(new DriveStraight(-2)); // Use Encoders if ultrasonic
// is broken
addParallel(new SetWristSetpoint(-45));
addSequential(new CloseClaw());
}
}

View File

@@ -1,46 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* Closes the claw for one second. Real robots should use sensors, stalling
* motors is BAD!
*/
public class CloseClaw extends Command {
public CloseClaw() {
requires(Robot.m_claw);
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
Robot.m_claw.close();
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return Robot.m_claw.isGrabbing();
}
// Called once after isFinished returns true
@Override
protected void end() {
// NOTE: Doesn't stop in simulation due to lower friction causing the
// can to fall out
// + there is no need to worry about stalling the motor or crushing the
// can.
if (!Robot.isSimulation()) {
Robot.m_claw.stop();
}
}
}

View File

@@ -1,62 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.controller.PIDController;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* Drive the given distance straight (negative values go backwards). Uses a
* local PID controller to run a simple PID loop that is only enabled while this
* command is running. The input is the averaged values of the left and right
* encoders.
*/
public class DriveStraight extends Command {
private final PIDController m_pid = new PIDController(4, 0, 0);
/**
* Create a new DriveStraight command.
* @param distance The distance to drive
*/
public DriveStraight(double distance) {
requires(Robot.m_drivetrain);
m_pid.setAbsoluteTolerance(0.01);
m_pid.setSetpoint(distance);
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
// Get everything in a safe starting state.
Robot.m_drivetrain.reset();
m_pid.reset();
}
@Override
protected void execute() {
double pidOut = m_pid.calculate(Robot.m_drivetrain.getDistance());
Robot.m_drivetrain.drive(pidOut, pidOut);
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return m_pid.atSetpoint();
}
// Called once after isFinished returns true
@Override
protected void end() {
// Stop the wheels
Robot.m_drivetrain.drive(0, 0);
}
}

View File

@@ -1,35 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.TimedCommand;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* Opens the claw for one second. Real robots should use sensors, stalling
* motors is BAD!
*/
public class OpenClaw extends TimedCommand {
public OpenClaw() {
super(1);
requires(Robot.m_claw);
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
Robot.m_claw.open();
}
// Called once after isFinished returns true
@Override
protected void end() {
Robot.m_claw.stop();
}
}

View File

@@ -1,25 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.CommandGroup;
/**
* Pickup a soda can (if one is between the open claws) and get it in a safe
* state to drive around.
*/
public class Pickup extends CommandGroup {
/**
* Create a new pickup command.
*/
public Pickup() {
addSequential(new CloseClaw());
addParallel(new SetWristSetpoint(-45));
addSequential(new SetElevatorSetpoint(0.25));
}
}

View File

@@ -1,24 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.CommandGroup;
/**
* Place a held soda can onto the platform.
*/
public class Place extends CommandGroup {
/**
* Create a new place command.
*/
public Place() {
addSequential(new SetElevatorSetpoint(0.25));
addSequential(new SetWristSetpoint(0));
addSequential(new OpenClaw());
}
}

View File

@@ -1,24 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.CommandGroup;
/**
* Make sure the robot is in a state to pickup soda cans.
*/
public class PrepareToPickup extends CommandGroup {
/**
* Create a new prepare to pickup command.
*/
public PrepareToPickup() {
addParallel(new OpenClaw());
addParallel(new SetWristSetpoint(0));
addSequential(new SetElevatorSetpoint(0));
}
}

View File

@@ -1,62 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.controller.PIDController;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* Drive until the robot is the given distance away from the box. Uses a local
* PID controller to run a simple PID loop that is only enabled while this
* command is running. The input is the averaged values of the left and right
* encoders.
*/
public class SetDistanceToBox extends Command {
private final PIDController m_pid = new PIDController(-2, 0, 0);
/**
* Create a new set distance to box command.
* @param distance The distance away from the box to drive to
*/
public SetDistanceToBox(double distance) {
requires(Robot.m_drivetrain);
m_pid.setAbsoluteTolerance(0.01);
m_pid.setSetpoint(distance);
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
// Get everything in a safe starting state.
Robot.m_drivetrain.reset();
m_pid.reset();
}
@Override
protected void execute() {
double pidOut = m_pid.calculate(Robot.m_drivetrain.getDistanceToObstacle());
Robot.m_drivetrain.drive(pidOut, pidOut);
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return m_pid.atSetpoint();
}
// Called once after isFinished returns true
@Override
protected void end() {
// Stop the wheels
Robot.m_drivetrain.drive(0, 0);
}
}

View File

@@ -1,40 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* Move the elevator to a given location. This command finishes when it is
* within the tolerance, but leaves the PID loop running to maintain the
* position. Other commands using the elevator should make sure they disable
* PID!
*/
public class SetElevatorSetpoint extends Command {
private final double m_setpoint;
public SetElevatorSetpoint(double setpoint) {
m_setpoint = setpoint;
requires(Robot.m_elevator);
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
Robot.m_elevator.enable();
Robot.m_elevator.setSetpoint(m_setpoint);
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return Robot.m_elevator.onTarget();
}
}

View File

@@ -1,39 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* Move the wrist to a given angle. This command finishes when it is within the
* tolerance, but leaves the PID loop running to maintain the position. Other
* commands using the wrist should make sure they disable PID!
*/
public class SetWristSetpoint extends Command {
private final double m_setpoint;
public SetWristSetpoint(double setpoint) {
m_setpoint = setpoint;
requires(Robot.m_wrist);
}
// Called just before this Command runs the first time
@Override
protected void initialize() {
Robot.m_wrist.enable();
Robot.m_wrist.setSetpoint(m_setpoint);
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return Robot.m_wrist.onTarget();
}
}

View File

@@ -1,39 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.commands;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* Have the robot drive tank style using the PS3 Joystick until interrupted.
*/
public class TankDriveWithJoystick extends Command {
public TankDriveWithJoystick() {
requires(Robot.m_drivetrain);
}
// Called repeatedly when this Command is scheduled to run
@Override
protected void execute() {
Robot.m_drivetrain.drive(Robot.m_oi.getJoystick());
}
// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
return false; // Runs until interrupted
}
// Called once after isFinished returns true
@Override
protected void end() {
Robot.m_drivetrain.drive(0, 0);
}
}

View File

@@ -1,70 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.subsystems;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.command.Subsystem;
/**
* The claw subsystem is a simple system with a motor for opening and closing.
* If using stronger motors, you should probably use a sensor so that the motors
* don't stall.
*/
public class Claw extends Subsystem {
private final Victor m_motor = new Victor(7);
private final DigitalInput m_contact = new DigitalInput(5);
/**
* Create a new claw subsystem.
*/
public Claw() {
super();
// Let's name everything on the LiveWindow
addChild("Motor", m_motor);
addChild("Limit Switch", m_contact);
}
@Override
public void initDefaultCommand() {
}
public void log() {
}
/**
* Set the claw motor to move in the open direction.
*/
public void open() {
m_motor.set(-1);
}
/**
* Set the claw motor to move in the close direction.
*/
@Override
public void close() {
m_motor.set(1);
}
/**
* Stops the claw motor from moving.
*/
public void stop() {
m_motor.set(0);
}
/**
* Return true when the robot is grabbing an object hard enough to trigger
* the limit switch.
*/
public boolean isGrabbing() {
return m_contact.get();
}
}

View File

@@ -1,146 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-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 edu.wpi.first.wpilibj.examples.gearsbot.subsystems;
import edu.wpi.first.wpilibj.AnalogGyro;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.PWMVictorSPX;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
import edu.wpi.first.wpilibj.examples.gearsbot.commands.TankDriveWithJoystick;
/**
* The DriveTrain subsystem incorporates the sensors and actuators attached to
* the robots chassis. These include four drive motors, a left and right encoder
* and a gyro.
*/
public class DriveTrain extends Subsystem {
private final SpeedController m_leftMotor
= new SpeedControllerGroup(new PWMVictorSPX(0), new PWMVictorSPX(1));
private final SpeedController m_rightMotor
= new SpeedControllerGroup(new PWMVictorSPX(2), new PWMVictorSPX(3));
private final DifferentialDrive m_drive
= new DifferentialDrive(m_leftMotor, m_rightMotor);
private final Encoder m_leftEncoder = new Encoder(1, 2);
private final Encoder m_rightEncoder = new Encoder(3, 4);
private final AnalogInput m_rangefinder = new AnalogInput(6);
private final AnalogGyro m_gyro = new AnalogGyro(1);
/**
* Create a new drive train subsystem.
*/
public DriveTrain() {
super();
// Encoders may measure differently in the real world and in
// simulation. In this example the robot moves 0.042 barleycorns
// per tick in the real world, but the simulated encoders
// simulate 360 tick encoders. This if statement allows for the
// real robot to handle this difference in devices.
if (Robot.isReal()) {
m_leftEncoder.setDistancePerPulse(0.042);
m_rightEncoder.setDistancePerPulse(0.042);
} else {
// Circumference in ft = 4in/12(in/ft)*PI
m_leftEncoder.setDistancePerPulse((4.0 / 12.0 * Math.PI) / 360.0);
m_rightEncoder.setDistancePerPulse((4.0 / 12.0 * Math.PI) / 360.0);
}
// Let's name the sensors on the LiveWindow
addChild("Drive", m_drive);
addChild("Left Encoder", m_leftEncoder);
addChild("Right Encoder", m_rightEncoder);
addChild("Rangefinder", m_rangefinder);
addChild("Gyro", m_gyro);
}
/**
* When no other command is running let the operator drive around using the
* PS3 joystick.
*/
@Override
public void initDefaultCommand() {
setDefaultCommand(new TankDriveWithJoystick());
}
/**
* The log method puts interesting information to the SmartDashboard.
*/
public void log() {
SmartDashboard.putNumber("Left Distance", m_leftEncoder.getDistance());
SmartDashboard.putNumber("Right Distance", m_rightEncoder.getDistance());
SmartDashboard.putNumber("Left Speed", m_leftEncoder.getRate());
SmartDashboard.putNumber("Right Speed", m_rightEncoder.getRate());
SmartDashboard.putNumber("Gyro", m_gyro.getAngle());
}
/**
* Tank style driving for the DriveTrain.
*
* @param left Speed in range [-1,1]
* @param right Speed in range [-1,1]
*/
public void drive(double left, double right) {
m_drive.tankDrive(left, right);
}
/**
* Tank style driving for the DriveTrain.
*
* @param joy The ps3 style joystick to use to drive tank style.
*/
public void drive(Joystick joy) {
drive(-joy.getY(), -joy.getThrottle());
}
/**
* Get the robot's heading.
*
* @return The robots heading in degrees.
*/
public double getHeading() {
return m_gyro.getAngle();
}
/**
* Reset the robots sensors to the zero states.
*/
public void reset() {
m_gyro.reset();
m_leftEncoder.reset();
m_rightEncoder.reset();
}
/**
* Get the average distance of the encoders since the last reset.
*
* @return The distance driven (average of left and right encoders).
*/
public double getDistance() {
return (m_leftEncoder.getDistance() + m_rightEncoder.getDistance()) / 2;
}
/**
* Get the distance to the obstacle.
*
* @return The distance to the obstacle detected by the rangefinder.
*/
public double getDistanceToObstacle() {
// Really meters in simulation since it's a rangefinder...
return m_rangefinder.getAverageVoltage();
}
}

View File

@@ -1,84 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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 edu.wpi.first.wpilibj.examples.gearsbot.subsystems;
import edu.wpi.first.wpilibj.AnalogPotentiometer;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.command.PIDSubsystem;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* The elevator subsystem uses PID to go to a given height. Unfortunately, in
* it's current state PID values for simulation are different than in the real
* world do to minor differences.
*/
public class Elevator extends PIDSubsystem {
private final Victor m_motor;
private final AnalogPotentiometer m_pot;
private static final double kP_real = 4;
private static final double kI_real = 0.07;
private static final double kP_simulation = 18;
private static final double kI_simulation = 0.2;
/**
* Create a new elevator subsystem.
*/
public Elevator() {
super(kP_real, kI_real, 0);
if (Robot.isSimulation()) { // Check for simulation and update PID values
getPIDController().setPID(kP_simulation, kI_simulation, 0, 0);
}
setAbsoluteTolerance(0.005);
m_motor = new Victor(5);
// Conversion value of potentiometer varies between the real world and
// simulation
if (Robot.isReal()) {
m_pot = new AnalogPotentiometer(2, -2.0 / 5);
} else {
m_pot = new AnalogPotentiometer(2); // Defaults to meters
}
// Let's name everything on the LiveWindow
addChild("Motor", m_motor);
addChild("Pot", m_pot);
}
@Override
public void initDefaultCommand() {
}
/**
* The log method puts interesting information to the SmartDashboard.
*/
public void log() {
SmartDashboard.putData("Elevator Pot", m_pot);
}
/**
* Use the potentiometer as the PID sensor. This method is automatically
* called by the subsystem.
*/
@Override
protected double returnPIDInput() {
return m_pot.get();
}
/**
* Use the motor as the PID output. This method is automatically called by
* the subsystem.
*/
@Override
protected void usePIDOutput(double power) {
m_motor.set(power);
}
}

View File

@@ -1,81 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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 edu.wpi.first.wpilibj.examples.gearsbot.subsystems;
import edu.wpi.first.wpilibj.AnalogPotentiometer;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.command.PIDSubsystem;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* The wrist subsystem is like the elevator, but with a rotational joint instead
* of a linear joint.
*/
public class Wrist extends PIDSubsystem {
private final Victor m_motor;
private final AnalogPotentiometer m_pot;
private static final double kP_real = 1;
private static final double kP_simulation = 0.05;
/**
* Create a new wrist subsystem.
*/
public Wrist() {
super(kP_real, 0, 0);
if (Robot.isSimulation()) { // Check for simulation and update PID values
getPIDController().setPID(kP_simulation, 0, 0, 0);
}
setAbsoluteTolerance(2.5);
m_motor = new Victor(6);
// Conversion value of potentiometer varies between the real world and
// simulation
if (Robot.isReal()) {
m_pot = new AnalogPotentiometer(3, -270.0 / 5);
} else {
m_pot = new AnalogPotentiometer(3); // Defaults to degrees
}
// Let's name everything on the LiveWindow
addChild("Motor", m_motor);
addChild("Pot", m_pot);
}
@Override
public void initDefaultCommand() {
}
/**
* The log method puts interesting information to the SmartDashboard.
*/
public void log() {
SmartDashboard.putData("Wrist Angle", m_pot);
}
/**
* Use the potentiometer as the PID sensor. This method is automatically
* called by the subsystem.
*/
@Override
protected double returnPIDInput() {
return m_pot.get();
}
/**
* Use the motor as the PID output. This method is automatically called by
* the subsystem.
*/
@Override
protected void usePIDOutput(double power) {
m_motor.set(power);
}
}

View File

@@ -9,8 +9,6 @@ package edu.wpi.first.wpilibj.examples.gearsbotnew;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
/**
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what
* you are doing, do not modify this file except to change the parameter class to the startRobot

View File

@@ -17,7 +17,7 @@ import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
import edu.wpi.first.wpilibj.examples.gearsbotnew.Robot;
public class DriveTrain extends SubsystemBase {
/**

View File

@@ -13,7 +13,7 @@ import edu.wpi.first.wpilibj.controller.PIDController;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.PIDSubsystem;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
import edu.wpi.first.wpilibj.examples.gearsbotnew.Robot;
/**
* The elevator subsystem uses PID to go to a given height. Unfortunately, in it's current state PID

View File

@@ -13,7 +13,7 @@ import edu.wpi.first.wpilibj.controller.PIDController;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.PIDSubsystem;
import edu.wpi.first.wpilibj.examples.gearsbot.Robot;
import edu.wpi.first.wpilibj.examples.gearsbotnew.Robot;
/**
* The wrist subsystem is like the elevator, but with a rotational joint instead of a linear joint.