Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | 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
 
38
*/
39
 
40
#include "rcdsl.h"
41
 
42
#include "main.h"
43
 
44
volatile unsigned char data_counter   = 0;
45
volatile unsigned char last_byte      = 0;
46
unsigned char check_sum      = 0;
47
unsigned char paket_len      = 0;
48
 
49
#define UART1_BAUDRATE 38400
50
 
51
volatile uint8_t rcdsl_RSSI = 0;
52
uint8_t rcdsl_battery=0;
53
uint8_t rcdsl_allocation=0;
54
uint8_t data[6];
55
 
56
typedef union {
57
        uint16_t pos[2];
58
        uint8_t dat[4];
59
} servos_t;
60
 
61
void rcdsl_init(void) {
62
 
63
    // Save state of status register and disable interrupts
64
    uint8_t sreg = SREG;
65
    cli();
66
 
67
    // Set baud rate
68
    UBRR1 = (uint16_t) ((uint32_t) F_CPU/(16*UART1_BAUDRATE) - 1);
69
 
70
 
71
    // UART Receiver und Transmitter anschalten 
72
    // Data mode 8N1, asynchron 
73
    UCSR1B = (1 << RXEN1) | (1 << TXEN0) | (1 << RXCIE1);
74
    UCSR1C = (1 << UCSZ11) | (1 << UCSZ10);
75
 
76
    // Flush Receive-Buffer
77
    do { UDR1; }
78
    while (UCSR1A & (1 << RXC1));
79
 
80
    // Clear rx and tx interrupt flags
81
    UCSR1A = (1 << RXC1) | (1 << TXC1);
82
 
83
    // Reactive interrupts, if they were active before
84
    SREG = sreg;
85
 
86
}
87
 
88
void rcdsl_new_signal(uint8_t channel, int16_t signal)
89
// This function is called, when a new servo signal is properly received.
90
// Parameters: servo  - servo number (0-9)
91
//             signal - servo signal between 7373 (1ms) and 14745 (2ms)
92
{  
93
    volatile signed int tmp;
94
    uint8_t index = channel+1; // Der MK fängt bei 1 an zu zählen
95
 
96
 
97
    // Signal vom  ACTDSL-Empfänger liegt zwischen
98
    // 7373 (1ms) und 14745 (2ms). 
99
    signal-= 11059;     // Neutralstellung zur Null verschieben
100
    signal/= 24;        // Auf MK Skalierung umrechnen
101
 
102
    // Stabiles Signal
103
    //if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10;}
104
 
105
    tmp = (3 * (PPM_in[index]) + signal) / 4;  
106
    if(tmp > signal+1) tmp--; else
107
    if(tmp < signal-1) tmp++;
108
    if(SenderOkay >= 105)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
109
    else PPM_diff[index] = 0;
110
    PPM_in[index] = tmp;
111
    //NewPpmData = 0;
112
    //NewActData = 1;
113
    if(index == 4)  {
114
        //NewActData = 1;
115
        NewPpmData = 0;
116
        //PORTC = (PORTC&(~(1<<PC4))) | ((~PORTC)&(1<<PC4));
117
        //PINC = (1<<PC4);
118
    }
119
    //PPM_in[index] = signal;      
120
 
121
}
122
 
123
 
124
void rcdsl_incoming_paket(void)
125
// This function is called within rcdsl_parse_data(), when a complete 
126
// data paket with matching checksum has been received.
127
{
128
 
129
    uint8_t  i;
130
    static servos_t servos;
131
    // Look for status headers
132
    if ((data[0])==0x1F) {
133
        // Get frequency allocation
134
        rcdsl_allocation = data[0+1];
135
        // Get signal quality
136
        rcdsl_RSSI = data[2+1];
137
        // Get voltage of battery supply
138
        rcdsl_battery = data[3+1];
139
    }
140
 
141
    // Look for signal headers
142
    if ((data[0]&0xF0)==0x10) {
143
 
144
        i = data[0]&0x0F;   // Last 4 bits of the header indicates servo pair
145
 
146
        if (i<10) {
147
            // Convert byte array to two uint16
148
            servos.dat[1] = data[1];
149
            servos.dat[0] = data[2];
150
            servos.dat[3] = data[3];
151
            servos.dat[2] = data[4];
152
 
153
            rcdsl_new_signal(i  ,  (int16_t)servos.pos[0]);
154
            rcdsl_new_signal(i+1,  (int16_t)servos.pos[1]);
155
        }
156
    }
157
}
158
 
159
 
160
void rcdsl_parse_data(uint8_t b)
161
// This function should be called externaly, when a new data byte has 
162
// been received over uart.
163
{
164
 
165
    // Check for sync condition
166
    if ((b==0xFF) && (last_byte==0xFF)) {
167
        data_counter = 0;
168
        check_sum = 0;
169
        return;
170
    }
171
 
172
    // First byte is cmd
173
    if (data_counter == 0) {
174
        if (b==0x1F) paket_len = 5; else paket_len=4;
175
    }
176
 
177
    // Last byte is checksum
178
    if (data_counter > paket_len) {
179
 
180
        // Calculate checksum
181
        check_sum = ~check_sum;
182
        if (check_sum==0xFF) check_sum=0xFE;
183
 
184
        // If it match the received one, then apply data
185
        if (b==check_sum) rcdsl_incoming_paket();
186
 
187
        // Prepare for a new data paket
188
        data_counter = 0;
189
        check_sum = 0;
190
 
191
 
192
    // New byte within a paket
193
    } else {
194
 
195
        data[data_counter]  = b;
196
        check_sum          += b;
197
        data_counter++;
198
 
199
    }
200
 
201
    // Remember last byte received for detection of sync condition
202
    last_byte = b;
203
}
204
 
205
 
206
ISR (USART1_RX_vect)
207
{
208
    rcdsl_parse_data(UDR1);
209
}