Advertisement
Guest User

GoPro Arm Control

a guest
Jan 28th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. NOTES TO MATT
  3. */
  4.  
  5. const int controlPin1 = 2; //Wrist
  6. const int controlPin2 = 3;
  7. const int control2Pin1 = 12; //Elbow
  8. const int control2Pin2 = 13;
  9. const int control3Pin1 = 8; //Spin
  10. const int control3Pin2 = 7;
  11. const int enablePin = 9; // Wrist motor
  12. const int enablePin2 = 11; // Elbow motor
  13. const int enablePin3 = 10; // Spin motor
  14. const int onOffPin = 5;
  15.  
  16. int onOffState = LOW;
  17. int previousOnOffState = LOW;
  18. int previousDirectionSwitchState = 0;
  19. int spinMotorEnabled = 0;
  20. int elbowMotorEnabled = 0;
  21. int wristMotorEnabled = 0;
  22. int spinMotorSpeed = 55; // these speeds are for the 12v lead battery, they are defaults, real speeds are in the main loop
  23. int elbowMotorSpeed = 50;
  24. int wristMotorSpeed = 42; // use about 5/7 elbow because that's the ration of the length of the wrist to the arm
  25. int spinMotorDirection = 0; //Left is 0
  26. int elbowMotorDirection = 1;
  27. int wristMotorDirection = 0; //Wrist's motor needs to move opposite to elbowMotor
  28. int count = 0;
  29. int spinCount = 0;
  30. int upDist = 4500;
  31. int downDist = 2700;
  32. int spinDist = 3200;
  33. int spinFlag = 0;
  34. int mode = 0; //Mode can be 0, 1, or 2 representing the off state, motion and reverse motion
  35.  
  36. void setup(){
  37.   //Serial.begin(9600);
  38.   pinMode(onOffPin, INPUT);
  39.   pinMode(controlPin1, OUTPUT);
  40.   pinMode(controlPin2, OUTPUT);
  41.   pinMode(control2Pin1, OUTPUT);
  42.   pinMode(control2Pin2, OUTPUT);
  43.   pinMode(control3Pin1, OUTPUT);
  44.   pinMode(control3Pin2, OUTPUT);
  45.   pinMode(enablePin, OUTPUT);
  46.   digitalWrite(enablePin, LOW);
  47.   pinMode(enablePin2, OUTPUT);
  48.   digitalWrite(enablePin2, LOW);
  49.   pinMode(enablePin3, OUTPUT);
  50.   digitalWrite(enablePin3, LOW);
  51. }
  52.  
  53. boolean debounce(boolean last)
  54. {
  55.   boolean current = digitalRead(onOffPin);
  56.   if (last != current)
  57.   {
  58.     delay(5); //wait 5ms
  59.     current = digitalRead(onOffPin);
  60.   }
  61.   return current;
  62. }
  63.  
  64. void loop(){
  65.   onOffState = debounce(previousOnOffState);
  66.   delay(1);
  67.  
  68.   if(previousOnOffState == LOW && onOffState == HIGH){
  69.     elbowMotorEnabled = !elbowMotorEnabled;
  70.     spinMotorEnabled = !spinMotorEnabled;
  71.   }
  72.  
  73.   //Check if it's time to start raising the arm
  74.   if (spinCount >= 2900){
  75.     spinFlag = 1;
  76.   }
  77.  
  78.   // checking if the motor movement is complete
  79.   if (elbowMotorEnabled == HIGH && spinFlag == 1) {
  80.     if(count < upDist && elbowMotorDirection == 1){
  81.       //Serial.println(spinFlag);
  82.       //Serial.println(spinCount);
  83.       count = count + 1;
  84.     }
  85.     else if (count < downDist && elbowMotorDirection == 0) {
  86.       count = count + 1;
  87.     }
  88.     else{
  89.       count = 0;
  90.       spinFlag = 0;
  91.       elbowMotorEnabled = 0;
  92.       elbowMotorDirection = !elbowMotorDirection;
  93.       wristMotorDirection = !wristMotorDirection;
  94.     }
  95.   }
  96.  
  97.   //checking spin motor movement against distance
  98.   if (spinMotorEnabled == HIGH){
  99.     if (spinCount < spinDist){
  100.       spinCount = spinCount + 1;
  101.     }
  102.     else{
  103.       spinCount = 0;
  104.       spinMotorEnabled = 0;
  105.       spinMotorDirection = !spinMotorDirection;
  106.     }
  107.   }
  108.  
  109.   // Telling the H-bridge the direction of the elbow motor
  110.   if (elbowMotorDirection == 1){
  111.     digitalWrite(control2Pin1, HIGH); // elbow
  112.     digitalWrite(control2Pin2, LOW);
  113.     //elbowMotorSpeed = 200; // for 6v batt
  114.     elbowMotorSpeed = 70; // for 12v batt
  115.   }
  116.   else{
  117.     digitalWrite(control2Pin1, LOW); // elbow
  118.     digitalWrite(control2Pin2, HIGH);
  119.     //elbowMotorSpeed = 200; // for 6v batt
  120.     elbowMotorSpeed = 50; // for 12v batt
  121.   }
  122.  
  123.   // Telling the H-bridge the direction of the wrist motor
  124.   if (wristMotorDirection == 1){
  125.     digitalWrite(controlPin1, LOW); // wrist
  126.     digitalWrite(controlPin2, HIGH);
  127.     //wristMotorSpeed = 200; // for 6v batt
  128.     wristMotorSpeed = 53; // for 12v batt
  129.   }
  130.   else{
  131.     digitalWrite(controlPin1, HIGH); // wrist
  132.     digitalWrite(controlPin2, LOW);
  133.     //wristMotorSpeed = 130; // for 6v batt
  134.     wristMotorSpeed = 40; // for 12v batt, minimum value is ~35
  135.   }
  136.  
  137.   // Telling the H-bridge the direction of the spin motor
  138.   if (spinMotorDirection == 1){
  139.     digitalWrite(control3Pin1, HIGH); // elbow
  140.     digitalWrite(control3Pin2, LOW);
  141.   }
  142.   else{
  143.     digitalWrite(control3Pin1, LOW); // elbow
  144.     digitalWrite(control3Pin2, HIGH);
  145.   }
  146.  
  147.   //Controlling vertical speed
  148.   if(elbowMotorEnabled == 1 && spinFlag == 1){
  149.     if (elbowMotorDirection == 1 && count % 2 == 0){
  150.       analogWrite(enablePin, wristMotorSpeed);
  151.       //Serial.println(count);
  152.     }
  153.     else if (elbowMotorDirection == 1 && count % 2 != 0){
  154.       analogWrite(enablePin, 0);
  155.     }
  156.     else if (elbowMotorDirection == 0){
  157.       analogWrite(enablePin, wristMotorSpeed);
  158.     }
  159.  
  160.     analogWrite(enablePin2, elbowMotorSpeed);
  161.   }
  162.   else{
  163.     analogWrite(enablePin, 0);
  164.     analogWrite(enablePin2, 0);
  165.   }
  166.  
  167.   // Controlling the spin motor speed
  168.   if(spinMotorEnabled == 1){
  169.     analogWrite(enablePin3, spinMotorSpeed);
  170.   }
  171.   else{
  172.     analogWrite(enablePin3, 0);
  173.   }
  174.   //Serial.println(elbowMotorDirection);
  175.   previousOnOffState = onOffState;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement