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 0
/
Copy pathSTM32_MultipleRandomServos.ino
246 lines (192 loc) · 7.76 KB
/
STM32_MultipleRandomServos.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/****************************************************************************************************************************
STM32_MultipleRandomServos.ino
For STM32F/L/H/G/WB/MP1 boards with stm32duino Arduino_Core_STM32 core
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/STM32_ISR_Servo
Licensed under MIT license
*****************************************************************************************************************************/
/****************************************************************************************************************************
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 STM32 can supply limited current at only 3.3V, and servos draw
considerable power, we will connect servo power to the VBat pin of the STM32 (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 (STM32, servo, and external power).
In this example, we just connect STM32 ground to servo ground. The servo signal pins
connect to any available GPIO pins on the STM32 (in this example, we use pins (D1-D6).
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, 800 and 2450 are pretty close to 0 and 180.
*****************************************************************************************************************************/
#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \
defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \
defined(STM32WB) || defined(STM32MP1) || defined(STM32L5))
#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting.
#endif
#define TIMER_INTERRUPT_DEBUG 0
#define ISR_SERVO_DEBUG 1
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "STM32_ISR_Servo.h"
// Default is TIMER_SERVO (TIM7 for many boards)
#define USE_STM32_TIMER_NO TIMER_SERVO
// Published values for SG90 servos; adjust if needed
#define MIN_MICROS 800 //544
#define MAX_MICROS 2450
#define SERVO_PIN_1 PA_0 //D1
#define SERVO_PIN_2 PA_1 //D2
#define SERVO_PIN_3 D3
#define SERVO_PIN_4 D4
#define SERVO_PIN_5 D5
#define SERVO_PIN_6 D6
typedef struct
{
int servoIndex;
uint8_t servoPin;
} ISR_servo_t;
#if ( defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32L5) )
#define NUM_SERVOS 3
ISR_servo_t ISR_servo[NUM_SERVOS] =
{
{ -1, SERVO_PIN_1 }, { -1, SERVO_PIN_2 }, { -1, SERVO_PIN_3 }
};
#else
#define NUM_SERVOS 6
ISR_servo_t ISR_servo[NUM_SERVOS] =
{
{ -1, SERVO_PIN_1 }, { -1, SERVO_PIN_2 }, { -1, SERVO_PIN_3 }, { -1, SERVO_PIN_4 }, { -1, SERVO_PIN_5 }, { -1, SERVO_PIN_6 }
};
#endif
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStarting STM32_MultipleRandomServos on "));
Serial.println(BOARD_NAME);
Serial.println(STM32_ISR_SERVO_VERSION);
//Select STM32 timer USE_STM32_TIMER_NO
STM32_ISR_Servos.useTimer(USE_STM32_TIMER_NO);
for (int index = 0; index < NUM_SERVOS; index++)
{
ISR_servo[index].servoIndex = STM32_ISR_Servos.setupServo(ISR_servo[index].servoPin, MIN_MICROS, MAX_MICROS);
if (ISR_servo[index].servoIndex != -1)
{
Serial.print(F("Setup OK Servo index = "));
Serial.println(ISR_servo[index].servoIndex);
}
else
{
Serial.print(F("Setup Failed Servo index = "));
Serial.println(ISR_servo[index].servoIndex);
}
}
}
void printServoInfo(int indexServo)
{
Serial.print(F("Servos idx = "));
Serial.print(indexServo);
Serial.print(F(", act. pos. (deg) = "));
Serial.print(STM32_ISR_Servos.getPosition(ISR_servo[indexServo].servoIndex) );
Serial.print(F(", pulseWidth (us) = "));
Serial.println(STM32_ISR_Servos.getPulseWidth(ISR_servo[indexServo].servoIndex));
}
void loop()
{
int position; // position in degrees
position = 0;
Serial.println(F("Servos @ 0 degree"));
for (int index = 0; index < NUM_SERVOS; index++)
{
STM32_ISR_Servos.setPosition(ISR_servo[index].servoIndex, position );
printServoInfo(index);
}
// waits 5s between test
delay(5000);
position = 90;
Serial.println(F("Servos @ 90 degree"));
for (int index = 0; index < NUM_SERVOS; index++)
{
STM32_ISR_Servos.setPosition(ISR_servo[index].servoIndex, position );
printServoInfo(index);
}
// waits 5s between test
delay(5000);
position = 180;
Serial.println(F("Servos @ 180 degree"));
for (int index = 0; index < NUM_SERVOS; index++)
{
STM32_ISR_Servos.setPosition(ISR_servo[index].servoIndex, position );
printServoInfo(index);
}
// waits 5s between test
delay(5000);
Serial.println(F("Servos sweeps from 0-180 degress"));
for (position = 0; position <= 180; position += 5)
{
// goes from 0 degrees to 180 degrees
// in steps of 1 degree
for (int index = 0; index < NUM_SERVOS; index++)
{
STM32_ISR_Servos.setPosition(ISR_servo[index].servoIndex, position );
}
// waits 50ms for the servo to reach the position
delay(50);
}
// waits 5s between test
delay(5000);
Serial.println(F("Servos sweeps from 180-0 degress"));
for (position = 180; position >= 0; position -= 5)
{
// goes from 0 degrees to 180 degrees
// in steps of 1 degree
for (int index = 0; index < NUM_SERVOS; index++)
{
STM32_ISR_Servos.setPosition(ISR_servo[index].servoIndex, position );
}
// waits 50ms for the servo to reach the position
delay(50);
}
// waits 5s between test
delay(5000);
Serial.println(F("Servos, index depending, be somewhere from 0-180 degress"));
for (position = 0; position <= 180; position += 5)
{
// goes from 0 degrees to 180 degrees
// in steps of 1 degree
for (int index = 0; index < NUM_SERVOS; index++)
{
STM32_ISR_Servos.setPosition(ISR_servo[index].servoIndex, (position + index * (180 / NUM_SERVOS)) % 180 );
}
// waits 50ms for the servo to reach the position
delay(50);
}
delay(5000);
Serial.println(F("Servos, index depending, be somewhere from 180-0 degress"));
for (position = 180; position >= 0; position -= 5)
{
// goes from 0 degrees to 180 degrees
// in steps of 1 degree
for (int index = 0; index < NUM_SERVOS; index++)
{
STM32_ISR_Servos.setPosition(ISR_servo[index].servoIndex, (position + index * (180 / NUM_SERVOS)) % 180 );
}
// waits 50ms for the servo to reach the position
delay(50);
}
// waits 5s between test
delay(5000);
}