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
/
nRF52_MultipleRandomServos.ino
172 lines (136 loc) · 5.59 KB
/
nRF52_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
/****************************************************************************************************************************
NRF52_MultipleRandomServos.ino
For :
- nRF52832-based boards such as AdaFruit Feather nRF52832,
- nRF52840-based boards such as Adafruit nRF52840 Express, Itsy-Bitsy nRF52840 Express, NINA_B302_ublox, etc.
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/NRF52_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 NRF52 can supply limited current at only 3.3V, and servos draw
considerable power, we will connect servo power to the VBat pin of the NRF52 (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 (NRF52, servo, and external power).
In this example, we just connect NRF52 ground to servo ground. The servo signal pins
connect to any available GPIO pins on the NRF52 (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.
*****************************************************************************************************************************/
#define TIMER_INTERRUPT_DEBUG 4
#define ISR_SERVO_DEBUG 4
#include "NRF52_ISR_Servo.h"
// Published values for SG90 servos; adjust if needed
#define MIN_MICROS 800
#define MAX_MICROS 2450
#define SERVO_PIN_1 A0
#define SERVO_PIN_2 A1
#define SERVO_PIN_3 A2
#define SERVO_PIN_4 A3
#define SERVO_PIN_5 A4
#define SERVO_PIN_6 A5
typedef struct
{
int servoIndex;
uint8_t servoPin;
} ISR_servo_t;
#define NUM_SERVOS 6
ISR_servo_t ISR_servo[] =
{
{ -1, SERVO_PIN_1 }, { -1, SERVO_PIN_2 }, { -1, SERVO_PIN_3 }, { -1, SERVO_PIN_4 }, { -1, SERVO_PIN_5 }, { -1, SERVO_PIN_6 }
};
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
Serial.print(F("\nStarting NRF52_MultipleRandomServos on "));
Serial.println(BOARD_NAME);
Serial.println(NRF52_ISR_SERVO_VERSION);
for (int index = 0; index < NUM_SERVOS; index++)
{
ISR_servo[index].servoIndex = NRF52_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);
NRF52_ISR_Servos.setPosition(ISR_servo[index].servoIndex, 0);
}
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(NRF52_ISR_Servos.getPosition(ISR_servo[indexServo].servoIndex) );
Serial.print(F(", pulseWidth (us) = "));
Serial.println(NRF52_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++)
{
NRF52_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++)
{
NRF52_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++)
{
NRF52_ISR_Servos.setPosition(ISR_servo[index].servoIndex, position );
printServoInfo(index);
}
// waits 5s between test
delay(5000);
Serial.println(F("Servos sweeps from 0-180 degree"));
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++)
{
NRF52_ISR_Servos.setPosition(ISR_servo[index].servoIndex, position );
}
// waits 0.1s for the servo to reach the position
delay(100);
}
// waits 5s between test
delay(5000);
}