Advertisement
timor2542

[4 DOF Robot Arm Keyestudio][Lab 12][i-Duino UNO R3B] Simple Trajectory

Aug 3rd, 2021
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * SymmetricEasing.cpp
  3.  *
  4.  *  Shows symmetric (end movement is mirror of start movement) linear, quadratic and cubic movements for 3 servos synchronously.
  5.  *
  6.  *  Copyright (C) 2019-2021  Armin Joachimsmeyer
  7.  *
  8.  *  This file is part of ServoEasing https://github.com/ArminJo/ServoEasing.
  9.  *
  10.  *  ServoEasing is free software: you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation, either version 3 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
  22.  */
  23.  
  24.  
  25. /*
  26.  * To generate the Arduino plotter output, you must activate the line #define PRINT_FOR_SERIAL_PLOTTER in ServoEasing.h
  27.  */
  28.  
  29. /*
  30.  * Pin mapping table for different platforms
  31.  *
  32.  * Platform     Servo1      Servo2      Servo3      Analog
  33.  * -------------------------------------------------------
  34.  * AVR + SAMD   9           10          11          A0
  35.  * ESP8266      14 // D5    12 // D6    13 // D7    0
  36.  * ESP32        5           18          19          A0
  37.  * BluePill     PB7         PB8         PB9         PA0
  38.  * APOLLO3      11          12          13          A3
  39.  * ATX-2        13          14          15          knob
  40.  */
  41.  
  42. #include "ServoEasing.h"
  43.  
  44. #define SERVO1_PIN A1
  45. #define SERVO2_PIN A0
  46. #define SERVO3_PIN 6
  47. #define SERVO4_PIN 9
  48.  
  49. #define START_SERVO1_DEGREE_VALUE 80
  50. #define START_SERVO2_DEGREE_VALUE 60
  51. #define START_SERVO3_DEGREE_VALUE 100
  52. #define START_SERVO4_DEGREE_VALUE 85
  53.  
  54. ServoEasing Servo1;
  55. ServoEasing Servo2;
  56. ServoEasing Servo3;
  57. ServoEasing Servo4;
  58. // ServoEasing Servo4;
  59.  
  60. #define START_DEGREE_VALUE 90
  61.  
  62. void setup() {
  63.     delay(5000);
  64.     Serial.begin(115200);
  65.     /************************************************************
  66.      * Attach servo to pin and set servos to start position.
  67.      * This is the position where the movement starts.
  68.      *
  69.      * The order of the attach() determine the position
  70.      * of the Servos in internal ServoEasing::ServoEasingArray[]
  71.      ***********************************************************/
  72.      Serial.println(F("Waiting..."));
  73.      while(!Serial){
  74.       }
  75.      Serial.println(F("OK."));
  76. #ifndef PRINT_FOR_SERIAL_PLOTTER
  77.     Serial.print(F("Attach servo at pin "));
  78.     Serial.println(SERVO1_PIN);
  79. #endif
  80.     if (Servo1.attach(SERVO1_PIN, START_SERVO1_DEGREE_VALUE, DEFAULT_MICROSECONDS_FOR_0_DEGREE,
  81.     DEFAULT_MICROSECONDS_FOR_180_DEGREE) == INVALID_SERVO) {
  82.         Serial.println(F("Error attaching Servo1"));
  83.         while(1){}
  84.     }
  85.     Serial.println(F("Servo1: PASS"));
  86.    
  87. #ifndef PRINT_FOR_SERIAL_PLOTTER
  88.     Serial.print(F("Attach servo at pin "));
  89.     Serial.println(SERVO2_PIN);
  90. #endif
  91.     if (Servo2.attach(SERVO2_PIN, START_SERVO2_DEGREE_VALUE, DEFAULT_MICROSECONDS_FOR_0_DEGREE,
  92.     DEFAULT_MICROSECONDS_FOR_180_DEGREE) == INVALID_SERVO) {
  93.         Serial.println(F("Error attaching Servo2"));
  94.         while(1){}
  95.     }
  96.     Serial.println(F("Servo2: PASS"));
  97.     /*
  98.      * Check at least the last call to attach()
  99.      */
  100. #ifndef PRINT_FOR_SERIAL_PLOTTER
  101.     Serial.print(F("Attach servo at pin "));
  102.     Serial.println(SERVO3_PIN);
  103. #endif
  104.     if (Servo3.attach(SERVO3_PIN, START_SERVO3_DEGREE_VALUE, DEFAULT_MICROSECONDS_FOR_0_DEGREE,
  105.     DEFAULT_MICROSECONDS_FOR_180_DEGREE) == INVALID_SERVO) {
  106.         Serial.println(F("Error attaching Servo3"));
  107.         while(1){}
  108.     }
  109.     Serial.println(F("Servo3: PASS"));
  110.    
  111. #ifndef PRINT_FOR_SERIAL_PLOTTER
  112.     Serial.print(F("Attach servo at pin "));
  113.     Serial.println(SERVO3_PIN);
  114. #endif
  115.     if (Servo4.attach(SERVO4_PIN, START_SERVO4_DEGREE_VALUE, DEFAULT_MICROSECONDS_FOR_0_DEGREE,
  116.     DEFAULT_MICROSECONDS_FOR_180_DEGREE) == INVALID_SERVO) {
  117.         Serial.println(F("Error attaching Servo4"));
  118.         while(1){}
  119.     }
  120.     Serial.println(F("Servo4: PASS"));
  121.     delay(2000);
  122.  
  123. #ifdef PRINT_FOR_SERIAL_PLOTTER
  124.     // Legend for Arduino Serial plotter
  125.     Serial.println();
  126.     Serial.println("Quadratic");
  127. #endif
  128.  
  129. #ifndef PRINT_FOR_SERIAL_PLOTTER
  130.     Serial.println(F("Move from 90 to 45 degree in 1 second"));
  131. #endif
  132.     Servo1.startEaseToD(80, 1000);
  133.     Servo2.startEaseToD(60, 1000);
  134.     Servo3.startEaseToD(100, 1000);
  135.     Servo4.startEaseToD(85, 1000);
  136.     delay(1000);
  137.  
  138.     Servo1.setEasingType(EASE_QUADRATIC_IN_OUT);
  139.     Servo2.setEasingType(EASE_QUADRATIC_IN_OUT);
  140.     Servo3.setEasingType(EASE_QUADRATIC_IN_OUT);
  141.     Servo4.setEasingType(EASE_QUADRATIC_IN_OUT);
  142.  
  143.     delay(500);
  144. }
  145. void loop() {
  146.     setSpeedForAllServos(100);
  147.     __4PosTraj(80, 60, 100, 165);
  148.     __4PosTraj(150, 60, 80, 165);
  149.     __4PosTraj(150, 60, 80, 85);
  150.     __4PosTraj(80, 60, 100, 85);
  151.     __4PosTraj(10, 60, 80, 85);
  152.     __4PosTraj(10, 60, 80, 165);
  153.     __4PosTraj(80, 60, 100, 165);
  154.     __4PosTraj(10, 60, 80, 165);
  155.     __4PosTraj(10, 60, 80, 85);
  156.     __4PosTraj(80, 60, 100, 85);
  157.     __4PosTraj(150, 60, 80, 85);
  158.     __4PosTraj(150, 60, 80, 165);
  159.     __4PosTraj(80, 60, 100, 165);
  160.     __4PosTraj(80, 60, 100, 85);
  161. }
  162.  
  163. void __4PosTraj(int pos1, int pos2, int pos3, int pos4)
  164. {
  165.     setDegreeForAllServos(4, pos1, pos2, pos3, pos4);
  166.     setEaseToForAllServos();
  167.     synchronizeAllServosAndStartInterrupt(false); // false, since we call updateAllServos() manually below
  168.  
  169.     do {
  170.         // here you can call your own program
  171.         delay(REFRESH_INTERVAL / 1000); // optional 20ms delay - REFRESH_INTERVAL is in Microseconds
  172.     } while (!updateAllServos());
  173.     delay(500);
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement