-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.ino
324 lines (264 loc) · 6.05 KB
/
core.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "ESP8266WiFi.h";
#include "ESP8266WebServer.h";
#include "ArduinoJson.h";
#include "Servo.h";
#include <GoProControl.h>;
#define CAMERA HERO4
const char *ssid = "JOAO__.2.4 G";
const char *password = "159683247";
const char *GOPRO_SSID = "bmwixespm";
const char *GOPRO_PASS = "goprohero4";
GoProControl gp(GOPRO_SSID, GOPRO_PASS, CAMERA);
ESP8266WebServer server(80);
const int SERVO_PIN = D1;
const int MOTOR_CC_PIN1 = D6;
const int MOTOR_CC_PIN2 = D7;
const int LED = D0;
Servo steeringServo;
int currentAngle = 90;
const int ANGLE_INCREMENT = 5;
void setup() {
Serial.begin(115200);
gp.enableDebug(&Serial);
digitalWrite(LED, HIGH);
pinMode(MOTOR_CC_PIN1, OUTPUT);
pinMode(MOTOR_CC_PIN2, OUTPUT);
steeringServo.attach(SERVO_PIN);
// Centraliza o servo no início com movimentos rápidos
for (int angle = currentAngle; angle != 90; ) {
if (currentAngle > 90) {
angle = max(90, angle - 10); // Movendo para o centro em passos maiores
} else {
angle = min(90, angle + 10); // Movendo para o centro em passos maiores
}
steeringServo.write(angle);
delay(30); // Aumenta a velocidade
}
currentAngle = 90;
pinMode(MOTOR_CC_PIN1, OUTPUT);
pinMode(MOTOR_CC_PIN2, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando ao WiFi...");
}
Serial.println("Conectado ao WiFi");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
server.on("/move", HTTP_POST, handleMove);
server.begin();
Serial.println("Servidor HTTP iniciado");
}
void loop()
{
server.handleClient();
char in = 0;
if (Serial.available() > 0)
{
in = Serial.read();
}
switch (in)
{
default:
break;
// Connect
case 'b':
gp.begin();
break;
case 'c':
Serial.print("Connected: ");
Serial.println(gp.isConnected() == true ? "Yes" : "No");
break;
case 'p':
gp.confirmPairing();
break;
case 's':
if (CAMERA == HERO3)
{
char *statusChar;
statusChar = gp.getStatus();
Serial.println("Status :");
for (int i = 0; i < 56; i++)
{
Serial.print(statusChar[i], HEX);
Serial.print(" ");
}
Serial.println("");
Serial.println("End Status.");
if (statusChar[0] == 0x00)
{
Serial.println("camera ON");
}
else
{
Serial.println("camera OFF");
}
free(statusChar); // Don't forget to free memory
}
else
{
char *statusChar;
statusChar = gp.getStatus();
Serial.println("Status :");
Serial.println(statusChar);
free(statusChar); // Don't forget to free memory
}
break;
case 'm': // DO NOT USE WHEN CAMERA IS OFF, IT FREEZE ESP
char *medialist;
medialist = gp.getMediaList();
Serial.println("Media List:");
Serial.println(medialist);
free(medialist); // Don't forget to free memory
break;
// Turn on and off
case 'T':
gp.turnOn();
break;
case 't':
gp.turnOff();
break;
// Take a picture of start a video
case 'A':
gp.shoot();
break;
// Stop the video
case 'S':
gp.stopShoot();
break;
// Check if it is recording
case 'r':
Serial.print("Recording: ");
Serial.println(gp.isRecording() == true ? "Yes" : "No");
break;
// Set modes
case 'V':
gp.setMode(VIDEO_MODE);
break;
case 'P':
gp.setMode(PHOTO_MODE);
break;
case 'M':
gp.setMode(MULTISHOT_MODE);
break;
// Change the orientation
case 'U':
gp.setOrientation(ORIENTATION_UP);
break;
case 'D':
gp.setOrientation(ORIENTATION_DOWN);
break;
// Change other parameters
case 'f':
gp.setVideoFov(MEDIUM_FOV);
break;
case 'F':
gp.setFrameRate(FR_120);
break;
case 'R':
gp.setVideoResolution(VR_1080p);
break;
case 'h':
gp.setPhotoResolution(PR_12MP_WIDE);
break;
case 'L':
gp.setTimeLapseInterval(60);
break;
// Localize the camera
case 'O':
gp.localizationOn();
break;
case 'o':
gp.localizationOff();
break;
// Delete some files, be carefull!
case 'l':
gp.deleteLast();
break;
case 'g':
gp.deleteAll();
break;
// Print useful data
case 'd':
gp.printStatus();
break;
// Close the connection
case 'X':
gp.end();
break;
}
gp.keepAlive(); // not needed on HERO3
}
void handleMove()
{
if (server.hasArg("plain"))
{
String body = server.arg("plain");
DynamicJsonDocument doc(1024);
deserializeJson(doc, body);
String direction = doc["direction"];
int speed = doc["speed"];
if (direction == "forward")
{
moveForward(speed);
}
else if (direction == "backward")
{
moveBackward(speed);
}
else if (direction == "left")
{
turnLeft();
}
else if (direction == "right")
{
turnRight();
}
else if (direction == "stop")
{
stopMotors();
}
else if (direction == "center")
{
centerSteering();
}
server.send(200, "application/json", "{\"status\":\"success\",\"currentAngle\":" + String(currentAngle) + "}");
}
else
{
server.send(400, "application/json", "{\"status\":\"error\",\"message\":\"No data received\"}");
}
}
void moveForward(int speed) {
analogWrite(MOTOR_CC_PIN1, constrain(speed, 0, 1023));
digitalWrite(MOTOR_CC_PIN2, LOW);
}
void moveBackward(int speed)
{
digitalWrite(MOTOR_CC_PIN1, LOW);
analogWrite(MOTOR_CC_PIN2, speed);
}
void turnLeft() {
if (currentAngle > 45) {
currentAngle -= 10; // Incremento maior para mais velocidade
steeringServo.write(currentAngle);
Serial.println("Turning left, angle: " + String(currentAngle));
}
}
void turnRight() {
if (currentAngle < 135) {
currentAngle += 10; // Incremento maior para mais velocidade
steeringServo.write(currentAngle);
Serial.println("Turning right, angle: " + String(currentAngle));
}
}
void centerSteering()
{
currentAngle = 90;
steeringServo.write(currentAngle);
}
void stopMotors()
{
digitalWrite(MOTOR_CC_PIN1, LOW);
digitalWrite(MOTOR_CC_PIN2, LOW);
}