R9000

Arduino Brushless Linear Actuator Code for Limit Switches

Jan 23rd, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <Servo.h>  //Servo library to control your ESC.
  2. Servo esc;  //Declare the servo name as 'esc'.
  3.  
  4. int ch1; //PPM input from the Rx (RC receiver) goes here.
  5. int throttle = 0; //Serial input is mapped onto this value, should be within the range 0 to 179.
  6. int count = 0;  //Variable to delay activation until ESC has bound.
  7. int lowerSwitchState = 0; //Variable to track the status of the lower limit switch.
  8. int upperSwitchState = 0; //Variable to track the status of the upper limit switch.
  9.  
  10. void setup() {
  11.  
  12.   pinMode(5, INPUT); //Input from Rx
  13.   pinMode(2, INPUT);  //Lower switch (grey wire)
  14.   pinMode(3, INPUT);  //Upper switch (green wire)
  15.  
  16.   Serial.begin(9600); //Start the serial output (for debugging purposes).
  17.  
  18.   esc.attach(9);  //Bind the ESC to the arduino, just as it would to a receiver.
  19.  
  20. }
  21.  
  22. void loop() {
  23.   lowerSwitchState = digitalRead(2); //Read pin 2 (the lower limit switch).
  24.   upperSwitchState = digitalRead(3); //Read pin 3 (the upper limit switch).
  25.   ch1 = pulseIn(5, HIGH, 25000);  //Read the PPM input from the Rx data line.
  26.   if(ch1 > 1056){ /*Reject any PPM values that are outside the accepted range.
  27.     I found the motor tends to jitter horribly if I don't have this line, as for some reason,
  28.     0 is sent through every other time it reads.*/
  29.   throttle = map(ch1, 1057, 1880, 80, 100); /*Map the values between 1057 and 1880
  30.   (observed upper and lower limits of the throttle signal from the Rx) to the
  31.   upper and lower limits of the ESC signal. These can be in the range 0-179, where
  32.   90 is the middle position (0 movement if your ESC is reversible).
  33.   Here it's only mapped from 80-100 in order to slow the motor down.
  34.   You can adjust this to suit your motor or application.*/
  35.   //NOTE: If your ESC only supports one-way movement, you'll need to
  36.   //change any '90's in this program to '0' (for no movement) and adjust other values accordingly.
  37.   }
  38.  
  39.   if(count < 500){
  40.     esc.write(90);  //Essentially wait for 500 loops to ensure the ESC has bound.
  41.     count ++;
  42.   }else if(lowerSwitchState == LOW && throttle < 90){
  43.     esc.write(90);  //If the signal is trying to push the actuator
  44.     //past its lower limit, set the output to 90 for no movement.
  45.   }else if(upperSwitchState == LOW && throttle > 90){
  46.     esc.write(90);  //Same as above, but with upper limit.
  47.   }else{
  48.   esc.write(throttle);  //If all is well, relay the input Rx signal
  49.   //to the ESC.
  50.   }
  51.  
  52.   //NOTE: If your motor still tries to push past the limits, remember
  53.   //to check that your motor (and actuator) direction is congruent with the
  54.   //numbers here (0 for full reverse, 179 for full forwards).
  55.   //If it isn't, you can switch two of your three motor leads around, or
  56.   //swap the '<' and '>' in the above two 'if' statements.
  57. }
Add Comment
Please, Sign In to add comment