Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <MEncoder.h>
  2. #include <Servo.h>
  3.  
  4. #define joyXPin A0
  5. #define joyYPin A1
  6. #define joy2XPin A2
  7. #define joy2YPin A3
  8.  
  9. #define gearRatio 4.2 //Motor to large gear
  10. #define cyclesPerRev 252.882 //Encoder cycles
  11.  
  12. #define AHipYawEncoder 20
  13. #define BHipYawEncoder 21
  14.  
  15. #define AHipPitchEncoder 22
  16. #define BHipPitchEncoder 23
  17.  
  18. #define AHipRollEncoder 24
  19. #define BHipRollEncoder 25
  20.  
  21. #define hipYawMotorPin 9
  22. #define hipPitchMotorPin 8
  23. #define hipRollMotorPin 10
  24.  
  25. #define pitchSwitchPin 24
  26.  
  27. int joyValueX = 0;
  28. int joyValueY = 0;
  29. int joy2ValueX = 0;
  30. int jou2ValueY = 0;
  31.  
  32. int hipPitchMotorSpeed = 0;
  33. int hipYawMotorSpeed = 0;
  34. int hipRollMotorSpeed = 0;
  35.  
  36. bool setupComplete = false;
  37.  
  38. Servo hipMotorYaw;
  39. Servo hipMotorPitch;
  40. Servo hipMotorRoll;
  41.  
  42. MEncoder hipYawEncoder(AHipYawEncoder, BHipYawEncoder, gearRatio, cyclesPerRev);
  43. MEncoder hipPitchEncoder(AHipPitchEncoder, BHipPitchEncoder, gearRatio, cyclesPerRev);
  44. MEncoder hipRollEncoder(AHipRollEncoder, BHipRollEncoder, gearRatio, cyclesPerRev);
  45. /**
  46. Pin assignment
  47. */
  48. void setup() {
  49. Serial.begin(9600);
  50.  
  51. pinMode(hipYawMotorPin, OUTPUT);
  52. pinMode(hipPitchMotorPin, OUTPUT);
  53. pinMode(hipRollMotorPin, OUTPUT);
  54.  
  55.  
  56. hipMotorYaw.attach(hipYawMotorPin);
  57. hipMotorPitch.attach(hipPitchMotorPin);
  58. hipMotorRoll.attach(hipRollMotorPin);
  59.  
  60. attachInterrupt(AHipYawEncoder, updateYawEncoder, CHANGE);
  61. attachInterrupt(AHipPitchEncoder, updatePitchEncoder, CHANGE);
  62. attachInterrupt(AHipRollEncoder, updateRollEncoder, CHANGE);
  63.  
  64. Serial.println("START READING");
  65. while (!setupComplete) {
  66. Serial.println("SETUP STARTING");
  67. startup(); //Start home positioning
  68. }
  69. }
  70.  
  71. /**
  72. Sets the home position by hitting limit switch twice
  73. */
  74. void startup() {
  75.  
  76. if (digitalRead(pitchSwitchPin) == 0) { //Motor engaged limit switch: At home position (Max rotation)
  77.  
  78. Serial.println("RECALIBRATE");
  79. hipMotorPitch.write(30); //Go backwards
  80. delay(1000);
  81.  
  82. while (!(digitalRead(pitchSwitchPin) == 0)) //While switch isn't engaged, go forward (Recalibration stage)
  83. {
  84. hipMotorPitch.write(120);
  85. }
  86. hipMotorPitch.write(90); //Stop motor
  87. Serial.println("DONE");
  88.  
  89. setupComplete = true;
  90. }
  91. else //Motor has not engaged limit switch
  92. {
  93. for (int posPitch = 90; posPitch < 180; posPitch += 1)
  94. {
  95. if (digitalRead(pitchSwitchPin) == 0) {
  96. setupComplete = true;
  97. delay(1000);
  98. break;
  99. }
  100. hipMotorPitch.write(posPitch);
  101. }
  102. }
  103. }
  104. /**
  105. Handles manual control of all motors
  106. */
  107. void loop()
  108. {
  109.  
  110. joyValueX = (map(analogRead(joyXPin), 0, 1023, 0, 180) - 10);
  111. joyValueY = (map(analogRead(joyYPin), 0, 1023, 0, 180) - 10);
  112.  
  113. if (digitalRead(pitchSwitchPin) == 0)
  114. {
  115. Serial.println("Contact");
  116. if (joyValueY > 90) {
  117. hipMotorPitch.write(90);
  118. }
  119. else
  120. {
  121. hipMotorPitch.write(joyValueY);
  122. }
  123. hipMotorYaw.write(joyValueX);
  124.  
  125. }
  126. else
  127. {
  128. hipMotorYaw.write(joyValueX);
  129. hipMotorPitch.write(joyValueY);
  130. }
  131. }
  132. /**
  133. Updates count (position) value for encoder object
  134. */
  135. void encoderUpdater(MEncoder encoder)
  136. {
  137. encoder.update();
  138. }
  139.  
  140. void updateYawEncoder() {
  141. encoderUpdater(hipYawEncoder);
  142. }
  143. void updatePitchEncoder() {
  144. encoderUpdater(hipPitchEncoder);
  145. }
  146. void updateRollEncoder() {
  147. encoderUpdater(hipRollEncoder);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement