-
Notifications
You must be signed in to change notification settings - Fork 1
/
MKZ4_tank_standalone_V01.ino
285 lines (245 loc) · 6 KB
/
MKZ4_tank_standalone_V01.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
//MKZ4_tank stand alone
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Set these to your desired credentials. */
const char *ssid = "MKZ4_Tank";
const char *password = "";
ESP8266WebServer server(8080);
ESP8266WebServer server_8080(8080);
//DRV8835 port assign
#define AIN1 14
#define AIN2 13
#define BIN1 4
#define BIN2 5
//Fire LED assign
#define fire_LED_ON (digitalWrite( 16, LOW ))
#define fire_LED_OFF (digitalWrite( 16, HIGH ))
//unsigned char drive_mode = 0;
int pwm[]={512,700,1023};
int t_wait[]={2000,1000,500};
unsigned char s = 1;
unsigned char drive_mode = 0;
boolean action = 0;
//http://192.168.4.1:8080
//HTML User interface
String form ="<html>"
"<head><meta name=viewport content=width=device-width></head>"
"<head>"
"<div style=background-color:#0097C1;>"
"</style>"
"</head>"
"<center>"
"<form action=forward><input type= submit value=forward target=tif></form>"
"<form action=back><input type=submit value=back target=tif></form>"
"<form action=left><input type= submit value=left target=tif></form>"
"<form action=right><input type= submit value=right target=tif></form>"
"<form action=stop><input type=submit value=stop target=tif></form>"
"<form action=fire><input type= submit value=fire target=tif></form>"
"<form action=turn><input type=submit value=turn target=tif></form>"
"<form action=fast><input type= submit value=fast target=tif></form>"
"<form action=slow><input type=submit value=slow target=tif></form>"
"</center>"
"</div></html>";
//Motor DriverDRV8835 Control
/*
drive mode0 : stop
drive mode1 : forward
drive mode2 : back
drive mode3 : R left turn
drive mode4 : R right turn
drive mode5 : left turn
drive mode6 : right turn
*/
void DRV8835_Control(){
if (drive_mode == 0){
analogWrite(AIN1,pwm[s]);
analogWrite(AIN2,pwm[s]);
analogWrite(BIN1,pwm[s]);
analogWrite(BIN2,pwm[s]);
delay(100);
}
if (drive_mode == 1){
analogWrite(AIN1,pwm[s]);
analogWrite(AIN2,0);
analogWrite(BIN1,pwm[s]);
analogWrite(BIN2,0);
delay(100);
}
if (drive_mode == 2){
analogWrite(AIN1,0);
analogWrite(AIN2,pwm[s]);
analogWrite(BIN1,0);
analogWrite(BIN2,pwm[s]);
delay(100);
}
if (drive_mode == 3){
analogWrite(AIN1,0);
analogWrite(AIN2,pwm[s]);
analogWrite(BIN1,pwm[s]);
analogWrite(BIN2,0);
delay(100);
}
if (drive_mode == 4){
analogWrite(AIN1,pwm[s]);
analogWrite(AIN2,0);
analogWrite(BIN1,0);
analogWrite(BIN2,pwm[s]);
delay(100);
}
if (drive_mode == 5){
analogWrite(AIN1,0);
analogWrite(AIN2,0);
analogWrite(BIN1,pwm[s]);
analogWrite(BIN2,0);
delay(100);
}
if (drive_mode == 6){
analogWrite(AIN1,pwm[s]);
analogWrite(AIN2,0);
analogWrite(BIN1,0);
analogWrite(BIN2,0);
delay(100);
}
}
/* Just a little test message. Go to http://192.168.4.1:8080 in a web browser
* connected to this access point to see it.
*/
void setup() {
//port define
pinMode(16,OUTPUT);
fire_LED_ON;
delay(1000);
fire_LED_OFF;
//Serial setup
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* 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);
//UI control
server.on("/", handleRoot);
server.on("/stop", handle_stop);
server.on("/forward", handle_forward);
server.on("/back", handle_back);
server.on("/left", handle_left);
server.on("/right", handle_right);
server.on("/turn", handle_turn);
server.on("/fire", handle_fire);
server.on("/fast", handle_fast);
server.on("/slow", handle_slow);
server.begin();
server_8080.begin();
Serial.println("HTTP server started");
}
//main loop
void loop() {
server.handleClient();
}
//No control
void handleRoot() {
action = 0;
server.send(200, "text/html", form);
}
//Tank stop
void handle_stop() {
Serial.println("stop");
drive_mode = 0;
DRV8835_Control();
action = 0;
server.send(200, "text/html", form);
}
//Tank forward
void handle_forward() {
Serial.println("forward");
drive_mode = 1;
DRV8835_Control();
server.send(200, "text/html", form);
}
//Tank Back
void handle_back() {
Serial.println("back");
drive_mode = 2;
DRV8835_Control();
server.send(200, "text/html", form);
}
//Tank left turn
void handle_left() {
action=1;
if(action) {
Serial.println("left");
drive_mode = 5;
DRV8835_Control();
delay(t_wait[s]);
handle_stop();
}
server.send(200, "text/html", form);
}
//Tank right turn
void handle_right() {
action=1;
if(action) {
Serial.println("right");
drive_mode = 6;
DRV8835_Control();
delay(t_wait[s]);
handle_stop();
}
server.send(200, "text/html", form);
}
//Tank rapid turn
void handle_turn() {
Serial.println("turn");
drive_mode = 4;
DRV8835_Control();
server.send(200, "text/html", form);
}
//Tank fire action
void handle_fire() {
action=1;
if(action) {
Serial.println("fire");
handle_back() ;
delay(50);
handle_stop();
delay(300);
fire_LED_ON;
handle_forward() ;
delay(50);
handle_stop();
fire_LED_OFF;
delay(10);
}
server.send(200, "text/html", form);
}
//Tank speed up(DRV8835 PWM change)
void handle_fast() {
action=1;
if(action) {
Serial.println(s);
s++;
Serial.println(s);
if (s>=2){s=2;}else{s=1;}
Serial.print("fast:");
Serial.println(s);
handle_stop();
}
server.send(200, "text/html", form);
}
//Tank speed down(DRV8835 PWM change)
void handle_slow() {
action=1;
if(action) {
Serial.println(s);
s--;
Serial.println(s);
if (s<=1){s=1;}else{s=2;}
Serial.print("slow:");
Serial.println(s);
handle_stop();
}
server.send(200, "text/html", form);
}