-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMagicCarController.ino
83 lines (68 loc) · 2 KB
/
MagicCarController.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
#include <ESPmDNS.h>
#include <Smartcar.h>
#include <WebServer.h>
#include <WiFi.h>
const auto ssid = "yourSSID";
const auto password = "yourWifiPassword";
const auto lightsPin = 2;
WebServer server(80);
ArduinoRuntime arduinoRuntime;
BrushedMotor leftMotor(arduinoRuntime, smartcarlib::pins::v2::leftMotorPins);
BrushedMotor rightMotor(arduinoRuntime, smartcarlib::pins::v2::rightMotorPins);
DifferentialControl control(leftMotor, rightMotor);
GY50 gyroscope(arduinoRuntime, 11);
HeadingCar car(control, gyroscope);
void setup(void)
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
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());
if (MDNS.begin("smartcar"))
{
MDNS.addService("http", "tcp", 80);
Serial.println("MDNS responder started");
}
pinMode(lightsPin, OUTPUT);
server.on("/drive", []() {
const auto arguments = server.args();
for (auto i = 0; i < arguments; i++)
{
const auto command = server.argName(i);
if (command == "speed")
{
const auto userSpeed = server.arg(i).toInt();
car.setSpeed(userSpeed);
digitalWrite(lightsPin, userSpeed == 0 ? HIGH : LOW);
}
else if (command == "angle")
{
car.setAngle(server.arg(i).toInt());
}
}
server.send(200, "text/plain", "ok");
});
server.on("/gyro", []() {
server.send(200, "text/plain", String(car.getHeading()));
});
server.onNotFound(
[]() { server.send(404, "text/plain", "Unknown command"); });
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
server.handleClient();
car.update();
}