-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathNetwork.ino
More file actions
150 lines (138 loc) · 3.61 KB
/
Network.ino
File metadata and controls
150 lines (138 loc) · 3.61 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
// BROBOT EVO 2 by JJROBOTS
// SELF BALANCE ARDUINO ROBOT WITH STEPPER MOTORS
// License: GPL v2
// Network functions (ESP module)
// Read control PID parameters from user. This is only for advanced users that want to "play" with the controllers...
void readControlParameters()
{
// Parameters Mode (page2 controls)
// Parameter initialization (first time we enter page2)
if (!modifing_control_parameters)
{
for (uint8_t i = 0; i < 4; i++)
OSCfader[i] = 0.5;
OSCtoggle[0] = 0;
modifing_control_parameters = true;
Serial1.println("$P2");
}
// User could adjust KP, KD, KP_THROTTLE and KI_THROTTLE (fadder1,2,3,4)
// Now we need to adjust all the parameters all the times because we dont know what parameter has been moved
Kp_user = KP * 2 * OSCfader[0];
Kd_user = KD * 2 * OSCfader[1];
Kp_thr_user = KP_THROTTLE * 2 * OSCfader[2];
Ki_thr_user = KI_THROTTLE * 2 * OSCfader[3];
// Send a special telemetry message with the new parameters
char auxS[50];
sprintf(auxS, "$tP,%d,%d,%d,%d", int(Kp_user * 1000), int(Kd_user * 1000), int(Kp_thr_user * 1000), int(Ki_thr_user * 1000));
Serial1.println(auxS);
#if DEBUG>0
Serial.print("Par: ");
Serial.print(Kp_user);
Serial.print(" ");
Serial.print(Kd_user);
Serial.print(" ");
Serial.print(Kp_thr_user);
Serial.print(" ");
Serial.println(Ki_thr_user);
#endif
// Calibration mode??
if (OSCpush[2] == 1)
{
Serial.print("Calibration MODE ");
angle_offset = angle_adjusted_filtered;
Serial.println(angle_offset);
}
// Kill robot => Sleep
while (OSCtoggle[0] == 1)
{
//Reset external parameters
PID_errorSum = 0;
timer_old = millis();
setMotorSpeedM1(0);
setMotorSpeedM2(0);
digitalWrite(4, HIGH); // Disable motors
OSC_MsgRead();
}
}
int ESPwait(String stopstr, int timeout_secs)
{
String response;
bool found = false;
char c;
long timer_init;
long timer;
timer_init = millis();
while (!found) {
timer = millis();
if (((timer - timer_init) / 1000) > timeout_secs) { // Timeout?
Serial.println("!Timeout!");
return 0; // timeout
}
if (Serial1.available()) {
c = Serial1.read();
Serial.print(c);
response += c;
if (response.endsWith(stopstr)) {
found = true;
delay(10);
Serial1.flush();
Serial.println();
}
} // end Serial1_available()
} // end while (!found)
return 1;
}
// getMacAddress from ESP wifi module
int ESPgetMac()
{
char c1, c2;
bool timeout = false;
long timer_init;
long timer;
uint8_t state = 0;
uint8_t index = 0;
MAC = "";
timer_init = millis();
while (!timeout) {
timer = millis();
if (((timer - timer_init) / 1000) > 5) // Timeout?
timeout = true;
if (Serial1.available()) {
c2 = c1;
c1 = Serial1.read();
Serial.print(c1);
switch (state) {
case 0:
if (c1 == ':')
state = 1;
break;
case 1:
if (c1 == '\r') {
MAC.toUpperCase();
state = 2;
}
else {
if ((c1 != '"') && (c1 != ':'))
MAC += c1; // Uppercase
}
break;
case 2:
if ((c2 == 'O') && (c1 == 'K')) {
Serial.println();
Serial1.flush();
return 1; // Ok
}
break;
} // end switch
} // Serial_available
} // while (!timeout)
Serial.println("!Timeout!");
Serial1.flush();
return -1; // timeout
}
int ESPsendCommand(char *command, String stopstr, int timeout_secs)
{
Serial1.println(command);
ESPwait(stopstr, timeout_secs);
delay(250);
}