Subversion Repositories FlightCtrl

Rev

Rev 1152 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1152 grottenfli 1
/*
2
    Copyright (c) 2008 Stefan Engelke <stefan@tinkerer.eu>
3
 
4
    Permission is hereby granted, free of charge, to any person
5
    obtaining a copy of this software and associated documentation
6
    files (the "Software"), to deal in the Software without
7
    restriction, including without limitation the rights to use, copy,
8
    modify, merge, publish, distribute, sublicense, and/or sell copies
9
    of the Software, and to permit persons to whom the Software is
10
    furnished to do so, subject to the following conditions:
11
 
12
    The above copyright notice and this permission notice shall be
13
    included in all copies or substantial portions of the Software.
14
 
15
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
    DEALINGS IN THE SOFTWARE.
23
 
24
    $Id: rcdsl.c 62 2008-08-21 16:09:07Z tensor $
25
 
26
    RCDSL.H and RCDSL.C is an INOFFICIAL implementation of the
27
    communication protocol used by DSL receivers of Act Europe.
28
    The DSL receivers have a serial communication port to connect
29
    two receivers in diversity mode. Each receiver is sending the
30
    received servo signals periodically over this port. This fact
31
    can be used to connect the receiver to the control unit of the
32
    model via UART instead of evaluating the PPM signal.
33
    The UART parameters are
34
    38400 Baud, 8-N-1
35
 
36
    If you have any questions, feel free to send me an e-mail.
37
 
1208 krheinwald 38
Connection of DSL to SV1 of FC:
39
( DSL Pin1 is on side of channel 4 )
40
 
41
1. GND <--> pin 7 (GND)
42
2. TXD <--> pin 3 (RXD1 Atmega644p)
43
3. RXD <--> pin 4 (TXD1 Atmega644p) optional
44
4. 5V  <--> pin 2 (5V)
45
 
46
Do not connect the receiver via PPM-Sumsignal output the same time.
47
 
48
Data are send at 38400baud8n1
49
 
50
Data Frame: |0xFF|0xFF|0x1F|FREQALLOC|??|RSSI|VBAT|??|CRC|10|CH0D1|CH0D0|CH1D1|CH1D0|CRC| ...etc
51
 
52
FREQALLOC = 35, 40, 72
53
RSSI = 0.. 255 // Received signal strength indicator
54
VBAT = 0...255 // supply voltage (0.0V.. 8.2V)
55
 
56
Servo Pair:   |0x1X|CHXD1|CHXD0|CHX+1D1|CHX+1D0|CRC|
57
X is channel index of 1 servo value
58
D1D0 is servo value as u16 in range of 7373 (1ms) to 14745 (2ms)
59
there are 8 channels submitted, i.e 4 servo pairs
60
 
61
 
62
Frame examples with signel received
63
 
64
FFFF 1F23F079A304AD 1036012B1E6F 122AFB2AECB2 142B4D2B4404 1636872B33CE
65
FFFF 1F23F079A304AD 1036002B1F6F 122AFE2AEBB0 142B4B2B4406 1636872B33CE
66
FFFF 1F23F079A304AD 1035FF2B226E 122AFC2AEAB3 142B4E2B4304 1636882B33CD
67
FFFF 1F23F079A304AD 1036022B1E6E 122AFB2AEEB0 142B4A2B4506 1636872B33CE
68
FFFF 1F23F079A304AD 1036022B1E6E 122AFE2AEBB0 142B4B2B4406 1636882B33CD
69
FFFF 1F23F079A304AD 1036012B1E6F 122AFD2AEAB2 142B4E2B4403 1636862B33CF
70
FFFF 1F23F079A304AD 1036032B1D6E 122AFD2AEBB1 142B4C2B4504 1636862B33CF
71
 
72
Frame examples with no signal received
73
 
74
FFFF 1F23F000A30426
75
FFFF 1F23F000A30426
76
FFFF 1F23F000A30426
77
FFFF 1F23F000A30426
78
FFFF 1F23F000A30426
79
FFFF 1F23F000A30426
80
FFFF 1F23F000A30426
1152 grottenfli 81
*/
82
 
83
#include "rcdsl.h"
84
 
85
#include "main.h"
86
 
87
volatile unsigned char data_counter   = 0;
88
volatile unsigned char last_byte      = 0;
89
unsigned char check_sum      = 0;
90
unsigned char paket_len      = 0;
91
 
92
#define UART1_BAUDRATE 38400
93
 
94
volatile uint8_t rcdsl_RSSI = 0;
1208 krheinwald 95
volatile uint8_t rcdsl_battery=0;
96
volatile uint8_t rcdsl_allocation=0;
97
volatile uint8_t data[6];
1152 grottenfli 98
 
99
typedef union {
100
        uint16_t pos[2];
101
        uint8_t dat[4];
102
} servos_t;
103
 
1208 krheinwald 104
 
1152 grottenfli 105
void rcdsl_init(void) {
106
 
107
    // Save state of status register and disable interrupts
108
    uint8_t sreg = SREG;
109
    cli();
110
 
111
    // Set baud rate
112
    UBRR1 = (uint16_t) ((uint32_t) F_CPU/(16*UART1_BAUDRATE) - 1);
113
 
114
 
115
    // UART Receiver und Transmitter anschalten 
116
    // Data mode 8N1, asynchron 
117
    UCSR1B = (1 << RXEN1) | (1 << TXEN0) | (1 << RXCIE1);
118
    UCSR1C = (1 << UCSZ11) | (1 << UCSZ10);
119
 
120
    // Flush Receive-Buffer
121
    do { UDR1; }
122
    while (UCSR1A & (1 << RXC1));
123
 
124
    // Clear rx and tx interrupt flags
125
    UCSR1A = (1 << RXC1) | (1 << TXC1);
126
 
127
    // Reactive interrupts, if they were active before
128
    SREG = sreg;
129
 
1208 krheinwald 130
    printf("\n\rUART 1 initialized.");
131
 
1152 grottenfli 132
}
133
 
134
void rcdsl_new_signal(uint8_t channel, int16_t signal)
135
// This function is called, when a new servo signal is properly received.
136
// Parameters: servo  - servo number (0-9)
137
//             signal - servo signal between 7373 (1ms) and 14745 (2ms)
1208 krheinwald 138
{
1152 grottenfli 139
    volatile signed int tmp;
140
    uint8_t index = channel+1; // Der MK fängt bei 1 an zu zählen
141
 
142
 
143
    // Signal vom  ACTDSL-Empfänger liegt zwischen
144
    // 7373 (1ms) und 14745 (2ms). 
145
    signal-= 11059;     // Neutralstellung zur Null verschieben
146
    signal/= 24;        // Auf MK Skalierung umrechnen
147
 
148
    // Stabiles Signal
149
    //if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10;}
150
 
151
    tmp = (3 * (PPM_in[index]) + signal) / 4;  
152
    if(tmp > signal+1) tmp--; else
153
    if(tmp < signal-1) tmp++;
154
    if(SenderOkay >= 105)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
155
    else PPM_diff[index] = 0;
156
    PPM_in[index] = tmp;
157
    //NewPpmData = 0;
158
    //NewActData = 1;
159
    if(index == 4)  {
160
        //NewActData = 1;
161
        NewPpmData = 0;
162
        //PORTC = (PORTC&(~(1<<PC4))) | ((~PORTC)&(1<<PC4));
163
        //PINC = (1<<PC4);
164
    }
165
    //PPM_in[index] = signal;      
166
 
167
}
168
 
169
 
170
void rcdsl_incoming_paket(void)
171
// This function is called within rcdsl_parse_data(), when a complete 
172
// data paket with matching checksum has been received.
173
{
174
 
175
    uint8_t  i;
176
    static servos_t servos;
177
    // Look for status headers
178
    if ((data[0])==0x1F) {
179
        // Get frequency allocation
180
        rcdsl_allocation = data[0+1];
1208 krheinwald 181
        // Get signal quality 
1152 grottenfli 182
        rcdsl_RSSI = data[2+1];
1208 krheinwald 183
        SenderOkay = (212 * (uint16_t) rcdsl_RSSI) / 128; // have to be scaled approx. by a factor of 1.66 to get 200 at full level
184
        if(SenderOkay > 255) SenderOkay = 255;
1152 grottenfli 185
        // Get voltage of battery supply
186
        rcdsl_battery = data[3+1];
187
    }
188
 
189
    // Look for signal headers
190
    if ((data[0]&0xF0)==0x10) {
191
 
192
        i = data[0]&0x0F;   // Last 4 bits of the header indicates servo pair
193
 
194
        if (i<10) {
195
            // Convert byte array to two uint16
196
            servos.dat[1] = data[1];
197
            servos.dat[0] = data[2];
198
            servos.dat[3] = data[3];
199
            servos.dat[2] = data[4];
200
 
201
            rcdsl_new_signal(i  ,  (int16_t)servos.pos[0]);
202
            rcdsl_new_signal(i+1,  (int16_t)servos.pos[1]);
203
        }
204
    }
205
}
206
 
207
 
208
void rcdsl_parse_data(uint8_t b)
209
// This function should be called externaly, when a new data byte has 
210
// been received over uart.
211
{
212
 
213
    // Check for sync condition
214
    if ((b==0xFF) && (last_byte==0xFF)) {
215
        data_counter = 0;
216
        check_sum = 0;
217
        return;
218
    }
219
 
220
    // First byte is cmd
221
    if (data_counter == 0) {
222
        if (b==0x1F) paket_len = 5; else paket_len=4;
223
    }
224
 
225
    // Last byte is checksum
226
    if (data_counter > paket_len) {
227
 
228
        // Calculate checksum
229
        check_sum = ~check_sum;
230
        if (check_sum==0xFF) check_sum=0xFE;
231
 
232
        // If it match the received one, then apply data
233
        if (b==check_sum) rcdsl_incoming_paket();
234
 
235
        // Prepare for a new data paket
236
        data_counter = 0;
237
        check_sum = 0;
238
 
239
 
240
    // New byte within a paket
241
    } else {
242
 
243
        data[data_counter]  = b;
244
        check_sum          += b;
245
        data_counter++;
246
 
247
    }
248
 
249
    // Remember last byte received for detection of sync condition
250
    last_byte = b;
251
}
252
 
253
 
254
ISR (USART1_RX_vect)
255
{
256
    rcdsl_parse_data(UDR1);
257
}