-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.cpp
More file actions
174 lines (161 loc) · 4.31 KB
/
web.cpp
File metadata and controls
174 lines (161 loc) · 4.31 KB
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
#include "web.h"
#include "config.h"
HTML510Server h(80);
void handleRoot()
{
h.sendhtml(body);
}
void getSpeed()
{
robot.userSpeed = h.getVal();
}
void startAttack()
{
robot.attacking = true;
robot.attackingTime = millis();
robot.sensors.usedWifi += 1;
}
void stopAttack()
{
robot.attacking = false;
robot.sensors.usedWifi += 1;
}
void press()
{
int direction = h.getVal();
// robot.activeKeys = std::set<int>();
// String res = h.getText();
// for (int i = 0; i < res.length(); i++) {
// char currentChar = res.charAt(i);
// if (currentChar == 'w') {
// robot.activeKeys.insert(1);
// } else if (currentChar == 'a') {
// robot.activeKeys.insert(3);
// } else if (currentChar == 's') {
// robot.activeKeys.insert(2);
// } else if (currentChar == 'd') {
// robot.activeKeys.insert(4);
// }
// }
robot.activeKeys.insert(direction);
robot.state = 1;
robot.sensors.usedWifi += 1;
// #ifdef DEBUG
// Serial.print("Set contents after press request: ");
// for (const int &element : robot.activeKeys) {
// Serial.print(element);
// Serial.print(" "); // Space between elements
// }
// Serial.println(); // Newline at the end
// #endif
}
void release()
{
// robot.activeKeys = std::set<int>();
// String res = h.getText();
// for (int i = 0; i < res.length(); i++) {
// char currentChar = res.charAt(i);
// if (currentChar == 'w') {
// robot.activeKeys.insert(1);
// } else if (currentChar == 'a') {
// robot.activeKeys.insert(3);
// } else if (currentChar == 's') {
// robot.activeKeys.insert(2);
// } else if (currentChar == 'd') {
// robot.activeKeys.insert(4);
// }
// }
int direction = h.getVal();
robot.activeKeys.erase(direction);
robot.state = 0;
robot.sensors.usedWifi += 1;
}
void nav()
{
String res = h.getText();
int x = 0, y = 0, angle = 0;
int firstComma = res.indexOf(',');
int secondComma = res.indexOf(',', firstComma + 1);
if (firstComma != -1 && secondComma != -1)
{
x = res.substring(0, firstComma).toInt();
y = res.substring(firstComma + 1, secondComma).toInt();
angle = res.substring(secondComma + 1).toInt();
robot.target.setPoint(x, y);
robot.target_bearing = angle * PI / 180;
robot.state = 2;
robot.sensors.usedWifi += 1;
robot.substate = 0;
}
else
{
Serial.println("Error: Invalid input format. Expected 'x,y,angle'.");
}
}
void attackClosest()
{
robot.target_bearing = robot.bearing;
robot.state = 3;
robot.attacking = false;
robot.substate = 0;
robot.sensors.usedWifi += 1;
}
void attackStructure()
{
robot.state = 4;
robot.attacking = false;
robot.substate = 0;
int structure = h.getVal();
switch (structure) {
case 0: {
robot.target.setPoint(RED_NEXUS);
robot.target_bearing = LOOK_AT_RED_NEXUS;
break;
}
case 1: {
robot.target.setPoint(BLUE_NEXUS);
robot.target_bearing = LOOK_AT_BLUE_NEXUS;
break;
}
case 2: {
robot.target.setPoint(RED_TOWER);
robot.target_bearing = LOOK_AT_RED_NEXUS;
break;
}
case 3: {
robot.target.setPoint(BLUE_TOWER);
robot.target_bearing = LOOK_AT_BLUE_NEXUS;
break;
}
case 4: {
robot.target.setPoint(RED_UPPER_TOWER);
robot.target_bearing = LOOK_AT_TOP;
break;
}
case 5: {
robot.target.setPoint(BLUE_UPPER_TOWER);
robot.target_bearing = LOOK_AT_TOP;
break;
}
case 6: {
#ifdef VIVE
if ((robot.bearing > 90 && robot.bearing <= 190) || (robot.bearing < -180 && robot.bearing < 0)) robot.target_bearing = PI;
if (robot.bearing < -90 && robot.bearing >= -180) robot.target_bearing = -PI / 2;
if (robot.bearing < 0 && robot.bearing >= -90) robot.target_bearing = 0;
if (robot.bearing < 90 && robot.bearing >= 0) robot.target_bearing = PI / 2;
#endif
robot.state = 5;
break;
}
case 7: {
robot.state = 6;
}
default:
break;
}
robot.sensors.usedWifi += 1;
}
void updateState() {
String buffer = String(robot.state) + "," + String(robot.health) + "," + String(robot.leftRPM) + "," + String(robot.rightRPM) + "," + String(robot.lSpeed) + "," + String(robot.rSpeed) + "," + String(robot.bearing_deg) + "," + String(robot.location.x) + "," + String(robot.location.y) + "," + String(robot.forwardDistance) + "," + String(robot.rightwardDistance);
h.sendplain(buffer);
}