Initial commit of the WPILib simulation support in an alpha quality state.

Fixes to deal with the switch to .hpp files in the HAL and other misc problems due to rebasing.

Added Omar's changes to the compressor interface

Fixes to make C++ plugin compile on linux.

Added import of the WPILibSim code from the graduate class. It shows up as wpilibJavaSim to follow the convention set by wpilibJava, wpilibJavaJNI and wpilibJavaFinal.

Fixed wpilibJavaSim artifactId to mirror the new convention.

Modified the build of the java plugin to pull in the simulation dependencies.

Added stacktrace printing.

Fixed support for creating projects.

Added support for the isReal() and isSimulation() methods along with the AnalogPotentiometer object to support simulating GearsBot.

Added support for a "WPILib Simulate" button.

Added GearsBot to the built in examples.

Added support for specifying the world file during project creation and switched the default from BluntObjectBot to GearsBot.

Removed unused import.

Added file browser for world files.

Added support for debugging in simulation.

Change simulate icon to be a Gazebo icon.

Switched over to the gazebo messaging system.

Updated location of default world file.

Reverted cmake change.

Fixed bug in WPILibJSim, added better logging and cleaned up code.

Made the frc_gazebo_plugin build using raw cmake instead of catkin, breaking the final ROS dependencies.

Added installation to frc_gazebo_plugin Makefile.

Fixed running of simulation to actually use frcsim.

Initial commit of simulation library for C++. Has the minimal subset of features necessary for having a Simple Robot run in teleoperated mode.

Added notes for generating protobuf messages.

Import of the debuild process into the main repository.

Moved frc_gazebo_plugin under simulation and removed the gazebo folder.

Updated the gazebo plugin to remove excessive printing and limit motor signal to [-1,1].

Updated WPILibJSim to support latching messages and to sleep for 20ms in iterative robot.

Reduced delay between starting frcsim and the users program to 1 second.

Updated GearsBot example.

Fixed a few minor issues for demoable state.

Added simulator support for Victors, Jaguars and Talons.

Added NetworkTables, SmartDashboard and LiveWindow to the simulator.

Added AnalogPotentiometer for simulation.

Added support for simulating encoders.

Added simulation support for Gyro.

Added IterativeRobot, Fixed Timers, Notifiers, PIDControllers and other minor fixes + cleanup.

Added RobotDrive support to simulation.

Separated out JavaGazebo so that SimDS will be able to reuse it.

Separated out SimDS into its own application..

Fixes so that the SimDS is distributed and runs properly for Java with the eclipse plugins.

Added DriverStation support to WPILibCSim

Cleanup of DriverStation, WaitUntilCommand and AnalogPotentiometer for WPILibCSim.

Cleanup of includes for WPILibCSim

Added AnalogPotentiometer to the real WPILibC.

Added AnalogPotentiometer to the real WPILibC.

Added GearsBot example to C++ eclipse plugin.

WPILibCSim fixes to work with launching from the plugin.

Package libwpilibsim in a deb file.

Added includes to plugin distribution.

Added support for external-limit-switches to Gazebo, Java and C++.

Added support for Gazebo Rangefinders and Analog channels to read their values in C++ and Java.

Added support for internal limit switches.

Updated GearsBot programs to use limit switches + range finders.

Added disabling of motors when robot is disabled to more closely mimic the real robot.

Fixes to deal with the switch to .hpp files in the HAL and other misc problems due to rebasing.

Change-Id: I624c5f4d0f28282616a7c92083575bf68adcdce2
This commit is contained in:
Alex Henning
2014-06-12 11:02:26 -07:00
committed by thomasclark
parent d5a509c7e7
commit cb56c9a144
425 changed files with 38450 additions and 335 deletions

View File

@@ -179,6 +179,41 @@
mode="debug">
</description>
</shortcut>
<shortcut
class="edu.wpi.first.wpilib.plugins.java.launching.SimulateLaunchShortcut"
description="Test your WPILib program with the Gazebo simulator."
icon="resources/icons/Gazebo.png"
id="edu.wpi.first.wpilib.plugins.java.launching.simulate"
label="WPILib Simulation"
modes="run,debug">
<contextualLaunch>
<enablement>
<with
variable="selection">
<iterate>
<and>
<test
args="edu.wpi.first.wpilib.plugins.core.nature.FRCProjectNature"
property="org.eclipse.jdt.launching.hasProjectNature">
</test>
<test
args="org.eclipse.jdt.core.javanature"
property="org.eclipse.jdt.launching.hasProjectNature">
</test>
</and>
</iterate>
</with>
</enablement>
</contextualLaunch>
<description
description="Test your WPILib program with the Gazebo simulator."
mode="run">
</description>
<description
description="Test your WPILib program with the Gazebo simulator and use the debugger to debug."
mode="debug">
</description>
</shortcut>
</extension>
<extension
point="org.eclipse.ui.startup">

View File

@@ -16,19 +16,6 @@
<build-number>DEVELOPMENT</build-number>
<java-zip>${project.build.directory}/java-zip</java-zip>
</properties>
<repositories>
<!-- repository>
<id>sonatype</id>
<name>Sonatype OSS Snapshots Repository</name>
<url>http://oss.sonatype.org/content/groups/public</url>
</repository-->
<!-- For old snapshots, please use groupId `com.jnaerator` and the following repo -->
<!-- repository>
<id>nativelibs4java-repo</id>
<url>http://nativelibs4java.sourceforge.net/maven</url>
</repository-->
</repositories>
<build>
<resources>
@@ -88,27 +75,12 @@
</artifactItem>
<artifactItem>
<groupId>edu.wpi.first.wpilibj</groupId>
<artifactId>wpilibJavaFinal</artifactId>
<artifactId>wpilibJava</artifactId>
<version>0.1.0-SNAPSHOT</version>
<type>jar</type>
<destFileName>WPILib.jar</destFileName>
<outputDirectory>${java-zip}/lib</outputDirectory>
</artifactItem>
<!-- artifactItem>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.0.0</version>
<type>jar</type>
<outputDirectory>${java-zip}/lib</outputDirectory>
</artifactItem-->
<!-- artifactItem>
<groupId>com.nativelibs4java</groupId>
<artifactId>jnaerator-runtime</artifactId>
<version>0.12-SNAPSHOT</version>
<type>jar</type>
<destFileName>jnaerator-runtime.jar</destFileName>
<outputDirectory>${java-zip}/lib</outputDirectory>
</artifactItem-->
<!-- Library sources for debugging WPILib on the Athena -->
<artifactItem>
<groupId>edu.wpi.first.wpilib.networktables.java</groupId>
@@ -136,10 +108,76 @@
<outputDirectory>${java-zip}/javadoc-jar</outputDirectory>
</artifactItem>
</artifactItems>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
<!-- Simulation -->
<execution>
<id>fetch-sim-jar-zip-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${java-zip}/sim/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<artifactItems>
<artifactItem>
<groupId>org.gazebosim</groupId>
<artifactId>JavaGazebo</artifactId>
<version>0.1.0-SNAPSHOT</version>
</artifactItem>
<artifactItem>
<groupId>edu.wpi.first.wpilibj</groupId>
<artifactId>wpilibJavaSim</artifactId>
<version>0.1.0-SNAPSHOT</version>
<type>jar</type>
</artifactItem>
<artifactItem>
<groupId>edu.wpi.first.wpilib.networktables.java</groupId>
<artifactId>NetworkTables</artifactId>
<version>0.1.0-SNAPSHOT</version>
</artifactItem>
<artifactItem>
<groupId>net.java.jinput</groupId>
<artifactId>jinput</artifactId>
<version>2.0.5</version>
</artifactItem>
<artifactItem>
<groupId>net.java.jinput</groupId>
<artifactId>jinput-platform</artifactId>
<version>2.0.5</version>
<classifier>natives-linux</classifier>
<type>jar</type>
</artifactItem>
<artifactItem>
<groupId>net.java.jutils</groupId>
<artifactId>jutils</artifactId>
<version>1.0.0</version>
</artifactItem>
<artifactItem>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</artifactItem>
<artifactItem>
<groupId>edu.wpi.first.wpilibj.simulation</groupId>
<artifactId>SimDS</artifactId>
<version>0.1.0-SNAPSHOT</version>
<destFileName>SimDS.jar</destFileName>
<outputDirectory>${java-zip}/sim/tools</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
@@ -187,7 +225,23 @@
</target>
</configuration>
</execution>
<!-- Unzip jinput *.so's -->
<execution>
<id>unzip-jinput-libs</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<unzip src="${java-zip}/sim/lib/jinput-platform-2.0.5-natives-linux.jar"
dest="${java-zip}/sim/lib"
overwrite="true" />
</target>
</configuration>
</execution>
<!-- Generate zip file to unzip for the user. -->
<execution>
<id>generate-jar-zip</id>
@@ -259,7 +313,13 @@
</dependency>
<dependency>
<groupId>edu.wpi.first.wpilibj</groupId>
<artifactId>wpilibJavaFinal</artifactId>
<artifactId>wpilibJava</artifactId>
<version>0.1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>edu.wpi.first.wpilibj</groupId>
<artifactId>wpilibJavaSim</artifactId>
<version>0.1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
@@ -287,7 +347,6 @@
<groupId>edu.wpi.first.wpilibj</groupId>
<artifactId>wpilibJava</artifactId>
<version>0.1.0-SNAPSHOT</version>
<classifier>sources</classifier>
</dependency>
<!-- Javadoc -->

Binary file not shown.

After

Width:  |  Height:  |  Size: 780 B

View File

@@ -1,3 +1,4 @@
# Project specific information
package=$package
main=${package}.Robot
robot.class=${package}.Robot
simulation.world.file=$world

View File

@@ -0,0 +1,57 @@
package $package;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import $package.commands.*;
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 {
private Joystick joy = new Joystick(1);
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
JoystickButton d_up = new JoystickButton(joy, 5);
JoystickButton d_right= new JoystickButton(joy, 6);
JoystickButton d_down= new JoystickButton(joy, 7);
JoystickButton d_left = new JoystickButton(joy, 8);
JoystickButton l2 = new JoystickButton(joy, 9);
JoystickButton r2 = new JoystickButton(joy, 10);
JoystickButton l1 = new JoystickButton(joy, 11);
JoystickButton r1 = new JoystickButton(joy, 12);
// Connect the buttons to commands
d_up.whenPressed(new SetElevatorSetpoint(0.2));
d_down.whenPressed(new SetElevatorSetpoint(-0.2));
d_right.whenPressed(new CloseClaw());
d_left.whenPressed(new OpenClaw());
r1.whenPressed(new PrepareToPickup());
r2.whenPressed(new Pickup());
l1.whenPressed(new Place());
l2.whenPressed(new Autonomous());
}
public Joystick getJoystick() {
return joy;
}
}

View File

@@ -0,0 +1,103 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. 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 $package;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import $package.commands.Autonomous;
import $package.subsystems.Claw;
import $package.subsystems.DriveTrain;
import $package.subsystems.Elevator;
import $package.subsystems.Wrist;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* 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 IterativeRobot {
Command autonomousCommand;
public static DriveTrain drivetrain;
public static Elevator elevator;
public static Wrist wrist;
public static Claw claw;
public static OI oi;
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
// Initialize all subsystems
drivetrain = new DriveTrain();
elevator = new Elevator();
wrist = new Wrist();
claw = new Claw();
oi = new OI();
// instantiate the command used for the autonomous period
autonomousCommand = new Autonomous();
// Show what command your subsystem is running on the SmartDashboard
SmartDashboard.putData(drivetrain);
SmartDashboard.putData(elevator);
SmartDashboard.putData(wrist);
SmartDashboard.putData(claw);
}
public void autonomousInit() {
autonomousCommand.start(); // schedule the autonomous command (example)
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
Scheduler.getInstance().run();
log();
}
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.
autonomousCommand.cancel();
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
Scheduler.getInstance().run();
log();
}
/**
* This function is called periodically during test mode
*/
public void testPeriodic() {
LiveWindow.run();
}
/**
* The log method puts interesting information to the SmartDashboard.
*/
private void log() {
wrist.log();
elevator.log();
drivetrain.log();
claw.log();
}
}

