From 10ba68d393a88ab217191ea07c537228c27e2d7d Mon Sep 17 00:00:00 2001 From: ergouser Date: Tue, 9 Oct 2012 12:39:23 -0600 Subject: [PATCH] Sensor and vision code. --- .../com/team4153/oppie2012/DataServer.java | 169 +++++++++++++++++ .../src/com/team4153/oppie2012/Oppie.java | 126 ++++++++++--- .../com/team4153/oppie2012/ShooterHead.java | 176 +++++++++++++++++- .../com/team4153/oppie2012/StateModel.java | 19 +- 4 files changed, 458 insertions(+), 32 deletions(-) create mode 100644 Oppie2012/src/com/team4153/oppie2012/DataServer.java diff --git a/Oppie2012/src/com/team4153/oppie2012/DataServer.java b/Oppie2012/src/com/team4153/oppie2012/DataServer.java new file mode 100644 index 0000000..4a8590f --- /dev/null +++ b/Oppie2012/src/com/team4153/oppie2012/DataServer.java @@ -0,0 +1,169 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.team4153.oppie2012; + +import edu.wpi.first.wpilibj.ADXL345_I2C; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import javax.microedition.io.Connector; +import javax.microedition.io.ServerSocketConnection; +import javax.microedition.io.SocketConnection; + +public class DataServer { + + public static final int CALIBRATION_MODE = 1; + + // received values. These are read in the order they are defined here. The + // values will be populated by any client calling in on this socket. + /** + * The mode of operation. Currently, 1 is calibration mode, everything else + * is "normal" mode. + */ + public int mode; + /** + * The angle to the target. This is provided by the vision system and is the + * number of pixels the robot must rotate in order to be aligned with the + * target. Any value greater than 1000 is ignored. That is, anything other + * than the vision system should send in a large angle (except for testing). + */ + public float angleToTarget; + /** + * The distance to the target. This is provided by the vision system and is + * used in conjunction with the calibration data to determine the head angle + * and shooter speed to use when firing the ball. Any value < 0 is ignored, + * so any system that is not the vision system should send in a negative + * value. + */ + public float distanceToTarget; + /** + * The speed to run the shooter motor when the "shoot" button is pressed. + * This is used only in calibration mode (this.mode=1) and is otherwise + * ignored. The value is a percentage (0-100) of the power applied to the + * motor. That is, the encoder is assumed broken. + */ + public int shooterSpeed; + /** + * The angle of the shooter head. This is assumed a percentage from 0-100. + * Zero is at the bottom of the travel and 100 at the top. Values are read + * from the shooter head analog input. + */ + public int shooterHeadPercent; + + /** The thread that is running that data server. */ + Thread listeningThread; + + /** The data server singleton. */ + protected static DataServer dataServer; + + /** Returns the DataServer Singleton. */ + public static DataServer getDataServer () { + if ( dataServer == null ) { + dataServer = new DataServer(); + } + return dataServer; + } + + /** + * Default constructor. + */ + protected DataServer() { + Runnable r = new Runnable() { + + public void run() { + while (true) { + DataInputStream dis = null; + DataOutputStream dos = null; + SocketConnection socketConnection = null; + ServerSocketConnection socket = null; + try { + System.err.println("Waiting for connection"); + // I think "socket" is correct, but "serversocket" seems to be needed for the simulator. + ///socket = (ServerSocketConnection)Connector.open("socket://:5000"); + socket = (ServerSocketConnection) Connector.open("serversocket://:5000"); + + // Wait for a connection. + socketConnection = (SocketConnection) socket.acceptAndOpen(); + // I really can't be bothered to track these connections so I'm going to require a reconnect + // everytime. There's a minor extra overhead on this (not much, this is the normal behavior of http). + // There's also possible problems with simultaneous connections. Depends on the TCP stack, but + // I'm building a debug tool not an enterprise server, so I really don't care. + // What happens next is we read values from the stream (here we simulate two ints a float ) + // After that we send the values that have been stored in this object + // then close and clean up everything.. + System.err.println("Connection accepted from: " + socketConnection.getAddress()); + dis = new DataInputStream(socketConnection.openInputStream()); + dos = new DataOutputStream(socketConnection.openOutputStream()); + + mode = dis.readInt(); + + float tAngle = dis.readFloat(); + if (tAngle < 1000) { + angleToTarget = tAngle; + } + float tDistance = dis.readFloat(); + if (tDistance > 0) { + distanceToTarget = tDistance; + } + System.out.println ("AutoShoot: " + tDistance + " " +angleToTarget); + shooterSpeed = dis.readInt(); + shooterHeadPercent = dis.readInt(); + + // OK, so current list is 4 doubles (x,y,z,angle). + + ADXL345_I2C.AllAxes axes = Oppie.robotInstance.accelerometer.getAccelerations(); + + dos.writeDouble (axes.XAxis); + dos.writeDouble (axes.YAxis); + dos.writeDouble (axes.ZAxis); + dos.writeDouble (Oppie.robotInstance.gyro.getAngle()); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (dis != null) { + try { + dis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + dis = null; + } + if (dos != null) { + try { + dos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + dos = null; + } + if (socketConnection != null) { + try { + socketConnection.close(); + } catch (IOException e) { + e.printStackTrace(); + } + socketConnection = null; + } + if (socket != null) { + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + socket = null; + } + } + } + } + }; + + if (listeningThread == null) { + listeningThread = new Thread(r, "Data Thread"); + listeningThread.start(); + } + + } +} \ No newline at end of file diff --git a/Oppie2012/src/com/team4153/oppie2012/Oppie.java b/Oppie2012/src/com/team4153/oppie2012/Oppie.java index 053b50e..d758e5d 100644 --- a/Oppie2012/src/com/team4153/oppie2012/Oppie.java +++ b/Oppie2012/src/com/team4153/oppie2012/Oppie.java @@ -17,7 +17,14 @@ * directory. */ public class Oppie extends SimpleRobot { - + + /**Bridge Balance Kp parameter. */ + public static final double KP = 1.5; + /**Bridge Balance Ki parameter. */ + public static final double KI = 0.6; + /**Bridge Balance Kd parameter. */ + public static final double KD = 0.2; + public static final int LEFT_FRONT_MOTOR = 2; public static final int LEFT_REAR_MOTOR = 6; public static final int RIGHT_FRONT_MOTOR = 4; @@ -25,12 +32,23 @@ public class Oppie extends SimpleRobot { public static final int LEFT_JOYSTICK = 1; public static final int RIGHT_JOYSTICK = 2; public static final int MANIPULATOR_JOYSTICK = 3; - Joystick leftStick = new Joystick(LEFT_JOYSTICK); - Joystick rightStick = new Joystick(RIGHT_JOYSTICK); - Joystick manipulatorStick = new Joystick(MANIPULATOR_JOYSTICK); - RobotDrive drive; - + protected Joystick leftStick = new Joystick(LEFT_JOYSTICK); + protected Joystick rightStick = new Joystick(RIGHT_JOYSTICK); + protected Joystick manipulatorStick = new Joystick(MANIPULATOR_JOYSTICK); + protected RobotDrive drive; public static Oppie robotInstance; + public ADXL345_I2C accelerometer; + public Gyro gyro; + + /** The slot into which the accelerometer is plugged. */ + public static int ACCELEROMETER_SLOT = 1; + /** The slot into which the gyro is plugged. */ + public static int GYRO_SLOT = 1; + /** The gyro analog channel. */ + public static int GYRO_ANALOG_CHANNEL = 2; + + /** The PIDController. */ + public PIDController pidController; /** * Create the state model for the shooter. @@ -41,11 +59,14 @@ protected void robotInit() { try { drive = new RobotDrive(new CANJaguar(LEFT_FRONT_MOTOR), new CANJaguar(LEFT_REAR_MOTOR), new CANJaguar(RIGHT_FRONT_MOTOR), new CANJaguar(RIGHT_REAR_MOTOR)); drive.setSafetyEnabled(false); - } catch (Exception any) { + } catch (Exception any) { any.printStackTrace(); } robotInstance = this; - } + accelerometer = new ADXL345_I2C(ACCELEROMETER_SLOT, ADXL345_I2C.DataFormat_Range.k4G); + gyro = new Gyro(GYRO_SLOT, GYRO_ANALOG_CHANNEL); + DataServer.getDataServer(); // make sure this is listening. + } /** * This function is called once each time the robot enters autonomous mode. @@ -57,30 +78,81 @@ public void autonomous() { * This function is called once each time the robot enters operator control. */ public void operatorControl() { - System.err.println ("Entering Teleop"); - StateModel.getStateModel().reset(); - while (isOperatorControl() && isEnabled()) { + System.err.println("Entering Teleop"); + StateModel stateModel = StateModel.getStateModel(); + stateModel.reset(); + DataServer dataServer = DataServer.getDataServer(); + while (isOperatorControl() && isEnabled()) { try { - drive.tankDrive(rightStick, leftStick); - //System.err.println("Teleop"); - //StateModel.getStateModel().runRoller(manipulatorStick.getRawButton(3)); - //StateModel.getStateModel().runBelt(manipulatorStick.getRawButton(4)); - //StateModel.getStateModel().runMetering(manipulatorStick.getRawButton(5)); - StateModel.getStateModel().task(); - StateModel.getStateModel().shoot(manipulatorStick.getRawButton(1)); - //StateModel.getStateModel().runRoller(manipulatorStick.getRawButton(2)); - if ( manipulatorStick.getRawButton(8) ) { - StateModel.getStateModel().reset(); +// System.err.println("Mode " + dataServer.mode + " percent " + dataServer.shooterHeadPercent + " speed " + dataServer.shooterSpeed); +// if (dataServer.mode == DataServer.CALIBRATION_MODE) { // calibration mode +// if (manipulatorStick.getRawButton(1)) { +// // shoot with the provided parameters. +// stateModel.shooterHeadSpeed = dataServer.shooterSpeed / 100f; +// ShooterHead.getShooterHead().setPosition(dataServer.shooterHeadPercent); +// stateModel.shoot(true); +// } +// Timer.delay(0.005); +// } + if (manipulatorStick.getRawButton(3)) { // autoshoot... + ShooterHead.getShooterHead().autoShoot(); + Timer.delay(0.005); +// } +// else if (manipulatorStick.getRawButton(9)) { // autobalance... +// if ( pidController == null ) { +// PIDOutput driveOutput = new PIDOutput () { +// +// public void pidWrite(double output) { +// drive.tankDrive(-output, -output); +// } +// +// +// }; +// PIDSource accelSource = new PIDSource () { +// +// public double pidGet() { +// return accelerometer.getAcceleration(ADXL345_I2C.Axes.kX); +// } +// +// }; +// pidController = new PIDController(KP, KI, KD, accelSource, driveOutput, 0.01); +// } + } else { // normal operation. + if ( pidController != null ) { + pidController.disable(); + pidController = null; + } + System.out.println ("To Robot drive " + rightStick.getY() + " " + leftStick.getY() ); + double right = rightStick.getY(); + if ( right > 0.55 ) { + right = 0.5; + } + double left = leftStick.getY(); + if ( left > 0.55 ) { + left = 0.5; + } + drive.tankDrive(right, left); + //drive.tankDrive(rightStick, leftStick); + //System.err.println("Teleop"); + //StateModel.getStateModel().runRoller(manipulatorStick.getRawButton(3)); + //StateModel.getStateModel().runBelt(manipulatorStick.getRawButton(4)); + //StateModel.getStateModel().runMetering(manipulatorStick.getRawButton(5)); + stateModel.task(); + stateModel.shoot(manipulatorStick.getRawButton(1)); + //StateModel.getStateModel().runRoller(manipulatorStick.getRawButton(2)); + if (manipulatorStick.getRawButton(8)) { + stateModel.reset(); + } + //Gyro g = new Gyro(); + //System.err.println("Teleop - running BMS"); + BMS.getBMS().task(); + ShooterHead.getShooterHead().task(); + Timer.delay(0.005); } - //Gyro g = new Gyro(); - //System.err.println("Teleop - running BMS"); - BMS.getBMS().task(); - ShooterHead.getShooterHead().task(); - Timer.delay(0.005); } catch (Exception ex) { ex.printStackTrace(); } } - + } } diff --git a/Oppie2012/src/com/team4153/oppie2012/ShooterHead.java b/Oppie2012/src/com/team4153/oppie2012/ShooterHead.java index c234100..a961533 100644 --- a/Oppie2012/src/com/team4153/oppie2012/ShooterHead.java +++ b/Oppie2012/src/com/team4153/oppie2012/ShooterHead.java @@ -6,7 +6,22 @@ import edu.wpi.first.wpilibj.AnalogChannel; import edu.wpi.first.wpilibj.CANJaguar; +import edu.wpi.first.wpilibj.RobotDrive; import edu.wpi.first.wpilibj.can.CANTimeoutException; +import java.util.Vector; + +class CalibrationData { + + public double distance; // inches + public double shooterHeadPosition; // volts + public double shooterSpeed; // percent + + public CalibrationData(double distance, double shooterHeadPosition, double shooterSpeed) { + this.distance = distance; + this.shooterHeadPosition = shooterHeadPosition; + this.shooterSpeed = shooterSpeed; + } +} /** * @@ -44,6 +59,11 @@ public class ShooterHead { * The bridge management system singleton. */ protected static ShooterHead shooter; + /** + * The calibration data. This is a Vector of CalibrationData. It must be + * ordered by increasing distance. + */ + protected Vector calibrationData; /** * State model is a singleton. @@ -65,6 +85,11 @@ protected ShooterHead() { } catch (Exception e) { e.printStackTrace(); } + calibrationData = new Vector(); + // so, irritating hand filling of the Vector, but it's easy and it works.. + // DUMMY DATA SO FAR + calibrationData.addElement(new CalibrationData(24, 4.0, 0.25)); + calibrationData.addElement(new CalibrationData(120, 4.0, 0.75)); } /** @@ -72,7 +97,7 @@ protected ShooterHead() { */ public void task() { boolean buttonPressed = Oppie.robotInstance.manipulatorStick.getRawButton(6) || Oppie.robotInstance.manipulatorStick.getRawButton(11); - System.err.println("Shooter head throttle " + " " + buttonPressed + " x " + Oppie.robotInstance.manipulatorStick.getX() + " y " + Oppie.robotInstance.manipulatorStick.getY() + " " + positionInput.getAverageVoltage()); + //System.err.println("Shooter head throttle " + " " + buttonPressed + " x " + Oppie.robotInstance.manipulatorStick.getX() + " y " + Oppie.robotInstance.manipulatorStick.getY() + " " + positionInput.getAverageVoltage()); if (buttonPressed) { try { double amount = Oppie.robotInstance.manipulatorStick.getY(); @@ -85,7 +110,7 @@ public void task() { } } - if ( positionInput.getAverageVoltage() > LOWER_LIMT && amount > 0) { + if (positionInput.getAverageVoltage() > LOWER_LIMT && amount > 0) { System.err.println("Shooter head throttle " + Oppie.robotInstance.manipulatorStick.getThrottle() + " " + positionInput.getAverageVoltage()); headDrive.setX(amount); } else { @@ -99,4 +124,151 @@ public void task() { } } } + + void setPosition(int shooterHeadPercent) throws CANTimeoutException { + double targetVoltage = (TOP_LIMT - LOWER_LIMT) * shooterHeadPercent / 100f; + double amount = 0.5; // don't know which is up and which is down, so someone need to CAREFULLY test this. + // do this until the two match (within ~0.3% - we can probably do better than that... A2D is 10 bit (I think) so, 1 in 1024 ~0.001V + while (targetVoltage > (positionInput.getAverageVoltage() + 0.01) || targetVoltage < (positionInput.getAverageVoltage() - 0.01)) { + if (targetVoltage > positionInput.getAverageVoltage()) { + // move down (maybe not, that's why both limits are in here. I'm not sure of the sign of amount to move in the down direction + if (positionInput.getAverageVoltage() < TOP_LIMT && positionInput.getAverageVoltage() > LOWER_LIMT && positionInput.getAverageVoltage() < targetVoltage) { + System.err.println("Shooter head throttle " + Oppie.robotInstance.manipulatorStick.getThrottle() + " " + positionInput.getAverageVoltage()); + headDrive.setX(amount); + } else { + headDrive.setX(0); + } + } else { + if (positionInput.getAverageVoltage() < TOP_LIMT && positionInput.getAverageVoltage() > LOWER_LIMT && positionInput.getAverageVoltage() > targetVoltage) { + System.err.println("Shooter head throttle " + Oppie.robotInstance.manipulatorStick.getThrottle() + " " + positionInput.getAverageVoltage()); + headDrive.setX(-amount); + } else { + headDrive.setX(0); + } + + } + } + headDrive.setX(0); + } + + /** + * This will move the shooter head. It should be checked perioidically to + * ensure that the limits have not been reached. + */ + void moveHead(double targetVoltage, double speed) throws CANTimeoutException { + try { + if (targetVoltage > (positionInput.getAverageVoltage() + 0.01) || targetVoltage < (positionInput.getAverageVoltage() - 0.01)) { + if (targetVoltage > positionInput.getAverageVoltage()) { + // move down (maybe not, that's why both limits are in here. I'm not sure of the sign of amount to move in the down direction + if (positionInput.getAverageVoltage() < TOP_LIMT && positionInput.getAverageVoltage() > LOWER_LIMT && positionInput.getAverageVoltage() < targetVoltage) { + //System.err.println("Shooter head throttle " + Oppie.robotInstance.manipulatorStick.getThrottle() + " " + positionInput.getAverageVoltage()); + headDrive.setX(speed); + } else { + headDrive.setX(0); + } + } else { + if (positionInput.getAverageVoltage() < TOP_LIMT && positionInput.getAverageVoltage() > LOWER_LIMT && positionInput.getAverageVoltage() > targetVoltage) { + //System.err.println("Shooter head throttle " + Oppie.robotInstance.manipulatorStick.getThrottle() + " " + positionInput.getAverageVoltage()); + headDrive.setX(-speed); + } else { + headDrive.setX(0); + } + + } + } + } finally { + headDrive.setX(0); + } + } + + /** + * Here's where we auto-target and shoot. The shooter head is trashed, so + * rather than try to shoot from our current position, we're going to move + * until we are 52" from the target (image is 63048 sq. pixels) and aligned + * with it and we'll shoot from there. + */ + void autoShoot() throws CANTimeoutException { + DataServer dataServer = DataServer.getDataServer(); + StateModel stateModel = StateModel.getStateModel(); + RobotDrive drive = Oppie.robotInstance.drive; + int currentInchesFromTarget = -1; + System.out.println("AutoShoot: " + dataServer.distanceToTarget + " " + dataServer.angleToTarget); + while (Oppie.robotInstance.manipulatorStick.getRawButton(3) && dataServer.distanceToTarget > 62 && dataServer.distanceToTarget > 0) { + System.out.println("AutoShoot: " + dataServer.distanceToTarget + " " + dataServer.angleToTarget); + if (dataServer.distanceToTarget > 62) { + // move forward (we hope). + int factor; + if (dataServer.angleToTarget < 0 ) { + factor = 1; + } + else { + factor = -1; + } + drive.tankDrive(-0.36-factor*0.1, -0.36+factor*0.1); + } else if (dataServer.distanceToTarget < 62) { + // move backwards + drive.tankDrive(0.3, 0.3); + } + } + drive.tankDrive(0, 0); + if ( dataServer.distanceToTarget < 62 ){ + StateModel.getStateModel().shoot(true); + } + drive.tankDrive(0, 0); + + } +// /** +// * Here's where we auto-target and shoot. We need the angle and distance +// * from the vision system. The angle is used to re-orient the robot, then +// * the distance is used to calculate the shooter head position and the +// * speed. +// */ +// void autoShoot() throws CANTimeoutException { +// DataServer dataServer = DataServer.getDataServer(); +// StateModel stateModel = StateModel.getStateModel(); +// try { +// //OK, we're going to start the robot moving, then we're going to start the head moving. +// if (dataServer.angleToTarget >= 1000) { // not doing anything, don't have any data. +// return; +// } +// double targetVoltage = -1; +// double shooterHeadSpeed = -1; +// for (int counter = 0; counter < calibrationData.size() - 1; counter++) { +// CalibrationData cd = (CalibrationData) calibrationData.elementAt(counter); +// CalibrationData nextCd = (CalibrationData) calibrationData.elementAt(counter + 1); +// if (dataServer.distanceToTarget > cd.distance) { +////estimatedDistance = (areas[d1] -boundingArea)/(areas[d1]-areas[d1+1]) * (distances[d1+1]-distances[d1]) + distances[d1]; +// double fraction = (dataServer.distanceToTarget - cd.distance) / (nextCd.distance - cd.distance); +// shooterHeadSpeed = fraction * (nextCd.shooterSpeed - cd.shooterSpeed) + cd.shooterSpeed; +// targetVoltage = fraction * (nextCd.shooterHeadPosition - cd.shooterHeadPosition) + cd.shooterHeadPosition; +// break; +// } +// } +// System.err.println("Shooter head angle " + targetVoltage + " and speed " + shooterHeadSpeed + " Vision Angle " + dataServer.angleToTarget + " distance " + dataServer.distanceToTarget); +// if (targetVoltage < 0 || shooterHeadSpeed < 0) { +// return; +// } +// while (dataServer.angleToTarget > 5 && dataServer.angleToTarget < -5 && dataServer.angleToTarget < 1000) { +// // rotate the drive +// if (dataServer.angleToTarget < 0) { +// Oppie.robotInstance.drive.tankDrive(0.25, -0.25); +// } else { +// Oppie.robotInstance.drive.tankDrive(-0.25, 0.25); +// } +// moveHead(targetVoltage, 0.5); +// } +// // everything is ready.... +// stateModel.meteringDrive.set(Relay.Value.kReverse); +// stateModel.shootDrive.setX(shooterHeadSpeed); +// while ( Oppie.robotInstance.manipulatorStick.getRawButton(3) ) { +// System.err.println ("If the shot is off, you can let go of the button"); +// } +// +// } finally { +// stateModel.meteringDrive.set(Relay.Value.kOff); +// stateModel.shootDrive.setX(0); +// headDrive.setX(0); +// } +// } +// } \ No newline at end of file diff --git a/Oppie2012/src/com/team4153/oppie2012/StateModel.java b/Oppie2012/src/com/team4153/oppie2012/StateModel.java index 32f90c7..3e4c84f 100644 --- a/Oppie2012/src/com/team4153/oppie2012/StateModel.java +++ b/Oppie2012/src/com/team4153/oppie2012/StateModel.java @@ -132,6 +132,9 @@ public String toString() { * Description of all future states. */ FutureStates[] futureStates; + + /** The shooter head speed. */ + protected float shooterHeadSpeed; /** * State model is a singleton. @@ -236,7 +239,7 @@ public void runRoller(boolean roller) throws CANTimeoutException { public void runBelt(boolean belt) throws CANTimeoutException { if (beltDrive != null) { if (belt) { - beltDrive.setX(-0.5); + beltDrive.setX(-0.75); } else { beltDrive.setX(0); } @@ -264,9 +267,19 @@ public void runMetering(boolean metering) throws CANTimeoutException { public void shoot(boolean shoot) throws CANTimeoutException { if (shootDrive != null) { if (shoot) { + shootDrive.setX(-0.40); + try { + Thread.sleep(700); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } meteringDrive.set(Relay.Value.kReverse); - shootDrive.setX(-0.25); - } else { + try { + Thread.sleep(800); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + } else { shootDrive.setX(0); } }