forked from jamro/tiny-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpca9685_servos.cpp
More file actions
132 lines (99 loc) · 2.07 KB
/
Copy pathpca9685_servos.cpp
File metadata and controls
132 lines (99 loc) · 2.07 KB
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
#include <Arduino.h>
#include "pins.h"
#include "servos.h"
#include "display/oled.h"
#include "hardware/rgb.h"
#include "hardware/servo_wrapper.h"
#include "hardware/pca9685_servos.h"
#include "serial_log.h"
bool pca9685Connected() {
return i2cDeviceConnected(PCA9685_ADDRESS);
}
void initPca9685() {
serialLogPrintln();
serialLogPrintln("Checking PCA9685 at 0x40...");
if (!pca9685Connected()) {
serialLogPrintln("ERROR: PCA9685 not found");
showOledText(
"PCA9685 ERROR",
"Not found"
);
setRgb(64, 0, 0);
while (true) {
delay(1000);
}
}
serialLogPrintln("PCA9685 found");
initServoPwmDriver();
}
bool moveServoSmooth(int index, float toAngle) {
if (index < 0 || index >= SERVO_COUNT) {
return false;
}
return servoAt(index).moveTo(toAngle);
}
void centerAllServos() {
serialLogPrintln("Centering servos to mid (min+max)/2");
float targets[SERVO_COUNT];
for (int servo = 0; servo < SERVO_COUNT; servo++) {
targets[servo] = servoMid(SERVO_SPECS[servo]);
}
servoMoveAllSmoothTo(targets);
}
void runServoTest() {
constexpr int TOTAL_STEPS = 4;
serialLogPrintln();
serialLogPrintln("==========================");
serialLogPrintln("SERVO TEST - HEAD/NECK/HANDS/BODY");
serialLogPrintln("==========================");
serialLogPrintln(
"1. All servos -> 90 deg"
);
showServoProgress(
1,
TOTAL_STEPS,
"ALL -> 90"
);
servoMoveAllSmooth(
SERVO_CENTER
);
delay(1000);
serialLogPrintln(
"2. All servos: 90 -> 105 deg"
);
showServoProgress(
2,
TOTAL_STEPS,
"90 -> 105"
);
servoMoveAllSmooth(
SERVO_HIGH
);
serialLogPrintln(
"3. All servos: 105 -> 75 deg"
);
showServoProgress(
3,
TOTAL_STEPS,
"105 -> 75"
);
servoMoveAllSmooth(
SERVO_LOW
);
serialLogPrintln(
"4. All servos: 75 -> 90 deg"
);
showServoProgress(
4,
TOTAL_STEPS,
"75 -> 90"
);
servoMoveAllSmooth(
SERVO_CENTER
);
showServoTestFinished();
serialLogPrintln(
"All 5 servos test finished"
);
delay(1000);
}