Advertisement
Electgpl

ARDUINO - Braso Robot Seguidor

Aug 2nd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.33 KB | None | 0 0
  1.  /*
  2. serial config:
  3. Board: Arduiono Pro / Pro Mini
  4. Port:  tty.usbseriala400eMNr
  5. Programmer: USBtinyISP
  6. */
  7.  
  8. // Definitionen
  9. #include <Servo.h> // servo treiber
  10.  
  11. Servo servo_0;
  12. Servo servo_1;
  13. Servo servo_2;
  14. Servo servo_3;
  15.  
  16. int sensorPin0 = A0;    // Schulter
  17. int sensorPin1 = A1;    // Hand
  18. int sensorPin2 = A2;    // Ellbogen
  19. int sensorPin3 = A3;    // Zange
  20. int count0, arrayStep, arrayMax, countverz, Taster, stepsMax, steps, time = 1000, del = 1000, temp;
  21. // arraystep = memory what pos in the array
  22. // arrayMax = max steps we safed to array
  23. // countverz = seems to be something to calculate the delay between complete moves
  24. // Taster = Button
  25. // stepsMax = longest way a servo have to travel
  26. // steps = single steps for a move between stored positions
  27. unsigned int  verz = 0;
  28.  
  29. long previousMillis1 = 0;
  30. long previousMillis2 = 0;
  31. long previousMillis3 = 0;
  32. long previousMillis4 = 0;
  33. long previousMicros = 0;
  34. unsigned long currentMillis = millis();
  35. unsigned long currentMicros = micros();
  36.  
  37. // arrays
  38. int Delay[7] = {0,0,1,3,15,60,300}; // array to map gripper pot to delay in seconds
  39. int SensVal[4]; // sensor value
  40. float dif[4], ist[4], sol[4],  dir[4]; // difference between stored position and momentary position
  41. int joint0[180];// array for servo(s)
  42. int joint1[180];
  43. int joint2[180];
  44. int joint3[180];
  45. int top = 179; // we should not write over the end from a array
  46. // status
  47. boolean playmode = false, Step = false;
  48.  
  49. void setup()
  50. {
  51.   pinMode(4, INPUT);  // sets the digital pin 4 as input
  52.   pinMode(6, INPUT);
  53.   pinMode(13, OUTPUT);  // sets the digital pin 13 as outtput
  54.   digitalWrite(13, HIGH);   // sets the LED on
  55.   servo_0.attach(3); // attaches the servo
  56.   servo_1.attach(5);
  57.   servo_2.attach(10);
  58.   servo_3.attach(11);
  59.   Serial.begin(115200); // Baudrate have to be same on the IDE
  60.   Serial.println("mini robot ready...");    
  61.   //delay(1000);
  62.   digitalWrite(13, LOW);
  63. }
  64.  
  65. void loop() // here we go!
  66. {
  67.   currentMillis = millis(); // all is about timing
  68.   currentMicros = micros();
  69.  
  70.   // read the button  
  71.   Button();
  72.  
  73.   if(!playmode) // manualy modus
  74.   {        
  75.     if(currentMillis - previousMillis1 > 25) // 25miliseconds until next manual mode update
  76.     {
  77.       if (arrayStep < top)
  78.       {
  79.         previousMillis1 = currentMillis; //reset
  80.         readPot(); // get the value from potentiometers
  81.         mapping(); // map to milliseconds for servos
  82.         move_servo(); // setz newservo position
  83.         //record();  
  84.       } // end counter < max
  85.     } // end step check
  86.   } // ende manualy move
  87.    
  88.   else if(playmode) // play
  89.   {
  90.     if (Step) // next step read from array
  91.     {
  92.       digitalWrite(13, HIGH); //LED
  93.       if (arrayStep < arrayMax) // we not reach the end from stored data
  94.       {
  95.         arrayStep += 1; // next array pos
  96.         Read(); // from the arrays
  97.         calculate(); // find biggest travel distance and calculate the other 3 servos (the have to do smaler steps to be finished at same time!)
  98.         Step = 0;
  99.         digitalWrite(13, LOW);  
  100.       }
  101.       else // array read finished > start over
  102.       {
  103.         arrayStep = 0; //
  104.         calc_pause(); // delay between moves read from potentiometer
  105.         countverz = 0; // used for the delay
  106.         while(countverz < verz) // verz = time getting from calc_pause();
  107.         { // here we do loop and wait until next start over
  108.           countverz += 1;
  109.           calc_pause();
  110.           digitalWrite(13, HIGH); delay(25);  
  111.           digitalWrite(13, LOW); delay(975);
  112.         }
  113.       }
  114.       //Serial.println(arrayStep);
  115.     }
  116.     else // do the servos!
  117.     {
  118.       if (currentMicros - previousMicros > time) // here we do a single micro step
  119.       { //
  120.         previousMicros = currentMicros;
  121.         play_servo();
  122.       }
  123.     }
  124.   }// ende playmode
  125.  
  126. // ---------------------------------------------------------------------------------Hardware pause switch PIN 6
  127.     while (digitalRead(4) == true)
  128.       {
  129.         digitalWrite(13, HIGH); delay(500);  
  130.         digitalWrite(13, LOW); delay(500);
  131.       }
  132. // ---------------------------------------------------------------------------------- Textout serial
  133.     // serial ausgabe 1 sek
  134.     /*if(currentMillis - previousMillis2 > 5000)
  135.     {
  136.       previousMillis2 = currentMillis;
  137.       /*count0 = 0;
  138.       while(count0 < 4)
  139.       {
  140.         int val = SensVal[count0];
  141.       // val = map(val, 142, 888, 0, 180);
  142.         Serial.println(val);
  143.         //Serial.println("test");
  144.         count0 += 1;
  145.       }
  146.       Serial.println(playmode);
  147.       Serial.println(arrayStep);    
  148.       Serial.println(arrayMax);    
  149.       Serial.println(" ");    
  150.     }*/
  151. }
  152.  
  153. // ---------------------------------------------------------------------------------------- sub routinen
  154. void calc_pause() // read pot and map to usable delay time after a complete move is done
  155. {
  156.     readPot();
  157.     temp = SensVal[3];
  158.     if (temp < 0) temp = 0;
  159.     temp = map(temp, 0, 680, 0 ,6);
  160.     verz = Delay[temp]; // verz = delay in second
  161.     /*Serial.print(temp);
  162.           Serial.print(" ");
  163.           Serial.print(verz);
  164.           Serial.print(" ");
  165.           Serial.println(countverz);*/
  166. }
  167.  
  168. void readPot() // read analog inputs and add some offsets (mechanical corrections)
  169. {
  170.    SensVal[0] = analogRead(sensorPin0); SensVal[0] += -10; // rotate
  171.    SensVal[1] = analogRead(sensorPin1); SensVal[1] += 280; // Shoulder
  172.    SensVal[2] = analogRead(sensorPin2); SensVal[2] += -50; // hand
  173.    SensVal[3] = analogRead(sensorPin3); // SensVal[3] += 0;// gripper
  174.    //Serial.print(SensVal[2]);Serial.print(" "); // CHECK
  175. }
  176. void mapping() // we need microsecond for the servos instead potentiometer values
  177. {
  178.   ist[0] = map(SensVal[0], 150, 900, 600, 2400);//  drehen
  179.   ist[1] = map(SensVal[1], 1000, 100, 550, 2400);// Schulter
  180.   ist[2] = map(SensVal[2], 120, 860, 400, 2500);// Hand
  181.   ist[3] = map(SensVal[3], 1023, 0, 500, 2500);// Zange
  182.   //Serial.println(ist[2]); // CHECK
  183. }
  184. void record()
  185. {
  186.     joint0[arrayStep] = ist[0]; // write positions in servo array
  187.     joint1[arrayStep] = ist[1];
  188.     joint2[arrayStep] = ist[2];
  189.     joint3[arrayStep] = ist[3];
  190. }
  191. void Read()
  192. {
  193.     sol[0] = joint0[arrayStep]; // read from the array
  194.     sol[1] = joint1[arrayStep];
  195.     sol[2] = joint2[arrayStep];
  196.     sol[3] = joint3[arrayStep];
  197. }
  198. void move_servo()
  199. {      
  200.   servo_0.writeMicroseconds(ist[3]); // send milissecond values to servos
  201.   servo_1.writeMicroseconds(ist[2]);
  202.   servo_2.writeMicroseconds(ist[0]);
  203.   servo_3.writeMicroseconds(ist[1]);
  204. }
  205.  
  206. // ------------------------------------------------------------ single steps calculating
  207. void calculate()
  208. {
  209.       // travel distance for each servo
  210.       dif[0] = abs(ist[0]-sol[0]);
  211.       dif[1] = abs(ist[1]-sol[1]);
  212.       dif[2] = abs(ist[2]-sol[2]);
  213.       dif[3] = abs(ist[3]-sol[3]);
  214.  
  215.       // biggest travel way from all 4 servos
  216.       stepsMax = max(dif[0],dif[1]);
  217.       stepsMax = max(stepsMax,dif[2]);
  218.       stepsMax = max(stepsMax,dif[3]);
  219.       // stepsMax is the biggest distance a servo have to do beween momentary position and new pos read from the array
  220.      
  221.       //Serial.println(stepsMax);
  222.      
  223.       if (stepsMax < 500) // del(ay) between a single step is bigger is move is smaler. just looks cool
  224.         del = 1200;
  225.       else
  226.         del = 600;
  227.      
  228.        // calculating single (micro) step for each servo
  229.        // need that to do move all servos in a loop (stepsMax times done) with different values.
  230.        // This makes all servos have done the traveling distance at same time
  231.       if (sol[0] < ist[0]) dir[0] = 0-dif[0]/stepsMax; else dir[0] = dif[0]/stepsMax;
  232.       if (sol[1] < ist[1]) dir[1] = 0-dif[1]/stepsMax; else dir[1] = dif[1]/stepsMax;
  233.       if (sol[2] < ist[2]) dir[2] = 0-dif[2]/stepsMax; else dir[2] = dif[2]/stepsMax;
  234.       if (sol[3] < ist[3]) dir[3] = 0-dif[3]/stepsMax; else dir[3] = dif[3]/stepsMax;
  235.         //Serial.println(dir4);
  236.  
  237. }
  238. void play_servo()
  239. {
  240.     steps += 1;
  241.     if (steps < stepsMax) // sure we not reach the end from a move
  242.     {
  243.       //time = del*5;// anfahr rampe
  244.       if(steps == 20) time = del*4;         // ramp up
  245.       else if(steps == 40) time = del*3;    // time is the delay in microsecns we wait in the mainloop until
  246.       else if(steps == 80) time = del*2;    // a micro step will be done
  247.       else if(steps == 100) time = del-1;    // cannot explain here is not del*1
  248.      
  249.       if(steps == stepsMax-200) time = del*2;        // stop ramp down (200 microsteps before end time will be increased
  250.       else if(steps == stepsMax-80) time = del*3;
  251.       else if(steps == stepsMax-40) time = del*4;
  252.       else if(steps == stepsMax-20) time = del*5;
  253.      
  254.       ist[0] += dir[0]; // set new pos
  255.       ist[1] += dir[1];
  256.       ist[2] += dir[2];
  257.       ist[3] += dir[3];
  258.  
  259.       servo_0.writeMicroseconds(ist[3]); // Zange //anschlüsse gemappt!
  260.       servo_1.writeMicroseconds(ist[2]); // Hand
  261.       servo_2.writeMicroseconds(ist[0]); // Schulter
  262.       servo_3.writeMicroseconds(ist[1]); // Ellbogen
  263.     }
  264.     else
  265.     {
  266.       Step = 1; // next step aus array lesen
  267.       steps = 0; // servo zwischenschritte
  268.     }
  269. }
  270.  
  271. void data_out() // just to write the recorded data to serial
  272. {
  273.   int i = 0;
  274.   while(i < arrayMax)
  275.   {
  276.     digitalWrite(13, HIGH);
  277.     i += 1;
  278.     Serial.print(joint0[i]); Serial.print(", ");
  279.   }
  280.   Serial.println("Joint0");
  281.   i = 0;
  282.   while(i < arrayMax)
  283.   {
  284.     digitalWrite(13, HIGH);
  285.     i += 1;
  286.     Serial.print(joint1[i]); Serial.print(", ");
  287.   }
  288.   Serial.println("Joint1");
  289.   i = 0;
  290.   while(i < arrayMax)
  291.   {
  292.     digitalWrite(13, HIGH);
  293.     i += 1;
  294.     Serial.print(joint2[i]); Serial.print(", ");
  295.   }
  296.   Serial.println("Joint2");
  297.   i = 0;
  298.   while(i < arrayMax)
  299.   {
  300.     digitalWrite(13, HIGH);
  301.     i += 1;
  302.     Serial.print(joint3[i]); Serial.print(", ");
  303.   }
  304.   Serial.println("Joint3");
  305. }
  306.  
  307. void Button() // check buttons for single and doubleclick
  308. {
  309.   if (digitalRead(6) == false)
  310.   {
  311.     delay(20);
  312.     if (digitalRead(6) == true) // taster losgelassen
  313.     {
  314.       if (Taster == 0)
  315.       {
  316.         Taster = 1;
  317.         previousMillis3 = currentMillis;
  318.         //Serial.print("Status Record "); Serial.println(Taster);
  319.       }
  320.       else if ((Taster == 1) && (currentMillis - previousMillis3 < 250))
  321.       {
  322.         Taster = 2;
  323.         //Serial.println(Taster);
  324.       }
  325.       /*else if ((Taster == 2) && (currentMillis - previousMillis3 < 500))
  326.       {
  327.         Taster = 3;
  328.         Serial.println(Taster);
  329.       }*/
  330.     }
  331.   }
  332.    
  333.     if ((Taster == 1) && (currentMillis - previousMillis3 > 1000)) // write to array
  334.     {
  335.       arrayStep += 1;
  336.       arrayMax = arrayStep;
  337.       record();
  338.       Taster = 0;
  339.       playmode = false;
  340.       Serial.print("Record Step: "); Serial.println(arrayStep);
  341.       digitalWrite(13, HIGH);
  342.       delay(100);
  343.       digitalWrite(13, LOW);
  344.     }
  345.     else if (Taster == 2)
  346.     {
  347.       arrayStep = 0;
  348.       playmode = true;
  349.       Taster = 0;
  350.       Step = 1;
  351.       Serial.println("playmode ");
  352.       data_out();
  353.       delay(250);  
  354.       digitalWrite(13, LOW);    
  355.     }
  356.     /*if (Taster == 3)
  357.     {
  358.       // ++ arrayStep
  359.       // playmode = 1;
  360.       Taster = 0;
  361.       Serial.println("Clear ");
  362.     }*/
  363.     if (currentMillis - previousMillis3 > 2000) // button Status clear
  364.     {
  365.       Taster = 0;
  366.       //Serial.println("restart ");
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement