forked from garfield38/DJI_DBUS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDJI_DBUS.cpp
executable file
·227 lines (210 loc) · 5.77 KB
/
DJI_DBUS.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
DJI DBUS data decoder library
(c) S. Driussi 20141215
Not for commercial use
Work started from mikeshub Futaba library
https://github.com/mikeshub/FUTABA_SBUS
Refer to naza_dbus_decoder_wiring.jpg diagram for proper connection
*/
#include "DJI_DBUS.h"
void DJI_DBUS::begin(){
uint8_t loc_sbusData[25] = {
0x0f,0x01,0x04,0x20,0x00,0xff,0x07,0x40,0x00,0x02,0x10,0x80,0x2c,0x64,0x21,0x0b,0x59,0x08,0x40,0x00,0x02,0x10,0x80,0x00,0x00};
int16_t loc_channels[18] = {
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,0,0};
int16_t loc_servos[18] = {
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,0,0};
_serial.begin(BAUDRATE);
memcpy(sbusData,loc_sbusData,25);
memcpy(channels,loc_channels,18);
memcpy(servos,loc_servos,18);
failsafe_status = DBUS_SIGNAL_OK;
sbus_passthrough = 1;
toChannels = 0;
bufferIndex=0;
feedState = 0;
}
int16_t DJI_DBUS::Channel(uint8_t ch) {
// Read channel data
if ((ch>0)&&(ch<=16)){
return channels[ch-1];
}
else{
return 1023;
}
}
uint8_t DJI_DBUS::DigiChannel(uint8_t ch) {
// Read digital channel data
if ((ch>0) && (ch<=2)) {
return channels[15+ch];
}
else{
return 0;
}
}
void DJI_DBUS::Servo(uint8_t ch, int16_t position) {
// Set servo position
if ((ch>0)&&(ch<=16)) {
if (position>2048) {
position=2048;
}
servos[ch-1] = position;
}
}
void DJI_DBUS::DigiServo(uint8_t ch, uint8_t position) {
// Set digital servo position
if ((ch>0) && (ch<=2)) {
if (position>1) {
position=1;
}
servos[15+ch] = position;
}
}
uint8_t DJI_DBUS::Failsafe(void) {
return failsafe_status;
}
void DJI_DBUS::PassthroughSet(int mode) {
// Set passtrough mode, if true, received channel data is send to servos
sbus_passthrough = mode;
}
int DJI_DBUS::PassthroughRet(void) {
// Return current passthrough mode
return sbus_passthrough;
}
void DJI_DBUS::UpdateServos(void) {
// Send data to servos
// Passtrough mode = false >> send own servo data
// Passtrough mode = true >> send received channel data
uint8_t i;
if (sbus_passthrough==0) {
// clear received channel data
for (i=1; i<24; i++) {
sbusData[i] = 0;
}
// reset counters
ch = 0;
bit_in_servo = 0;
byte_in_sbus = 1;
bit_in_sbus = 0;
// store servo data
for (i=0; i<176; i++) {
if (servos[ch] & (1<<bit_in_servo)) {
sbusData[byte_in_sbus] |= (1<<bit_in_sbus);
}
bit_in_sbus++;
bit_in_servo++;
if (bit_in_sbus == 8) {
bit_in_sbus =0;
byte_in_sbus++;
}
if (bit_in_servo == 11) {
bit_in_servo =0;
ch++;
}
}
// DigiChannel 1
if (channels[16] == 1) {
sbusData[23] |= (1<<0);
}
// DigiChannel 2
if (channels[17] == 1) {
sbusData[23] |= (1<<1);
}
// Failsafe
if (failsafe_status == DBUS_SIGNAL_LOST) {
sbusData[23] |= (1<<2);
}
if (failsafe_status == DBUS_SIGNAL_FAILSAFE) {
sbusData[23] |= (1<<2);
sbusData[23] |= (1<<3);
}
}
// send data out
//serialPort.write(sbusData,25);
//for (i=0;i<25;i++) {
// _serial.write(sbusData[i]);
//}
}
void DJI_DBUS::UpdateChannels(void) {
uint8_t i;
channels[0] = ((sbusData[1]|sbusData[2]<< 8) & 0x07FF);
channels[1] = ((sbusData[2]>>3|sbusData[3]<<5) & 0x07FF);
channels[2] = ((sbusData[3]>>6|sbusData[4]<<2|sbusData[5]<<10) & 0x07FF);
channels[3] = ((sbusData[5]>>1|sbusData[6]<<7) & 0x07FF);
channels[4] = ((sbusData[6]>>4|sbusData[7]<<4) & 0x07FF);
channels[5] = ((sbusData[7]>>7|sbusData[8]<<1|sbusData[9]<<9) & 0x07FF);
channels[6] = ((sbusData[9]>>2|sbusData[10]<<6) & 0x07FF);
channels[7] = ((sbusData[10]>>5|sbusData[11]<<3) & 0x07FF); // & the other 8 + 2 channels if you need them
#ifdef ALL_CHANNELS
channels[8] = ((sbusData[12]|sbusData[13]<< 8) & 0x07FF);
channels[9] = ((sbusData[13]>>3|sbusData[14]<<5) & 0x07FF);
channels[10] = ((sbusData[14]>>6|sbusData[15]<<2|sbusData[16]<<10) & 0x07FF);
channels[11] = ((sbusData[16]>>1|sbusData[17]<<7) & 0x07FF);
channels[12] = ((sbusData[17]>>4|sbusData[18]<<4) & 0x07FF);
channels[13] = ((sbusData[18]>>7|sbusData[19]<<1|sbusData[20]<<9) & 0x07FF);
channels[14] = ((sbusData[20]>>2|sbusData[21]<<6) & 0x07FF);
channels[15] = ((sbusData[21]>>5|sbusData[22]<<3) & 0x07FF);
// DigiChannel 1
if (sbusData[23] & (1<<0)) {
channels[16] = 1;
}
else{
channels[16] = 0;
}
// DigiChannel 2
if (sbusData[23] & (1<<1)) {
channels[17] = 1;
}
else{
channels[17] = 0;
}
#endif
for (i=0; i<8; i++) {
channels[i] = map(channels[i],364,1684,1000,2000);
}
// Failsafe
failsafe_status = DBUS_SIGNAL_OK;
if (sbusData[23] & (1<<2)) {
failsafe_status = DBUS_SIGNAL_FAILSAFE;
}
if (sbusData[23] & (1<<3)) {
failsafe_status = DBUS_SIGNAL_LOST;
}
}
void DJI_DBUS::FeedLine(void){
if (_serial.available() > 24){
while(_serial.available() > 0){
inData = _serial.read();
switch (feedState){
case 0:
if (inData != 0x0f){
while(_serial.available() > 0){//read the contents of in buffer this should resync the transmission
inData = _serial.read();
}
return;
}
else{
bufferIndex = 0;
inBuffer[bufferIndex] = inData;
inBuffer[24] = 0xff;
feedState = 1;
}
break;
case 1:
bufferIndex ++;
inBuffer[bufferIndex] = inData;
if (bufferIndex < 24 && _serial.available() == 0){
feedState = 0;
}
if (bufferIndex == 24){
feedState = 0;
if (inBuffer[0]==0x0f && inBuffer[24] == 0x00){
memcpy(sbusData,inBuffer,25);
toChannels = 1;
}
}
break;
}
}
}
}