View File

@@ -0,0 +1,22 @@
package $package.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 {
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

@@ -0,0 +1,47 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.commands;
import edu.wpi.first.wpilibj.command.Command;
import $package.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.claw);
}
// Called just before this Command runs the first time
protected void initialize() {
Robot.claw.close();
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return Robot.claw.isGrabbing();
}
// Called once after isFinished returns true
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.claw.stop();
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
end();
}
}

View File

@@ -0,0 +1,63 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.commands;
import edu.wpi.first.wpilibj.PIDController;
import edu.wpi.first.wpilibj.PIDOutput;
import edu.wpi.first.wpilibj.PIDSource;
import edu.wpi.first.wpilibj.command.Command;
import $package.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 PIDController pid;
public DriveStraight(double distance) {
requires(Robot.drivetrain);
pid = new PIDController(4, 0, 0,
new PIDSource() { public double pidGet() {
return Robot.drivetrain.getDistance();
}},
new PIDOutput() { public void pidWrite(double d) {
Robot.drivetrain.drive(d, d);
}});
pid.setAbsoluteTolerance(0.01);
pid.setSetpoint(distance);
}
// Called just before this Command runs the first time
protected void initialize() {
// Get everything in a safe starting state.
Robot.drivetrain.reset();
pid.reset();
pid.enable();
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return pid.onTarget();
}
// Called once after isFinished returns true
protected void end() {
// Stop PID and the wheels
pid.disable();
Robot.drivetrain.drive(0, 0);
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
end();
}
}

View File

@@ -0,0 +1,45 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.commands;
import edu.wpi.first.wpilibj.command.Command;
import $package.Robot;
/**
* Opens the claw for one second. Real robots should use sensors, stalling
* motors is BAD!
*/
public class OpenClaw extends Command {
public OpenClaw() {
requires(Robot.claw);
setTimeout(1);
}
// Called just before this Command runs the first time
protected void initialize() {
Robot.claw.open();
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return isTimedOut();
}
// Called once after isFinished returns true
protected void end() {
Robot.claw.stop();
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
end();
}
}

View File

@@ -0,0 +1,15 @@
package $package.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 {
public Pickup() {
addSequential(new CloseClaw());
addParallel(new SetWristSetpoint(-45));
addSequential(new SetElevatorSetpoint(0.25));
}
}

View File

@@ -0,0 +1,14 @@
package $package.commands;
import edu.wpi.first.wpilibj.command.CommandGroup;
/**
* Place a held soda can onto the platform.
*/
public class Place extends CommandGroup {
public Place() {
addSequential(new SetElevatorSetpoint(0.25));
addSequential(new SetWristSetpoint(0));
addSequential(new OpenClaw());
}
}

View File

@@ -0,0 +1,14 @@
package $package.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 {
public PrepareToPickup() {
addParallel(new OpenClaw());
addParallel(new SetWristSetpoint(0));
addSequential(new SetElevatorSetpoint(0));
}
}

View File

@@ -0,0 +1,66 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.commands;
import edu.wpi.first.wpilibj.PIDController;
import edu.wpi.first.wpilibj.PIDOutput;
import edu.wpi.first.wpilibj.PIDSource;
import edu.wpi.first.wpilibj.command.Command;
import $package.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 PIDController pid;
public SetDistanceToBox(double distance) {
requires(Robot.drivetrain);
pid = new PIDController(-2, 0, 0, new PIDSource() {
public double pidGet() {
return Robot.drivetrain.getDistanceToObstacle();
}
}, new PIDOutput() {
public void pidWrite(double d) {
Robot.drivetrain.drive(d, d);
}
});
pid.setAbsoluteTolerance(0.01);
pid.setSetpoint(distance);
}
// Called just before this Command runs the first time
protected void initialize() {
// Get everything in a safe starting state.
Robot.drivetrain.reset();
pid.reset();
pid.enable();
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return pid.onTarget();
}
// Called once after isFinished returns true
protected void end() {
// Stop PID and the wheels
pid.disable();
Robot.drivetrain.drive(0, 0);
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
end();
}
}

View File

@@ -0,0 +1,46 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.commands;
import edu.wpi.first.wpilibj.command.Command;
import $package.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 double setpoint;
public SetElevatorSetpoint(double setpoint) {
this.setpoint = setpoint;
requires(Robot.elevator);
}
// Called just before this Command runs the first time
protected void initialize() {
Robot.elevator.enable();
Robot.elevator.setSetpoint(setpoint);
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return Robot.elevator.onTarget();
}
// Called once after isFinished returns true
protected void end() {
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
}
}

View File

@@ -0,0 +1,46 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.commands;
import edu.wpi.first.wpilibj.command.Command;
import $package.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 double setpoint;
public SetWristSetpoint(double setpoint) {
this.setpoint = setpoint;
requires(Robot.wrist);
}
// Called just before this Command runs the first time
protected void initialize() {
Robot.wrist.enable();
Robot.wrist.setSetpoint(setpoint);
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return Robot.wrist.onTarget();
}
// Called once after isFinished returns true
protected void end() {
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
}
}

View File

@@ -0,0 +1,42 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.commands;
import $package.Robot;
import edu.wpi.first.wpilibj.command.Command;
/**
* Have the robot drive tank style using the PS3 Joystick until interrupted.
*/
public class TankDriveWithJoystick extends Command {
public TankDriveWithJoystick() {
requires(Robot.drivetrain);
}
// Called just before this Command runs the f iirst time
protected void initialize() {}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
Robot.drivetrain.drive(Robot.oi.getJoystick());
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return false; // Runs until interrupted
}
// Called once after isFinished returns true
protected void end() {
Robot.drivetrain.drive(0, 0);
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
end();
}
}

View File

@@ -0,0 +1,63 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.subsystems;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
/**
* 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 SpeedController motor;
private DigitalInput contact;
public Claw() {
super();
motor = new Victor(7);
contact = new DigitalInput(5);
// Let's show everything on the LiveWindow
LiveWindow.addActuator("Claw", "Motor", (Victor) motor);
LiveWindow.addActuator("Claw", "Limit Switch", contact);
}
public void initDefaultCommand() {}
public void log() {}
/**
* Set the claw motor to move in the open direction.
*/
public void open() {
motor.set(-1);
}
/**
* Set the claw motor to move in the close direction.
*/
public void close() {
motor.set(1);
}
/**
* Stops the claw motor from moving.
*/
public void stop() {
motor.set(0);
}
/**
* Return true when the robot is grabbing an object hard enough
* to trigger the limit switch.
*/
public boolean isGrabbing() {
return contact.get();
}
}

View File

@@ -0,0 +1,137 @@
package $package.subsystems;
import edu.wpi.first.wpilibj.AnalogChannel;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Joystick.AxisType;
import edu.wpi.first.wpilibj.Gyro;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import $package.Robot;
import $package.commands.TankDriveWithJoystick;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* 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 SpeedController front_left_motor, back_left_motor,
front_right_motor, back_right_motor;
private RobotDrive drive;
private Encoder left_encoder, right_encoder;
private AnalogChannel rangefinder;
private Gyro gyro;
public DriveTrain() {
super();
front_left_motor = new Talon(1);
back_left_motor = new Talon(2);
front_right_motor = new Talon(3);
back_right_motor = new Talon(4);
drive = new RobotDrive(front_left_motor, back_left_motor,
front_right_motor, back_right_motor);
left_encoder = new Encoder(1, 2);
right_encoder = new Encoder(3, 4);
// 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()) {
left_encoder.setDistancePerPulse(0.042);
right_encoder.setDistancePerPulse(0.042);
} else {
// Circumference in ft = 4in/12(in/ft)*PI
left_encoder.setDistancePerPulse((4.0/12.0*Math.PI) / 360.0);
right_encoder.setDistancePerPulse((4.0/12.0*Math.PI) / 360.0);
}
left_encoder.start();
right_encoder.start();
rangefinder = new AnalogChannel(6);
gyro = new Gyro(1);
// Let's show everything on the LiveWindow
LiveWindow.addActuator("Drive Train", "Front_Left Motor", (Talon) front_left_motor);
LiveWindow.addActuator("Drive Train", "Back Left Motor", (Talon) back_left_motor);
LiveWindow.addActuator("Drive Train", "Front Right Motor", (Talon) front_right_motor);
LiveWindow.addActuator("Drive Train", "Back Right Motor", (Talon) back_right_motor);
LiveWindow.addSensor("Drive Train", "Left Encoder", left_encoder);
LiveWindow.addSensor("Drive Train", "Right Encoder", right_encoder);
LiveWindow.addSensor("Drive Train", "Rangefinder", rangefinder);
LiveWindow.addSensor("Drive Train", "Gyro", gyro);
}
/**
* When no other command is running let the operator drive around
* using the PS3 joystick.
*/
public void initDefaultCommand() {
setDefaultCommand(new TankDriveWithJoystick());
}
/**
* The log method puts interesting information to the SmartDashboard.
*/
public void log() {
SmartDashboard.putNumber("Left Distance", left_encoder.getDistance());
SmartDashboard.putNumber("Right Distance", right_encoder.getDistance());
SmartDashboard.putNumber("Left Speed", left_encoder.getRate());
SmartDashboard.putNumber("Right Speed", right_encoder.getRate());
SmartDashboard.putNumber("Gyro", 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) {
drive.tankDrive(left, right);
}
/**
* @param joy The ps3 style joystick to use to drive tank style.
*/
public void drive(Joystick joy) {
drive(-joy.getY(), -joy.getAxis(AxisType.kThrottle));
}
/**
* @return The robots heading in degrees.
*/
public double getHeading() {
return gyro.getAngle();
}
/**
* Reset the robots sensors to the zero states.
*/
public void reset() {
gyro.reset();
left_encoder.reset();
right_encoder.reset();
}
/**
* @return The distance driven (average of left and right encoders).
*/
public double getDistance() {
return (left_encoder.getDistance() + right_encoder.getDistance())/2;
}
/**
* @return The distance to the obstacle detected by the rangefinder.
*/
public double getDistanceToObstacle() {
// Really meters in simulation since it's a rangefinder...
return rangefinder.getAverageVoltage();
}
}

View File

@@ -0,0 +1,75 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.subsystems;
import edu.wpi.first.wpilibj.AnalogPotentiometer;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.command.PIDSubsystem;
import edu.wpi.first.wpilibj.interfaces.Potentiometer;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import $package.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 SpeedController motor;
private Potentiometer pot;
private static final double kP_real = 4, kI_real = 0.07,
kP_simulation = 18, kI_simulation = 0.2;
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);
motor = new Victor(5);
// Conversion value of potentiometer varies between the real world and simulation
if (Robot.isReal()) {
pot = new AnalogPotentiometer(2, -2.0/5);
} else {
pot = new AnalogPotentiometer(2); // Defaults to meters
}
// Let's show everything on the LiveWindow
LiveWindow.addActuator("Elevator", "Motor", (Victor) motor);
LiveWindow.addSensor("Elevator", "Pot", (AnalogPotentiometer) pot);
LiveWindow.addActuator("Elevator", "PID", getPIDController());
}
public void initDefaultCommand() {}
/**
* The log method puts interesting information to the SmartDashboard.
*/
public void log() {
SmartDashboard.putData("Wrist Pot", (AnalogPotentiometer) pot);
}
/**
* Use the potentiometer as the PID sensor. This method is automatically
* called by the subsystem.
*/
protected double returnPIDInput() {
return pot.get();
}
/**
* Use the motor as the PID output. This method is automatically called by
* the subsystem.
*/
protected void usePIDOutput(double d) {
motor.set(d);
}
}

View File

