Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. /**
  2. * This is a very simple robot program that can be used to send telemetry to
  3. * the data_logger script to characterize your drivetrain. If you wish to use
  4. * your actual robot code, you only need to implement the simple logic in the
  5. * autonomousPeriodic function and change the NetworkTables update rate
  6. *
  7. * This program assumes that you are using TalonSRX motor controllers and that
  8. * the drivetrain encoders are attached to the TalonSRX
  9. */
  10.  
  11. package dc;
  12.  
  13. import java.util.function.Supplier;
  14.  
  15. import com.ctre.phoenix.motorcontrol.FeedbackDevice;
  16. import com.ctre.phoenix.motorcontrol.NeutralMode;
  17. import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
  18. import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
  19.  
  20. import edu.wpi.first.networktables.NetworkTableEntry;
  21. import edu.wpi.first.networktables.NetworkTableInstance;
  22. import edu.wpi.first.wpilibj.Joystick;
  23. import edu.wpi.first.wpilibj.RobotController;
  24. import edu.wpi.first.wpilibj.SpeedControllerGroup;
  25. import edu.wpi.first.wpilibj.TimedRobot;
  26. import edu.wpi.first.wpilibj.Timer;
  27. import edu.wpi.first.wpilibj.drive.DifferentialDrive;
  28. import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
  29.  
  30. public class Robot extends TimedRobot {
  31.  
  32. static private double WHEEL_DIAMETER = 6;
  33. static private double ENCODER_PULSE_PER_REV = 4096;
  34. static private int PIDIDX = 0;
  35.  
  36. Joystick stick;
  37. DifferentialDrive drive;
  38.  
  39. static WPI_TalonSRX leftMaster;
  40. static WPI_TalonSRX rightMaster;
  41.  
  42. Supplier<Double> leftEncoderPosition;
  43. Supplier<Double> leftEncoderRate;
  44. Supplier<Double> rightEncoderPosition;
  45. Supplier<Double> rightEncoderRate;
  46.  
  47. NetworkTableEntry autoSpeedEntry =
  48. NetworkTableInstance.getDefault().getEntry("/robot/autospeed");
  49. NetworkTableEntry telemetryEntry =
  50. NetworkTableInstance.getDefault().getEntry("/robot/telemetry");
  51.  
  52. double priorAutospeed = 0;
  53. Number[] numberArray = new Number[9];
  54.  
  55. @Override
  56. public void robotInit() {
  57. if (!isReal()) SmartDashboard.putData(new SimEnabler());
  58.  
  59. stick = new Joystick(0);
  60. System.out.println("Setting up talons.");
  61. WPI_TalonSRX leftMaster = new WPI_TalonSRX(23);
  62. leftMaster.setInverted(false);
  63. leftMaster.setSensorPhase(false);
  64. leftMaster.setNeutralMode(NeutralMode.Brake);
  65.  
  66. WPI_TalonSRX rightMaster = new WPI_TalonSRX(21);
  67. rightMaster.setInverted(false);
  68. rightMaster.setSensorPhase(false);
  69. rightMaster.setNeutralMode(NeutralMode.Brake);
  70.  
  71. WPI_TalonSRX leftSlave0 = new WPI_TalonSRX(24);
  72. leftSlave0.setInverted(false);
  73. leftSlave0.follow(leftMaster);
  74. leftSlave0.setNeutralMode(NeutralMode.Brake);
  75.  
  76. WPI_TalonSRX rightSlave0 = new WPI_TalonSRX(22);
  77. rightSlave0.setInverted(false);
  78. rightSlave0.follow(rightMaster);
  79. rightSlave0.setNeutralMode(NeutralMode.Brake);
  80.  
  81. //
  82. // Configure drivetrain movement
  83. //
  84.  
  85. drive = new DifferentialDrive(leftMaster, rightMaster);
  86.  
  87. drive.setDeadband(0);
  88.  
  89. //
  90. // Configure encoder related functions -- getDistance and getrate should
  91. // return units and units/s
  92. //
  93.  
  94. double encoderConstant =
  95. (1 / ENCODER_PULSE_PER_REV) * WHEEL_DIAMETER * Math.PI;
  96.  
  97. leftMaster.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder,
  98. PIDIDX, 10);
  99. leftEncoderPosition = ()
  100. -> leftMaster.getSelectedSensorPosition(PIDIDX) * encoderConstant;
  101. leftEncoderRate = ()
  102. -> leftMaster.getSelectedSensorVelocity(PIDIDX) * encoderConstant *
  103. 10;
  104.  
  105. rightMaster.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder,
  106. PIDIDX, 10);
  107. rightEncoderPosition = ()
  108. -> rightMaster.getSelectedSensorPosition(PIDIDX) * encoderConstant;
  109. rightEncoderRate = ()
  110. -> rightMaster.getSelectedSensorVelocity(PIDIDX) * encoderConstant *
  111. 10;
  112.  
  113. // Reset encoders
  114. leftMaster.setSelectedSensorPosition(0);
  115. rightMaster.setSelectedSensorPosition(0);
  116.  
  117. // Set the update rate instead of using flush because of a ntcore bug
  118. // -> probably don't want to do this on a robot in competition
  119. NetworkTableInstance.getDefault().setUpdateRate(0.010);
  120. }
  121.  
  122. @Override
  123. public void disabledInit() {
  124. System.out.println("Robot disabled");
  125. drive.tankDrive(0, 0);
  126. }
  127.  
  128. @Override
  129. public void disabledPeriodic() {
  130. }
  131.  
  132. @Override
  133. public void robotPeriodic() {
  134. // feedback for users, but not used by the control program
  135. SmartDashboard.putNumber("l_encoder_pos", leftEncoderPosition.get());
  136. SmartDashboard.putNumber("l_encoder_rate", leftEncoderRate.get());
  137. SmartDashboard.putNumber("r_encoder_pos", rightEncoderPosition.get());
  138. SmartDashboard.putNumber("r_encoder_rate", rightEncoderRate.get());
  139. }
  140.  
  141. @Override
  142. public void teleopInit() {
  143. System.out.println("Robot in operator control mode");
  144. }
  145.  
  146. @Override
  147. public void teleopPeriodic() {
  148. drive.arcadeDrive(-stick.getY(), stick.getX());
  149. }
  150.  
  151. @Override
  152. public void autonomousInit() {
  153. System.out.println("Robot in autonomous mode");
  154. }
  155.  
  156. /**
  157. * If you wish to just use your own robot program to use with the data logging
  158. * program, you only need to copy/paste the logic below into your code and
  159. * ensure it gets called periodically in autonomous mode
  160. *
  161. * Additionally, you need to set NetworkTables update rate to 10ms using the
  162. * setUpdateRate call.
  163. */
  164. @Override
  165. public void autonomousPeriodic() {
  166.  
  167. // Retrieve values to send back before telling the motors to do something
  168. double now = Timer.getFPGATimestamp();
  169.  
  170. System.out.println(leftMaster.toString());
  171.  
  172. double leftPosition = leftEncoderPosition.get();
  173. double leftRate = leftEncoderRate.get();
  174.  
  175. double rightPosition = rightEncoderPosition.get();
  176. double rightRate = rightEncoderRate.get();
  177.  
  178. double battery = RobotController.getBatteryVoltage();
  179. double leftMotorVolts = leftMaster.getMotorOutputVoltage();
  180. double rightMotorVolts = rightMaster.getMotorOutputVoltage();
  181.  
  182. // Retrieve the commanded speed from NetworkTables
  183. double autospeed = autoSpeedEntry.getDouble(0);
  184. priorAutospeed = autospeed;
  185.  
  186. // command motors to do things
  187. drive.tankDrive(autospeed, autospeed, false);
  188.  
  189. // send telemetry data array back to NT
  190. numberArray[0] = now;
  191. numberArray[1] = battery;
  192. numberArray[2] = autospeed;
  193. numberArray[3] = leftMotorVolts;
  194. numberArray[4] = rightMotorVolts;
  195. numberArray[5] = leftPosition;
  196. numberArray[6] = rightPosition;
  197. numberArray[7] = leftRate;
  198. numberArray[8] = rightRate;
  199.  
  200. telemetryEntry.setNumberArray(numberArray);
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement