This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.cpp
165 lines (139 loc) · 3.68 KB
/
main.cpp
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
#include "RobotController.h"
#include "hCloudClient.h"
RobotController rc;
float speed = 0;
void cfgHandler()
{
auto l1 = platform.ui.label("l1");
auto g1 = platform.ui.button("g1");
auto g2 = platform.ui.button("g2");
auto g3 = platform.ui.button("g3");
auto stop = platform.ui.button("stop");
platform.ui.loadHtml({Resource::WEBIDE, "/WebUI/ui.html"});
platform.ui.video.enable();
}
void onKeyEvent(KeyEventType type, KeyCode code)
{
static float right = 0;
static float gear = 1.0;
static bool motorState = 1;
static bool parkingState = 1;
static bool up_on = false;
static bool down_on = false;
static bool left_on = false;
static bool right_on = false;
platform.ui.console("cl1").printf("\r\nKE: t:%d, c:%d [%dms]", type, code, sys.getRefTime());
bool isPressed = (type == KeyEventType::Pressed);
if (code == KeyCode::Key_W || code == KeyCode::Up)
up_on = isPressed;
if (code == KeyCode::Key_S || code == KeyCode::Down)
down_on = isPressed;
if (code == KeyCode::Key_A || code == KeyCode::Left)
left_on = isPressed;
if (code == KeyCode::Key_D || code == KeyCode::Right)
right_on = isPressed;
if (type == KeyEventType::Pressed) {
LED1.toggle();
switch (code) {
case KeyCode::Key_U: {
gear = 0.5;
break;
}
case KeyCode::Key_I: {
gear = 1.0;
break;
}
case KeyCode::Key_O: {
gear = 2.0;
break;
}
case KeyCode::Key_F: {
motorState = !motorState;
rc.enableMotors(motorState);
platform.ui.console("cl1").printf("\r\nmotor %s", motorState ? "on" : "off");
break;
}
case KeyCode::Key_P: {
parkingState = !parkingState;
rc.enableParking(parkingState);
if(parkingState == 1) {
platform.ui.button("stop").setText("start");
} else {
platform.ui.button("stop").setText("stop");
}
platform.ui.console("cl1").printf("\r\nparking %s", parkingState ? "on" : "off");
break;
}
case KeyCode::Key_C: {
float angle = rc.calibrateIMU();
platform.ui.console("cl1").printf("\r\ncalibrate IMU, angle = %f", angle);
break;
}
default : break;
}
}
if (up_on) {
speed = gear;
} else {
if (down_on) {
speed = -gear;
} else {
speed = 0.0;
}
}
if (left_on) {
right = -0.5;
} else {
if (right_on) {
right = 0.5;
} else {
right = 0.0;
}
}
platform.ui.console("cl1").printf("\r\nspeed = %f, right = %f", speed, right);
rc.setSpeed(speed, right);
}
void onButtonEvent(hId id, ButtonEventType type)
{
//make unified interface and use onKeyEvent after mapping id and type
platform.ui.console("cl1").printf("\r\nBE: t:%d, i:%s [%dms]", type, id.str(), sys.getRefTime());
KeyEventType t = ((type == ButtonEventType::Pressed) ? KeyEventType::Pressed : KeyEventType::Released);
KeyCode c = KeyCode::Key_X;
if (id == "g1") {c = KeyCode::Key_U;}
if (id == "g2") {c = KeyCode::Key_I;}
if (id == "g3") {c = KeyCode::Key_O;}
if (id == "stop") {c = KeyCode::Key_P;}
if (id == "move_up") {c = KeyCode::Key_W;}
if (id == "move_down") {c = KeyCode::Key_S;}
if (id == "move_left") {c = KeyCode::Key_A;}
if (id == "move_right") {c = KeyCode::Key_D;}
onKeyEvent(t, c);
}
void statusTask()
{
while (1) {
platform.ui.label("lb_bat").setText("%2f", sys.getSupplyVoltage());
sys.delay(2000);
}
}
void hMain()
{
// web UI handlers
platform.begin(&RPi);
platform.ui.configHandler = cfgHandler;
platform.ui.onKeyEvent = onKeyEvent;
platform.ui.onButtonEvent = onButtonEvent;
platform.ui.setProjectId("@@@PROJECT_ID@@@");
sys.taskCreate(statusTask);
// logs from robot control algorithm
hSens3.serial.init(115200);
hSens4.serial.init(115200);
rc.setLogDev1(hSens3.serial);
rc.setLogDev2(hSens4.serial);
// enable robot controller
rc.begin();
for (;;) {
LED3.toggle();
sys.delay(100);
}
}