@@ -0,0 +1,74 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package $package.subsystems;
import edu.wpi.first.wpilibj.AnalogPotentiometer;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.command.PIDSubsystem;
import edu.wpi.first.wpilibj.interfaces.Potentiometer;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import $package.Robot;
/**
* The wrist subsystem is like the elevator, but with a rotational joint instead
* of a linear joint.
*/
public class Wrist extends PIDSubsystem {
private SpeedController motor;
private Potentiometer pot;
private static final double kP_real = 1,
kP_simulation = 0.05;
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);
motor = new Victor(6);
// Conversion value of potentiometer varies between the real world and simulation
if (Robot.isReal()) {
pot = new AnalogPotentiometer(3, -270.0/5);
} else {
pot = new AnalogPotentiometer(3); // Defaults to degrees
}
// Let's show everything on the LiveWindow
LiveWindow.addActuator("Wrist", "Motor", (Victor) motor);
LiveWindow.addSensor("Wrist", "Pot", (AnalogPotentiometer) pot);
LiveWindow.addActuator("Wrist", "PID", getPIDController());
}
public void initDefaultCommand() {}
/**
* The log method puts interesting information to the SmartDashboard.
*/
public void log() {
SmartDashboard.putData("Wrist Angle", (AnalogPotentiometer) pot);
}
/**
* Use the potentiometer as the PID sensor. This method is automatically
* called by the subsystem.
*/
protected double returnPIDInput() {
return pot.get();
}
/**
* Use the motor as the PID output. This method is automatically called by
* the subsystem.
*/
protected void usePIDOutput(double d) {
motor.set(d);
}
}

View File

@@ -4,11 +4,19 @@
<name>Simple Robot</name>
<description>Examples for simple robot programs.</description>
</tagDescription>
<tagDescription>
<name>CommandBased Robot</name>
<description>Examples for CommandBased robot programs.</description>
</tagDescription>
<tagDescription>
<name>Simulation</name>
<description>Examples that can be run in simulation.</description>
</tagDescription>
<tagDescription>
<name>Network Tables</name>
<description>Examples of how to use Network Tables to accomplish a
variety of tasks such as sending and receiving values to both
dashboards and co-processors..</description>
dashboards and co-processors.</description>
</tagDescription>
<example>
@@ -43,5 +51,37 @@
<file source="examples/Network Table Counter/Robot.java" destination="src/$package-dir/Robot.java" />
</files>
</example>
<example>
<name>GearsBot</name>
<description>A fully functional example CommandBased program for WPIs GearsBot robot. This code can run on your computer if it supports simulation.</description>
<tags>
<tag>CommandBased Robot</tag>
<tag>Simulation</tag>
</tags>
<packages>
<package>src/$package-dir</package>
<package>src/$package-dir/commands</package>
<package>src/$package-dir/subsystems</package>
</packages>
<files>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/OI.java" destination="src/$package-dir/OI.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/Robot.java" destination="src/$package-dir/Robot.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/Autonomous.java" destination="src/$package-dir/commands/Autonomous.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/CloseClaw.java" destination="src/$package-dir/commands/CloseClaw.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/DriveStraight.java" destination="src/$package-dir/commands/DriveStraight.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/OpenClaw.java" destination="src/$package-dir/commands/OpenClaw.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/Pickup.java" destination="src/$package-dir/commands/Pickup.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/Place.java" destination="src/$package-dir/commands/Place.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/PrepareToPickup.java" destination="src/$package-dir/commands/PrepareToPickup.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/SetDistanceToBox.java" destination="src/$package-dir/commands/SetDistanceToBox.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/SetElevatorSetpoint.java" destination="src/$package-dir/commands/SetElevatorSetpoint.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/SetWristSetpoint.java" destination="src/$package-dir/commands/SetWristSetpoint.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/commands/TankDriveWithJoystick.java" destination="src/$package-dir/commands/TankDriveWithJoystick.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/subsystems/Claw.java" destination="src/$package-dir/subsystems/Claw.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/subsystems/DriveTrain.java" destination="src/$package-dir/subsystems/DriveTrain.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/subsystems/Elevator.java" destination="src/$package-dir/subsystems/Elevator.java"></file>
<file source="examples/GearsBot/src/org/usfirst/frc/team190/robot/subsystems/Wrist.java" destination="src/$package-dir/subsystems/Wrist.java"></file>
</files>
</example>
</examples>

View File

@@ -73,6 +73,7 @@ public class WPILibJavaPlugin extends AbstractUIPlugin implements IStartup {
return props.getProperty("version");
}
} catch (CoreException e) {
e.printStackTrace(System.err);
return "DEVELOPMENT";
}
}

View File

