Advertisement
timor2542

[4 DOF Robot Arm Keyestudio][Lab 13][ATX-2] Simple Trajectory

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