Spike Coding
<< SmartDashboard | FRC | Jaguar Coding >>
In the Commands package, make a static instance of BallCollector in the CommandBase.java
package org.usfirst.frc4101.commands;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import org.usfirst.frc4101.OI;
import org.usfirst.frc4101.subsystems.BallCollecter;
import org.usfirst.frc4101.subsystems.DriveTrain;
/**
* The base for all commands. All atomic commands should subclass CommandBase.
* CommandBase stores creates and stores each control system. To access a
* subsystem elsewhere in your code in your code use CommandBase.exampleSubsystem
* @author Author
*/
public abstract class CommandBase extends Command {
public static OI oi;
// Create a single static instance of all of your subsystems
public static DriveTrain drivetrain = new DriveTrain();
public static BallCollecter ballCollecter = new BallCollecter();
// we'll add later the elevator motor
// wel'll add the the tilt shooter motor
// we'll add later the actuator to shoot
// we'll add later the the actuator to deploy the wedge
public static void init() {
// This MUST be here. If the OI creates Commands (which it very likely
// will), constructing it during the construction of CommandBase (from
// which commands extend), subsystems are not guaranteed to be
// yet. Thus, their requires() statements may grab null pointers. Bad
// news. Don't move it.
oi = new OI();
// Show what command your subsystem is running on the SmartDashboard
SmartDashboard.putData(drivetrain);
SmartDashboard.putData(ballCollecter);
}
public CommandBase(String name) {
super(name);
}
public CommandBase() {
super();
}
}
Add a new Subsystem in the subsystem package:
package org.usfirst.frc4101.subsystems;
import org.usfirst.frc4101.RobotMap;
import org.usfirst.frc4101.commands.BallCollectorStop;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.command.Subsystem;
public class BallCollecter extends Subsystem {
Relay spike;
public BallCollecter (){
super("BallCollector");
spike = new Relay(RobotMap.ballCollectorMotor);
spike.setDirection(Relay.Direction.kBoth);
}
public void initDefaultCommand() {
setDefaultCommand(new BallCollectorStop());
}
public void up(){
spike.set(Relay.Value.kForward);
spike.set(Relay.Value.kOn);
}
public void down(){
spike.set(Relay.Value.kReverse);
spike.set(Relay.Value.kOn);
}
public void stop(){
spike.set(Relay.Value.kOff);
}
}
In the Command package, add Commands:
BallCollecterStop.java
package org.usfirst.frc4101.commands;
public class BallCollecterStop extends CommandBase
{
public BallCollecterStop(){
requires(ballCollecter);
}
protected void initialize() {
}
protected void execute() {
ballCollecter.stop();
}
protected boolean isFinished() {
return false;
}
protected void end() {
}
protected void interrupted() {
}
}
BallCollecterUp.java
package org.usfirst.frc4101.commands;
public class BallCollecterUp extends CommandBase
{
public BallCollecterUp(){
requires(ballCollecter);
}
protected void initialize() {
}
protected void execute() {
ballCollecter.up();
}
protected boolean isFinished() {
return false;
}
protected void end() {
}
protected void interrupted() {
}
}
BallCollectorDown.java
package org.usfirst.frc4101.commands;
public class BallCollecterDown extends CommandBase
{
public BallCollecterDown(){
requires(ballCollecter);
}
protected void initialize() {
}
protected void execute() {
ballCollecter.down();
}
protected boolean isFinished() {
return false;
}
protected void end() {
}
protected void interrupted() {
}
}
In the templates package assign buttons to commands in OI.java
package org.usfirst.frc4101;
import org.usfirst.frc4101.commands.*;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.KinectStick;
import edu.wpi.first.wpilibj.buttons.Button;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
public class OI {
//adjust for joystick axis 1.0 means the axis goes from -1.0 to 1.0
public static final double max=1.0;
// Process operator interface input here.
Joystick joystick = new Joystick(1);
Button button1 = new JoystickButton(joystick, 1),
button2 = new JoystickButton(joystick, 2);
Button button10 = new JoystickButton(joystick, 10),
button11 = new JoystickButton(joystick, 11),
button12 = new JoystickButton(joystick, 12);
KinectStick leftArm = new KinectStick(1),
rightArm = new KinectStick(2);
public OI() {
// bind buttons to commands here, for example to test the driving modes:
button1.whenPressed(new DriveWithJoystick() );
button2.whenPressed(new DriveWithKenect() );
button10.whenPressed(new BallCollecterUp() );
button11.whenPressed(new BallCollecterDown() );
button12.whenPressed(new BallCollecterStop() );
// For testing, you can activate your commands from the SmartDashboard:
SmartDashboard.putData(new DriveWithJoystick());
SmartDashboard.putData(new DriveWithKenect());
}
public double getLeftArmSpeed() {
// For safety, reduce to 33% of speed
return leftArm.getY()*.33;
}
public double getRightArmSpeed() {
// For safety, reduce to 33% of speed
return rightArm.getY()*.33;
}
public double getLeftSpeed() {
// see http://home.kendra.com/mauser/Joystick.html for algorithm
double x=-1.0*joystick.getX(); //invert x
double y=joystick.getY();
double v=(max-Math.abs(x))*(y/max)+y;
double w=(max-Math.abs(y))*(x/max)+x;
return .5*(v-w);
}
public double getRightSpeed() {
// see http://home.kendra.com/mauser/Joystick.html for algorithm
double x=-1.0*joystick.getX(); //invert x
double y=joystick.getY();
double v=(max-Math.abs(x))*(y/max)+y;
double w=(max-Math.abs(y))*(x/max)+x;
return .5*(v+w);
}
public void updateStatus(){
// if we had a sensor like a pot or a rangefinder, we can see the status
// on the smartDashboard like this:
SmartDashboard.putDouble(" joystick X", joystick.getX());
SmartDashboard.putDouble(" joystick Y", joystick.getY());
SmartDashboard.putDouble(" kenect right", getRightArmSpeed());
SmartDashboard.putDouble(" kenect left", getLeftArmSpeed());
}
}
in the templates package update the RobotMap
ackage org.usfirst.frc4101;
/**
* The RobotMap is a mapping from the ports sensors and actuators are wired into
* to a variable name. This provides flexibility changing wiring, makes checking
* the wiring easier and significantly reduces the number of magic numbers
* floating around.
*/
public class RobotMap {
//Motors
public static final int
leftDriveMotor = 1,
rightDriveMotor = 2,
elevatorArm = 3, //vex motor
bridgePusherMotor = 4, //
upperShooterMotor = 5,
lowerShooterMoter = 6,
aimingMotor = 7,
ballCollectorMotor = 1, //Spike Relay 1
conveyorBeltMotor = 2; //Spike relay 2
}
