-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathTelemetry.cpp
116 lines (89 loc) · 2.85 KB
/
Telemetry.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
#include "./Telemetry.h"
Telemetry::Telemetry() {
this->numRegisters = 0;
};
Telemetry::~Telemetry(){
};
uint8_t Telemetry::num_telemetry = 0;
uint8_t Telemetry::telemetry_ctrl = 0;
Telemetry* Telemetry::telemetries[TELEMETRY_MAX_TELEMETRIES];
void Telemetry::setTelemetryRegisters(uint8_t numRegisters, uint8_t* registers, uint8_t* motors){
if (numRegisters<=TELEMETRY_MAX_REGISTERS) {
this->numRegisters = numRegisters;
for (uint8_t i=0; i<numRegisters; i++) {
this->registers[i] = registers[i];
if (motors!=NULL)
this->registers_motor[i] = motors[i];
else
this->registers_motor[i] = 0;
}
}
headerSent = false;
};
void Telemetry::init(PacketIO& _comms) {
comms = &_comms;
if (Telemetry::num_telemetry < TELEMETRY_MAX_TELEMETRIES) {
Telemetry::telemetries[Telemetry::num_telemetry] = this;
}
this->id = Telemetry::num_telemetry++;
headerSent = false;
if (SimpleFOCRegisters::regs == NULL) {
SimpleFOCRegisters::regs = new SimpleFOCRegisters();
}
};
void Telemetry::run() {
if (numRegisters<1)
return;
unsigned long now = 0;
if (id==0) {
now = _micros();
iterations++;
if (now - last_iter_time >= 1000000) { // TODO add overflow protection
last_iter_time = now;
last_iterations = iterations;
iterations = 0;
}
}
if (downsample==0)
return;
if (downsample==0 || downsampleCnt++ < downsample) return;
downsampleCnt = 0;
if (min_elapsed_time > 0) {
if (id!=0) now = _micros();
if (now - last_run_time < min_elapsed_time) return; // TODO add overflow protection
last_run_time = now;
}
if (!headerSent) {
sendHeader();
headerSent = true;
}
sendTelemetry();
}
void Telemetry::addMotor(FOCMotor* motor) {
if (numMotors < TELEMETRY_MAX_MOTORS) {
motors[numMotors] = motor;
numMotors++;
}
};
void Telemetry::sendHeader() {
if (numRegisters > 0) {
*comms << START_PACKET(PacketType::TELEMETRY_HEADER, 2*numRegisters + 1) << id << Separator('=');
for (uint8_t i = 0; i < numRegisters; i++) {
*comms << registers_motor[i] << Separator(':') << registers[i];
}
*comms << END_PACKET;
};
};
void Telemetry::sendTelemetry(){
if (numRegisters > 0) {
uint8_t size = 1;
for (uint8_t i = 0; i < numRegisters; i++) {
size += SimpleFOCRegisters::regs->sizeOfRegister(registers[i]);
}
*comms << START_PACKET(PacketType::TELEMETRY, size) << id << Separator('=');
for (uint8_t i = 0; i < numRegisters; i++) {
SimpleFOCRegisters::regs->registerToComms(*comms, registers[i], motors[registers_motor[i]]);
};
*comms << END_PACKET;
}
};