This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathISR_MultiServos.ino
149 lines (121 loc) · 6.14 KB
/
ISR_MultiServos.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/****************************************************************************************************************************
ISR_MultiServos.ino
For ESP8266 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_ISR_Servo
Licensed under MIT license
The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega.
The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available.
The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short.
Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!
Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
Therefore, their executions are not blocked by bad-behaving functions / tasks.
This important feature is absolutely necessary for mission-critical tasks.
*****************************************************************************************************************************/
/****************************************************************************************************************************
This example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs.
Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet
and Blynk services. You can also have many (up to 16) timers to use.
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
You'll see blynkTimer is blocked while connecting to WiFi / Internet / Blynk, and elapsed time is very unaccurate
In this super simple example, you don't see much different after Blynk is connected, because of no competing task is
written
From ESP32 Servo Example Using Arduino ESP32 Servo Library
John K. Bennett
March, 2017
Different servos require different pulse widths to vary servo angle, but the range is
an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
sweep 180 degrees, so the lowest number in the published range for a particular servo
represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
degrees.
Circuit:
Servo motors have three wires: power, ground, and signal. The power wire is typically red,
the ground wire is typically black or brown, and the signal wire is typically yellow,
orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
considerable power, we will connect servo power to the VBat pin of the ESP32 (located
near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
We could also connect servo power to a separate external
power source (as long as we connect all of the grounds (ESP32, servo, and external power).
In this example, we just connect ESP32 ground to servo ground. The servo signal pins
connect to any available GPIO pins on the ESP32 (in this example, we use pins
22, 19, 23, & 18).
In this example, we assume four Tower Pro SG90 small servos.
The published min and max for this servo are 500 and 2400, respectively.
These values actually drive the servos a little past 0 and 180, so
if you are particular, adjust the min and max values to match your needs.
Experimentally, 550 and 2350 are pretty close to 0 and 180.
*****************************************************************************************************************************/
#ifndef ESP8266
#error This code is designed to run on ESP8266 platform! Please check your Tools->Board setting.
#endif
#define TIMER_INTERRUPT_DEBUG 1
#define ISR_SERVO_DEBUG 1
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "ESP8266_ISR_Servo.h"
// Published values for SG90 servos; adjust if needed
#define MIN_MICROS 800 //544
#define MAX_MICROS 2450
int servoIndex1 = -1;
int servoIndex2 = -1;
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStarting ISR_MultiServos on "));
Serial.println(ARDUINO_BOARD);
Serial.println(ESP8266_ISR_SERVO_VERSION);
servoIndex1 = ISR_Servo.setupServo(D8, MIN_MICROS, MAX_MICROS);
servoIndex2 = ISR_Servo.setupServo(D7, MIN_MICROS, MAX_MICROS);
if (servoIndex1 != -1)
Serial.println(F("Setup Servo1 OK"));
else
Serial.println(F("Setup Servo1 failed"));
if (servoIndex2 != -1)
Serial.println(F("Setup Servo2 OK"));
else
Serial.println(F("Setup Servo2 failed"));
}
void loop()
{
int position;
if ( ( servoIndex1 != -1) && ( servoIndex2 != -1) )
{
for (position = 0; position <= 180; position++)
{
// goes from 0 degrees to 180 degrees
// in steps of 1 degree
if (position % 30 == 0)
{
Serial.print(F("Servo1 pos = "));
Serial.print(position);
Serial.print(F(", Servo2 pos = "));
Serial.println(180 - position);
}
ISR_Servo.setPosition(servoIndex1, position);
ISR_Servo.setPosition(servoIndex2, 180 - position);
// waits 15ms for the servo to reach the position
delay(50 /*15*/);
}
delay(5000);
for (position = 180; position >= 0; position--)
{
// goes from 180 degrees to 0 degrees
if (position % 30 == 0)
{
Serial.print(F("Servo1 pos = "));
Serial.print(position);
Serial.print(F(", Servo2 pos = "));
Serial.println(180 - position);
}
ISR_Servo.setPosition(servoIndex1, position);
ISR_Servo.setPosition(servoIndex2, 180 - position);
// waits 15ms for the servo to reach the position
delay(50 /*15*/);
}
delay(5000);
}
}