-
Notifications
You must be signed in to change notification settings - Fork 0
/
angkaewone-v5.ino
560 lines (470 loc) · 13.4 KB
/
angkaewone-v5.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#include <ESP32Servo.h>
#include <MPU6050_tockn.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <WiFi.h>
#include <MQTT.h>
#include <math.h>
#define bldc1 4
#define bldc2 5
#define in1 12
#define in2 14
// Define a structure to hold point coordinates
struct Point {
float x = 0;
float y = 0;
float x2 = 0;
float y2 = 0;
};
//define for Navigation
int AngletoPoint1 = 0;
int AngletoPoint2 = 0;
int AngletoPoint3 = 0;
int Angletoback = 0;
int distance1 = 0;
int distance2 = 0;
int distance3 = 0;
int distancetoback = 0;
//define Pump
const int pump_pin1 = 15;
const int pump_pin2 = 16;
const int pump_pin3 = 17;
long timer_bldc = 0;
int throttleValue1 = 180;
int throttleValue2 = 180;
int reverse1 = LOW;
int reverse2 = LOW;
Servo ESC1;
Servo ESC2;
//Wifi Form Phone
const char ssid[] = "Ok";
const char pass[] = "q12345678";
//MQTT Server Data
const char mqtt_broker[] = "test.mosquitto.org";
const char mqtt_topic[] = "angkaewone/1";
const char mqtt_topictwo[] = "angkaewtwo/1";
const char mqtt_client_id[] = "arduino_group_x"; // must change this string to a unique value
int MQTT_PORT = 1883;
int counter = 0;
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
//messageReceived Variable coordinates
// Define the points with unique variable names
float x_start = 0, y_start = 0;
float x_point1 = 0, y_point1 = 0;
float x_point2 = 0, y_point2 = 0;
float x_point3 = 0, y_point3 = 0;
int x_point = 0, y_point = 0;
//start
Point start_point;
float backhome = 180;
//boolean Start
bool startEngine = false;
bool point1topoint2 = false;
bool point2topoint3 = false;
bool point3tostartEngine = false;
int timerCounting = 0;
bool startCalculate = false;
bool home = false;
char buf1[100], buf2[100], buf3[100], buf4[100], buf5[100], buf6[100];
char result1[10], result2[10], result3[10], result4[10], result5[10], result6[10];
Point coordinates[3];
int payloadCount;
int currentIntIndex = 0;
//Gyro Variable
MPU6050 mpu6050(Wire);
int constant_angle = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
long timer = 0;
//Define for PID Using Function
int angle;
int rightPIDPosition;
int leftPIDPosition;
float extractNumber(String &str);
// Define the throttle range
const float MIN_THROTTLE = 6.0;
const float MAX_THROTTLE = 12.0;
// Function to calculate motor throttle based on angle
void calculateThrottle(float angle, float &rightThrottle, float &leftThrottle) {
// Ensure the angle is within the range -360 to 360
if (angle < -360) angle = -360;
if (angle > 360) angle = 360;
// Map the angle to throttle values
if (angle >= 0) {
// Positive angle: increase right motor throttle, decrease left motor throttle
rightThrottle = map(angle, 0, 360, MIN_THROTTLE, MAX_THROTTLE);
leftThrottle = map(angle, 0, 360, MAX_THROTTLE, MIN_THROTTLE);
} else {
// Negative angle: decrease right motor throttle, increase left motor throttle
rightThrottle = map(angle, -360, 0, MAX_THROTTLE, MIN_THROTTLE);
leftThrottle = map(angle, -360, 0, MIN_THROTTLE, MAX_THROTTLE);
}
}
void connect() {
lcd.setCursor(0, 0);
lcd.print("checking wifi...");
Serial.print("checking wifi...");
lcd.setCursor(15, 0);
while (WiFi.status() != WL_CONNECTED) {
lcd.print(".");
delay(1000);
}
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("connecting...");
Serial.print("\nconnecting...");
lcd.setCursor(15, 0);
while (!client.connect(mqtt_client_id)) {
lcd.print(".");
delay(1000);
}
lcd.clear();
lcd.print("connected!");
Serial.println("\nconnected!");
delay(500);
client.subscribe(mqtt_topic);
// client.unsubscribe("/hello");
}
void messageReceived(String &topic, String &payload) {
//store from payload to coordinates array
currentIntIndex = currentIntIndex % 3;
if (payload) {
payload.toCharArray(buf1, 100);
payload.toCharArray(buf2, 100);
StrContains(buf1, "x", result1);
StrContains(buf2, "y", result2);
Serial.print("messageReceivedCounting : ");
Serial.println(currentIntIndex);
if (strcmp(result1, "Found") == 0) {
x_point = extractNumber(payload);
coordinates[currentIntIndex].x = x_point/100.0;
Serial.print("Stored X: ");
Serial.println(coordinates[currentIntIndex].x);
}
if (strcmp(result2, "Found") == 0) {
y_point = extractNumber(payload);
coordinates[currentIntIndex].y = y_point/100.0;
Serial.print("Stored Y: ");
Serial.println(coordinates[currentIntIndex].y);
}
currentIntIndex++;
}
if (currentIntIndex == 1) {
Serial.println("Array is full. Cannot store more strings.");
client.publish(mqtt_topictwo, "FULL.");
for (int i = 0; i < currentIntIndex; i++) {
Serial.print("point ");
Serial.print(i);
Serial.print(" >> ");
Serial.print(coordinates[i].x);
Serial.print(", ");
Serial.println(coordinates[i].y);
}
startEngine = true;
startCalculate = true;
}
}
float extractNumber(String &str) {
String numberStr = ""; // Store the extracted number as a string
int number = 0; // Store the extracted number as a float
bool foundNumber = false; // Flag to track if a number has been found
bool hasDecimal = false; // Flag to track if a decimal point has been found
int charCount = 0;
// Iterate through each character of the string
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
charCount++;
// Check if the character is a digit or a decimal point
if (isdigit(c) || (c == '.' && !hasDecimal)) {
numberStr += c; // Append digit or decimal point to the number string
foundNumber = true; // Set the flag indicating a number has been found
if (c == '.') {
hasDecimal = true; // Set the flag indicating a decimal point has been found
}
} else if (foundNumber) {
// If a number has been found and we encounter a non-digit/non-decimal character,
// break out of the loop
break;
}
}
// Convert the number string to a Int
number = numberStr.toInt();
// Remove the extracted number from the original string
if (foundNumber) {
str.remove(0, charCount);
}
return number; // Return the extracted number
}
void StrContains(char *str, const char *sfind, char *result) {
int found = 0;
int index = 0;
int len = strlen(str);
int sfindLen = strlen(sfind);
if (sfindLen > len) {
strcpy(result, "NotFound");
return;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (sfindLen == found) {
strcpy(result, "Found");
return;
}
} else {
found = 0;
}
index++;
}
strcpy(result, "NotFound");
}
// Function to calculate distance
float calculateDistance(float x1, float y1, float x2, float y2) {
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
// Function to calculate angle in degrees
float calculateAngle(float x1, float y1, float x2, float y2) {
if(x2 - x1 < 0 || y2 - y1 < 0){
Serial.println("negativvvvvvvvvv");
float result = atan2(y2 - y1, x2 - x1) * (180.0 / PI);
result = result/(-1.0);
return result ;
}
else{
return atan2(y2 - y1, x2 - x1) * (180.0 / PI);
}
}
// ------------------------------------- MAIN SET UP --------------------------------------------------//
void setup() {
Serial.begin(15200);
Wire.begin();
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
pinMode(pump_pin1, OUTPUT);
pinMode(pump_pin2, OUTPUT);
pinMode(pump_pin3, OUTPUT);
digitalWrite(pump_pin1, LOW);
digitalWrite(pump_pin2, LOW);
digitalWrite(pump_pin3, LOW);
//Brushless setup
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
startEngine = false;
point1topoint2 = false;
point2topoint3 = false;
point3tostartEngine = false;
startCalculate = false;
lcd.setCursor(2, 0);
lcd.print("Setup BLDC ...");
ESC1.attach(bldc1, 1000, 2000);
ESC2.attach(bldc2, 1000, 2000);
ESC1.write(0);
ESC2.write(0);
delay(5000);
throttleValue1 = 0;
throttleValue2 = 0;
lcd.setCursor(2, 1);
lcd.print("Complete.");
delay(500);
lcd.clear();
Wire.begin();
mpu6050.begin();
lcd.setCursor(0, 0);
lcd.print("Calculating gyro");
lcd.setCursor(0, 1);
lcd.print("DO NOT MOVE MPU.");
mpu6050.calcGyroOffsets(true);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Program Will ");
lcd.setCursor(0, 1);
lcd.print("Start in 3 Sec..");
delay(3000);
lcd.clear();
mpu6050.update();
Serial.print("\tangleZ : ");
Serial.println(mpu6050.getAngleZ());
constant_angle = mpu6050.getAngleZ();
timer = millis();
lcd.clear();
start_point.x = 0.0;
start_point.y = 0.0;
WiFi.begin(ssid, pass);
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
// by Arduino. You need to set the IP address directly.
client.begin(mqtt_broker, MQTT_PORT, net);
client.onMessage(messageReceived);
connect();
}
void loop() {
client.loop();
delay(10); // <- fixes some issues with WiFi stability
if (!client.connected()) {
connect();
}
// publish a message roughly every second.
// not that we don't use delay() because we need to keep calling the client.loop()
// to keep the connection alive
if (millis() - lastMillis > 1000) {
lastMillis = millis();
}
lcd.clear();
lcd.print("Waiting");
lcd.setCursor(0, 1);
lcd.print("For Input ");
client.publish(mqtt_topictwo, "Ready for Input");
while (!input) {
Serial.println("waiting for input..");
client.loop();
delay(10); // <- fixes some issues with WiFi stability
if (!client.connected()) {
connect();
}
}
lcd.setCursor(0, 1);
lcd.print("Calculating");
//client.publish(mqtt_topictwo, "Calculating Angle and Distance");
home = false;
float distance1 = calculateDistance(start_point.x, start_point.y, coordinates[0].x, coordinates[0].y);
float AngletoPoint1 = calculateAngle(start_point.x, start_point.y, coordinates[0].x, coordinates[0].y);
if(coordinates[0].x - start_point.x < 0 ||coordinates[0].y - start_point.y < 0 ){
AngletoPoint1 = (-1)*AngletoPoint1;
}
Serial.print("angle Point 1 : ");
Serial.println(String(AngletoPoint1));
Serial.print("distance Point 1 : ");
Serial.println(distance1);
delay(1000);
lcd.clear();
lcd.print("Calculatie Succes");
//client.publish(mqtt_topictwo, "Calculating Complete");
while (startEngine == true) {
//PID Function Setting
lcd.setCursor(0, 1);
lcd.print("Point 1 : ");
lcd.setCursor(10, 1);
lcd.print(AngletoPoint1);
//client.publish(mqtt_topictwo, "Navigating from start to point 1");
rightPIDPosition = map(angle, 0, AngletoPoint1, 12, 6);
leftPIDPosition = map(angle, AngletoPoint1, 0, 6, 12);
if (angle < AngletoPoint1 ) {
Serial.print("Brushless Right on at :");
Serial.println(rightPIDPosition);
ESC2.write(rightPIDPosition);
}
else if (angle > AngletoPoint1) {
Serial.print("Brushless Left on at :");
Serial.println(leftPIDPosition);
ESC1.write(leftPIDPosition);
}
if (angle >= AngletoPoint1 -5 && angle <= AngletoPoint1 + 5) {
startEngine = false;
if (angle < AngletoPoint1 ) {
ESC1.write(8);
delay(1000);
ESC1.write(0);
}
else if (angle > AngletoPoint1) {
ESC2.write(8);
delay(1000);
ESC2.write(0);
}
ESC1.write(6);
ESC2.write(6);
delay(5000);
ESC1.write(0);
ESC2.write(0);
breaked();
// collecting water
lcd.setCursor(0, 0);
lcd.print("Collecting Water..");
for(int k = 0; k < 10; k++){
digitalWrite(pump_pin1,HIGH);
delay(2000);
digitalWrite(pump_pin1,LOW);
delay(500);
}
lcd.clear();
delay(1000);
// HOME COMING
lcd.setCursor(0, 0);
lcd.print("HOMING!");
homing();
delay(1000);
lcd.clear();
}
realTimeAngle();
}
delay(1);
}
// ------------------------------------------------ FUNCTION ------------------------------------------------------------------- //
void homing(){
while(!home){
angle = 0;
//PID Function Setting
rightPIDPosition = map(angle, 0, backhome, 12, 6);
leftPIDPosition = map(angle, backhome, 0, 6, 12);
if (angle < backhome ) {
Serial.print("Brushless Right on at :");
Serial.println(rightPIDPosition);
ESC2.write(rightPIDPosition);
} else {
ESC2.write(0);
}
if (angle > backhome) {
Serial.print("Brushless Left on at :");
Serial.println(leftPIDPosition);
ESC1.write(leftPIDPosition);
} else {
ESC1.write(0);
}
if (angle >= backhome -5 && angle <= backhome + 5) {
ESC1.write(6);
ESC2.write(6);
delay(5000);
ESC1.write(0);
ESC2.write(0);
breaked();
home = true;
}
realTimeAngle();
}
}
void breaked(){
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
delay(20);
ESC1.write(8);
ESC1.write(8);
delay(2000);
ESC1.write(0);
ESC1.write(0);
delay(20);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
delay(500);
}
void realTimeAngle() {
mpu6050.update();
// Serial.print("angleX : ");
// Serial.print(mpu6050.getAngleX());
// Serial.print("\tangleY : ");
// Serial.print(mpu6050.getAngleY());
if (millis() - timer > 1000) {
timer = millis();
lcd.clear();
}
//angle position
lcd.setCursor(0, 0);
lcd.print("angle : ");
angle = (int)(mpu6050.getAngleZ() - constant_angle);
angle = angle % 360;
// if (angle < 0) {
// angle = 360 + angle;
// }
lcd.print(angle);
}