-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild18CatEars.ino
259 lines (228 loc) · 5.64 KB
/
Build18CatEars.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
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* @file Build18CatEars.ino
*
* @brief Arduino sketch to control pan-tilt servo motors that move cat ears
* according to user's brain activity
*
* States
* - Relaxed: Ears drooped down
* - Neutral: Ears perked up
* - Excited: Ears wiggle around
* */
#include <Servo.h>
// Macros
#define L_PAN_PIN 10
#define L_TILT_PIN 11
#define R_PAN_PIN 5
#define R_TILT_PIN 6
#define BAUDRATE 115200
// Neutral Position
#define L_T_NEUTRAL 60
#define R_T_NEUTRAL 60
#define L_P_NEUTRAL 90
#define R_P_NEUTRAL 90
#define MAX_COUNT 32767
// Enum for speed
typedef enum { FAST, SLOW } speed_t;
// Create servo objects for each servo
Servo l_pan;
Servo l_tilt;
Servo r_pan;
Servo r_tilt;
// Variables
int l_t_pos;
int r_t_pos;
int l_p_pos;
int r_p_pos;
int state = 0; // Tracks the state. 0 = Relaxed, 1 = Neutral, 2 = Excited
int count = 0; // Counts the number of times no new status has been sent
void setup() {
// Initialize pins
l_pan.attach(L_PAN_PIN);
l_tilt.attach(L_TILT_PIN);
r_pan.attach(R_PAN_PIN);
r_tilt.attach(R_TILT_PIN);
// Initialize servo positions
updatePosition(L_T_NEUTRAL, R_T_NEUTRAL, L_P_NEUTRAL, R_P_NEUTRAL);
l_tilt.write(l_t_pos);
r_tilt.write(r_t_pos);
l_pan.write(l_p_pos);
r_pan.write(r_p_pos);
delay(1000);
// // Test movements on startup
// wiggleEars(3);
// delay(1000);
// droopEars();
// delay(1000);
// droopWiggle();
// delay(1000);
// neutralEars();
// delay(1000);
// neutralWiggle();
// delay(1000);
count = 0;
state = 0;
// Begin Serial Communication
Serial.begin(BAUDRATE);
Serial.print("Connecting to serial port...");
Serial.print("\n");
}
void loop() {
// Data received from serial port
if (Serial.available()){
count = 0;
Serial.print("Data received: ");
String data = Serial.readStringUntil('\n');
Serial.print(data);
Serial.print("\n");
float val = data.toInt();
if (val == 2) {
state = 2;
wiggleEars(3);
}
else if (val ==1) {
state = 1;
// Move ears to neutral position
neutralEars();
}
else if (val == 0) {
state = 0;
// Droop ears accoringly
droopEars();
}
delay(500);
}
else {
count += 1;
if (count == MAX_COUNT) {
count = 0;
if (state == 2) {
state = 2;
// wiggleEars(3);
}
else if (state ==1) {
// Move ears to neutral position
// neutralWiggle();
}
else if (state == 0) {
// Droop ears accoringly
// droopWiggle();
}
}
}
}
/**
* @brief Sets the position variables to the input positions
*
* @param[in] lt Angle position for left tilt servo
* @param[in] rt Angle position for right tilt servo
* @param[in] lp Angle position for left pan servo
* @param[in] rp Angle position for right pan servo
*/
void updatePosition(int lt, int rt, int lp, int rp) {
l_t_pos = lt;
r_t_pos = rt;
l_p_pos = lp;
r_p_pos = rp;
}
/**
* @brief Get the dif to move servos by
*
* @param[in] start_pos Starting angle
* @param[in] end_pos Ending angle
* @return 1, -1, or 0 depending on the difference between start and end
*/
int dif(int start_pos, int end_pos) {
if (start_pos < end_pos) {
return 1;
}
if (start_pos == end_pos) {
return 0;
}
return -1;
}
/**
* @brief Sweeps servos to desired position
*
* @param[in] lt Angle position for left tilt servo
* @param[in] rt Angle position for right tilt servo
* @param[in] lp Angle position for left pan servo
* @param[in] rp Angle position for right pan servo
* @param[in] speed Speed of movement
*/
void sweepToPosition(int lt, int rt, int lp, int rp, speed_t speed) {
int d_lp = dif(l_p_pos, lp);
int d_lt = dif(l_t_pos, lt);
int d_rp = dif(r_p_pos, rp);
int d_rt = dif(r_t_pos, rt);
while (l_t_pos!=lt || r_t_pos!=rt || l_p_pos!=lp || r_p_pos!=rp) {
// Update positions
if (l_t_pos!=lt) l_t_pos = l_t_pos + d_lt;
if (r_t_pos!=rt) r_t_pos = r_t_pos + d_rt;
if (l_p_pos!=lp) l_p_pos = l_p_pos + d_lp;
if (r_p_pos!=rp) r_p_pos = r_p_pos + d_rp;
l_tilt.write(l_t_pos);
r_tilt.write(r_t_pos);
l_pan.write(l_p_pos);
r_pan.write(r_p_pos);
if (speed == FAST) {
delay(1);
}
else {
delay(12);
}
}
}
/**
* @brief Sets the servo positions to wiggle ears a certain amount of times
*
* @param[in] count Number of times to wiggle ears
*/
void wiggleEars(int count) {
for (int i = 0; i < count; i++) {
sweepToPosition(40, 40, 50, 130, FAST);
delay(150);
sweepToPosition(60, 60, 90, 90, FAST);
delay(150);
}
}
/**
* @brief Sets the servo positions to droop ears down when relaxed
*/
void droopEars() {
sweepToPosition(100, 100, 30, 150, SLOW);
delay(100);
}
/**
* @brief Wiggles ears when drooped
*/
void droopWiggle() {
sweepToPosition(90, 90, 40, 140, SLOW);
delay(100);
sweepToPosition(100, 100, 40, 140, SLOW);
delay(100);
sweepToPosition(90, 90, 40, 140, SLOW);
delay(100);
sweepToPosition(100, 100, 40, 140, SLOW);
delay(100);
}
/**
* @brief Sets the servo positions to the neutral position
*/
void neutralEars() {
sweepToPosition(L_T_NEUTRAL, R_T_NEUTRAL, L_P_NEUTRAL, R_P_NEUTRAL, SLOW);
delay(100);
}
/**
* @brief Sets the servo positions to the neutral position
*/
void neutralWiggle() {
sweepToPosition(L_T_NEUTRAL + 10, R_T_NEUTRAL + 10, L_P_NEUTRAL, R_P_NEUTRAL, SLOW);
delay(100);
sweepToPosition(L_T_NEUTRAL, R_T_NEUTRAL, L_P_NEUTRAL, R_P_NEUTRAL, SLOW);
delay(100);
sweepToPosition(L_T_NEUTRAL + 10, R_T_NEUTRAL + 10, L_P_NEUTRAL, R_P_NEUTRAL, SLOW);
delay(100);
sweepToPosition(L_T_NEUTRAL, R_T_NEUTRAL, L_P_NEUTRAL, R_P_NEUTRAL, SLOW);
delay(100);
}