Skip to content

Commit 959edc0

Browse files
committedJan 11, 2015
Arcade Drive Command-Based Joystick Demo
This project is based on the Command-Based approach of the WPILIB library
1 parent aa438ad commit 959edc0

File tree

15 files changed

+454
-1
lines changed

15 files changed

+454
-1
lines changed
 

‎JoystickDemo/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

‎JoystickDemo/src/org/usfirst/frc/team5518/robot/Robot.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Robot() { // initialize variables in constructor
3737
myRobot = new RobotDrive(FRONT_LEFT_MOTOR, REAR_LEFT_MOTOR,
3838
FRONT_RIGHT_MOTOR, REAR_RIGHT_MOTOR);
3939
myRobot.setExpiration(0.1);
40-
stick = new Joystick(0);
40+
stick = new Joystick(0); // set the stick to refer to joystick #0
4141
}
4242

4343
/**

‎JoystickDemo2/.classpath

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<classpath>
2+
<classpathentry kind="src" path="src"/>
3+
<classpathentry kind="var" path="wpilib" sourcepath="wpilib.sources"/>
4+
<classpathentry kind="var" path="networktables" sourcepath="networktables.sources"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

‎JoystickDemo2/.project

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JoystickDemo2</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
<nature>edu.wpi.first.wpilib.plugins.core.nature.FRCProjectNature</nature>
17+
</natures>
18+
</projectDescription>

‎JoystickDemo2/build.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Project specific information
2+
package=org.usfirst.frc.team5518.robot
3+
robot.class=${package}.Robot
4+
simulation.world.file=/usr/share/frcsim/worlds/GearsBotDemo.world

‎JoystickDemo2/build.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project name="FRC Deployment" default="deploy">
4+
5+
<!--
6+
The following properties can be defined to override system level
7+
settings. These should not be touched unless you know what you're
8+
doing. The primary use is to override the wpilib version when
9+
working with older robots that can't compile with the latest
10+
libraries.
11+
-->
12+
13+
<!-- By default the system version of WPI is used -->
14+
<!-- <property name="version" value=""/> -->
15+
16+
<!-- By default the system team number is used -->
17+
<!-- <property name="team-number" value=""/> -->
18+
19+
<!-- By default the target is set to 10.TE.AM.2 -->
20+
<!-- <property name="target" value=""/> -->
21+
22+
<!-- Any other property in build.properties can also be overridden. -->
23+
24+
<property file="${user.home}/wpilib/wpilib.properties"/>
25+
<property file="build.properties"/>
26+
<property file="${user.home}/wpilib/java/${version}/ant/build.properties"/>
27+
28+
<import file="${wpilib.ant.dir}/build.xml"/>
29+
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.usfirst.frc.team5518.robot;
2+
3+
import edu.wpi.first.wpilibj.Joystick;
4+
5+
/**
6+
* This class is the glue that binds the controls on the physical operator
7+
* interface to the commands and command groups that allow control of the robot.
8+
*/
9+
public class OI {
10+
//// CREATING BUTTONS
11+
// One type of button is a joystick button which is any button on a joystick.
12+
// You create one by telling it which joystick it's on and which button
13+
// number it is.
14+
// Joystick stick = new Joystick(port);
15+
// Button button = new JoystickButton(stick, buttonNumber);
16+
17+
// There are a few additional built in buttons you can use. Additionally,
18+
// by subclassing Button you can create custom triggers and bind those to
19+
// commands the same as any other Button.
20+
21+
//// TRIGGERING COMMANDS WITH BUTTONS
22+
// Once you have a button, it's trivial to bind it to a button in one of
23+
// three ways:
24+
25+
// Start the command when the button is pressed and let it run the command
26+
// until it is finished as determined by it's isFinished method.
27+
// button.whenPressed(new ExampleCommand());
28+
29+
// Run the command while the button is being held down and interrupt it once
30+
// the button is released.
31+
// button.whileHeld(new ExampleCommand());
32+
33+
// Start the command when the button is released and let it run the command
34+
// until it is finished as determined by it's isFinished method.
35+
// button.whenReleased(new ExampleCommand());
36+
37+
private Joystick stick;
38+
39+
public OI() {
40+
41+
// put buttons onto SmartDashboard
42+
dashboardInit();
43+
44+
// instantiate Joystick variable
45+
stick = new Joystick(0);
46+
47+
// instantiate joystick buttons here
48+
buttonsInit();
49+
50+
// connect commands to joystick buttons
51+
buttonsExec();
52+
53+
}
54+
55+
private void dashboardInit() {
56+
57+
}
58+
59+
private void buttonsInit() {
60+
61+
}
62+
63+
private void buttonsExec() {
64+
65+
}
66+
67+
public void setJoystick(int port) {
68+
stick = new Joystick(port);
69+
}
70+
71+
public Joystick getJoystick() {
72+
return stick;
73+
}
74+
75+
}
76+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
package org.usfirst.frc.team5518.robot;
3+
4+
import edu.wpi.first.wpilibj.IterativeRobot;
5+
import edu.wpi.first.wpilibj.command.Command;
6+
import edu.wpi.first.wpilibj.command.Scheduler;
7+
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
8+
9+
import org.usfirst.frc.team5518.robot.commands.ArcadeDriveJoystick;
10+
import org.usfirst.frc.team5518.robot.commands.AutonomousCmd;
11+
import org.usfirst.frc.team5518.robot.subsystems.AutonomousSys;
12+
import org.usfirst.frc.team5518.robot.subsystems.DrivingSys;
13+
14+
/**
15+
* The VM is configured to automatically run this class, and to call the
16+
* functions corresponding to each mode, as described in the IterativeRobot
17+
* documentation. If you change the name of this class or the package after
18+
* creating this project, you must also update the manifest file in the resource
19+
* directory.
20+
*/
21+
public class Robot extends IterativeRobot {
22+
23+
public static AutonomousSys autoSys;
24+
public static DrivingSys driveSys;
25+
public static OI oi;
26+
27+
Command autonomousCommand;
28+
29+
/**
30+
* This function is run when the robot is first started up and should be
31+
* used for any initialization code.
32+
*/
33+
public void robotInit() {
34+
35+
// initialize all subsystems and important classes
36+
oi = new OI();
37+
autoSys = new AutonomousSys();
38+
driveSys = new DrivingSys();
39+
40+
// instantiate the command used for the autonomous period
41+
autonomousCommand = new AutonomousCmd();
42+
}
43+
44+
public void disabledPeriodic() {
45+
Scheduler.getInstance().run();
46+
}
47+
48+
public void autonomousInit() {
49+
// schedule the autonomous command (example)
50+
if (autonomousCommand != null) autonomousCommand.start();
51+
}
52+
53+
/**
54+
* This function is called periodically during autonomous
55+
*/
56+
public void autonomousPeriodic() {
57+
Scheduler.getInstance().run();
58+
driveSys.log();
59+
}
60+
61+
public void teleopInit() {
62+
// This makes sure that the autonomous stops running when
63+
// teleop starts running. If you want the autonomous to
64+
// continue until interrupted by another command, remove
65+
// this line or comment it out.
66+
if (autonomousCommand != null) autonomousCommand.cancel();
67+
}
68+
69+
/**
70+
* This function is called when the disabled button is hit.
71+
* You can use it to reset subsystems before shutting down.
72+
*/
73+
public void disabledInit(){
74+
75+
}
76+
77+
/**
78+
* This function is called periodically during operator control
79+
*/
80+
public void teleopPeriodic() {
81+
Scheduler.getInstance().run();
82+
driveSys.log();
83+
}
84+
85+
/**
86+
* This function is called periodically during test mode
87+
*/
88+
public void testPeriodic() {
89+
LiveWindow.run();
90+
driveSys.log();
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.usfirst.frc.team5518.robot;
2+
/**
3+
* The RobotMap is a mapping from the ports sensors and actuators are wired into
4+
* to a variable name. This provides flexibility changing wiring, makes checking
5+
* the wiring easier and significantly reduces the number of magic numbers
6+
* floating around.
7+
*/
8+
public class RobotMap {
9+
// For example to map the left and right motors, you could define the
10+
// following variables to use with your drivetrain subsystem.
11+
// public static int leftMotor = 1;
12+
// public static int rightMotor = 2;
13+
14+
// If you are using multiple modules, make sure to define both the port
15+
// number and the module. For example you with a rangefinder:
16+
// public static int rangefinderPort = 1;
17+
// public static int rangefinderModule = 1;
18+
19+
public static final int FRONT_LEFT_MOTOR = 2;
20+
public static final int REAR_LEFT_MOTOR = 1;
21+
public static final int FRONT_RIGHT_MOTOR = 3;
22+
public static final int REAR_RIGHT_MOTOR = 0;
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.usfirst.frc.team5518.robot.commands;
2+
3+
import org.usfirst.frc.team5518.robot.Robot;
4+
5+
import edu.wpi.first.wpilibj.command.Command;
6+
7+
/**
8+
*
9+
*/
10+
public class ArcadeDriveJoystick extends Command {
11+
12+
public ArcadeDriveJoystick() {
13+
// Use requires() here to declare subsystem dependencies
14+
// eg. requires(chassis);
15+
requires(Robot.driveSys);
16+
}
17+
18+
// Called just before this Command runs the first time
19+
protected void initialize() {
20+
}
21+
22+
// Called repeatedly when this Command is scheduled to run
23+
protected void execute() {
24+
Robot.driveSys.drive(Robot.oi.getJoystick());
25+
}
26+
27+
// Make this return true when this Command no longer needs to run execute()
28+
protected boolean isFinished() {
29+
return false;
30+
}
31+
32+
// Called once after isFinished returns true
33+
protected void end() {
34+
Robot.driveSys.drive(0, 0);
35+
}
36+
37+
// Called when another command which requires one or more of the same
38+
// subsystems is scheduled to run
39+
protected void interrupted() {
40+
end();
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
package org.usfirst.frc.team5518.robot.commands;
3+
4+
import edu.wpi.first.wpilibj.command.Command;
5+
6+
import org.usfirst.frc.team5518.robot.Robot;
7+
import org.usfirst.frc.team5518.robot.subsystems.AutonomousSys;
8+
9+
/**
10+
*
11+
*/
12+
public class AutonomousCmd extends Command {
13+
14+
private int autoLoopCounter;
15+
16+
public AutonomousCmd() {
17+
// Use requires() here to declare subsystem dependencies
18+
requires(Robot.autoSys);
19+
}
20+
21+
// Called just before this Command runs the first time
22+
protected void initialize() {
23+
autoLoopCounter = 0;
24+
}
25+
26+
// Called repeatedly when this Command is scheduled to run
27+
protected void execute() {
28+
if (autoLoopCounter < 100) {
29+
Robot.autoSys.drive();
30+
autoLoopCounter++;
31+
} else {
32+
Robot.autoSys.stop();
33+
}
34+
}
35+
36+
// Make this return true when this Command no longer needs to run execute()
37+
protected boolean isFinished() {
38+
return false;
39+
}
40+
41+
// Called once after isFinished returns true
42+
protected void end() {
43+
}
44+
45+
// Called when another command which requires one or more of the same
46+
// subsystems is scheduled to run
47+
protected void interrupted() {
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.usfirst.frc.team5518.robot.commands;
2+
3+
import edu.wpi.first.wpilibj.command.Command;
4+
5+
/**
6+
*
7+
*/
8+
public class TeleopCmd extends Command {
9+
10+
public TeleopCmd() {
11+
// Use requires() here to declare subsystem dependencies
12+
// eg. requires(chassis);
13+
}
14+
15+
// Called just before this Command runs the first time
16+
protected void initialize() {
17+
}
18+
19+
// Called repeatedly when this Command is scheduled to run
20+
protected void execute() {
21+
}
22+
23+
// Make this return true when this Command no longer needs to run execute()
24+
protected boolean isFinished() {
25+
return false;
26+
}
27+
28+
// Called once after isFinished returns true
29+
protected void end() {
30+
}
31+
32+
// Called when another command which requires one or more of the same
33+
// subsystems is scheduled to run
34+
protected void interrupted() {
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
package org.usfirst.frc.team5518.robot.subsystems;
3+
4+
import edu.wpi.first.wpilibj.command.Subsystem;
5+
6+
import org.usfirst.frc.team5518.robot.Robot;
7+
8+
/**
9+
*
10+
*/
11+
public class AutonomousSys extends Subsystem {
12+
13+
// Put methods for controlling this subsystem
14+
// here. Call these from Commands.
15+
16+
public void initDefaultCommand() {
17+
// Set the default command for a subsystem here.
18+
//setDefaultCommand(new MySpecialCommand());
19+
}
20+
21+
public void drive() {
22+
}
23+
24+
public void stop() {
25+
}
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.usfirst.frc.team5518.robot.subsystems;
2+
3+
import org.usfirst.frc.team5518.robot.RobotMap;
4+
import org.usfirst.frc.team5518.robot.commands.ArcadeDriveJoystick;
5+
6+
import edu.wpi.first.wpilibj.GenericHID;
7+
import edu.wpi.first.wpilibj.Joystick;
8+
import edu.wpi.first.wpilibj.RobotDrive;
9+
import edu.wpi.first.wpilibj.command.Subsystem;
10+
11+
/**
12+
*
13+
*/
14+
public class DrivingSys extends Subsystem {
15+
16+
private RobotDrive robotDrive;
17+
18+
public DrivingSys() {
19+
super();
20+
robotDrive = new RobotDrive(RobotMap.FRONT_LEFT_MOTOR, RobotMap.REAR_LEFT_MOTOR,
21+
RobotMap.FRONT_RIGHT_MOTOR, RobotMap.FRONT_LEFT_MOTOR);
22+
}
23+
24+
public void initDefaultCommand() {
25+
// Set the default command for a subsystem here.
26+
setDefaultCommand(new ArcadeDriveJoystick());
27+
}
28+
29+
public void drive(Joystick stick) {
30+
robotDrive.arcadeDrive(stick);
31+
}
32+
33+
public void drive(GenericHID moveStick, final int moveAxis,
34+
GenericHID rotateStick, final int rotateAxis) {
35+
robotDrive.arcadeDrive(moveStick, moveAxis, rotateStick, rotateAxis);
36+
}
37+
38+
public void drive(double moveValue, double rotateValue) {
39+
robotDrive.arcadeDrive(moveValue, rotateValue);
40+
}
41+
42+
public void log() {
43+
44+
}
45+
46+
}
47+

‎JoystickDemo2/sysProps.xml

5.96 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.