Subversion Repositories FlightCtrl

Rev

Rev 1152 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
    Copyright (c) 2008 Stefan Engelke <stefan@tinkerer.eu>

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy,
    modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
   
    $Id: rcdsl.c 62 2008-08-21 16:09:07Z tensor $
   
    RCDSL.H and RCDSL.C is an INOFFICIAL implementation of the
    communication protocol used by DSL receivers of Act Europe.
    The DSL receivers have a serial communication port to connect
    two receivers in diversity mode. Each receiver is sending the
    received servo signals periodically over this port. This fact
    can be used to connect the receiver to the control unit of the
    model via UART instead of evaluating the PPM signal.
    The UART parameters are
    38400 Baud, 8-N-1
   
    If you have any questions, feel free to send me an e-mail.

Connection of DSL to SV1 of FC:
( DSL Pin1 is on side of channel 4 )

1. GND <--> pin 7 (GND)
2. TXD <--> pin 3 (RXD1 Atmega644p)
3. RXD <--> pin 4 (TXD1 Atmega644p) optional
4. 5V  <--> pin 2 (5V)

Do not connect the receiver via PPM-Sumsignal output the same time.

Data are send at 38400baud8n1

Data Frame: |0xFF|0xFF|0x1F|FREQALLOC|??|RSSI|VBAT|??|CRC|10|CH0D1|CH0D0|CH1D1|CH1D0|CRC| ...etc

FREQALLOC = 35, 40, 72
RSSI = 0.. 255 // Received signal strength indicator
VBAT = 0...255 // supply voltage (0.0V.. 8.2V)

Servo Pair:   |0x1X|CHXD1|CHXD0|CHX+1D1|CHX+1D0|CRC|
X is channel index of 1 servo value
D1D0 is servo value as u16 in range of 7373 (1ms) to 14745 (2ms)
there are 8 channels submitted, i.e 4 servo pairs


Frame examples with signel received

FFFF 1F23F079A304AD 1036012B1E6F 122AFB2AECB2 142B4D2B4404 1636872B33CE
FFFF 1F23F079A304AD 1036002B1F6F 122AFE2AEBB0 142B4B2B4406 1636872B33CE
FFFF 1F23F079A304AD 1035FF2B226E 122AFC2AEAB3 142B4E2B4304 1636882B33CD
FFFF 1F23F079A304AD 1036022B1E6E 122AFB2AEEB0 142B4A2B4506 1636872B33CE
FFFF 1F23F079A304AD 1036022B1E6E 122AFE2AEBB0 142B4B2B4406 1636882B33CD
FFFF 1F23F079A304AD 1036012B1E6F 122AFD2AEAB2 142B4E2B4403 1636862B33CF
FFFF 1F23F079A304AD 1036032B1D6E 122AFD2AEBB1 142B4C2B4504 1636862B33CF

Frame examples with no signal received

FFFF 1F23F000A30426
FFFF 1F23F000A30426
FFFF 1F23F000A30426
FFFF 1F23F000A30426
FFFF 1F23F000A30426
FFFF 1F23F000A30426
FFFF 1F23F000A30426
*/


#include "rcdsl.h"

#include "main.h"

volatile unsigned char data_counter   = 0;
volatile unsigned char last_byte      = 0;
unsigned char check_sum      = 0;
unsigned char paket_len      = 0;

#define UART1_BAUDRATE 38400

volatile uint8_t rcdsl_RSSI = 0;
volatile uint8_t rcdsl_battery=0;
volatile uint8_t rcdsl_allocation=0;
volatile uint8_t data[6];

typedef union {
        uint16_t pos[2];
        uint8_t dat[4];
} servos_t;


void rcdsl_init(void) {

    // Save state of status register and disable interrupts
    uint8_t sreg = SREG;
    cli();

    // Set baud rate
    UBRR1 = (uint16_t) ((uint32_t) F_CPU/(16*UART1_BAUDRATE) - 1);
 
 
    // UART Receiver und Transmitter anschalten
    // Data mode 8N1, asynchron
    UCSR1B = (1 << RXEN1) | (1 << TXEN0) | (1 << RXCIE1);
    UCSR1C = (1 << UCSZ11) | (1 << UCSZ10);

    // Flush Receive-Buffer
    do { UDR1; }
    while (UCSR1A & (1 << RXC1));

    // Clear rx and tx interrupt flags
    UCSR1A = (1 << RXC1) | (1 << TXC1);

    // Reactive interrupts, if they were active before
    SREG = sreg;

    printf("\n\rUART 1 initialized.");

}

