-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathautopilot.cpp
More file actions
64 lines (56 loc) · 1.38 KB
/
autopilot.cpp
File metadata and controls
64 lines (56 loc) · 1.38 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
/*
* Flybrix Flight Controller -- Copyright 2018 Flying Selfie Inc. d/b/a Flybrix
*
* http://www.flybrix.com
*/
#include "autopilot.h"
#include "cardManagement.h"
#include "serial.h"
Autopilot::Autopilot(SerialComm& serial) : serial_(serial) {
}
void Autopilot::start(ClockTime now) {
sdcard::reading::close();
sdcard::reading::open();
running_ = true;
start_time_ = now;
wait_until_ = 0;
}
bool Autopilot::run(ClockTime now) {
bool did_something{false};
if (!running_) {
return did_something;
}
while (sdcard::reading::hasMore()) {
if (wait_until_ > now - start_time_) {
return did_something;
}
readCobs();
did_something = true;
}
running_ = false;
return did_something;
}
void Autopilot::stop() {
running_ = false;
sdcard::reading::close();
}
void Autopilot::readCobs() {
while (sdcard::reading::hasMore()) {
data_input.AppendToBuffer(sdcard::reading::read());
if (data_input.IsDone()) {
handleCobs();
break;
}
}
}
void Autopilot::handleCobs() {
SerialComm::MessageType command;
if (!data_input.PeekInto(command)) {
return;
}
if (SerialComm::MessageType::AutopilotWait == command) {
data_input.ParseInto(command, wait_until_);
} else {
serial_.ProcessData(data_input, false);
}
}