Guest User

Untitled

a guest
Feb 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. package frc.subsystems;
  2.  
  3. import com.ctre.phoenix.motorcontrol.ControlMode;
  4. import com.ctre.phoenix.motorcontrol.FeedbackDevice;
  5. import com.ctre.phoenix.motorcontrol.InvertType;
  6. import com.ctre.phoenix.motorcontrol.RemoteSensorSource;
  7. import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
  8.  
  9. import edu.wpi.first.wpilibj.command.Subsystem;
  10. import frc.commands.RunArm;
  11. import frc.robot.RobotMap;
  12.  
  13. /**
  14. * This class manages the arm
  15. */
  16. public class Arm extends Subsystem {
  17.  
  18. // Create motors
  19. public WPI_TalonSRX armMaster, armFollower;
  20.  
  21. public Arm() {
  22. // Initialize motors
  23. armMaster = new WPI_TalonSRX(RobotMap.ARM_MASTER_ID);
  24. armFollower = new WPI_TalonSRX(RobotMap.ARM_FOLLOWER_ID);
  25.  
  26. // Configure the encoder
  27. armMaster.configFactoryDefault();
  28. armMaster.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Absolute, RobotMap.PID_INDEX,
  29. RobotMap.TIMEOUT);
  30. armMaster.setInverted(RobotMap.ARM_INVERT);
  31. armMaster.setSensorPhase(RobotMap.ARM_PHASE);
  32. armMaster.configAllowableClosedloopError(RobotMap.PID_INDEX, RobotMap.ARM_TOLERANCE, RobotMap.TIMEOUT);
  33. armMaster.config_kF(RobotMap.PID_INDEX, 0.0, RobotMap.TIMEOUT);
  34. armMaster.config_kP(RobotMap.PID_INDEX, 0.0, RobotMap.TIMEOUT);
  35. armMaster.config_kI(RobotMap.PID_INDEX, 0.0, RobotMap.TIMEOUT);
  36. armMaster.config_kD(RobotMap.PID_INDEX, 0.0, RobotMap.TIMEOUT);
  37.  
  38. // Sets the second motor to follow the master
  39. armFollower.follow(armMaster);
  40. armFollower.setInverted(InvertType.FollowMaster);
  41.  
  42. int absolutePosition = armMaster.getSensorCollection().getPulseWidthPosition();
  43.  
  44. /* Mask out overflows, keep bottom 12 bits */
  45. absolutePosition &= 0xFFF;
  46. if (RobotMap.ARM_PHASE) {
  47. absolutePosition *= -1;
  48. }
  49. if (RobotMap.ARM_INVERT) {
  50. absolutePosition *= -1;
  51. }
  52.  
  53. System.out.println("Absolute: " + absolutePosition);
  54.  
  55. /* Set the quadrature (relative) sensor to match absolute */
  56. armMaster.setSelectedSensorPosition(absolutePosition, 0, 30);
  57. }
  58.  
  59. /**
  60. * Set the position of the arm
  61. *
  62. * @param counts count to go to
  63. */
  64. public void setArmPosition(int counts) {
  65. armMaster.set(ControlMode.Position, counts);
  66. }
  67.  
  68. /**
  69. * Run the arm using the joystick
  70. *
  71. * @param speed joystick input
  72. */
  73. public void run(double speed) {
  74. armMaster.set(ControlMode.PercentOutput, speed);
  75. }
  76.  
  77. /**
  78. * Brake all motors on the arm
  79. *
  80. */
  81. public void stop() {
  82. armMaster.stopMotor();
  83. armFollower.stopMotor();
  84. }
  85.  
  86. /**
  87. * Returns the arm encoder count
  88. *
  89. * @return the arm encoder counts
  90. */
  91. public int getArmPosition() {
  92. return armMaster.getSelectedSensorPosition();
  93. }
  94.  
  95. /**
  96. * Returns whether the encoder is on target
  97. *
  98. * @param counts
  99. * @return onTarget
  100. */
  101. public boolean isOnTarget(int counts) {
  102. return Math.abs(armMaster.getSelectedSensorPosition() - counts) < RobotMap.ARM_TOLERANCE;
  103. }
  104.  
  105. @Override
  106. protected void initDefaultCommand() {
  107. setDefaultCommand(new RunArm());
  108. }
  109.  
  110. }
Add Comment
Please, Sign In to add comment