-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.h
195 lines (175 loc) · 6.01 KB
/
receiver.h
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef RECEIVER_H
#define RECEIVER_H
// this is an sbus receiver implementation
#include "shared.h"
// this is sport, but you could comment it out and implement another telemetry mechanism. this seems fairly flexible, so it's probably your first preference.
// you can add on to the smartport system with your own vendor specific codes - we don't have to be compatible with frsky nor do our own checksumming thanks to protocol layer checksums and retransmission (chinese espnow radio stack)
// note the custom behaviour in the telemetry system to handle dead packets and transmit time bound historical averages to the controller
#include "telemetry.h"
ControlPacket currentPacket;
bool mutatedCurrentPacket = false;
SBusMessage currentMessage;
bool outstandingPacket = false;
#define FAILSAFE_THRESHOLD 250
#define LOST_PKT_THRESHOLD 50
#define SBUS_FAILSAFE_OFFSET 3
#define SBUS_LOST_PKT_OFFSET 2
short dead = 0;
void reversi() {
currentMessage.footer = 0x0f;
currentMessage.header = 0x00;
}
#define SBUS_CHANNEL_SIZE 3
void refreshSbusMessage() {
memset(¤tMessage, 0, sizeof(SBusMessage));
currentMessage.header = 0x0F;
//currentPacket.channels[22]; // 16 11bit channels
if (!mutatedCurrentPacket) {
for (int i = 0; i < MAX_ANALOG_CHANNELS; ++i) {
currentPacket.channels[i] = map(currentPacket.channels[i], 1000, 2000, 173, 1811);//192, 1792);
}
mutatedCurrentPacket = true;
}
short offset = 0;
short byteindex = 1;
// Bind wire analog channels to SBus channels (currently RPTY to reduce packet size)
for (unsigned i = 0; (i < MAX_ANALOG_CHANNELS) && (i < MAX_SBUS_CHANNELS); ++i) {
/*protect from out of bounds values and limit to 11 bits*/
if (currentPacket.channels[i] > 0x07ff) {
currentPacket.channels[i] = 0x07ff;
}
while (offset >= 8) {
++byteindex;
offset -= 8;
}
((byte*)¤tMessage)[byteindex] |= (currentPacket.channels[i] << (offset)) & 0xff;
((byte*)¤tMessage)[byteindex + 1] |= (currentPacket.channels[i] >> (8 - offset)) & 0xff;
((byte*)¤tMessage)[byteindex + 2] |= (currentPacket.channels[i] >> (16 - offset)) & 0xff;
offset += 11;
}
// Doesn't currently bind wire digital channels to SBus AUX channels
short chanIndex = 0;
for (int i = 0; i < MAX_DIGITAL_BYTES; ++i) {
for (int j = 0; j < 4; ++j) {
if (i == MAX_DIGITAL_BYTES-1 && j >= 2) {
// protect last two channels from assignment, leaving them for postfix 17 and 18
break;
}
// If it's on, it's on. If it's off, it's off.
chanIndex += 1;
}
}
if ( (currentPacket.digitalChannels[MAX_DIGITAL_BYTES-1]) & 0x1) {
currentMessage.postfix |= 1;
}
if ( (currentPacket.digitalChannels[MAX_DIGITAL_BYTES-1] >> 1) & 0x1) {
currentMessage.postfix |= 1<<1;
}
if (dead > LOST_PKT_THRESHOLD) {
currentMessage.postfix |= 1<<SBUS_LOST_PKT_OFFSET;
}
if (dead > FAILSAFE_THRESHOLD) {
currentMessage.postfix |= 1<<SBUS_FAILSAFE_OFFSET;
}
currentMessage.footer = 0x00;
//reversi();
}
unsigned long ok = 0;
void writeSBusMessage() {
long written = Serial2.write((uint8_t*)¤tMessage, sizeof(SBusMessage));
if (written == sizeof(SBusMessage)) {
ok += 1;
if (ok % 100 == 0) {
Serial.print(ok, DEC);
Serial.println(" messages written to SBus OK.");
Serial.print(currentPacket.digitalChannels[MAX_DIGITAL_BYTES-1], BIN);
Serial.print(" digi, ");
Serial.print(currentMessage.postfix, BIN);
Serial.println(" postfix.");
}
} else {
Serial.println(" bytes written to SBus does not match expected size!");
}
}
byte txPin;
void emitTelemetryStatus(byte status) {
Serial.print("Telemetry status send: ");
Serial.print(status, DEC);
Serial.println();
// check last emission time, don't say it more than every 100ms
if (lastPacketSent < millis()-1000) {
TelemetryPacket packet;
memset(&packet, 0, sizeof(TelemetryPacket));
packet.status = status;
}
}
void emitTelemetry() {
emitTelemetryStatus(RECVR_OK);
}
bool connectSBus() {
Serial2.begin(100000, SERIAL_8E2, 25, txPin);
if (Serial2) {
Serial.println("SBUS opened OK");
return true;
} else {
Serial.println("SBUS fail");
emitTelemetryStatus(RECVR_SBUS_NOT_READY);
return false;
}
}
// use another class with similar function names if you want to swap out telemetry. ensure everything is delegated to the telemetry class
SmartPort telemetry;
void init_receiver(byte sbus_tx, byte telemetry_rx, byte telemetry_tx) {
// clear packet
memset(¤tPacket, 0, sizeof(ControlPacket));
outstandingPacket = false;
// configure sbus
txPin = sbus_tx;
// start sbus
connectSBus();
//telemetry.init(telemetry_rx, telemetry_tx);
}
void maintain_sbus();
void loop_receiver() {
maintain_sbus();
//telemetry.maintain();
if (dead > 0) {
delay(1); // be lenient, wait 2ms per dead increment
}
}
void maintain_sbus() {
if (!Serial2) {
connectSBus();
return;
}
if (outstandingPacket) {
// Convert the packet into an SBUS message
ControlPacket* packet = ¤tPacket;
//Serial.printf("R: %d P: %d Y: %d T: %d digi: %d dig2: %d\n", packet->channels[0], packet->channels[1], packet->channels[2], packet->channels[3], packet->digitalChannels[0], packet->digitalChannels[1]);
refreshSbusMessage();
outstandingPacket = false;
dead = 0;
writeSBusMessage();
} else {
dead += 1;
if (dead > LOST_PKT_THRESHOLD) {
currentMessage.postfix |= 1<<SBUS_LOST_PKT_OFFSET;
}
if (dead > FAILSAFE_THRESHOLD) {
currentMessage.postfix |= 1<<SBUS_FAILSAFE_OFFSET;
}
writeSBusMessage();
}
}
void send_callback(const uint8_t* peerMac, bool sent) {
}
void recv_callback(const uint8_t* peerMac, const uint8_t *data, int data_len) {
// Probably got a control packet
if (data_len == sizeof(ControlPacket)) {
ControlPacket packet = *(ControlPacket*)data;
memcpy(¤tPacket, &packet, sizeof(ControlPacket));
mutatedCurrentPacket = false;
outstandingPacket = true;
}
}
#endif // RECEIVER_H