Guest User

Untitled

a guest
Nov 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*/
  2. /* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
  3. /* Open Source Software - may be modified and shared by FRC teams. The code */
  4. /* must be accompanied by the FIRST BSD license file in the root directory of */
  5. /* the project. */
  6. /*----------------------------------------------------------------------------*/
  7.  
  8. // import necessary packages/modules
  9. package frc.robot.subsystems;
  10.  
  11. import edu.wpi.first.wpilibj.GenericHID;
  12. import edu.wpi.first.wpilibj.command.Subsystem;
  13. import edu.wpi.first.wpilibj.drive.DifferentialDrive;
  14. import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
  15. import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
  16.  
  17. import com.kauailabs.navx.frc.AHRS;
  18. import frc.robot.Robot;
  19. import frc.robot.RobotMap;
  20. import frc.robot.commands.Drive;
  21.  
  22. public class DriveSubsystem extends Subsystem {
  23.  
  24. // instantiate navX
  25. public static AHRS navX;
  26.  
  27. // instantiate variables
  28. public static WPI_TalonSRX talon_fl;
  29. public static WPI_TalonSRX talon_fr;
  30. public static WPI_VictorSPX victor_bl;
  31. public static WPI_VictorSPX victor_br;
  32.  
  33. public static DifferentialDrive robotDrive;
  34. public boolean driveInverted;
  35. private double rumblePower;
  36.  
  37. // instantiate kP, kF, kD, and kI constants
  38. double kP;
  39. double kF;
  40. double kD;
  41. double kI;
  42.  
  43. // class constructor
  44. public DriveSubsystem() {
  45. super("Drive Subsystem");
  46.  
  47. // yeet yeet navX
  48. navX = new AHRS();
  49. // instantiate motor controllers
  50. talon_fl = new WPI_TalonSRX(RobotMap.talon_fl);
  51. talon_fr = new WPI_TalonSRX(RobotMap.talon_fr);
  52. victor_bl = new WPI_VictorSPX(RobotMap.victor_bl);
  53. victor_br = new WPI_VictorSPX(RobotMap.victor_br);
  54. robotDrive = new DifferentialDrive(talon_fl, talon_fr);
  55.  
  56. // default drivetrain inversion
  57. driveInverted = true;
  58.  
  59. // configure motor controllers
  60. victor_bl.follow(talon_fl);
  61. victor_br.follow(talon_fr);
  62. talon_fl.setInverted(driveInverted);
  63. talon_fr.setInverted(driveInverted);
  64. victor_bl.setInverted(driveInverted);
  65. victor_br.setInverted(driveInverted);
  66.  
  67. talon_fl.configPeakCurrentLimit(80);
  68. talon_fr.configPeakCurrentLimit(80);
  69. talon_fl.configPeakCurrentDuration(50);
  70. talon_fr.configPeakCurrentDuration(50);
  71. talon_fl.enableCurrentLimit(true);
  72. talon_fr.enableCurrentLimit(true);
  73.  
  74. talon_fl.setSafetyEnabled(false);
  75. talon_fr.setSafetyEnabled(false);
  76. victor_bl.setSafetyEnabled(false);
  77. victor_br.setSafetyEnabled(false);
  78.  
  79. // set values of PID constants
  80. kP = 0.000001;
  81. kF = 0.0;
  82. kD = 0.0;
  83. kI = 0.0;
  84. }
  85.  
  86. // curve drive method
  87. public void curveDrive(double speed, double rotation, boolean isQuickTurn) {
  88. robotDrive.curvatureDrive(speed, rotation, isQuickTurn);
  89. }
  90.  
  91. // turn
  92. public void turnPID(double desiredAngle) {
  93. double errorAngle = desiredAngle - navX.getAngle();
  94. double PValue = 0;
  95. double DValue = 0;
  96. double IValue = 0;
  97.  
  98. while(errorAngle != 0) {
  99. PValue = errorAngle * kP;
  100. DValue = (errorAngle - navX.getAngle()) * kD;
  101. IValue += errorAngle;
  102. double rotationValue = (PValue + kF + DValue + IValue);
  103. robotDrive.curvatureDrive(0, rotationValue, true);
  104. errorAngle = desiredAngle - navX.getAngle();
  105. }
  106. }
  107.  
  108. // invert drive direction
  109. public void invertDirection() {
  110. driveInverted = !driveInverted;
  111.  
  112. talon_fl.setInverted(driveInverted);
  113. talon_fr.setInverted(driveInverted);
  114. victor_bl.setInverted(driveInverted);
  115. victor_br.setInverted(driveInverted);
  116. }
  117.  
  118. @Override
  119. public void initDefaultCommand() {
  120. //setDefaultCommand(new Drive());
  121. }
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. /*----------------------------------------------------------------------------*/
  142. /* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
  143. /* Open Source Software - may be modified and shared by FRC teams. The code */
  144. /* must be accompanied by the FIRST BSD license file in the root directory of */
  145. /* the project. */
  146. /*----------------------------------------------------------------------------*/
  147.  
  148. package frc.robot.commands;
  149.  
  150. import edu.wpi.first.wpilibj.command.Command;
  151.  
  152. import frc.robot.Robot;
  153. import frc.robot.subsystems.DriveSubsystem;
  154. import frc.robot.OI;
  155.  
  156. public class Drive extends Command {
  157.  
  158. public double rumblePower;
  159.  
  160. public Drive() {
  161. requires(Robot.s_drive);
  162.  
  163. rumblePower = 0;
  164. }
  165.  
  166. @Override
  167. protected void initialize() {
  168. }
  169.  
  170. @Override
  171. protected void execute() {
  172. // invert drive direction
  173. if (Robot.oi.driveController.getYButtonPressed()) {
  174. Robot.s_drive.invertDirection();
  175. }
  176.  
  177. if (Robot.s_drive.driveInverted) {
  178. if (!Robot.oi.driveController.getXButton()) { // when not quick turning
  179. Robot.s_drive.curveDrive(Robot.oi.getTriggerMagnitude(Robot.oi.driveController),
  180. Robot.oi.getXMagnitude(Robot.oi.driveController) * 50,
  181. false);
  182. } else { // when quick turning
  183. Robot.s_drive.curveDrive(Robot.oi.getTriggerMagnitude(Robot.oi.driveController),
  184. Robot.oi.getXMagnitude(Robot.oi.driveController) * 0.8,
  185. true);
  186. }
  187. } else {
  188. if (!Robot.oi.driveController.getXButton()) { // when not quick turning
  189. Robot.s_drive.curveDrive(Robot.oi.getTriggerMagnitude(Robot.oi.driveController),
  190. Robot.oi.getXMagnitude(Robot.oi.driveController) * -50,
  191. false);
  192. } else { // when quick turning
  193. Robot.s_drive.curveDrive(Robot.oi.getTriggerMagnitude(Robot.oi.driveController),
  194. Robot.oi.getXMagnitude(Robot.oi.driveController) * -0.8,
  195. true);
  196. }
  197. }
  198.  
  199. /*
  200. if(Robot.oi.driveController.getPOV() == 0) {
  201. Robot.s_drive.turnPID(0);
  202. } else if(Robot.oi.driveController.getPOV() == 90) {
  203. Robot.s_drive.turnPID(90);
  204. } else if(Robot.oi.driveController.getPOV() == 180) {
  205. Robot.s_drive.turnPID(180);
  206. } else if(Robot.oi.driveController.getPOV() == 270) {
  207. Robot.s_drive.turnPID(270);
  208. } */
  209. }
  210.  
  211. @Override
  212. protected boolean isFinished() {
  213. return false;
  214. }
  215.  
  216. @Override
  217. protected void end() {
  218. }
  219.  
  220. @Override
  221. protected void interrupted() {
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment