PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/donovan/tags/newYorkRevisions/donSunMornNY/DonovanDriveTrain.java

https://github.com/prog694/frc
Java | 276 lines | 94 code | 39 blank | 143 comment | 12 complexity | 8e1938077e9aefa9d2222126ff19b8ca MD5 | raw file
  1. //@TODO: use a PID to drive straight, if necessary
  2. package edu.stuy;
  3. import edu.wpi.first.wpilibj.*;
  4. /**
  5. *
  6. * @author Prog
  7. */
  8. public class DonovanDriveTrain extends RobotDrive implements Ports {
  9. double SCALE = 30000;
  10. public boolean highGear = false;
  11. //public Gyro gyro;
  12. Encoder encoderLeft;
  13. Encoder encoderRight;
  14. Servo shifterLeft;
  15. Servo shifterRight;
  16. final double DISTANCE_PER_PULSE = 12.0 / 530.0; // 250 pulses per revolution, 530 pulses per foot
  17. Donovan donnie;
  18. PIDController straightController;
  19. final double FWD_T = .1; //timer per loop
  20. final double FWD_P = .05; //range .02 -> .1
  21. final double FWD_D = 0; // range .01 -> .05
  22. final double FWD_I = 0;
  23. public DonovanDriveTrain(int motor1, int motor2, int motor3, int motor4, Donovan d) {
  24. super(motor1, motor2, motor3, motor4);
  25. donnie = d;
  26. encoderLeft = new Encoder(ENCODER_CHANNEL_1A, ENCODER_CHANNEL_1B, true);
  27. encoderRight = new Encoder(ENCODER_CHANNEL_2A, ENCODER_CHANNEL_2B, true);
  28. shifterLeft = new Servo(SHIFTERLEFT_CHANNEL);
  29. shifterRight = new Servo(SHIFTERRIGHT_CHANNEL);
  30. encoderLeft.setDistancePerPulse(DISTANCE_PER_PULSE);
  31. encoderRight.setDistancePerPulse(DISTANCE_PER_PULSE);
  32. setInvertedMotor(RobotDrive.MotorType.kFrontRight, true);
  33. setInvertedMotor(RobotDrive.MotorType.kRearRight, true);
  34. setInvertedMotor(RobotDrive.MotorType.kFrontLeft, true);
  35. setInvertedMotor(RobotDrive.MotorType.kRearLeft, true);
  36. encoderLeft.start();
  37. encoderRight.start();
  38. straightController = new PIDController(PVAL, IVAL, DVAL, donnie.gyro, new PIDOutput() {
  39. public void pidWrite(double output) {
  40. arcadeDrive(-0.5, output);
  41. }
  42. }, 0.005);
  43. straightController.setInputRange(-360.0, 360.0);
  44. straightController.setTolerance(1 / 90. * 100);
  45. straightController.disable();
  46. if (shifterLeft.get() == 1 && shifterRight.get() == 1) {
  47. highGear = true;
  48. }
  49. }
  50. /**
  51. * The measured distance from the left encoder.
  52. * @return The distance (inches) from the left encoder.
  53. */
  54. public double getLeftEnc() {
  55. return encoderLeft.getDistance();
  56. }
  57. /**
  58. * The measured distance from the right encoder.
  59. * @return The distance (inches) counted from the right encoder.
  60. */
  61. public double getRightEnc() {
  62. return encoderRight.getDistance();
  63. }
  64. /*
  65. * A FAILED attempt at driving straight. When tested, it looked like it was having a seizure.
  66. * Tuning the SCALE value might help. However, it did go from point A to point B, though not in a linear path.
  67. */
  68. /*
  69. public void forwardInchesCharlie(int inches) {
  70. encoderRight.reset();
  71. encoderLeft.reset();
  72. double lm;
  73. double rm;
  74. int i = 0;
  75. while ((getAvgDistance() < inches) && donnie.isAutonomous() && donnie.isEnabled()) {
  76. /*if(i%10 == 0)
  77. System.out.println("diff: " + (encoderLeft.get()-encoderRight.get()));
  78. else if(i%50 == 0)
  79. System.out.println("left: " + encoderLeft.get() + " right: " + encoderRight.get());
  80. * */
  81. /*
  82. lm = .5 + ((encoderRight.get() - encoderLeft.get()) / (SCALE));
  83. rm = .5 + ((encoderLeft.get() - encoderRight.get()) / SCALE);
  84. tankDrive((-1*lm), (-1*rm));
  85. i++;
  86. }
  87. tankDrive(0, 0);
  88. }
  89. */
  90. public void forwardInchesOscar(int inches) {
  91. resetEncoders();
  92. straightController.enable();
  93. while (getAvgDistance() < inches && donnie.isAutonomous() && donnie.isEnabled()) {
  94. straightController.setSetpoint(0);
  95. }
  96. straightController.disable();
  97. tankDrive(0, 0);
  98. }
  99. /*
  100. * A SO FAR UNSUCCESSFUL attempt at drive straight code, ported from Michael1.
  101. * There may be an error in the code, and the p i d values may need tuning.
  102. */
  103. /*
  104. public void forwardInchesMike(int inches){
  105. resetEncoders();
  106. donnie.gyro.reset();
  107. double p, i, d, err, lastErr;
  108. double multiplier = 0.3;
  109. p = i = d = err = 0;
  110. lastErr = donnie.gyro.getAngle();
  111. while(getAvgDistance() < inches && donnie.isAutonomous() && donnie.isEnabled()) {
  112. err = donnie.gyro.getAngle();
  113. p = err;
  114. i = i + err * FWD_T;
  115. d = (err-lastErr)/FWD_T;
  116. lastErr = err;
  117. double diff = p*FWD_P + d*FWD_D + i*FWD_I;
  118. if (getAvgDistance() > inches - 7) {
  119. multiplier = (inches - getAvgDistance()) / 14;
  120. if (multiplier < .2) {
  121. multiplier = 0.2;
  122. }
  123. }
  124. donnie.dt.tankDrive((multiplier - diff), (multiplier + diff));
  125. Timer.delay(FWD_T);
  126. }
  127. donnie.dt.tankDrive(0, 0);
  128. }
  129. * */
  130. /*
  131. * A FAILED attempt at drive straight code.
  132. */
  133. /*
  134. public void forwardInchesTest(int inches) {
  135. encoderLeft.reset();
  136. encoderRight.reset();
  137. donnie.gyro.reset();
  138. straightController.setSetpoint(donnie.gyro.getAngle()); //0 ?
  139. while (getAvgDistance() < inches) {
  140. straightController.disable();
  141. System.out.println("onTarget: " + straightController.onTarget());
  142. System.out.println("gyro angle: " + donnie.gyro.getAngle());
  143. if (straightController.onTarget()) {
  144. straightController.disable();
  145. arcadeDrive(-0.5, 0); //speed may need adjustment
  146. } else {
  147. straightController.enable();
  148. }
  149. arcadeDrive(0, 0);
  150. }
  151. }
  152. */
  153. /*
  154. * Move forward an approximate distance using encoders, NOT necessarily straight.
  155. * The bot may drift and go farther than expected.
  156. * If the bot arcs horribly, make sure all victors are working properly.
  157. * @param inches distance in inches the bot will attempt to travel
  158. */
  159. /* public void forwardInches(int inches) {
  160. encoderLeft.reset();
  161. encoderRight.reset();
  162. while ((getAvgDistance() < inches) && donnie.isAutonomous() && donnie.isEnabled()) {
  163. tankDrive(-0.5, -0.5); //speed may need adjustment
  164. }
  165. tankDrive(0, 0);
  166. }
  167. */
  168. /**
  169. * Move backwards an approximate distance using encoders, NOT necessarily straight.
  170. * The bot may drift and go farther than expected.
  171. * If the bot arcs horribly, make sure all victors are working properly.
  172. * @param inches distance in inches the bot will attempt to travel in reverse.
  173. */
  174. public void backInches(int inches) {
  175. encoderLeft.reset();
  176. encoderRight.reset();
  177. while ((getAvgDistance() > -inches) && donnie.isAutonomous() && donnie.isEnabled()) {
  178. arcadeDrive(0.5, 0); //speed may need adjustment
  179. }
  180. arcadeDrive(0, 0);
  181. }
  182. /**
  183. * Reset both encoders' tick, distance, etc. count to zero
  184. */
  185. public void resetEncoders() {
  186. encoderLeft.reset();
  187. encoderRight.reset();
  188. }
  189. /**
  190. * Calculate average distance of the two encoders.
  191. * @return Average of the distances (inches) read by each encoder since they were last reset.
  192. */
  193. public double getAvgDistance() {
  194. return (encoderLeft.getDistance() + encoderRight.getDistance()) / 2.0;
  195. }
  196. /**
  197. * Shift the gear of the drive motors. If in high gear, set low - if in low gear, set high.
  198. */
  199. public void shift() {
  200. if (highGear) {
  201. setHigh();
  202. } else {
  203. setLow();
  204. }
  205. }
  206. /**
  207. * Set the drive motors to high gear.
  208. */
  209. public void setHigh() {
  210. shifterLeft.set(1);
  211. shifterRight.set(1);
  212. highGear = true;
  213. }
  214. /**
  215. * Set drive motors to low gear.
  216. */
  217. public void setLow() {
  218. shifterLeft.set(0);
  219. shifterRight.set(0);
  220. highGear = false;
  221. }
  222. /**
  223. * Whether the robot is in high gear or low gear.
  224. * @return highGear Whether or not the robot is in high gear. If true, its in high gear. If false, low gear.
  225. */
  226. public boolean getGear() {
  227. return highGear;
  228. }
  229. }