saimun1

Joystick with servos

May 7th, 2020
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.79 KB | None | 0 0
  1. #include <Servo.h>
  2. Servo myservo;  // create servo object to control a servo
  3.  
  4. #define analogInputX A0
  5. #define analogInputY A1
  6. #define pulseinInputS1 A2
  7. #define pulseinInputS2 A3
  8. #define servo1Pin A4
  9. #define servo2Pin A5
  10.  
  11. //Parameters
  12. #define motor_speedF 255 // Motor speed for forward
  13. #define MOTOR_SPEED_BASE 200 // Base motor speed
  14. #define motor1Speed 255
  15. #define motor2Speed 255
  16. #define xError 120
  17. #define yError 40
  18. #define xCenterValue 512
  19. #define yCenterValue 512
  20. #define PULSE_THRESHOLD 0.2 // pulse width minimum value in ms
  21. /* Motor states by joystick position
  22. FORWARD - M1&M2 forward
  23. BACKWARD -  M1&M2 backward
  24. FORWARD_L -  M1 backward M2 forward // duplicate with BACKWARD_R
  25. FORWARD_R - M1 forward M2 backward // duplicate with BACKWARD_L
  26. BACKWARD_L - M1 forward M2 backward
  27. BACKWARD_R - M1 backward M2 forward
  28. */
  29. #define NEUTRAL 0
  30. #define FORWARD 1
  31. #define BACKWARD 2
  32. #define FORWARD_L 3
  33. #define FORWARD_R 4
  34. #define BACKWARD_L 5
  35. #define BACKWARD_R 6
  36. #define PULSE_READ_STATE 7 //testing
  37.  
  38. float xDiff =0;
  39. float yDiff =0;
  40. float x,y=0;
  41. byte state = 0;
  42. float motor_speed;
  43. float motor_diff;
  44. float motor_speed_diff;
  45. float left_motor_speed, right_motor_speed = 0;
  46.  
  47. unsigned long readingPulse1 = 0;
  48. unsigned long readingPulse2 = 0;
  49. int pulseinCheck = 0; // reading pulse availability
  50. void setup() {
  51.   Serial.begin(9600);
  52.   Serial.println("START ---------------------------");
  53.   myservo.attach(servo1Pin);
  54.   delay(30);
  55.   myservo.write(0);
  56.   delay(30);
  57.   myservo.detach();
  58.   delay(30);
  59.   myservo.attach(servo2Pin);
  60.   delay(30);
  61.   myservo.write(0);
  62.   delay(30);
  63.   myservo.detach();
  64.   delay(30);
  65. }
  66. int pulseinRead()
  67. {
  68. readingPulse1 = pulseIn(pulseinInputS1, HIGH);
  69. //readingPulse2 = pulseIn(pulseinInputS2, HIGH);
  70. readingPulse2 = 0;
  71. // pulseIn() returns value in microseconds we work with miliseconds
  72. Serial.print("reading pulse 1 i 2  ");
  73. Serial.print(readingPulse1);
  74. Serial.print(" | ");
  75. Serial.print(readingPulse2);
  76. Serial.println("---");
  77.    
  78.  
  79. if((readingPulse1>PULSE_THRESHOLD) || (readingPulse2 >PULSE_THRESHOLD))
  80. {
  81.     Serial.print("Vliza pri ifa za pulsove ");
  82.     Serial.print(readingPulse1);
  83.     Serial.print(" ");
  84.     Serial.print(readingPulse2);
  85.     Serial.println();
  86.     Serial.println("Vliza reading pulse2 90gr");
  87.     myservo.attach(servo1Pin);
  88.     delay(20);
  89.     myservo.writeMicroseconds(readingPulse1);
  90.     delay(200);
  91.     myservo.detach();
  92.     delay(30);
  93.     /*
  94.     if((readingPulse1 >=500) && (readingPulse1<=560))
  95.     {
  96.       myservo.attach(servo1Pin);
  97.       myservo.write(0);
  98.       myservo.detach();
  99.     }
  100.     if((readingPulse2 >=500) && (readingPulse2<=560))
  101.     {
  102.       myservo.attach(servo2Pin);
  103.       myservo.write(0);
  104.         delay(50);
  105.       myservo.detach();
  106.       delay(30);
  107.     }
  108.     if((readingPulse1 >=1400) && (readingPulse1<=1500))
  109.     {
  110.       Serial.println("Vliza reading pulse1 90gr");
  111.       myservo.attach(servo1Pin);
  112.       delay(120);
  113.       myservo.write(90);
  114.       delay(150);
  115.       myservo.detach();
  116.       delay(100);
  117.     }
  118.     if((readingPulse2 >=1400) && (readingPulse2<=1500))
  119.     {
  120.      
  121.       Serial.println("Vliza reading pulse2 90gr");
  122.       myservo.attach(servo2Pin);
  123.       delay(20);
  124.       myservo.write(90);
  125.       delay(100);
  126.       myservo.detach();
  127.       delay(30);
  128.     }
  129.     if((readingPulse1 >=2400) && (readingPulse1<=2500))
  130.     {
  131.       myservo.attach(servo1Pin);
  132.       myservo.write(180);
  133.       delay(50);
  134.       myservo.detach();
  135.       delay(30);
  136.     }
  137.     if((readingPulse2 >=2400) && (readingPulse2<=2500))
  138.     {
  139.       myservo.attach(servo2Pin);
  140.       myservo.write(180);
  141.       delay(50);
  142.       myservo.detach();
  143.       delay(30);
  144.     }*/
  145.       state = PULSE_READ_STATE;    
  146.    return 1;
  147.   }
  148.   else
  149.   {
  150.     state = NEUTRAL;
  151.     return 0;
  152.   }
  153. }
  154.  
  155. void servoWrite(int pin,int degr)
  156. {
  157.   myservo.attach(pin);
  158.   delay(120);
  159.   myservo.write(degr);
  160.   delay(150);
  161.   myservo.detach();
  162.   delay(100);
  163. }
  164. void testPrint()
  165. {
  166.  
  167.   reading();
  168.   Serial.println();
  169.   Serial.print("The value of X is: ");
  170.   Serial.print(x);
  171.   Serial.println();
  172.   Serial.print("The value of Y is: ");
  173.   Serial.print(y);
  174.   Serial.println("------");
  175.   Serial.println();
  176.   delay(5);
  177. }
  178. void reading()
  179. {
  180.    x = analogRead(analogInputX);
  181.    y = analogRead(analogInputY);
  182.    y= map(y,0,1023,1023,0); // maping the Y value because Y is reversed
  183. }
  184. void stateFinding()
  185. {
  186.    
  187.    if((x>=xCenterValue-xError && x<=xCenterValue+xError)&& (y>=yCenterValue-yError && y<=yCenterValue+yError))
  188.    {
  189.       state = NEUTRAL;
  190.    }
  191.     else if(y>=yCenterValue+yError && x>=xCenterValue-xError && x<=xCenterValue+xError)
  192.    {
  193.     state = FORWARD;
  194.    }
  195.    else if(y<=yCenterValue-yError && x>=xCenterValue-xError && x<=xCenterValue+xError)
  196.    {
  197.     state = BACKWARD;
  198.    }
  199.     else if(y>=yCenterValue+yError && x<=xCenterValue-xError)
  200.    {
  201.     Serial.println("Vliza tuk pri FL");
  202.     state = FORWARD_L;
  203.    }
  204.    else if(y>=yCenterValue+yError && x>=xCenterValue+xError)
  205.    {
  206.     state = FORWARD_R;
  207.    }
  208.    else if(y<=yCenterValue-yError && x<=xCenterValue-xError)
  209.    {
  210.     state = BACKWARD_L;
  211.    }
  212.     else if(y<=yCenterValue-yError && x>=xCenterValue+xError)
  213.    {
  214.     state = BACKWARD_R;
  215.    }
  216.    else
  217.    {
  218.     state = NEUTRAL;
  219.    }
  220.    
  221. }
  222. void motor1(float LEFT_MOTOR_SPEED)
  223. {
  224.   if(LEFT_MOTOR_SPEED>= 255) LEFT_MOTOR_SPEED= 255;
  225.   //TODO
  226.   delay(20);
  227. }
  228. void motor2(float RIGHT_MOTOR_SPEED)
  229. {
  230.  
  231.   if(RIGHT_MOTOR_SPEED>= 255) RIGHT_MOTOR_SPEED = 255;
  232.   //TODO
  233.   delay(20);
  234. }
  235.  
  236.  
  237. void loop()
  238. {
  239.  
  240.    if(pulseinCheck==0) // If there aren't impulses avaiable
  241.    {
  242.    reading();
  243.    stateFinding();
  244.    testPrint();
  245.    yDiff = abs(yCenterValue -y);
  246.    xDiff = abs(xCenterValue -x);
  247.    }
  248.    pulseinCheck = 0;
  249.    switch(state)
  250.    {
  251.    case FORWARD:
  252.     servoWrite(servo1Pin, 0);
  253.     servoWrite(servo2Pin, 0);
  254.        
  255.     motor_speed = (yDiff/255);
  256.     left_motor_speed = motor_speedF; // for debug
  257.     right_motor_speed = motor_speedF;
  258.     motor1(motor_speedF);
  259.     motor2(motor_speedF);
  260.     break;
  261.  
  262.    case FORWARD_L: // M1 backward M2 forward
  263.     servoWrite(servo1Pin, 30);
  264.     servoWrite(servo2Pin, 90);
  265.     yDiff = map(yDiff, 0,512,0,255);
  266.     xDiff = map(xDiff, 0,512,0,255);
  267.    
  268.     motor_speed_diff =  (yDiff/255);    
  269.     right_motor_speed = MOTOR_SPEED_BASE*motor_speed_diff;
  270.     left_motor_speed = MOTOR_SPEED_BASE*motor_speed_diff;
  271.     left_motor_speed = -left_motor_speed;
  272.    
  273.     motor1(left_motor_speed);
  274.     motor2(right_motor_speed);
  275.     break;
  276.  
  277.    
  278.     case FORWARD_R: // M1 forward M2 backward
  279.     servoWrite(servo1Pin, 90);
  280.     servoWrite(servo2Pin, 180);
  281.     yDiff= map(yDiff,0,512,0,255);
  282.     xDiff = map(xDiff, 0,512,0,255);
  283.    
  284.     motor_speed_diff =  (yDiff/255);    
  285.     right_motor_speed = MOTOR_SPEED_BASE*motor_speed_diff;
  286.     left_motor_speed = MOTOR_SPEED_BASE*motor_speed_diff;
  287.     right_motor_speed = -right_motor_speed;
  288.        
  289.     motor1(left_motor_speed);
  290.     motor2(right_motor_speed);
  291.     break;
  292.  
  293.    case BACKWARD_L: // M1 forward M2 backward
  294.     servoWrite(servo1Pin, 90);
  295.     servoWrite(servo2Pin, 180);
  296.     yDiff= map(yDiff,0,512,0,255);
  297.     xDiff = map(xDiff, 0,512,0,255);
  298.    
  299.     motor_speed_diff = (yDiff/255);    
  300.     right_motor_speed = MOTOR_SPEED_BASE*motor_speed_diff;
  301.     left_motor_speed = MOTOR_SPEED_BASE*motor_speed_diff;
  302.     right_motor_speed = -right_motor_speed;
  303.    
  304.     motor1(left_motor_speed);
  305.     motor2(right_motor_speed);
  306.     break;
  307.  
  308.    
  309.    case BACKWARD_R: // M1 backward M2 forward
  310.     servoWrite(servo1Pin, 30);
  311.     servoWrite(servo2Pin, 90);
  312. //    servoWrite(240);
  313.     yDiff = map(yDiff, 0,512,0,255);
  314.     xDiff = map(xDiff, 0,512,0,255);
  315.     motor_diff = xDiff/255;
  316.     motor_speed_diff =  (yDiff/255);
  317.     right_motor_speed = MOTOR_SPEED_BASE*motor_speed_diff;
  318.     left_motor_speed = MOTOR_SPEED_BASE*motor_speed_diff;
  319.     left_motor_speed = -left_motor_speed; // M1 backward
  320.    
  321.     motor1(left_motor_speed);
  322.     motor2(right_motor_speed);
  323.     break;
  324.  
  325.    case BACKWARD:
  326.     servoWrite(servo1Pin, 180);
  327.     servoWrite(servo2Pin, 180);
  328.     left_motor_speed = MOTOR_SPEED_BASE*(-1);
  329.     right_motor_speed = MOTOR_SPEED_BASE*(-1);
  330.     break;
  331.    
  332.    case NEUTRAL:
  333.     servoWrite(servo1Pin, 90);
  334.     servoWrite(servo2Pin, 90);
  335.    left_motor_speed = 0;
  336.    right_motor_speed = 0;
  337.    pulseinCheck = pulseinRead();
  338.    if(pulseinCheck == 0) // If there are no impulses avaible, motors to 0
  339.    {
  340.     motor1(0);
  341.     motor2(0);
  342.    }
  343.     break;
  344.      
  345.    }
  346.   Serial.println();
  347.   Serial.print("The state is:  ");
  348.   Serial.println(state);
  349.   /*
  350.   Serial.print("The left motor speed is: ");
  351.   Serial.print(left_motor_speed);
  352.   Serial.println();
  353.   Serial.print("The rightt motor speed is: ");
  354.   Serial.print(right_motor_speed);
  355.   Serial.println("------");
  356.   delay(500);
  357.   */
  358.   delay(10);
  359. }
Add Comment
Please, Sign In to add comment