Advertisement
JonD1988

RoboticArmControlsRemoteTestRxRev4

Dec 24th, 2021
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.21 KB | None | 0 0
  1. //Code to Test Robotic Arm Remote Control - Written by Jonathan DeWitt
  2. //Difference Between Rev 0 and Rev 1
  3. //Rev 0 used Reference 1 as Technique for Transmitting data via the HC-12
  4. //Rev 1 used Reference 2 as Technique for Transmitting data via the HC-12
  5. //Rev 4 This is my attempt to completely clean up the code to make sure that the right value is getting transmitted to the servos
  6. //Rev 4 partially works.  The robot responds to the remote control.  But the robot shakes and is jittery from the moment the transmitter on the remote is turned on.
  7. //I tried some solutions in Rev 5 (don't use) to try to fix it.  But I was unable to fix it.
  8.  
  9. #include <Servo.h>
  10. #include <SoftwareSerial.h>
  11.  
  12. SoftwareSerial HC12(8,7); // Arduino Pin 8 Rx to Tx on HC-12, Arduino Pin 7 Tx to Rx on HC-12
  13.  
  14. Servo Servo1;
  15. Servo Servo2;
  16. Servo Servo3;
  17. Servo Servo4;
  18. Servo Servo5;
  19. Servo Servo6;
  20.  
  21. int J1 = 5; //J1 Servo Signal Wire Attached to Pin 5
  22. int J2 = 6; //J2 Servo Signal Wire Attached to Pin 6
  23. int J3 = 9; //J3 Servo Signal Wire Attached to Pin 9
  24. int J4 = 10; //J4 Servo Signal Wire Attached to Pin 10
  25. int J5 = 11; //J5 Servo Signal Wire Attached to Pin 11
  26. int J6 = 3; //J6 Servo Signal Wire Attached to Pin 3
  27.  
  28. //Home Position for Robot - Determined Through Careful Trial & Error Testing
  29. int J1H = 90; //Joint 1 Home Position 90°
  30. int J2H = 110; //Joint 2 Home Position 110°
  31. int J3H = 120; //Joint 3 Home Position 120°
  32. int J4H = 50; //Joint 4 Home Position 50°
  33. int J5H = 50; //Joint 5 Home Position 50°
  34. int J6H = 120; //Joint 6 Home Position 120°
  35.  
  36. int angle1 = J1H; //Stores Angle to Move Servo 1 To
  37. int angle2 = J2H; //Stores Angle to Move Servo 2 To
  38. int angle3 = J3H; //Stores Angle to Move Servo 3 To
  39. int angle4 = J4H; //Stores Angle to Move Servo 4 To
  40. int angle5 = J5H; //Stores Angle to Move Servo 5 To
  41. int angle6 = J6H; //Stores Angle to Move Servo 6 To
  42.  
  43. int angle1L = J1H; //angle 1 Last
  44. int angle2L = J2H; //angle 2 Last
  45. int angle3L = J3H; //angle 3 Last
  46. int angle4L = J4H; //angle 4 Last
  47. int angle5L = J5H; //angle 5 Last
  48. int angle6L = J6H; //angle 6 Last
  49.  
  50. ////The following variables are needed only to receive data from the HC-12 remote control using Reference 1 Method
  51. //String input;
  52. int boundLow;
  53. int boundHigh;
  54. int boundF;
  55. const char delimiter = ',';
  56. const char finald = '.';
  57.  
  58. //Following Section Copied from Data Logger Sketch i.e. from Reference 2 Method
  59. char incomingByte;
  60. String readBuffer = "";
  61. int byteD = 5; //byte delay is the delay in which each incoming byte is stored from the HC-12
  62. int SMd = 250; //Serial Monitor Delay - The delay used to set the interval with which strings from the buffer are sent to the serial monitor - was originally 5000
  63. //Note: SMd is dependent on sampP in the transmitter sketch.  SMd = 483 for the default sampP = 10.  This will need to be tweaked depending on frequency of sampling (sampP).
  64. int stupD = 500; //Setup Delay
  65.  
  66. void setup() {
  67.   // put your setup code here, to run once:
  68.   Serial.begin(9600);
  69.  
  70.   Servo1.attach(J1); //Calls this is like a pinmode command but also does more from the servo library
  71.   Servo2.attach(J2); //Calls this is like a pinmode command but also does more from the servo library
  72.   Servo3.attach(J3); //Calls this is like a pinmode command but also does more from the servo library
  73.   Servo4.attach(J4); //Calls this is like a pinmode command but also does more from the servo library
  74.   Servo5.attach(J5); //Calls this is like a pinmode command but also does more from the servo library
  75.   Servo6.attach(J6); //Calls this is like a pinmode command but also does more from the servo library
  76.  
  77.   //Move the robot to a good home pose
  78.   servoWriter(J1H, J2H, J3H, J4H, J5H, J6H); //servoWriter function call
  79.  
  80.   HC12.begin(9600); //Start the HC-12 Module
  81.   delay(stupD); //I assume this delay is to give the HC-12 time to read
  82. }
  83.  
  84. void loop() {
  85.   // put your main code here, to run repeatedly:
  86.  
  87. //It takes a while for the HC-12 to send a new message with servo angles
  88. //So, at the start of the loop the angles will be written to the servo motors
  89. servoWriter(angle1L, angle2L, angle3L, angle4L, angle5L, angle6L); //Function Call to Move Robot to Angles from End of Last Loop
  90.  
  91. //This section reads in the message coming from the HC-12 to a readBuffer
  92. readBuffer = "";
  93.   boolean start = false;
  94.   //Reads incoming value
  95.   while (HC12.available()) {        // If HC-12 has data
  96.     incomingByte = HC12.read();      // Store each incoming byte from HC-12
  97.     delay(byteD);
  98.     //Reads the data between the start "s" and end marker "e"
  99.     if (start == true) {
  100.       if (incomingByte != 'e') {
  101.         readBuffer += char(incomingByte); //Adds each byte to ReadBuffer string variable
  102.       }
  103.       else {
  104.         start = false;
  105.       }
  106.     }
  107.     else if (incomingByte == 's'){
  108.       start = true; //If true start reading the message
  109.     }
  110.   }
  111.  
  112. String message = readBuffer; //Saves readBuffer contents to new String
  113. //Serial.println(message); //For debugging purposes only
  114.  
  115. //This sections initializes the angles to a home position while the HC-12 message is not available
  116. if(message == ""){
  117.   angle1 = angle1L; //Stores Angle to Move Servo 1 To
  118.   angle2 = angle2L; //Stores Angle to Move Servo 2 To
  119.   angle3 = angle3L; //Stores Angle to Move Servo 3 To
  120.   angle4 = angle4L; //Stores Angle to Move Servo 4 To
  121.   angle5 = angle5L; //Stores Angle to Move Servo 5 To
  122.   angle6 = angle6L; //Stores Angle to Move Servo 6 To
  123.  
  124.   servoWriter(angle1, angle2, angle3, angle4, angle5, angle6); //Function Call to Maintain Same Angle if No New Message
  125.    
  126. }
  127. else{
  128. boundLow = message.indexOf(delimiter); //Locates index of first delimiter i.e. comma https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/indexof/
  129. angle1 = message.substring(0, boundLow).toInt(); //.substring creates a sub string of input string starting at index position zero up to but not including first comma https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/
  130.        
  131. boundHigh = message.indexOf(delimiter, boundLow+1); //This instance is going to search for the next delimiter i.e. comma starting at boundLow+1 i.e. one character after the previous comma https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/indexof/
  132. angle2 = message.substring(boundLow+1, boundHigh).toInt(); //Creates a substring of input starting one character after the first comma ending one character before the second comma
  133.  
  134. boundLow = message.indexOf(delimiter, boundHigh+1); //Does the same thing as the previous boundHigh line except this locates third comma's index
  135. angle3 = message.substring(boundHigh+1, boundLow).toInt(); //Creates a substring from after second comma up to just before third comma
  136.  
  137. boundHigh = message.indexOf(delimiter, boundLow+1); //Repeats first boundHigh line operation, Locates fourth comma's index
  138. angle4 = message.substring(boundLow+1, boundHigh).toInt(); //Creates a subtring of input starting one character after third comma up to just before fourth comma
  139.        
  140. boundLow = message.indexOf(delimiter, boundHigh+1); //Does the same thing as the previous boundHigh line except this locates fifth comma's index
  141. angle5 = message.substring(boundHigh+1, boundLow).toInt(); //Creates a substring from after fourth comma up to just before fifth comma
  142.  
  143. boundF = message.indexOf(finald);
  144. angle6 = message.substring(boundLow+1, boundF).toInt(); //Creates a substring of input starting one character after fifth comma up to just before final delimiter
  145.  
  146. servoWriter(angle1, angle2, angle3, angle4, angle5, angle6); //Function Call to Write New Angle to Servo Motors from Message
  147.  
  148. }
  149.  
  150. //Print out the angle for the servo motor to the serial monitor for debugging purposes
  151.   Serial.print("Angle 1: ");
  152.   Serial.print(angle1);
  153.   Serial.print(", ");
  154.   Serial.print("Angle 2: ");
  155.   Serial.print(angle2);
  156.   Serial.print(", ");
  157.   Serial.print("Angle 3: ");
  158.   Serial.print(angle3);
  159.   Serial.print(", ");
  160.   Serial.print("Angle 4: ");
  161.   Serial.print(angle4);
  162.   Serial.print(", ");
  163.   Serial.print("Angle 5: ");
  164.   Serial.print(angle5);
  165.   Serial.print(", ");
  166.   Serial.print("Angle 6: ");
  167.   Serial.print(angle6);
  168.   Serial.println(".");    
  169.  
  170.  
  171.   delay(SMd);  //483 appears to be the sweet spot number.  480 and it occasionally skips displaying a reading on a line.  485 and it occasionally doubles up on a reading two for a line
  172.  
  173.   //Sets all the current angles to the Last variables to be carried over to the next loop
  174.   angle1L = angle1; //Stores current loop's angle for next loop use
  175.   angle2L = angle2; //Stores current loop's angle for next loop use
  176.   angle3L = angle3; //Stores current loop's angle for next loop use
  177.   angle4L = angle4; //Stores current loop's angle for next loop use
  178.   angle5L = angle5; //Stores current loop's angle for next loop use
  179.   angle6L = angle6; //Stores current loop's angle for next loop use
  180.  
  181. }
  182.  
  183.  
  184. //Function Definitions
  185.  
  186. void servoWriter(int input1, int input2, int input3, int input4, int input5, int input6){
  187.   //servoWriter moves all 6 servo motors to the angles input to the function
  188.   Servo1.write(input1); //Moves Specified Servo to Specified Angle
  189.   delay(15); //Gives Servos time to move to position
  190.   Servo2.write(input2); //Moves Specified Servo to Specified Angle
  191.   delay(15); //Gives Servos time to move to position
  192.   Servo3.write(input3); //Moves Specified Servo to Specified Angle
  193.   delay(15); //Gives Servos time to move to position
  194.   Servo4.write(input4); //Moves Specified Servo to Specified Angle
  195.   delay(15); //Gives Servos time to move to position
  196.   Servo5.write(input5); //Moves Specified Servo to Specified Angle
  197.   delay(15); //Gives Servos time to move to position
  198.   Servo6.write(input6); //Moves Specified Servo to Specified Angle
  199.   delay(15); //Gives Servos time to move to position
  200. }
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. //Notes
  211. //Restrict Joint 1 from 20 to 150 degrees.  It can go down to 0 degrees but buzzes.  I haven't taken it over 150.  It might go further
  212. //Restrict Joint 2 from 110 to 150 degrees.  It can go down to 110 degrees but buzzes a little.  Any lower it can really buzz.  Over 150 it really buzzes.
  213. //Restrict Joint 3 from 100 to 180 degrees.  100 seems to be very high up position (arm raised).  180 seems to be the arm lowered.
  214. //Restrict Joint 4 from 0 to 140 degrees.  It can comfortably do this
  215. //Restrict Joint 5 from 10 to 140 degrees.  It can 0 to 150 (I tested these)
  216. //Restrict Joint 6 from 50 to 120 degrees.  50 is closed gripper.  120 is fully open.  
  217.  
  218. //A good resting pose
  219. //  Servo1.write(90); //Moves Specified Servo to Specified Angle
  220. //  Servo2.write(110); //Moves Specified Servo to Specified Angle
  221. //  Servo3.write(120); //Moves Specified Servo to Specified Angle
  222. //  Servo4.write(50); //Moves Specified Servo to Specified Angle
  223. //  Servo5.write(50); //Moves Specified Servo to Specified Angle
  224. //  Servo6.write(120); //Moves Specified Servo to Specified Angle
  225.  
  226. //Reference 1 https://rootsaid.com/long-range-remote-controller/
  227. //Reference 2 https://howtomechatronics.com/tutorials/arduino/arduino-and-hc-12-long-range-wireless-communication-module/
  228. //Reference 3 .remove function https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/remove/
  229. //.remove(index,count) index is where you start removing and count is the number of characters to remove.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement