-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrone2.ino
165 lines (119 loc) · 3.07 KB
/
drone2.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
#include <ESP8266WiFi.h>
#include "sensor.h"
#include "control.h"
FlightData flightData;
FlightControl flightControl;
unsigned long lastTime = 0;
WiFiServer server(333);
WiFiClient client;
void handle_request() {
int numToRead = client.available();
if(!numToRead) return;
//Serial.println(numToRead);
byte firstByte = client.read();
//Serial.println(firstByte);
float buf[4];
if(firstByte == 0b11111111) {
client.write(0b10101010);
}
else if(firstByte & 0b10000000) {//if is telemetry request
firstByte &= 0b01111111;
switch(firstByte) {
case 0: {
PYR attitude = flightData.getAttitude();
client.write((char *) &attitude, sizeof(PYR));
break;
}
case 1: {
float motors[4];
client.write((char *) motors, sizeof(motors));
break;
}
case 2: {
float battery = 1.0f;
client.write((char *) &battery, sizeof(float));
break;
}
default:
break;
}
}
else {//if is flight command
switch(firstByte) {
case 0:
client.read((char*)buf, 4*sizeof(float));
flightControl.setTarget(*(PYR *)buf);
flightControl.throttle = buf[3];
break;
case 1:
client.read((char*)buf, sizeof(float));
flightControl.throttle = *buf;
break;
case 2:
client.read((char*)buf, sizeof(float));
flightControl.pitchPID.target = *buf;
break;
default:
break;
/*
case 2:
client.read(buf, 4);
//control_pitch = getUnalignedFloat(buf);
memcpy(&control_pitch, buf, 4);
pitch_pid.setTarget(control_pitch);
break;
case 3:
client.read(buf, 4);
//control_roll = getUnalignedFloat(buf);
memcpy(&control_roll, buf, 4);
roll_pid.setTarget(control_roll);
break;
case 4:
client.read(buf, 4);
control_yaw = getUnalignedFloat(buf);
break;
//TODO: trim
default:
break;
*/
}
}
}
void setup() {
pinMode(0, OUTPUT);
Serial.begin(9600);
initSensor();
delay(5000);
Serial.print("whoami returns: ");
Serial.println(readReg(0x75));
Serial.print("Sleep register: ");
Serial.println(readReg(PWR_ADDR));
flightData.calibrate();
Serial.println("CALIBRATED");
bool apUP = WiFi.softAP("ESPBT");
bool apCON = WiFi.softAPConfig(IPAddress(192,168,0,1),IPAddress(192,168,0,1),IPAddress(255,255,255,0));
server.begin();
server.setNoDelay(true);
lastTime = micros();
}
void loop() {
unsigned long curTime = micros();
float dtime = (curTime - lastTime) / 1000000.0f;
lastTime = curTime;
if(client.connected()) {
if(client.available()) {
handle_request();
if(client.available()) digitalWrite(0, HIGH);
}
else {
digitalWrite(0, LOW);
}
}
else {
client = server.accept();
}
flightData.update(dtime);
flightControl.update(flightData.getAttitude(), dtime);
//flightData.isolatedAccel().print();
//Serial.println();
}