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
|
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebSrv.h>
// Some variables we will need along the way
const char *ssid = "Fablab";
const char *password = "Fabricationlab1";
const char *PARAM_MESSAGE = "message";
int webServerPort = 80;
int STEP_PIN_A = D5;
int DIR_PIN_A = D4;
int ENABLE_PIN = D10;
int STEP_PIN_B = D7;
int DIR_PIN_B = D6;
int STEP_PIN_C = D9;
int DIR_PIN_C = D8;
int stepCount = 10;
// Setting up our webserver
AsyncWebServer server(webServerPort);
// This function will be called when human will try to access undefined endpoint
void notFound(AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse(404, "text/plain", "Not found");
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
}
void sendResponse(AsyncWebServerRequest *request, String message) {
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", message);
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
}
void setup() {
Serial.begin(19200);
pinMode(STEP_PIN_A,OUTPUT);
pinMode(DIR_PIN_A,OUTPUT);
pinMode(STEP_PIN_B,OUTPUT);
pinMode(DIR_PIN_B,OUTPUT);
pinMode(STEP_PIN_C,OUTPUT);
pinMode(DIR_PIN_C,OUTPUT);
pinMode(ENABLE_PIN,OUTPUT);
digitalWrite(ENABLE_PIN, LOW);
delay(10);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// We want to know the IP address so we can send commands from our computer to the device
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Greet human when it tries to access the root / endpoint.
// This is a good place to send some documentation about other calls available if you wish.
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
sendResponse(request, "Hello!");
});
server.on("/motor", HTTP_GET, [](AsyncWebServerRequest *request) {
int stepValueA;
int stepValueB;
float accelValue;
if (request->hasParam("sentStepValueA")){
stepValueA = request->getParam("sentStepValueA")->value().toInt();
}
if (request->hasParam("sentStepValueB")){
stepValueB = request->getParam("sentStepValueB")->value().toInt();
}
if (request->hasParam("sentAccelValue")){
accelValue = request->getParam("sentAccelValue")->value().toFloat();
}
if (request->hasParam("sentDirA")) {
// The incoming params are Strings
String param = request->getParam("sentDirA")->value();
// .. so we have to interpret or cast them
if (param == "counter-clockwise") {
digitalWrite(DIR_PIN_A, LOW);
Serial.println("dir a ccw");
} else if (param == "clockwise") {
digitalWrite(DIR_PIN_A, HIGH);
Serial.println("dir a cw");
} else {
stepValueA = 0;
}
}
if (request->hasParam("sentDirB")) {
// The incoming params are Strings
String param = request->getParam("sentDirB")->value();
// .. so we have to interpret or cast them
if (param == "counter-clockwise") {
digitalWrite(DIR_PIN_B, LOW);
Serial.println("dir b ccw");
} else if (param == "clockwise") {
digitalWrite(DIR_PIN_B, HIGH);
Serial.println("dir b cw");
} else {
stepValueB=0;
}
}
// Send back message to human
String stateString; // Declare the variable outside the if statement
stateString = "done";
Serial.println(stepValueA);
Serial.println(stepValueB);
Serial.println(accelValue);
motorMove(stepValueA, stepValueB, accelValue);
/*
if (state == 2) {
Serial.println("turningcw");
digitalWrite(DIR_PIN_A, LOW);
digitalWrite(DIR_PIN_B, LOW);
stepCount = stepValue;
constantAccel(accelValue);
stateString = "isTurningCW";
} else if (state == 3) {
Serial.println("turningCCW");
digitalWrite(DIR_PIN_A, HIGH);
digitalWrite(DIR_PIN_B, HIGH);
stepCount = stepValue;
constantAccel(accelValue);
stateString = "isTurningCCW";
} else {
stateString = "notTurning";
}
*/
String responseJSON = "{\"motorState\":\"" + stateString + "\"}";
sendResponse(request, responseJSON);
});
server.on("/params", HTTP_GET, [](AsyncWebServerRequest *request) {
int param1 = random(100);
int param2 = random(100);
int param3 = random(100);
int param4 = random(100);
String responseJSON = "{";
responseJSON += "\"param1\":" + String(param1) + ",";
responseJSON += "\"param2\":" + String(param2) + ",";
responseJSON += "\"param3\":" + String(param3) + ",";
responseJSON += "\"param4\":" + String(param4) + ",";
responseJSON += "}";
sendResponse(request, responseJSON);
});
// If human tries endpoint no exist, exec this function
server.onNotFound(notFound);
Serial.print("Starting web server on port ");
Serial.println(webServerPort);
server.begin();
}
/*
void constantAccel(float accelVal){
int delays[stepCount];
float angle = 1;
float accel = accelVal;
float c0 = 2000 * sqrt(2 * angle / accel ) * 0.67703;
float lastDelay = 0;
int highSpeed = 100;
for (int i=0; i< stepCount; i++){
float d = c0;
if (i>0){
d = lastDelay - (2 * lastDelay)/(4*i+1);
}
if (d<highSpeed){
d = highSpeed;
}
delays[i] = d;
lastDelay = d;
}
for (int i= 0; i<stepCount || i<100; i++){
if(i<stepCount)
digitalWrite(STEP_PIN_A, HIGH);
if(i<100)
digitalWrite(STEP_PIN_B, HIGH);
delayMicroseconds (delays[i]);
if(i<stepCount)
digitalWrite(STEP_PIN_A, LOW);
if(i<100)
digitalWrite(STEP_PIN_B, LOW);
}
for (int i= 0; i<stepCount || i<100 ; i++){
if(i<stepCount)
digitalWrite(STEP_PIN_A, HIGH);
if(i<100)
digitalWrite(STEP_PIN_B, HIGH);
delayMicroseconds (delays[stepCount-i-1]);
if(i<stepCount)
digitalWrite(STEP_PIN_A, LOW);
if(i<100)
digitalWrite(STEP_PIN_B, LOW);
}
}
*/
void motorMove(int stepValueA, int stepValueB, float accelValue){
int delaysA[stepValueA];
float angleA = 1;
float accelA = accelValue;
float c0A = 2000 * sqrt(2 * angleA / accelA ) * 0.67703;
float lastDelayA = 0;
int highSpeedA = 100;
for (int i=0; i< stepValueA; i++){
float d = c0A;
if (i>0){
d = lastDelayA - (2 * lastDelayA)/(4*i+1);
}
if (d<highSpeedA){
d = highSpeedA;
}
delaysA[i] = d;
lastDelayA = d;
}
int delaysB[stepValueB];
float angleB = 1;
float accelB = accelValue;
float c0B = 2000 * sqrt(2 * angleB / accelB ) * 0.67703;
float lastDelayB = 0;
int highSpeedB = 100;
for (int i=0; i< stepValueB; i++){
float d = c0B;
if (i>0){
d = lastDelayB - (2 * lastDelayB)/(4*i+1);
}
if (d<highSpeedB){
d = highSpeedB;
}
delaysB[i] = d;
lastDelayB = d;
}
/*
for (int i= 0 int j=0; i<stepValueA || j<stepValueB; j++; i++){
if(i<stepValueA)
digitalWrite(STEP_PIN_A, HIGH);
if(i<stepValueB)
digitalWrite(STEP_PIN_B, HIGH);
if(i<stepValueA)
delayMicroseconds (delaysA[i]);
if(i<stepValueB)
delayMicroseconds (delaysB[i]);
if(i<stepValueA)
digitalWrite(STEP_PIN_A, LOW);
if(i<stepValueB)
digitalWrite(STEP_PIN_B, LOW);
}
for (int i= 0; i<stepValueA && i<stepValueB; i++){
if(i<stepValueA)
digitalWrite(STEP_PIN_A, HIGH);
if(i<stepValueB)
digitalWrite(STEP_PIN_B, HIGH);
if(i<stepValueA)
delayMicroseconds (delaysA[stepValueA-i-1]);
if(i<stepValueB)
delayMicroseconds (delaysB[stepValueB-i-1]);
if(i<stepValueA)
digitalWrite(STEP_PIN_A, LOW);
if(i<stepValueB)
digitalWrite(STEP_PIN_B, LOW);
}
*/
int i = 0;
int j = 0;
while (i < stepValueA || j < stepValueB) {
if (i < stepValueA) {
digitalWrite(STEP_PIN_A, HIGH);
delayMicroseconds(delaysA[i]);
digitalWrite(STEP_PIN_A, LOW);
i++;
}
if (j < stepValueB) {
digitalWrite(STEP_PIN_B, HIGH);
delayMicroseconds(delaysB[j]);
digitalWrite(STEP_PIN_B, LOW);
j++;
}
}
i = 0;
j = 0;
while (i < stepValueA || j < stepValueB) {
if (i < stepValueA) {
digitalWrite(STEP_PIN_A, HIGH);
delayMicroseconds(delaysA[stepValueA-i-1]);
digitalWrite(STEP_PIN_A, LOW);
i++;
}
if (j < stepValueB) {
digitalWrite(STEP_PIN_B, HIGH);
delayMicroseconds(delaysB[stepValueB-j-1]);
digitalWrite(STEP_PIN_B, LOW);
j++;
}
}
}
void loop() {
}
|