-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOTABoatCode.ino
339 lines (308 loc) · 9.59 KB
/
OTABoatCode.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
TaskHandle_t Task1;
#include <A4988.h>
#include <Smoothed.h>
#include <HardwareSerial.h>
#include <sbus.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
const char* host = "esp32";
const char* ssid = "Tevin";
const char* password = "password";
// Servo VESC;
WebServer server(80);
/*
* Login page
*/
const char* loginIndex =
"<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>"
"<td colspan=2>"
"<center><font size=4><b>ESP32 Login Page</b></font></center>"
"<br>"
"</td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td>Username:</td>"
"<td><input type='text' size=25 name='userid'><br></td>"
"</tr>"
"<br>"
"<br>"
"<tr>"
"<td>Password:</td>"
"<td><input type='Password' size=25 name='pwd'><br></td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
"</tr>"
"</table>"
"</form>"
"<script>"
"function check(form)"
"{"
"if(form.userid.value=='admin' && form.pwd.value=='admin')"
"{"
"window.open('/serverIndex')"
"}"
"else"
"{"
" alert('Error Password or Username')/*displays error message*/"
"}"
"}"
"</script>";
/*
* Server Index Page
*/
const char* serverIndex =
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update'>"
"</form>"
"<div id='prg'>progress: 0%</div>"
"<script>"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
" $.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!')"
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>";
#define RELAY_MIN 200
#define RELAY_MAX 1800
#define PWM_MIN 206
#define PWM_MAX 1800
bfs::SbusRx sbus_rx(&Serial2, 16, 17, true);
bfs::SbusData data;
/** Rudder control **/
#define STEPPER_PUL 2
#define STEPPER_DIR 4
#define STEPPER_ENA 15
#define STEPPER_PWM 17
#define PWM_MIN 206
#define PWM_MAX 1800
#define RUDDER_REFRESH 20
int CH7 = 0;
#define POWER_RELAY 26
#define VESC_PWM 27
//#define POWER_RELAY_PWM 16
#define POTENTIOMETER_MIDDLE 185
#define POTENTIOMETER_ERROR 5
// smooth input from rudder and the potentiometer value
Smoothed <int> rudderInput;
Smoothed <int> actualAngle;
A4988 stepper(200, STEPPER_DIR, STEPPER_PUL);
int msDelay = 500;
int curr_steps = 0;
int rudder_prev_time = 0;
void setup(void) {
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
Serial.begin(115200);
// // Connect to WiFi network
// WiFi.begin(ssid, password);
// Serial.println("");
// // Wait for connection
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.println("");
// Serial.print("Connected to ");
// Serial.println(ssid);
// Serial.print("IP address: ");
// Serial.println(WiFi.localIP());
// /*use mdns for host name resolution*/
// if (!MDNS.begin(host)) { //http://esp32.local
// Serial.println("Error setting up MDNS responder!");
// while (1) {
// delay(1000);
// }
// }
// Serial.println("mDNS responder started");
// /*return index page which is stored in serverIndex */
// server.on("/", HTTP_GET, []() {
// server.sendHeader("Connection", "close");
// server.send(200, "text/html", loginIndex);
// });
// server.on("/serverIndex", HTTP_GET, []() {
// server.sendHeader("Connection", "close");
// server.send(200, "text/html", serverIndex);
// });
// /*handling uploading firmware file */
// server.on("/update", HTTP_POST, []() {
// server.sendHeader("Connection", "close");
// server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
// ESP.restart();
// }, []() {
// HTTPUpload& upload = server.upload();
// if (upload.status == UPLOAD_FILE_START) {
// Serial.printf("Update: %s\n", upload.filename.c_str());
// if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
// Update.printError(Serial);
// }
// } else if (upload.status == UPLOAD_FILE_WRITE) {
// /* flashing firmware to ESP*/
// if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
// Update.printError(Serial);
// }
// } else if (upload.status == UPLOAD_FILE_END) {
// if (Update.end(true)) { //true to set the size to the current progress
// Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
// } else {
// Update.printError(Serial);
// }
// }
// });
// server.begin();
// setup power relay
pinMode(POWER_RELAY, OUTPUT);
pinMode(VESC_PWM, OUTPUT);
// VESC.attach(VESC_PWM); //200, 1796
//digitalWrite(POWER_RELAY, HIGH);
// put your setup code here, to run once:
/** setup stepper motor **/
pinMode(STEPPER_PUL, OUTPUT);
pinMode(STEPPER_DIR, OUTPUT);
pinMode(STEPPER_ENA, OUTPUT);
pinMode(STEPPER_PWM, INPUT);
/** Setup Serial port to display data */
//Serial.begin(9600);
sbus_rx.Begin();
stepper.begin(120, 1);
rudderInput.begin(SMOOTHED_AVERAGE, 5);
actualAngle.begin(SMOOTHED_AVERAGE, 15);
ledcSetup(0, 980, 8);
ledcAttachPin(VESC_PWM, 0);
}
void handleRudder()
{
int stepper_input = map(data.ch[0], PWM_MIN, PWM_MAX, 135, 225);
rudderInput.add(stepper_input);
int smoothed_stepper_input = rudderInput.get();
//Serial.print("Stepper Input: "); Serial.println(smoothed_stepper_input);
int actual_angle_measurment = map(analogRead(34), 0, 4095, 0, 360) + (180 - POTENTIOMETER_MIDDLE);
Serial.print("Read Value: "); Serial.println(analogRead(34));
actualAngle.add(actual_angle_measurment);
int smoothed_actual_angle = actualAngle.get();
//Serial.print("Actual Angle: "); Serial.println(actual_angle_measurment);
//delay(50);
/*
Possible angles: 135-225**
*/
int current_error = smoothed_actual_angle - smoothed_stepper_input;
if(abs(current_error < 5)) { // if close to correct angle
// if going straight
if(smoothed_stepper_input >= 175 && smoothed_stepper_input <= 185) {
if(abs(smoothed_actual_angle - 180) >= POTENTIOMETER_ERROR) { // allow for error
if(smoothed_actual_angle > 180) { // too far right
stepper.move(-1);
}
else { // too far left
stepper.move(1);
}
}
} else if(abs(smoothed_stepper_input - smoothed_actual_angle) >= POTENTIOMETER_ERROR) { // if not going straight, allow for two degree erro
if(smoothed_actual_angle > smoothed_stepper_input) { // too far right
stepper.move(-1); // move left
}
else { // too far right
stepper.move(1); // move left
}
}
}
else { // if need to make big correction
// if going straight
if(smoothed_stepper_input >= 175 && smoothed_stepper_input <= 185) {
if(abs(actual_angle_measurment - 180) >= POTENTIOMETER_ERROR) { // allow for error
if(actual_angle_measurment > 180) { // too far right
stepper.move(-1);
}
else { // too far left
stepper.move(1);
}
}
} else if(abs(smoothed_stepper_input - smoothed_actual_angle) >= 2) { // if not going straight, allow for two degree erro
if(actual_angle_measurment > smoothed_stepper_input) { // too far right
stepper.move(-1); // move left
}
else { // too far right
stepper.move(1); // move left
}
}
}
//delay(100);
// calculate step error
//int step_error = smooth_input - curr_steps;
// instruct motor to move to correct the error
//stepper.move(smooth_input);
// update the numnber of steps taken
//curr_steps += step_error;
//Serial.print("Input change: ");
//Serial.println(smooth_input);
}
void loop(void) {
// put your main code here, to run repeatedly:
if (sbus_rx.Read()) {
/* Grab the received data */
data = sbus_rx.data();
}
//Serial.println(data.ch[2]);
int smooth_input = map(data.ch[0], PWM_MIN, PWM_MAX, -100, 100);
int relay_output = map(data.ch[6], RELAY_MIN, RELAY_MAX, 0,1);
int VESC_output = map(data.ch[2], 200, 1800, 0,255);
//Serial.println(data.ch[2]);
//analogWrite(VESC_PWM,VESC_output);
//Serial.println(VESC_output);
// VESC.writeMicroseconds(VESC_output);
ledcWrite(0, VESC_output);
if(relay_output > 0)
{
digitalWrite(POWER_RELAY, HIGH);
}
else{
digitalWrite(POWER_RELAY, LOW);
}
handleRudder();
}
void Task1code( void * pvParameters ){
for(;;){
server.handleClient();
delay(1);
}
}