@@ -1,39 +1,6 @@
package edu.wpi.first.wpilib.plugins.java.launching;
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import org.eclipse.core.internal.resources.Resource;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.IStreamListener;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IVMConnector;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PlatformUI;
import com.sun.jdi.connect.Connector.Argument;
import edu.wpi.first.wpilib.plugins.core.WPILibCore;
import edu.wpi.first.wpilib.plugins.core.launching.AntLauncher;
/**
* Launch shortcut base functionality, common for deploying to the robot.
@@ -43,58 +10,7 @@ import edu.wpi.first.wpilib.plugins.core.launching.AntLauncher;
* @author Ryan O'Meara
* @author Alex Henning
*/
@SuppressWarnings("restriction")
public class DeployLaunchShortcut implements ILaunchShortcut {
//Class constants - used to delineate types for launch shortcuts
public static final String DEPLOY_TYPE = "edu.wpi.first.wpilib.plugins.core.deploy";
private static final String ANT_SERVER_THREAD_NAME = "Ant Build Server Connection";
// NOTE: This string must be changed if the port is changed.
private static final String DEBUG_START_TEXT = "Listening for transport dt_socket at address: 8348";
private static ILaunch lastDeploy = null;
/**
* Returns the launch type of the shortcut that was used, one of the constants
* defined in BaseLaunchShortcut
* @return Launch shortcut type
*/
public String getLaunchType() {return DEPLOY_TYPE;}
@Override
public void launch(ISelection selection, String mode) {
//Extract resource from selection
StructuredSelection sel = (StructuredSelection)selection;
IProject activeProject = null;
//NOTE: This caused issues earlier, as the sel return was treated as a workspace, instead of a project
//When it is a valid FIRST project, the selection is always a JavaProject
if(sel.getFirstElement() instanceof IJavaProject){
activeProject = ((IJavaProject)sel.getFirstElement()).getProject();
}else if(sel.getFirstElement() instanceof IJavaElement){
activeProject = ((IJavaElement)sel.getFirstElement()).getJavaProject().getProject();
}else{
return;
}
//Run config using project found in extracted resource, with indicated mode
runConfig(activeProject, mode);
}
@Override
public void launch(IEditorPart editor, String mode) {
//Extract resource from editor
if(editor != null){
IFileEditorInput input = (IFileEditorInput)editor.getEditorInput();
IFile file = input.getFile();
IProject activeProject = file.getProject();
//If editor existed, run config using extracted resource in indicated mode
runConfig(activeProject, mode);
}else{
System.err.println("editor was null");
}
}
public class DeployLaunchShortcut extends JavaLaunchShortcut {
/**
* Runs the ant script using the correct target for the indicated mode (deploy to cRIO or just compile)
@@ -102,106 +18,7 @@ public class DeployLaunchShortcut implements ILaunchShortcut {
* @param mode The mode it will be run in (ILaunchManager.RUN_MODE or ILaunchManager.DEBUG_MODE)
*/
public void runConfig(IProject activeProj, String mode){
String targets = "deploy";
if(mode.equals(ILaunchManager.RUN_MODE)){
if(getLaunchType().equals(DEPLOY_TYPE)){
targets = "deploy";
}
} else if ((mode.equals(ILaunchManager.DEBUG_MODE))&&(getLaunchType().equals(DEPLOY_TYPE))) {
targets = "debug-deploy";
try{
PlatformUI.getWorkbench().showPerspective(IDebugUIConstants.ID_DEBUG_PERSPECTIVE,
PlatformUI.getWorkbench().getActiveWorkbenchWindow());
}catch(Exception e){}
}
if((lastDeploy != null)&&(!lastDeploy.isTerminated())){
System.out.println("Last deploy running");
//Find the server connection thread and kill it
Vector<ThreadGroup> threadGroups = new Vector<ThreadGroup>();
ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
while (root.getParent() != null) {root = root.getParent();}
threadGroups.add(root);
ThreadGroup threadGroup = threadGroups.remove(0);
int numThreads = threadGroup.activeCount();
Thread[] threads = new Thread[numThreads*100];
numThreads = threadGroup.enumerate(threads, true);
for(Thread current: threads){
if(current != null){
if(current.getName().equals(ANT_SERVER_THREAD_NAME)){
try{
//Manually end thread and then try terminating launch
Method stopMethod = current.getClass().getMethod("stop");
stopMethod.invoke(current);
lastDeploy.terminate();
break;
}catch(Exception e){e.printStackTrace();}
}
}
}
System.out.println("Waiting");
try{wait(1000);}catch(Exception e){}
}
System.out.println("Running ant file: " + activeProj.getLocation().toOSString() + File.separator + "build.xml");
System.out.println("Targets: " + targets + ", Mode: " + mode);
lastDeploy = AntLauncher.runAntFile(new File (activeProj.getLocation().toOSString() + File.separator + "build.xml"), targets, null, mode);
if((mode.equals(ILaunchManager.DEBUG_MODE))&&(getLaunchType().equals(DEPLOY_TYPE))) {
ILaunchConfigurationWorkingCopy config;
try {
config = getRemoteDebugConfig(activeProj);
startDebugConfig(config, lastDeploy);
} catch (CoreException e) {
System.err.println("Debug attach failed.");
e.printStackTrace();
}
}
try {
activeProj.refreshLocal(Resource.DEPTH_INFINITE, null);
} catch (Exception e) {}
}
private ILaunchConfigurationWorkingCopy getRemoteDebugConfig(IProject activeProj) throws CoreException {
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_REMOTE_JAVA_APPLICATION);
ILaunchConfigurationWorkingCopy config = type.newInstance(null, "Debug "+activeProj.getName());
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, activeProj.getName());
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_ALLOW_TERMINATE, true);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR, IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR);
IVMConnector connector = JavaRuntime.getVMConnector(IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR);
Map<String, Argument> def = connector.getDefaultArguments();
Map<String, String> argMap = new HashMap<String, String>(def.size());
argMap.put("hostname", WPILibCore.getDefault().getTargetIP(activeProj));
argMap.put("port", "8348");
System.out.println(argMap);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, argMap);
return config;
}
private void startDebugConfig(final ILaunchConfigurationWorkingCopy config, ILaunch deploy) throws CoreException {
IStreamListener listener = new IStreamListener() {
@Override
public void streamAppended(String text, IStreamMonitor monitor) {
if (text.contains(DEBUG_START_TEXT)) {
try {
config.launch(ILaunchManager.DEBUG_MODE, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
monitor.removeListener(this);
}
}
};
deploy.getProcesses()[0].getStreamsProxy().getOutputStreamMonitor().addListener(listener);
runConfigHelper(activeProj, mode, "deploy", "debug-deploy");
}
}

View File

@@ -0,0 +1,208 @@
package edu.wpi.first.wpilib.plugins.java.launching;
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import org.eclipse.core.internal.resources.Resource;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.IStreamListener;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IVMConnector;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PlatformUI;
import com.sun.jdi.connect.Connector.Argument;
import edu.wpi.first.wpilib.plugins.core.WPILibCore;
import edu.wpi.first.wpilib.plugins.core.launching.AntLauncher;
@SuppressWarnings("restriction")
public abstract class JavaLaunchShortcut implements ILaunchShortcut {
//Class constants - used to delineate types for launch shortcuts
public static final String DEPLOY_TYPE = "edu.wpi.first.wpilib.plugins.core.deploy";
private static final String ANT_SERVER_THREAD_NAME = "Ant Build Server Connection";
// NOTE: This string must be changed if the port is changed.
private static final String DEBUG_START_TEXT = "Listening for transport dt_socket at address: 8348";
private static ILaunch lastDeploy = null;
/**
* Returns the launch type of the shortcut that was used, one of the constants
* defined in BaseLaunchShortcut
* @return Launch shortcut type
*/
public String getLaunchType() {return DEPLOY_TYPE;}
public void launch(ISelection selection, String mode) {
//Extract resource from selection
StructuredSelection sel = (StructuredSelection)selection;
IProject activeProject = null;
//NOTE: This caused issues earlier, as the sel return was treated as a workspace, instead of a project
//When it is a valid FIRST project, the selection is always a JavaProject
if(sel.getFirstElement() instanceof IJavaProject){
activeProject = ((IJavaProject)sel.getFirstElement()).getProject();
}else if(sel.getFirstElement() instanceof IJavaElement){
activeProject = ((IJavaElement)sel.getFirstElement()).getJavaProject().getProject();
}else{
return;
}
//Run config using project found in extracted resource, with indicated mode
runConfig(activeProject, mode);
}
@Override
public void launch(IEditorPart editor, String mode) {
//Extract resource from editor
if(editor != null){
IFileEditorInput input = (IFileEditorInput)editor.getEditorInput();
IFile file = input.getFile();
IProject activeProject = file.getProject();
//If editor existed, run config using extracted resource in indicated mode
runConfig(activeProject, mode);
}else{
System.err.println("editor was null");
}
}
/**
* Runs the ant script using the correct target for the indicated mode (deploy to cRIO or just compile)
* @param activeProj The project that the script will be run on/from
* @param mode The mode it will be run in (ILaunchManager.RUN_MODE or ILaunchManager.DEBUG_MODE)
* @return
*/
public abstract void runConfig(IProject activeProj, String mode);
/**
* Runs the ant script using the correct target for the indicated mode (deploy to cRIO or just compile)
* @param activeProj The project that the script will be run on/from
* @param mode The mode it will be run in (ILaunchManager.RUN_MODE or ILaunchManager.DEBUG_MODE)
*/
protected void runConfigHelper(IProject activeProj, String mode, String runTarget, String debugTarget){
String targets = runTarget;
if(mode.equals(ILaunchManager.RUN_MODE)){
if(getLaunchType().equals(DEPLOY_TYPE)){
targets = runTarget;
}
} else if ((mode.equals(ILaunchManager.DEBUG_MODE))&&(getLaunchType().equals(DEPLOY_TYPE))) {
targets = debugTarget;
try{
PlatformUI.getWorkbench().showPerspective(IDebugUIConstants.ID_DEBUG_PERSPECTIVE,
PlatformUI.getWorkbench().getActiveWorkbenchWindow());
}catch(Exception e){}
}
if((lastDeploy != null)&&(!lastDeploy.isTerminated())){
System.out.println("Last deploy running");
//Find the server connection thread and kill it
Vector<ThreadGroup> threadGroups = new Vector<ThreadGroup>();
ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
while (root.getParent() != null) {root = root.getParent();}
threadGroups.add(root);
ThreadGroup threadGroup = threadGroups.remove(0);
int numThreads = threadGroup.activeCount();
Thread[] threads = new Thread[numThreads*100];
numThreads = threadGroup.enumerate(threads, true);
for(Thread current: threads){
if(current != null){
if(current.getName().equals(ANT_SERVER_THREAD_NAME)){
try{
//Manually end thread and then try terminating launch
Method stopMethod = current.getClass().getMethod("stop");
stopMethod.invoke(current);
lastDeploy.terminate();
break;
}catch(Exception e){e.printStackTrace();}
}
}
}
System.out.println("Waiting");
try{wait(1000);}catch(Exception e){}
}
System.out.println("Running ant file: " + activeProj.getLocation().toOSString() + File.separator + "build.xml");
System.out.println("Targets: " + targets + ", Mode: " + mode);
lastDeploy = AntLauncher.runAntFile(new File (activeProj.getLocation().toOSString() + File.separator + "build.xml"), targets, null, mode);
if((mode.equals(ILaunchManager.DEBUG_MODE))&&(getLaunchType().equals(DEPLOY_TYPE))) {
ILaunchConfigurationWorkingCopy config;
try {
config = getRemoteDebugConfig(activeProj);
startDebugConfig(config, lastDeploy);
} catch (CoreException e) {
System.err.println("Debug attach failed.");
e.printStackTrace();
}
}
try {
activeProj.refreshLocal(Resource.DEPTH_INFINITE, null);
} catch (Exception e) {}
}
private ILaunchConfigurationWorkingCopy getRemoteDebugConfig(IProject activeProj) throws CoreException {
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_REMOTE_JAVA_APPLICATION);
ILaunchConfigurationWorkingCopy config = type.newInstance(null, "Debug "+activeProj.getName());
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, activeProj.getName());
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_ALLOW_TERMINATE, true);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR, IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR);
IVMConnector connector = JavaRuntime.getVMConnector(IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR);
Map<String, Argument> def = connector.getDefaultArguments();
Map<String, String> argMap = new HashMap<String, String>(def.size());
argMap.put("hostname", getHostname(activeProj));
argMap.put("port", "8348");
System.out.println(argMap);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, argMap);
return config;
}
protected String getHostname(IProject proj) {
return WPILibCore.getDefault().getTargetIP(proj);
}
private void startDebugConfig(final ILaunchConfigurationWorkingCopy config, ILaunch deploy) throws CoreException {
IStreamListener listener = new IStreamListener() {
@Override
public void streamAppended(String text, IStreamMonitor monitor) {
if (text.contains(DEBUG_START_TEXT)) {
try {
config.launch(ILaunchManager.DEBUG_MODE, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
monitor.removeListener(this);
}
}
};
deploy.getProcesses()[0].getStreamsProxy().getOutputStreamMonitor().addListener(listener);
}
}

View File

@@ -0,0 +1,20 @@
package edu.wpi.first.wpilib.plugins.java.launching;
import org.eclipse.core.resources.IProject;
public class SimulateLaunchShortcut extends JavaLaunchShortcut {
/**
* Runs the ant script using the correct target for the indicated mode (deploy to cRIO or just compile)
* @param activeProj The project that the script will be run on/from
* @param mode The mode it will be run in (ILaunchManager.RUN_MODE or ILaunchManager.DEBUG_MODE)
*/
public void runConfig(IProject activeProj, String mode){
runConfigHelper(activeProj, mode, "simulate", "debug-simulate");
}
protected String getHostname(IProject proj) {
return "localhost";
}
}

View File

@@ -35,7 +35,8 @@ public class ExampleJavaWizard extends ExampleWizard {
final String projectName = detailsPage.getProjectName();
final String packageName = detailsPage.getPackage();
ProjectCreationUtils.createProject(new WPIRobotJavaProjectCreator(projectName, packageName, ex));
final String worldName = detailsPage.getWorld();
ProjectCreationUtils.createProject(new WPIRobotJavaProjectCreator(projectName, packageName, ex, worldName));
}
@Override

View File

@@ -72,11 +72,12 @@ public class NewJavaWizard extends Wizard implements INewWizard {
final String teamNumber = TeamNumberPage.getTeamNumberFromPage(teamNumberPage);
final String packageName = page.getPackage();
final ProjectType projectType = page.getProjectType();
final String worldName = page.getWorld();
System.out.println("Project: "+projectName+" Package: "+packageName+" Project Type: "+projectType);
IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException {
try {
doFinish(projectName, teamNumber, packageName, projectType, monitor);
doFinish(projectName, teamNumber, packageName, projectType, worldName, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
@@ -102,11 +103,11 @@ public class NewJavaWizard extends Wizard implements INewWizard {
* the editor on the newly created file.
*/
private void doFinish(String projectName, String teamNumber, String packageName, ProjectType projectType, IProgressMonitor monitor) throws CoreException {
private void doFinish(String projectName, String teamNumber, String packageName, ProjectType projectType, String worldName, IProgressMonitor monitor) throws CoreException {
Properties props = WPILibCore.getDefault().getProjectProperties(null);
props.setProperty("team-number", teamNumber);
WPILibCore.getDefault().saveGlobalProperties(props);
ProjectCreationUtils.createProject(new WPIRobotJavaProjectCreator(projectName, packageName, projectType));
ProjectCreationUtils.createProject(new WPIRobotJavaProjectCreator(projectName, packageName, projectType, worldName));
}
/**

View File

@@ -15,13 +15,14 @@ import edu.wpi.first.wpilib.plugins.core.wizards.ProjectType;
import edu.wpi.first.wpilib.plugins.java.WPILibJavaPlugin;
public class WPIRobotJavaProjectCreator implements IProjectCreator {
String projectName, packageName;
String projectName, packageName, worldName;
ProjectType projectType;
public WPIRobotJavaProjectCreator(String projectName, String packageName, ProjectType projectType) {
public WPIRobotJavaProjectCreator(String projectName, String packageName, ProjectType projectType, String worldName) {
this.projectName = projectName;
this.packageName = packageName;
this.projectType = projectType;
this.worldName = worldName;
}
@Override
@@ -39,6 +40,7 @@ public class WPIRobotJavaProjectCreator implements IProjectCreator {
Map<String, String> vals = new HashMap<String, String>();
vals.put("$project", projectName);
vals.put("$package", packageName);
vals.put("$world", worldName);
return vals;
}

View File

@@ -28,4 +28,10 @@ src.dir=src
build.dir=build
build.jars=${build.dir}/jars
dist.dir=dist
dist.jar=${dist.dir}/${jar}
dist.jar=${dist.dir}/${jar}
# Simulation Information
simulation.dist.jar=${dist.dir}/FRCUserProgramSim.jar
wpilib.sim=${wpilib}/sim
wpilib.sim.lib=${wpilib.sim}/lib
wpilib.sim.tools=${wpilib.sim}/tools

View File

@@ -55,9 +55,9 @@
<jar destfile="${dist.jar}" update="false">
<manifest>
<attribute name="Main-Class" value="edu.wpi.first.wpilibj.RobotBase"/>
<attribute name="Robot-Class" value="${main}"/>
<attribute name="Class-Path" value="."/>
<attribute name="Main-Class" value="edu.wpi.first.wpilibj.RobotBase"/>
<attribute name="Robot-Class" value="${robot.class}"/>
<attribute name="Class-Path" value="."/>
</manifest>
<fileset dir="${build.dir}" includes="**/*.class"/>
@@ -96,4 +96,73 @@
trust="true"
command="chmod a+x debug*program; ${deploy.debug.command}"/>
</target>
<!-- Simulate -->
<target name="jar-for-simulation" depends="compile">
<echo>[jar-for-simulation] Building jar.</echo>
<jar destfile="${simulation.dist.jar}">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Robot-Class" value="${robot.class}"/>
<attribute name="Main-Class" value="edu.wpi.first.wpilibj.RobotBase"/>
</manifest>
<fileset dir="${build.dir}" />
<zipgroupfileset dir="${wpilib.sim.lib}">
<include name="**/*.jar" />
</zipgroupfileset>
</jar>
</target>
<target name="simulate" depends="jar-for-simulation">
<parallel>
<sequential>
<echo>[simulate] Running Gazebo.</echo>
<exec executable="frcsim">
<arg value="${simulation.world.file}"/>
</exec>
</sequential>
<sequential>
<sleep seconds="5"/>
<echo>[simulate] Running DriverStation.</echo>
<java jar="${wpilib.sim.tools}/SimDS.jar" fork="true">
<jvmarg value="-Djava.library.path=${wpilib.sim.lib}" />
</java>
</sequential>
<sequential>
<sleep seconds="5"/>
<echo>[simulate] Running Code.</echo>
<java jar="${simulation.dist.jar}" fork="true">
<jvmarg value="-Djava.library.path=${wpilib.sim.lib}" />
</java>
</sequential>
</parallel>
</target>
<target name="debug-simulate" depends="jar-for-simulation">
<parallel>
<sequential>
<echo>[debug-simulate] Running Gazebo.</echo>
<exec executable="frcsim">
<arg value="${simulation.world.file}"/>
</exec>
</sequential>
<sequential>
<sleep seconds="5"/>
<echo>[debug-simulate] Running DriverStation.</echo>
<java jar="${wpilib.sim.tools}/SimDS.jar" fork="true">
<jvmarg value="-Djava.library.path=${wpilib.sim.lib}" />
</java>
</sequential>
<sequential>
<sleep seconds="5"/>
<echo>[debug-simulate] Running Code.</echo>
<java jar="${simulation.dist.jar}" fork="true">
<jvmarg value="-Xdebug" />
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8348" />
<jvmarg value="-Djava.library.path=${wpilib.sim.lib}" />
</java>
</sequential>
</parallel>
</target>
</project>