void rcdsl_new_signal(uint8_t channel, int16_t signal)
// This function is called, when a new servo signal is properly received.
// Parameters: servo  - servo number (0-9)
//             signal - servo signal between 7373 (1ms) and 14745 (2ms)
{
    volatile signed int tmp;
    uint8_t index = channel+1; // Der MK fängt bei 1 an zu zählen
   

    // Signal vom  ACTDSL-Empfänger liegt zwischen
    // 7373 (1ms) und 14745 (2ms).
    signal-= 11059;     // Neutralstellung zur Null verschieben
    signal/= 24;        // Auf MK Skalierung umrechnen

    // Stabiles Signal
    //if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10;}

    tmp = (3 * (PPM_in[index]) + signal) / 4;  
    if(tmp > signal+1) tmp--; else
    if(tmp < signal-1) tmp++;
    if(SenderOkay >= 105)  PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
    else PPM_diff[index] = 0;
    PPM_in[index] = tmp;
    //NewPpmData = 0;
    //NewActData = 1;
    if(index == 4)  {
        //NewActData = 1;
        NewPpmData = 0;
        //PORTC = (PORTC&(~(1<<PC4))) | ((~PORTC)&(1<<PC4));
        //PINC = (1<<PC4);
    }
    //PPM_in[index] = signal;      

}


void rcdsl_incoming_paket(void)
// This function is called within rcdsl_parse_data(), when a complete
// data paket with matching checksum has been received.
{
   
    uint8_t  i;
    static servos_t servos;
    // Look for status headers
    if ((data[0])==0x1F) {
        // Get frequency allocation
        rcdsl_allocation = data[0+1];
        // Get signal quality
        rcdsl_RSSI = data[2+1];
        SenderOkay = (212 * (uint16_t) rcdsl_RSSI) / 128; // have to be scaled approx. by a factor of 1.66 to get 200 at full level
        if(SenderOkay > 255) SenderOkay = 255;
        // Get voltage of battery supply
        rcdsl_battery = data[3+1];
    }

    // Look for signal headers
    if ((data[0]&0xF0)==0x10) {
   
        i = data[0]&0x0F;   // Last 4 bits of the header indicates servo pair

        if (i<10) {
            // Convert byte array to two uint16
            servos.dat[1] = data[1];
            servos.dat[0] = data[2];
            servos.dat[3] = data[3];
            servos.dat[2] = data[4];
           
            rcdsl_new_signal(i  ,  (int16_t)servos.pos[0]);
            rcdsl_new_signal(i+1,  (int16_t)servos.pos[1]);
        }
    }
}


void rcdsl_parse_data(uint8_t b)
// This function should be called externaly, when a new data byte has
// been received over uart.
{

    // Check for sync condition
    if ((b==0xFF) && (last_byte==0xFF)) {
        data_counter = 0;
        check_sum = 0;
        return;
    }
   
    // First byte is cmd
    if (data_counter == 0) {
        if (b==0x1F) paket_len = 5; else paket_len=4;
    }

    // Last byte is checksum
    if (data_counter > paket_len) {
       
        // Calculate checksum
        check_sum = ~check_sum;
        if (check_sum==0xFF) check_sum=0xFE;

        // If it match the received one, then apply data
        if (b==check_sum) rcdsl_incoming_paket();

        // Prepare for a new data paket
        data_counter = 0;
        check_sum = 0;


    // New byte within a paket
    } else {

        data[data_counter]  = b;
        check_sum          += b;
        data_counter++;

    }
           
    // Remember last byte received for detection of sync condition
    last_byte = b;
}


ISR (USART1_RX_vect)
{
    rcdsl_parse_data(UDR1);
}