Advertisement
JonD1988

RoboticArmControlsRemoteTestRxRev4

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