|
| 1 | +/**************************************************************************************************************************** |
| 2 | + * examples/ISR_MultiServos.ino |
| 3 | + * For ESP8266 boards |
| 4 | + * Written by Khoi Hoang |
| 5 | + * |
| 6 | + * Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_ISR_Servo |
| 7 | + * Licensed under MIT license |
| 8 | + * Version: v1.0.0 |
| 9 | + * |
| 10 | + * The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega. |
| 11 | + * The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available. |
| 12 | + * The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short. |
| 13 | + * Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!! |
| 14 | + * |
| 15 | + * Now these new 16 ISR-based PWM servo contro uses only 1 hardware timer. |
| 16 | + * The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers |
| 17 | + * Therefore, their executions are not blocked by bad-behaving functions / tasks. |
| 18 | + * This important feature is absolutely necessary for mission-critical tasks. |
| 19 | + * |
| 20 | + * Notes: |
| 21 | + * Special design is necessary to share data between interrupt code and the rest of your program. |
| 22 | + * Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume |
| 23 | + * variable can not spontaneously change. Because your function may change variables while your program is using them, |
| 24 | + * the compiler needs this hint. But volatile alone is often not enough. |
| 25 | + * When accessing shared variables, usually interrupts must be disabled. Even with volatile, |
| 26 | + * if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. |
| 27 | + * If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled |
| 28 | + * or the entire sequence of your code which accesses the data. |
| 29 | + * |
| 30 | + * Version Modified By Date Comments |
| 31 | + * ------- ----------- ---------- ----------- |
| 32 | + * 1.0.0 K Hoang 04/12/2019 Initial release |
| 33 | +*****************************************************************************************************************************/ |
| 34 | + |
| 35 | +/**************************************************************************************************************************** |
| 36 | + * This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs. |
| 37 | + * Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet |
| 38 | + * and Blynk services. You can also have many (up to 16) timers to use. |
| 39 | + * This non-being-blocked important feature is absolutely necessary for mission-critical tasks. |
| 40 | + * You'll see blynkTimer is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate |
| 41 | + * In this super simple example, you don't see much different after Blynk is connected, because of no competing task is |
| 42 | + * written |
| 43 | + * |
| 44 | + * From ESP32 Servo Example Using Arduino ESP32 Servo Library |
| 45 | + * John K. Bennett |
| 46 | + * March, 2017 |
| 47 | + * |
| 48 | + * Different servos require different pulse widths to vary servo angle, but the range is |
| 49 | + * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos |
| 50 | + * sweep 180 degrees, so the lowest number in the published range for a particular servo |
| 51 | + * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top |
| 52 | + * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us, |
| 53 | + * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800 |
| 54 | + * degrees. |
| 55 | + * |
| 56 | + * Circuit: |
| 57 | + * Servo motors have three wires: power, ground, and signal. The power wire is typically red, |
| 58 | + * the ground wire is typically black or brown, and the signal wire is typically yellow, |
| 59 | + * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw |
| 60 | + * considerable power, we will connect servo power to the VBat pin of the ESP32 (located |
| 61 | + * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS. |
| 62 | + * |
| 63 | + * We could also connect servo power to a separate external |
| 64 | + * power source (as long as we connect all of the grounds (ESP32, servo, and external power). |
| 65 | + * In this example, we just connect ESP32 ground to servo ground. The servo signal pins |
| 66 | + * connect to any available GPIO pins on the ESP32 (in this example, we use pins |
| 67 | + * 22, 19, 23, & 18). |
| 68 | + * |
| 69 | + * In this example, we assume four Tower Pro SG90 small servos. |
| 70 | + * The published min and max for this servo are 500 and 2400, respectively. |
| 71 | + * These values actually drive the servos a little past 0 and 180, so |
| 72 | + * if you are particular, adjust the min and max values to match your needs. |
| 73 | + * Experimentally, 550 and 2350 are pretty close to 0 and 180.* |
| 74 | +*****************************************************************************************************************************/ |
| 75 | + |
| 76 | +#define TIMER_INTERRUPT_DEBUG 1 |
| 77 | +#define ISR_SERVO_DEBUG 1 |
| 78 | + |
| 79 | +#include "ESP8266_ISR_Servo.h" |
| 80 | + |
| 81 | + |
| 82 | +int servoIndex1 = -1; |
| 83 | +int servoIndex2 = -1; |
| 84 | + |
| 85 | +void setup() |
| 86 | +{ |
| 87 | + Serial.begin(115200); |
| 88 | + Serial.println("\nStarting"); |
| 89 | + |
| 90 | + servoIndex1 = ISR_Servo.setupServo(D8); |
| 91 | + servoIndex2 = ISR_Servo.setupServo(D7); |
| 92 | + |
| 93 | + if (servoIndex1 != -1) |
| 94 | + Serial.println("Setup Servo1 OK"); |
| 95 | + else |
| 96 | + Serial.println("Setup Servo1 failed"); |
| 97 | + |
| 98 | + if (servoIndex2 != -1) |
| 99 | + Serial.println("Setup Servo2 OK"); |
| 100 | + else |
| 101 | + Serial.println("Setup Servo2 failed"); |
| 102 | +} |
| 103 | + |
| 104 | +void loop() |
| 105 | +{ |
| 106 | + int position; |
| 107 | + |
| 108 | + if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) ) |
| 109 | + { |
| 110 | + for (position = 0; position <= 180; position++) |
| 111 | + { |
| 112 | + // goes from 0 degrees to 180 degrees |
| 113 | + // in steps of 1 degree |
| 114 | + ISR_Servo.setPosition(servoIndex1, position); |
| 115 | + ISR_Servo.setPosition(servoIndex2, 180 - position); |
| 116 | + // waits 15ms for the servo to reach the position |
| 117 | + delay(50 /*15*/); |
| 118 | + } |
| 119 | + |
| 120 | + for (position = 180; position >= 0; position--) |
| 121 | + { |
| 122 | + // goes from 180 degrees to 0 degrees |
| 123 | + ISR_Servo.setPosition(servoIndex1, position); |
| 124 | + ISR_Servo.setPosition(servoIndex2, 180 - position); |
| 125 | + // waits 15ms for the servo to reach the position |
| 126 | + delay(50 /*15*/); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments