-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRobotic_Arm.ino
443 lines (399 loc) · 13.4 KB
/
Robotic_Arm.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
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESP32Servo.h>
#include <iostream>
#include <sstream>
struct ServoPins
{
Servo servo;
int servoPin;
String servoName;
int initialPosition;
};
std::vector<ServoPins> servoPins =
{
{ Servo(), 18 , "Base", 90},
{ Servo(), 23 , "Shoulder", 90},
{ Servo(), 21 , "Elbow", 90},
{ Servo(), 22 , "Gripper", 90},
};
struct RecordedStep
{
int servoIndex;
int value;
int delayInStep;
};
std::vector<RecordedStep> recordedSteps;
bool recordSteps = false;
bool playRecordedSteps = false;
unsigned long previousTimeInMilli = millis();
const char* ssid = "RobotArm";
const char* password = "12345678"; //The IP address to connect to the web interface - http://192.168.4.1
AsyncWebServer server(80);
AsyncWebSocket wsRobotArmInput("/RobotArmInput");
const char* htmlHomePage PROGMEM = R"HTMLHOMEPAGE(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<style>
body {
background: linear-gradient(145deg, #ededed, #626262, #313131);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
min-height: 100vh;
align-items: center;
justify-content: center;
padding: 20px;
}
input[type=button]
{
background-color:rgb(255, 81, 0);color:rgb(0, 0, 0);border-radius:30px;width:100%;height:40px;font-size:20px;text-align:center;
}
.noselect {
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Safari */
-khtml-user-select: none; /* Konqueror HTML */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently
supported by Chrome and Opera */
}
.slidecontainer {
width: 100%;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 20px;
border-radius: 15px;
outline: none;
opacity: 0.6;
-webkit-transition: .2s;
transition: opacity .2s;
transition: opacity 0.3s ease, transform 0.3s ease, box-shadow 0.3s ease, background-color 0.3s ease;
background-color: #3a474c;
}
.slider:hover {
opacity: 1;
transform: scale(1.05);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
background-color: #ff5733;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 30px;
height: 30px;
border-radius: 50%;
background: rgb(0, 51, 255);
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 40px;
height: 40px;
border-radius: 50%;
background: red;
cursor: pointer;
}
</style>
</head>
<div class="container">
<body class="noselect" align="center" style="background-color:white">
<h1 style="color: rgb(0, 38, 255);text-align:center;">DRIEMS Polytechnic</h1>
<h3><b>CSE Group-8, Robotic Arm</b></h3>
<h2 style="color: rgb(255, 0, 0);text-align:center;">Control Panel</h2>
<table id="mainTable" style="width:380px;margin:auto;table-layout:fixed" CELLSPACING=10>
<tr/><tr/>
<tr/><tr/>
<tr>
<td style="text-align:left;font-size:25px"><b>Gripper:</b></td>
<td colspan=2>
<div class="slidecontainer">
<input type="range" min="0" max="180" value="90" class="slider" id="Gripper" oninput='sendButtonInput("Gripper",value)'>
</div>
</td>
</tr>
<tr/><tr/>
<tr>
<td style="text-align:left;font-size:25px"><b>Elbow:</b></td>
<td colspan=2>
<div class="slidecontainer">
<input type="range" min="0" max="180" value="90" class="slider" id="Elbow" oninput='sendButtonInput("Elbow",value)'>
</div>
</td>
</tr>
<tr/><tr/>
<tr>
<td style="text-align:left;font-size:25px"><b>Shoulder:</b></td>
<td colspan=2>
<div class="slidecontainer">
<input type="range" min="0" max="180" value="90" class="slider" id="Shoulder" oninput='sendButtonInput("Shoulder",value)'>
</div>
</td>
</tr>
<tr/><tr/>
<tr>
<td style="text-align:left;font-size:25px"><b>Base:</b></td>
<td colspan=2>
<div class="slidecontainer">
<input type="range" min="0" max="180" value="90" class="slider" id="Base" oninput='sendButtonInput("Base",value)'>
</div>
</td>
</tr>
<tr/><tr/>
<tr>
<td style="text-align:left;font-size:20px"><b>Record:</b></td>
<td><input type="button" id="Record" value="OFF" ontouchend='onclickButton(this)'></td>
<td></td>
</tr>
<tr/><tr/>
<tr>
<td style="text-align:left;font-size:20px"><b>Play:</b></td>
<td><input type="button" id="Play" value="OFF" ontouchend='onclickButton(this)'></td>
<td></td>
</tr>
</table>
<script>
var webSocketRobotArmInputUrl = "ws:\/\/" + window.location.hostname + "/RobotArmInput";
var websocketRobotArmInput;
function initRobotArmInputWebSocket()
{
websocketRobotArmInput = new WebSocket(webSocketRobotArmInputUrl);
websocketRobotArmInput.onopen = function(event){};
websocketRobotArmInput.onclose = function(event){setTimeout(initRobotArmInputWebSocket, 2000);};
websocketRobotArmInput.onmessage = function(event)
{
var keyValue = event.data.split(",");
var button = document.getElementById(keyValue[0]);
button.value = keyValue[1];
if (button.id == "Record" || button.id == "Play")
{
button.style.backgroundColor = (button.value == "ON" ? "green" : "red");
enableDisableButtonsSliders(button);
}
};
}
function sendButtonInput(key, value)
{
var data = key + "," + value;
websocketRobotArmInput.send(data);
}
function onclickButton(button)
{
button.value = (button.value == "ON") ? "OFF" : "ON" ;
button.style.backgroundColor = (button.value == "ON" ? "green" : "red");
var value = (button.value == "ON") ? 1 : 0 ;
sendButtonInput(button.id, value);
enableDisableButtonsSliders(button);
}
function enableDisableButtonsSliders(button)
{
if(button.id == "Play")
{
var disabled = "auto";
if (button.value == "ON")
{
disabled = "none";
}
document.getElementById("Gripper").style.pointerEvents = disabled;
document.getElementById("Elbow").style.pointerEvents = disabled;
document.getElementById("Shoulder").style.pointerEvents = disabled;
document.getElementById("Base").style.pointerEvents = disabled;
document.getElementById("Record").style.pointerEvents = disabled;
}
if(button.id == "Record")
{
var disabled = "auto";
if (button.value == "ON")
{
disabled = "none";
}
document.getElementById("Play").style.pointerEvents = disabled;
}
}
window.onload = initRobotArmInputWebSocket;
document.getElementById("mainTable").addEventListener("touchend", function(event){
event.preventDefault()
});
</script>
"Our project involves designing and developing a robotic arm powered by the ESP32 microcontroller. The arm is programmed for precision control and task automation, incorporating advanced features such as wireless connectivity for real-time command execution. This project demonstrates expertise in robotics, IoT integration, and microcontroller programming."<b>-Guided By: Jyoti Ranjan Nayak</b></div>
<div style="text-align:left;">
<b>Team Members:</b>
<li>K Rajtilak</li>
<li>Bikram Keshari Dash</li>
<li>MD Riyasat Ali</li>
<li>Gargee Dash</li>
<li>Biswajeet Muduli</li>
</div>
</body>
</html>
)HTMLHOMEPAGE";
void handleRoot(AsyncWebServerRequest *request)
{
request->send_P(200, "text/html", htmlHomePage);
}
void handleNotFound(AsyncWebServerRequest *request)
{
request->send(404, "text/plain", "File Not Found");
}
void onRobotArmInputWebSocketEvent(AsyncWebSocket *server,
AsyncWebSocketClient *client,
AwsEventType type,
void *arg,
uint8_t *data,
size_t len)
{
switch (type)
{
case WS_EVT_CONNECT:
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
sendCurrentRobotArmState();
break;
case WS_EVT_DISCONNECT:
Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
AwsFrameInfo *info;
info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT)
{
std::string myData = "";
myData.assign((char *)data, len);
std::istringstream ss(myData);
std::string key, value;
std::getline(ss, key, ',');
std::getline(ss, value, ',');
Serial.printf("Key [%s] Value[%s]\n", key.c_str(), value.c_str());
int valueInt = atoi(value.c_str());
if (key == "Record")
{
recordSteps = valueInt;
if (recordSteps)
{
recordedSteps.clear();
previousTimeInMilli = millis();
}
}
else if (key == "Play")
{
playRecordedSteps = valueInt;
}
else if (key == "Base")
{
writeServoValues(0, valueInt);
}
else if (key == "Shoulder")
{
writeServoValues(1, valueInt);
}
else if (key == "Elbow")
{
writeServoValues(2, valueInt);
}
else if (key == "Gripper")
{
writeServoValues(3, valueInt);
}
}
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
default:
break;
}
}
void sendCurrentRobotArmState()
{
for (int i = 0; i < servoPins.size(); i++)
{
wsRobotArmInput.textAll(servoPins[i].servoName + "," + servoPins[i].servo.read());
}
wsRobotArmInput.textAll(String("Record,") + (recordSteps ? "ON" : "OFF"));
wsRobotArmInput.textAll(String("Play,") + (playRecordedSteps ? "ON" : "OFF"));
}
void writeServoValues(int servoIndex, int value)
{
if (recordSteps)
{
RecordedStep recordedStep;
if (recordedSteps.size() == 0) // We will first record initial position of all servos.
{
for (int i = 0; i < servoPins.size(); i++)
{
recordedStep.servoIndex = i;
recordedStep.value = servoPins[i].servo.read();
recordedStep.delayInStep = 0;
recordedSteps.push_back(recordedStep);
}
}
unsigned long currentTime = millis();
recordedStep.servoIndex = servoIndex;
recordedStep.value = value;
recordedStep.delayInStep = currentTime - previousTimeInMilli;
recordedSteps.push_back(recordedStep);
previousTimeInMilli = currentTime;
}
servoPins[servoIndex].servo.write(value);
}
void playRecordedRobotArmSteps()
{
if (recordedSteps.size() == 0)
{
return;
}
//This is to move servo to initial position slowly. First 4 steps are initial position
for (int i = 0; i < 4 && playRecordedSteps; i++)
{
RecordedStep &recordedStep = recordedSteps[i];
int currentServoPosition = servoPins[recordedStep.servoIndex].servo.read();
while (currentServoPosition != recordedStep.value && playRecordedSteps)
{
currentServoPosition = (currentServoPosition > recordedStep.value ? currentServoPosition - 1 : currentServoPosition + 1);
servoPins[recordedStep.servoIndex].servo.write(currentServoPosition);
wsRobotArmInput.textAll(servoPins[recordedStep.servoIndex].servoName + "," + currentServoPosition);
delay(50);
}
}
delay(2000); // Delay before starting the actual steps.
for (int i = 4; i < recordedSteps.size() && playRecordedSteps ; i++)
{
RecordedStep &recordedStep = recordedSteps[i];
delay(recordedStep.delayInStep);
servoPins[recordedStep.servoIndex].servo.write(recordedStep.value);
wsRobotArmInput.textAll(servoPins[recordedStep.servoIndex].servoName + "," + recordedStep.value);
}
}
void setUpPinModes()
{
for (int i = 0; i < servoPins.size(); i++)
{
servoPins[i].servo.attach(servoPins[i].servoPin);
servoPins[i].servo.write(servoPins[i].initialPosition);
}
}
void setup(void)
{
setUpPinModes();
Serial.begin(115200);
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.on("/", HTTP_GET, handleRoot);
server.onNotFound(handleNotFound);
wsRobotArmInput.onEvent(onRobotArmInputWebSocketEvent);
server.addHandler(&wsRobotArmInput);
server.begin();
Serial.println("HTTP server started");
}
void loop()
{
wsRobotArmInput.cleanupClients();
if (playRecordedSteps)
{
playRecordedRobotArmSteps();
}
}