-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cerevo_techblog4th_WS.ino
348 lines (295 loc) · 8.37 KB
/
Cerevo_techblog4th_WS.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
/*
* Copyright (c) 2015, Majenko Technologies
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* * Neither the name of Majenko Technologies nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Create a WiFi access point and provide a web server on it. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Servo.h>
/* Set these to your desired credentials. */
const char *ssid = "Wild_WS";
const char *password = "";
ESP8266WebServer server(80);
ESP8266WebServer server_8080(8080);
/* set I2C library*/
#include <Wire.h>
#define ADDR1 0x64
#define command_start 0
#define command_stop 1
#define command_back 2
#define forward 0x01
#define reverse 0x02
#define LED_H (digitalWrite( 12, HIGH ))
#define LED_L (digitalWrite( 12, LOW ))
char state = command_stop;
int offset = 0;
String form ="<html>"
"<head>"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1\">"
"<style>"
"* { padding: 0; margin: 0; }"
"body { background-color: #0097C1; }"
"</style>"
"</head>"
"<body>"
"<div style=\"position: fixed; bottom: 0;\" id=\"value\"></div>"
"<form action=\"\" target=\"tif\" id=\"form\">"
"<iframe src=\"javascript: false;\" style=\"display: none;\" name=\"tif\" id=\"tif\"></iframe>"
"</form>"
"<script>"
"var offset = 50;"
"document.body.style.height = document.body.clientHeight + offset + \'px\';"
"document.body.style.width = document.body.clientWidth + offset + \'px\';"
"document.getElementsByTagName(\"html\")[0].style.height = document.body.style.height + \'px\';"
"document.getElementsByTagName(\"html\")[0].style.width = document.body.style.width + \'px\';"
"var moveHomePosition = function() {"
"document.body.scrollTop = offset / 2;"
"document.body.scrollLeft = offset / 2;"
"};"
"setTimeout(moveHomePosition, 500);"
"var startX = 0;var startY = 0;var command =\'/stop\';"
"var threshold = 40;"
"var esp_port = \'http://192.168.4.1:8080\';"
"var el_form = document.getElementById(\'form\');"
"document.body.ontouchstart = function(event) {"
"startX = event.touches[0].clientX;"
"startY = event.touches[0].clientY;"
"};"
"document.body.ontouchmove = function(event) {"
"var x = parseInt(event.touches[0].clientX - startX);"
"var y = parseInt(event.touches[0].clientY - startY);"
"var url = null;"
"if (x < (threshold * -1)) {"
"if (y < (threshold * -1)){"
"url = \'/f_left\';"
"} else if (y > threshold) {"
"url = \'/r_left\';"
"}else {"
"url = \'/left\';"
"}"
"} else if (x > threshold) {"
"if (y < (threshold * -1)) {"
"url = \'/f_right\';"
"} else if (y > threshold) {"
"url = \'/r_right\';"
"}else{"
"url = \'/right\';"
"}"
"} else {"
"if (y < (threshold * -1)) {"
"url = \'/drive\';"
" } else if (y > threshold) {"
"url = \'/back';"
"}"
"}"
"if (command != url) {"
"if (url) {"
"el_form.action = esp_port + url;"
"el_form.submit();"
"document.getElementById(\'value\').innerHTML = url;"
"}"
"}"
"command = url;"
"};"
"document.body.ontouchend = function(event) {"
"el_form.action = esp_port + \'/stop\';"
"el_form.submit();"
"setTimeout(moveHomePosition, 50);"
"document.getElementById('value').innerHTML = \'/stop\';"
"};"
"</script>"
"</body>"
"</html>";
/* Just a little test message. Go to http://192.168.4.1 in a web browser
* connected to this access point to see it.
*/
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
Wire.begin(4, 14);
delay(40);
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
// And as regular external functions:
server_8080.on("/stop", handle_stop);
server_8080.on("/drive", handle_drive);
server_8080.on("/back", handle_back);
server_8080.on("/left", handle_left);
server_8080.on("/right", handle_right);
server_8080.on("/f_left", handle_f_left);
server_8080.on("/f_right", handle_f_right);
server_8080.on("/r_left", handle_r_left);
server_8080.on("/r_right", handle_r_right);
server.begin();
server_8080.begin();
Serial.println("HTTP server started");
pinMode(16,OUTPUT);
pinMode(12,OUTPUT);
delay(100);
}
void loop() {
server.handleClient();
server_8080.handleClient();
}
void handleRoot() {
server.send(200, "text/html", form);
}
void handle_stop() {
Serial.print("stop\r\n");
LED_H;
stop_motor();
LED_L;
state = command_stop;
server.send(200, "text/html", "");
}
void handle_drive() {
Serial.print("drive\r\n");
LED_H;
drive();
servo_control(90);
LED_L;
server.send(200, "text/html", "");
}
void handle_back() {
Serial.print("back\r\n");
back();
servo_control(90);
}
void handle_left(){
Serial.print("left\r\n");
drive();
servo_control(180);
server.send(200, "text/html", "");
}
void handle_right(){
Serial.print("right\r\n");
drive();
servo_control(0);
server.send(200, "text/html", "");
}
void handle_f_left(){
Serial.print("f_left\r\n");
LED_H;
drive();
servo_control(135);
LED_L;
server.send(200, "text/html", "");
}
void handle_f_right(){
Serial.print("f_right\r\n");
LED_H;
drive();
servo_control(45);
LED_L;
server.send(200, "text/html", "");
}
void handle_r_left(){
Serial.print("r_left\r\n");
LED_H;
back();
servo_control(135);
LED_L;
server.send(200, "text/html", "");
}
void handle_r_right(){
Serial.print("r_right\r\n");
LED_H;
back();
servo_control(45);
LED_L;
server.send(200, "text/html", "");
}
void drive(){
if(state == command_back){
stop_motor();
delay(10);
start_motor();
}else if(state == command_stop){
start_motor();
}
state = command_start;
}
void back(){
if(state == command_start){
stop_motor();
delay(10);
reverse_motor();
}else if(state == command_stop){
reverse_motor();
}
state = command_back;
}
void start_motor(){
char i, volt;
volt = 0x20;
for(i=0;i<4;i++){
volt = volt + ((0x40)*i);
volt = volt | forward;
motor_func(ADDR1 , volt);
delay(10);
}
}
void reverse_motor(){
char i, volt;
volt = 0x20;
for(i=0;i<4;i++){
volt = volt + ((0x40)*i);
volt = volt | reverse;
motor_func(ADDR1 , volt);
delay(10);
}
}
void stop_motor(){
motor_func(ADDR1 , 0x18);
delay(10);
motor_func(ADDR1 , 0x1B);
delay(10);
}
void motor_func(char add , char duty){
Wire.beginTransmission(add);
Wire.write(0x00);
Wire.write(duty);
Wire.endTransmission();
}
void servo_control(int angle){
int microsec,i;
microsec = (5*(angle+offset))+ 1000;
for(i=0; i<20 ;i++){
digitalWrite( 16, HIGH );
delayMicroseconds( microsec );
digitalWrite( 16, LOW );
delayMicroseconds( 10000 - microsec );
}
}