|
2 | 2 | ESP32_S2_ISR_MultiServos.ino |
3 | 3 | For ESP32_S2 boards |
4 | 4 | Written by Khoi Hoang |
5 | | - |
6 | | - Built by Khoi Hoang https://github.com/khoih-prog/ESP32_ISR_Servo |
| 5 | +
|
| 6 | + Built by Khoi Hoang https://github.com/khoih-prog/ESP32_S2_ISR_Servo |
7 | 7 | Licensed under MIT license |
8 | 8 | |
9 | 9 | The ESP32 has two timer groups, each one with two general purpose hardware timers. All the timers |
10 | 10 | are based on 64 bits counters and 16 bit prescalers |
11 | 11 | The timer counters can be configured to count up or down and support automatic reload and software reload |
12 | 12 | They can also generate alarms when they reach a specific value, defined by the software. |
13 | 13 | The value of the counter can be read by the software program. |
14 | | - |
| 14 | +
|
15 | 15 | Now these new 16 ISR-based PWM servo contro uses only 1 hardware timer. |
16 | 16 | The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers |
17 | 17 | Therefore, their executions are not blocked by bad-behaving functions / tasks. |
18 | 18 | This important feature is absolutely necessary for mission-critical tasks. |
19 | | - |
| 19 | +
|
20 | 20 | Notes: |
21 | 21 | Special design is necessary to share data between interrupt code and the rest of your program. |
22 | 22 | Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume |
|
78 | 78 | #include "ESP32_S2_ISR_Servo.h" |
79 | 79 |
|
80 | 80 | //See file .../hardware/espressif/esp32/variants/(esp32|doitESP32devkitV1)/pins_arduino.h |
81 | | -#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED |
82 | 81 | #define PIN_LED 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED |
83 | 82 |
|
84 | 83 | #define PIN_D0 0 // Pin D0 mapped to pin GPIO0/BOOT/ADC11/TOUCH1 of ESP32 |
@@ -133,71 +132,77 @@ int servoIndex2 = -1; |
133 | 132 |
|
134 | 133 | void setup() |
135 | 134 | { |
136 | | - Serial.begin(115200); |
137 | | - while (!Serial); |
| 135 | + Serial.begin(115200); |
| 136 | + |
| 137 | + while (!Serial && millis() < 5000); |
138 | 138 |
|
139 | 139 | delay(500); |
140 | 140 |
|
141 | | - Serial.print(F("\nStarting ESP32_S2_ISR_MultiServos on ")); Serial.println(ARDUINO_BOARD); |
142 | | - Serial.println(ESP32_S2_ISR_SERVO_VERSION); |
143 | | - |
144 | | - //Select ESP32 timer USE_ESP32_TIMER_NO |
145 | | - ESP32_ISR_Servos.useTimer(USE_ESP32_TIMER_NO); |
| 141 | + Serial.print(F("\nStarting ESP32_S2_ISR_MultiServos on ")); |
| 142 | + Serial.println(ARDUINO_BOARD); |
| 143 | + Serial.println(ESP32_S2_ISR_SERVO_VERSION); |
| 144 | + |
| 145 | + //Select ESP32 timer USE_ESP32_TIMER_NO |
| 146 | + ESP32_ISR_Servos.useTimer(USE_ESP32_TIMER_NO); |
146 | 147 |
|
147 | | - servoIndex1 = ESP32_ISR_Servos.setupServo(PIN_D3, MIN_MICROS, MAX_MICROS); |
148 | | - servoIndex2 = ESP32_ISR_Servos.setupServo(PIN_D4, MIN_MICROS, MAX_MICROS); |
| 148 | + servoIndex1 = ESP32_ISR_Servos.setupServo(PIN_D3, MIN_MICROS, MAX_MICROS); |
| 149 | + servoIndex2 = ESP32_ISR_Servos.setupServo(PIN_D4, MIN_MICROS, MAX_MICROS); |
149 | 150 |
|
150 | | - if (servoIndex1 != -1) |
151 | | - Serial.println(F("Setup Servo1 OK")); |
152 | | - else |
153 | | - Serial.println(F("Setup Servo1 failed")); |
| 151 | + if (servoIndex1 != -1) |
| 152 | + Serial.println(F("Setup Servo1 OK")); |
| 153 | + else |
| 154 | + Serial.println(F("Setup Servo1 failed")); |
154 | 155 |
|
155 | | - if (servoIndex2 != -1) |
156 | | - Serial.println(F("Setup Servo2 OK")); |
157 | | - else |
158 | | - Serial.println(F("Setup Servo2 failed")); |
| 156 | + if (servoIndex2 != -1) |
| 157 | + Serial.println(F("Setup Servo2 OK")); |
| 158 | + else |
| 159 | + Serial.println(F("Setup Servo2 failed")); |
159 | 160 | } |
160 | 161 |
|
161 | 162 | void loop() |
162 | 163 | { |
163 | | - int position; |
164 | | - |
165 | | - if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) ) |
166 | | - { |
167 | | - for (position = 0; position <= 180; position++) |
168 | | - { |
169 | | - // goes from 0 degrees to 180 degrees |
170 | | - // in steps of 1 degree |
171 | | - |
172 | | - if (position % 30 == 0) |
173 | | - { |
174 | | - Serial.print(F("Servo1 pos = ")); Serial.print(position); |
175 | | - Serial.print(F(", Servo2 pos = ")); Serial.println(180 - position); |
176 | | - } |
177 | | - |
178 | | - ESP32_ISR_Servos.setPosition(servoIndex1, position); |
179 | | - ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position); |
180 | | - // waits 30ms for the servo to reach the position |
181 | | - delay(30); |
182 | | - } |
183 | | - |
184 | | - delay(5000); |
185 | | - |
186 | | - for (position = 180; position >= 0; position--) |
187 | | - { |
188 | | - // goes from 180 degrees to 0 degrees |
189 | | - if (position % 30 == 0) |
190 | | - { |
191 | | - Serial.print(F("Servo1 pos = ")); Serial.print(position); |
192 | | - Serial.print(F(", Servo2 pos = ")); Serial.println(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 | | - |
201 | | - delay(5000); |
202 | | - } |
| 164 | + int position; |
| 165 | + |
| 166 | + if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) ) |
| 167 | + { |
| 168 | + for (position = 0; position <= 180; position++) |
| 169 | + { |
| 170 | + // goes from 0 degrees to 180 degrees |
| 171 | + // in steps of 1 degree |
| 172 | + |
| 173 | + if (position % 30 == 0) |
| 174 | + { |
| 175 | + Serial.print(F("Servo1 pos = ")); |
| 176 | + Serial.print(position); |
| 177 | + Serial.print(F(", Servo2 pos = ")); |
| 178 | + Serial.println(180 - position); |
| 179 | + } |
| 180 | + |
| 181 | + ESP32_ISR_Servos.setPosition(servoIndex1, position); |
| 182 | + ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position); |
| 183 | + // waits 30ms for the servo to reach the position |
| 184 | + delay(30); |
| 185 | + } |
| 186 | + |
| 187 | + delay(5000); |
| 188 | + |
| 189 | + for (position = 180; position >= 0; position--) |
| 190 | + { |
| 191 | + // goes from 180 degrees to 0 degrees |
| 192 | + if (position % 30 == 0) |
| 193 | + { |
| 194 | + Serial.print(F("Servo1 pos = ")); |
| 195 | + Serial.print(position); |
| 196 | + Serial.print(F(", Servo2 pos = ")); |
| 197 | + Serial.println(180 - position); |
| 198 | + } |
| 199 | + |
| 200 | + ESP32_ISR_Servos.setPosition(servoIndex1, position); |
| 201 | + ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position); |
| 202 | + // waits 30ms for the servo to reach the position |
| 203 | + delay(30); |
| 204 | + } |
| 205 | + |
| 206 | + delay(5000); |
| 207 | + } |
203 | 208 | } |
0 commit comments