Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit fcbc080

Browse files
authored
Add files via upload
1 parent c2d5889 commit fcbc080

File tree

10 files changed

+1707
-0
lines changed

10 files changed

+1707
-0
lines changed

README.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# ESP32_ISR_Servo Library
2+
3+
This library enables you to use `1 Hardware Timer` on an ESP32-based board to control up to `16 independent servo motors`.
4+
5+
***Why do we need this ISR-based Servo controller?***
6+
7+
Imagine you have a system with a `mission-critical function`, controlling a `robot arm` or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().
8+
9+
So your function might not be executed, and the result would be disastrous.
10+
11+
You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).
12+
13+
The correct choice is to use a Hardware Timer with Interrupt to call your function.
14+
15+
These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.
16+
17+
Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.
18+
19+
The catch is your function is now part of an `ISR (Interrupt Service Routine)`, and must be `lean / mean`, and follow certain rules. More to read on:
20+
21+
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
22+
23+
**Important Notes:**
24+
1. Inside the attached function, `delay()` won’t work and the value returned by `millis()` will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
25+
26+
2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as `volatile`.
27+
28+
## Installation
29+
1. Navigate to (https://github.com/khoih-prog/ESP32_ISR_Servo) page.
30+
2. Download the latest release `ESP32_ISR_Servo-master.zip`.
31+
3. Extract the zip file to `ESP32_ISR_Servo-master` directory
32+
4. Copy whole folder to Arduino libraries' directory such as `.Arduino/libraries/ESP32_ISR_Servo-master`.
33+
34+
## More useful Information
35+
36+
The ESP32 has two timer groups, each one with two general purpose hardware timers.
37+
All the timers are based on 64 bits counters and 16 bit prescalers.
38+
The timer counters can be configured to count up or down and support automatic reload and software reload.
39+
They can also generate alarms when they reach a specific value, defined by the software. The value of the counter can be read by
40+
the software program.
41+
42+
## New from v1.0.0
43+
44+
1. Add functions `getPosition()` and `getPulseWidth()`
45+
2. Optimize the code
46+
3. Add more complicated example
47+
48+
Now these new `16 ISR-based Servo controllers` just use one ESP32 Hardware Timer. The number 16 is just arbitrarily chosen, and depending
49+
on application, you can increase that number to 32, 48, etc. without problem.
50+
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
51+
Therefore, their executions are not blocked by bad-behaving functions / tasks.
52+
This important feature is absolutely necessary for mission-critical tasks.
53+
54+
The `MultipleServos` example, which controls 6 servos independently, will demonstrate the nearly perfect accuracy.
55+
Being ISR-based servo controllers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet
56+
and Blynk services.
57+
58+
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
59+
You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task
60+
in loop(), using delay() function as an example. The elapsed time then is very unaccurate
61+
62+
## Supported Boards
63+
64+
- ESP32
65+
66+
## Usage
67+
68+
How to use:
69+
70+
```
71+
#ifndef ESP32
72+
#error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting.
73+
#endif
74+
75+
#define TIMER_INTERRUPT_DEBUG 1
76+
#define ISR_SERVO_DEBUG 1
77+
78+
// Select different ESP32 timer number (0-3) to avoid conflict
79+
#define USE_ESP32_TIMER_NO 3
80+
81+
#include "ESP32_ISR_Servo.h"
82+
83+
#define PIN_D25 25 // Pin D25 mapped to pin GPIO25/ADC18/DAC1 of ESP32
84+
#define PIN_D26 26 // Pin D26 mapped to pin GPIO26/ADC19/DAC2 of ESP32
85+
86+
// Published values for SG90 servos; adjust if needed
87+
#define MIN_MICROS 800 //544
88+
#define MAX_MICROS 2450
89+
90+
int servoIndex1 = -1;
91+
int servoIndex2 = -1;
92+
93+
void setup()
94+
{
95+
Serial.begin(115200);
96+
Serial.println("\nStarting");
97+
98+
//Select ESP32 timer USE_ESP32_TIMER_NO
99+
ESP32_ISR_Servos.useTimer(USE_ESP32_TIMER_NO);
100+
101+
servoIndex1 = ESP32_ISR_Servos.setupServo(PIN_D25, MIN_MICROS, MAX_MICROS);
102+
servoIndex2 = ESP32_ISR_Servos.setupServo(PIN_D26, MIN_MICROS, MAX_MICROS);
103+
104+
if (servoIndex1 != -1)
105+
Serial.println("Setup Servo1 OK");
106+
else
107+
Serial.println("Setup Servo1 failed");
108+
109+
if (servoIndex2 != -1)
110+
Serial.println("Setup Servo2 OK");
111+
else
112+
Serial.println("Setup Servo2 failed");
113+
}
114+
115+
void loop()
116+
{
117+
int position;
118+
119+
if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) )
120+
{
121+
for (position = 0; position <= 180; position++)
122+
{
123+
// goes from 0 degrees to 180 degrees
124+
// in steps of 1 degree
125+
126+
if (position %30 == 0)
127+
{
128+
Serial.println("Servo1 pos = " + String(position) + ", Servo2 pos = " + String(180 - position) );
129+
}
130+
131+
ESP32_ISR_Servos.setPosition(servoIndex1, position);
132+
ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position);
133+
// waits 30ms for the servo to reach the position
134+
delay(30);
135+
}
136+
delay(5000);
137+
138+
for (position = 180; position >= 0; position--)
139+
{
140+
// goes from 180 degrees to 0 degrees
141+
if (position %30 == 0)
142+
{
143+
Serial.println("Servo1 pos = " + String(position) + ", Servo2 pos = " + String(180 - position) );
144+
}
145+
146+
ESP32_ISR_Servos.setPosition(servoIndex1, position);
147+
ESP32_ISR_Servos.setPosition(servoIndex2, 180 - position);
148+
// waits 30ms for the servo to reach the position
149+
delay(30);
150+
}
151+
delay(5000);
152+
153+
}
154+
}
155+
156+
```
157+
## TO DO
158+
159+
1. Search for bug and improvement.
160+
2. Similar features for Arduino (UNO, Mega, etc...)
161+
162+
163+
## DONE
164+
165+
For current version v1.0.1
166+
167+
1. Basic 16 ISR-based servo controllers using 1 hardware timer for ESP32.
168+
169+
170+
## Contributing
171+
If you want to contribute to this project:
172+
- Report bugs and errors
173+
- Ask for enhancements
174+
- Create issues and pull requests
175+
- Tell other people about this library
176+
177+
## Copyright
178+
Copyright 2019- Khoi Hoang
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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

Comments
 (0)