|
| 1 | +/**************************************************************************************************************************** |
| 2 | + * examples/ISR_MultiServos.ino |
| 3 | + * For ESP32 boards |
| 4 | + * Written by Khoi Hoang |
| 5 | + * |
| 6 | + * Built by Khoi Hoang https://github.com/khoih-prog/ESP32_ISR_Servo |
| 7 | + * Licensed under MIT license |
| 8 | + * Version: v1.0.0 |
| 9 | + * |
| 10 | + * The ESP32 has two timer groups, each one with two general purpose hardware timers. All the timers |
| 11 | + * are based on 64 bits counters and 16 bit prescalers |
| 12 | + * The timer counters can be configured to count up or down and support automatic reload and software reload |
| 13 | + * They can also generate alarms when they reach a specific value, defined by the software. |
| 14 | + * The value of the counter can be read by the software program. |
| 15 | + * |
| 16 | + * Now these new 16 ISR-based PWM servo control uses only 1 hardware timer. |
| 17 | + * The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers |
| 18 | + * Therefore, their executions are not blocked by bad-behaving functions / tasks. |
| 19 | + * This important feature is absolutely necessary for mission-critical tasks. |
| 20 | + * |
| 21 | + * Notes: |
| 22 | + * Special design is necessary to share data between interrupt code and the rest of your program. |
| 23 | + * Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume |
| 24 | + * variable can not spontaneously change. Because your function may change variables while your program is using them, |
| 25 | + * the compiler needs this hint. But volatile alone is often not enough. |
| 26 | + * When accessing shared variables, usually interrupts must be disabled. Even with volatile, |
| 27 | + * if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. |
| 28 | + * If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled |
| 29 | + * or the entire sequence of your code which accesses the data. |
| 30 | + * |
| 31 | + * Version Modified By Date Comments |
| 32 | + * ------- ----------- ---------- ----------- |
| 33 | + * 1.0.0 K Hoang 13/12/2019 Initial release |
| 34 | +*****************************************************************************************************************************/ |
| 35 | + |
| 36 | +/**************************************************************************************************************************** |
| 37 | + * This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs. |
| 38 | + * Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet |
| 39 | + * and Blynk services. You can also have many (up to 16) timers to use. |
| 40 | + * This non-being-blocked important feature is absolutely necessary for mission-critical tasks. |
| 41 | + * You'll see blynkTimer is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate |
| 42 | + * In this super simple example, you don't see much different after Blynk is connected, because of no competing task is |
| 43 | + * written |
| 44 | + * |
| 45 | + * From ESP32 Servo Example Using Arduino ESP32 Servo Library |
| 46 | + * John K. Bennett |
| 47 | + * March, 2017 |
| 48 | + * |
| 49 | + * Different servos require different pulse widths to vary servo angle, but the range is |
| 50 | + * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos |
| 51 | + * sweep 180 degrees, so the lowest number in the published range for a particular servo |
| 52 | + * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top |
| 53 | + * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us, |
| 54 | + * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800 |
| 55 | + * degrees. |
| 56 | + * |
| 57 | + * Circuit: |
| 58 | + * Servo motors have three wires: power, ground, and signal. The power wire is typically red, |
| 59 | + * the ground wire is typically black or brown, and the signal wire is typically yellow, |
| 60 | + * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw |
| 61 | + * considerable power, we will connect servo power to the VBat pin of the ESP32 (located |
| 62 | + * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS. |
| 63 | + * |
| 64 | + * We could also connect servo power to a separate external |
| 65 | + * power source (as long as we connect all of the grounds (ESP32, servo, and external power). |
| 66 | + * In this example, we just connect ESP32 ground to servo ground. The servo signal pins |
| 67 | + * connect to any available GPIO pins on the ESP32 (in this example, we use pins |
| 68 | + * 22, 19, 23, & 18). |
| 69 | + * |
| 70 | + * In this example, we assume four Tower Pro SG90 small servos. |
| 71 | + * The published min and max for this servo are 500 and 2400, respectively. |
| 72 | + * These values actually drive the servos a little past 0 and 180, so |
| 73 | + * if you are particular, adjust the min and max values to match your needs. |
| 74 | + * Experimentally, 550 and 2350 are pretty close to 0 and 180.* |
| 75 | +*****************************************************************************************************************************/ |
| 76 | +#ifndef ESP32 |
| 77 | +#error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting. |
| 78 | +#endif |
| 79 | + |
| 80 | +#define TIMER_INTERRUPT_DEBUG 1 |
| 81 | +#define ISR_SERVO_DEBUG 1 |
| 82 | + |
| 83 | +// Select different ESP32 timer number (0-3) to avoid conflict |
| 84 | +#define USE_ESP32_TIMER_NO 3 |
| 85 | + |
| 86 | +#include "ESP32_ISR_Servo.h" |
| 87 | + |
| 88 | +//See file .../hardware/espressif/esp32/variants/(esp32|doitESP32devkitV1)/pins_arduino.h |
| 89 | +#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED |
| 90 | +#define PIN_LED 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED |
| 91 | + |
| 92 | +#define PIN_D0 0 // Pin D0 mapped to pin GPIO0/BOOT/ADC11/TOUCH1 of ESP32 |
| 93 | +#define PIN_D1 1 // Pin D1 mapped to pin GPIO1/TX0 of ESP32 |
| 94 | +#define PIN_D2 2 // Pin D2 mapped to pin GPIO2/ADC12/TOUCH2 of ESP32 |
| 95 | +#define PIN_D3 3 // Pin D3 mapped to pin GPIO3/RX0 of ESP32 |
| 96 | +#define PIN_D4 4 // Pin D4 mapped to pin GPIO4/ADC10/TOUCH0 of ESP32 |
| 97 | +#define PIN_D5 5 // Pin D5 mapped to pin GPIO5/SPISS/VSPI_SS of ESP32 |
| 98 | +#define PIN_D6 6 // Pin D6 mapped to pin GPIO6/FLASH_SCK of ESP32 |
| 99 | +#define PIN_D7 7 // Pin D7 mapped to pin GPIO7/FLASH_D0 of ESP32 |
| 100 | +#define PIN_D8 8 // Pin D8 mapped to pin GPIO8/FLASH_D1 of ESP32 |
| 101 | +#define PIN_D9 9 // Pin D9 mapped to pin GPIO9/FLASH_D2 of ESP32 |
| 102 | + |
| 103 | +#define PIN_D10 10 // Pin D10 mapped to pin GPIO10/FLASH_D3 of ESP32 |
| 104 | +#define PIN_D11 11 // Pin D11 mapped to pin GPIO11/FLASH_CMD of ESP32 |
| 105 | +#define PIN_D12 12 // Pin D12 mapped to pin GPIO12/HSPI_MISO/ADC15/TOUCH5/TDI of ESP32 |
| 106 | +#define PIN_D13 13 // Pin D13 mapped to pin GPIO13/HSPI_MOSI/ADC14/TOUCH4/TCK of ESP32 |
| 107 | +#define PIN_D14 14 // Pin D14 mapped to pin GPIO14/HSPI_SCK/ADC16/TOUCH6/TMS of ESP32 |
| 108 | +#define PIN_D15 15 // Pin D15 mapped to pin GPIO15/HSPI_SS/ADC13/TOUCH3/TDO of ESP32 |
| 109 | +#define PIN_D16 16 // Pin D16 mapped to pin GPIO16/TX2 of ESP32 |
| 110 | +#define PIN_D17 17 // Pin D17 mapped to pin GPIO17/RX2 of ESP32 |
| 111 | +#define PIN_D18 18 // Pin D18 mapped to pin GPIO18/VSPI_SCK of ESP32 |
| 112 | +#define PIN_D19 19 // Pin D19 mapped to pin GPIO19/VSPI_MISO of ESP32 |
| 113 | + |
| 114 | +#define PIN_D21 21 // Pin D21 mapped to pin GPIO21/SDA of ESP32 |
| 115 | +#define PIN_D22 22 // Pin D22 mapped to pin GPIO22/SCL of ESP32 |
| 116 | +#define PIN_D23 23 // Pin D23 mapped to pin GPIO23/VSPI_MOSI of ESP32 |
| 117 | +#define PIN_D24 24 // Pin D24 mapped to pin GPIO24 of ESP32 |
| 118 | +#define PIN_D25 25 // Pin D25 mapped to pin GPIO25/ADC18/DAC1 of ESP32 |
| 119 | +#define PIN_D26 26 // Pin D26 mapped to pin GPIO26/ADC19/DAC2 of ESP32 |
| 120 | +#define PIN_D27 27 // Pin D27 mapped to pin GPIO27/ADC17/TOUCH7 of ESP32 |
| 121 | + |
| 122 | +#define PIN_D32 32 // Pin D32 mapped to pin GPIO32/ADC4/TOUCH9 of ESP32 |
| 123 | +#define PIN_D33 33 // Pin D33 mapped to pin GPIO33/ADC5/TOUCH8 of ESP32 |
| 124 | +#define PIN_D34 34 // Pin D34 mapped to pin GPIO34/ADC6 of ESP32 |
| 125 | +#define PIN_D35 35 // Pin D35 mapped to pin GPIO35/ADC7 of ESP32 |
| 126 | +#define PIN_D36 36 // Pin D36 mapped to pin GPIO36/ADC0/SVP of ESP32 |
| 127 | +#define PIN_D39 39 // Pin D39 mapped to pin GPIO39/ADC3/SVN of ESP32 |
| 128 | + |
| 129 | +#define PIN_RX0 3 // Pin RX0 mapped to pin GPIO3/RX0 of ESP32 |
| 130 | +#define PIN_TX0 1 // Pin TX0 mapped to pin GPIO1/TX0 of ESP32 |
| 131 | + |
| 132 | +#define PIN_SCL 22 // Pin SCL mapped to pin GPIO22/SCL of ESP32 |
| 133 | +#define PIN_SDA 21 // Pin SDA mapped to pin GPIO21/SDA of ESP32 |
| 134 | + |
| 135 | +// Published values for SG90 servos; adjust if needed |
| 136 | +#define MIN_MICROS 800 //544 |
| 137 | +#define MAX_MICROS 2450 |
| 138 | + |
| 139 | +int servoIndex1 = -1; |
| 140 | +int servoIndex2 = -1; |
| 141 | + |
| 142 | +void setup() |
| 143 | +{ |
| 144 | + Serial.begin(115200); |
| 145 | + Serial.println("\nStarting"); |
| 146 | + |
| 147 | + //Select ESP32 timer USE_ESP32_TIMER_NO |
| 148 | + ESP32_ISR_Servos.useTimer(USE_ESP32_TIMER_NO); |
| 149 | + |
| 150 | + servoIndex1 = ESP32_ISR_Servos.setupServo(PIN_D25, MIN_MICROS, MAX_MICROS); |
| 151 | + servoIndex2 = ESP32_ISR_Servos.setupServo(PIN_D26, MIN_MICROS, MAX_MICROS); |
| 152 | + |
| 153 | + if (servoIndex1 != -1) |
| 154 | + Serial.println("Setup Servo1 OK"); |
| 155 | + else |
| 156 | + Serial.println("Setup Servo1 failed"); |
| 157 | + |
| 158 | + if (servoIndex2 != -1) |
| 159 | + Serial.println("Setup Servo2 OK"); |
| 160 | + else |
| 161 | + Serial.println("Setup Servo2 failed"); |
| 162 | +} |
| 163 | + |
| 164 | +void loop() |
| 165 | +{ |
| 166 | + int position; |
| 167 | + |
| 168 | + if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) ) |
| 169 | + { |
| 170 | + for (position = 0; position <= 180; position++) |
| 171 | + { |
| 172 | + // goes from 0 degrees to 180 degrees |
| 173 | + // in steps of 1 degree |
| 174 | + |
| 175 | + if (position %30 == 0) |
| 176 | + { |
| 177 | + Serial.println("Servo1 pos = " + String(position) + ", Servo2 pos = " + String(180 - position) ); |
| 178 | + } |
| 179 | + |
| 180 | + ESP32_ISR_Servos.setPosition(servoIndex1, position); |
| 181 | + ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position); |
| 182 | + // waits 30ms for the servo to reach the position |
| 183 | + delay(30); |
| 184 | + } |
| 185 | + delay(5000); |
| 186 | + |
| 187 | + for (position = 180; position >= 0; position--) |
| 188 | + { |
| 189 | + // goes from 180 degrees to 0 degrees |
| 190 | + if (position %30 == 0) |
| 191 | + { |
| 192 | + Serial.println("Servo1 pos = " + String(position) + ", Servo2 pos = " + String(180 - position) ); |
| 193 | + } |
| 194 | + |
| 195 | + ESP32_ISR_Servos.setPosition(servoIndex1, position); |
| 196 | + ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position); |
| 197 | + // waits 30ms for the servo to reach the position |
| 198 | + delay(30); |
| 199 | + } |
| 200 | + delay(5000); |
| 201 | + |
| 202 | + } |
| 203 | +} |
0 commit comments