-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
190 lines (162 loc) · 5.59 KB
/
main.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
//author: Steph Herbers
#include "Adafruit_VL53L0X.h"
#include <Adafruit_MPU6050.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebSrv.h>
#include <ESP32Servo.h>
#include "WiFi.h"
#include <Wire.h>
// Define motor control pins
const int frontPWM = 13;
const int front1Direction = 5;
const int front2Direction = 4;
const int middlePWM = 12;
const int middle1Direction = 16;
const int middle2Direction = 17;
const int backPWM = 14;
const int back1Direction = 18;
const int back2Direction = 19;
const int frequency = 500;
const int pwm_channel = 0;
const int resolution = 8;
int dir_flag = 0;
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
int sda_pin=21, scl_pin=22; //LiDAR pins
Adafruit_MPU6050 mpu; //IMU setup
const char* ssid = "Tufts_Robot";
const char* password = "";
const char* param_servo = "servo";
const char* param_target = "target";
AsyncWebServer server(80);
void setup() {
Serial.begin(9600);
// Set the motor control pins as outputs
pinMode(frontPWM, OUTPUT);
pinMode(front1Direction, OUTPUT);
pinMode(front2Direction, OUTPUT);
digitalWrite(front1Direction, LOW);
digitalWrite(front2Direction, LOW);
pinMode(middlePWM, OUTPUT);
pinMode(middle1Direction, OUTPUT);
pinMode(middle2Direction, OUTPUT);
digitalWrite(middle1Direction, LOW);
digitalWrite(middle2Direction, LOW);
pinMode(backPWM, OUTPUT);
pinMode(back1Direction, OUTPUT);
pinMode(back2Direction, OUTPUT);
digitalWrite(back1Direction, LOW);
digitalWrite(back2Direction, LOW);
ledcSetup(pwm_channel, frequency, resolution);
ledcAttachPin(frontPWM, pwm_channel);
ledcAttachPin(middlePWM, pwm_channel);
ledcAttachPin(backPWM, pwm_channel);
//LiDAR Setup
Wire.begin(sda_pin, scl_pin);
if (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
while(1);
}
//IMU setup
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html", generateRemoteControlPage());
});
// Route for handling actions from buttons
server.on("/control", HTTP_POST, [](AsyncWebServerRequest *request){
String command;
if (request->hasParam("command", true)) {
command = request->getParam("command", true)->value();
// Perform action based on the received command
if (command == "forward") {
dir_flag = 1;
Serial.println("Moving forward");
} else if (command == "backward") {
dir_flag = 2;
Serial.println("Moving backward");
} else if (command == "stop") {
dir_flag = 0;
Serial.println("Stop");
}
}
request->send(200, "text/html", generateRemoteControlPage());
});
server.begin();
}
String generateRemoteControlPage() {
String remote_page = "<html><head><style>";
remote_page += "body { text-align: center; }";
remote_page += "button { font-size: 24px; padding: 15px 20px; margin: 10px; }";
remote_page += "</style></head><body>";
remote_page += "<h1 style='text-align: center'>DocOckBot Remote Control</h1>";
remote_page += "<div class='container' style='border: 20px solid black; text-align: center;'>";
remote_page += "<form action='/control' method='post'>";
remote_page += "<button type='submit' name='command' value='forward' style='font-size: 100px; width: 50%; border: 4px solid #04AA6D; margin:10px;'>⇧</button><br>";
remote_page += "<button type='submit' name='command' value='stop' style='font-size:100px; width: 50%; border: 4px solid red; margin:10px;'>✕</button><br>";
remote_page += "<button type='submit' name='command' value='backward' style='font-size: 100px; width: 50%; border: 4px solid #04AA6D; margin:10px;'>⇩</button><br>";
remote_page += "</div></form></body></html>";
return remote_page;
}
void controlFrontMotorsSpeed(int speed, bool forward) {
ledcWrite(pwm_channel, speed);
if (forward) {
digitalWrite(front1Direction, LOW); // Set direction to forward
digitalWrite(front2Direction, HIGH);
} else {
digitalWrite(front1Direction, HIGH); // Set direction to backwards
digitalWrite(front2Direction, LOW);
}
}
void controlMiddleMotorsSpeed(int speed, bool forward) {
ledcWrite(pwm_channel, speed);
if (forward) {
digitalWrite(middle1Direction, LOW);
digitalWrite(middle2Direction, HIGH);
} else {
digitalWrite(middle1Direction, HIGH);
digitalWrite(middle2Direction, LOW);
}
}
void controlBackMotorsSpeed(int speed, bool forward) {
ledcWrite(pwm_channel, speed);
if (forward) {
digitalWrite(back1Direction, LOW);
digitalWrite(back2Direction, HIGH);
} else {
digitalWrite(back1Direction, HIGH);
digitalWrite(back2Direction, LOW);
}
}
void loop() {
if (dir_flag == 1) {
controlFrontMotorsSpeed(255, true);
controlMiddleMotorsSpeed(255, true);
controlBackMotorsSpeed(255, true);
} else if (dir_flag == 2) {
controlFrontMotorsSpeed(255, false);
controlMiddleMotorsSpeed(255, false);
controlBackMotorsSpeed(255, false);
} else {
controlFrontMotorsSpeed(0, true);
controlMiddleMotorsSpeed(0, true);
controlBackMotorsSpeed(0, true);
}
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false);
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float distanceToStair = measure.RangeMilliMeter; // lidar output in mm
}