Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
2108 - 1
#include <stdlib.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
 
5
#include "rc.h"
6
#include "controlMixer.h"
7
#include "configuration.h"
8
#include "commands.h"
9
#include "output.h"
10
 
11
// The channel array is 0-based!
12
volatile int16_t PPM_in[MAX_CHANNELS];
13
volatile uint8_t RCQuality;
14
 
15
uint8_t lastRCCommand = COMMAND_NONE;
16
uint8_t lastFlightMode = FLIGHT_MODE_NONE;
17
 
2109 - 18
#define TIME(s) ((int16_t)(((long)F_CPU/(long)8000)*(float)s))
19
 
2108 - 20
/***************************************************************
21
 *  16bit timer 1 is used to decode the PPM-Signal            
22
 ***************************************************************/
23
void RC_Init(void) {
24
  uint8_t sreg = SREG;
25
 
26
  // disable all interrupts before reconfiguration
27
  cli();
28
 
29
  // PPM-signal is connected to the Input Capture Pin (PD6) of timer 1
2109 - 30
  DDRB &= ~(1<<0);
31
  PORTB |= (1<<PORTB0);
2108 - 32
 
33
  // Timer/Counter1 Control Register A, B, C
34
  // Normal Mode (bits: WGM13=0, WGM12=0, WGM11=0, WGM10=0)
35
  // Compare output pin A & B is disabled (bits: COM1A1=0, COM1A0=0, COM1B1=0, COM1B0=0)
36
  // Set clock source to SYSCLK/64 (bit: CS12=0, CS11=1, CS10=1)
37
  // Enable input capture noise cancler (bit: ICNC1=1)
38
  // Trigger on positive edge of the input capture pin (bit: ICES1=1),
39
  // Therefore the counter incremets at a clock of 20 MHz/64 = 312.5 kHz or 3.2�s
40
  // The longest period is 0xFFFF / 312.5 kHz = 0.209712 s.
41
  TCCR1A &= ~((1 << COM1A1) | (1 << COM1A0) | (1 << COM1B1) | (1 << COM1B0) | (1 << WGM11) | (1 << WGM10));
42
  TCCR1B &= ~((1 << WGM13) | (1 << WGM12) | (1 << CS12));
2109 - 43
  TCCR1B |= (1 << CS11) | (1 << ICES1) | (1 << ICNC1);
2108 - 44
  TCCR1C &= ~((1 << FOC1A) | (1 << FOC1B));
45
 
46
  // Timer/Counter1 Interrupt Mask Register
47
  // Enable Input Capture Interrupt (bit: ICIE1=1)
48
  // Disable Output Compare A & B Match Interrupts (bit: OCIE1B=0, OICIE1A=0)
49
  // Enable Overflow Interrupt (bit: TOIE1=0)
50
  TIMSK1 &= ~((1<<OCIE1B) | (1<<OCIE1A) | (1<<TOIE1));
51
  TIMSK1 |= (1<<ICIE1);
52
 
53
  RCQuality = 0;
54
 
55
  SREG = sreg;
56
}
57
 
58
/********************************************************************/
59
/*         Every time a positive edge is detected at PD6            */
60
/********************************************************************/
61
/*                               t-Frame
62
    <----------------------------------------------------------------------->
63
     ____   ______   _____   ________                ______    sync gap      ____
64
    |    | |      | |     | |        |              |      |                |
65
    |    | |      | |     | |        |              |      |                |
66
 ___|    |_|      |_|     |_|        |_.............|      |________________|
67
    <-----><-------><------><-----------            <------>                <---
68
 t0       t1      t2       t4                     tn                     t0
69
 
70
 The PPM-Frame length is 22.5 ms.
71
 Channel high pulse width range is 0.7 ms to 1.7 ms completed by an 0.3 ms low pulse.
72
 The mininimum time delay of two events coding a channel is ( 0.7 + 0.3) ms = 1 ms.
73
 The maximum time delay of two events coding a channel is ( 1.7 + 0.3) ms = 2 ms.
74
 The minimum duration of all channels at minimum value is  8 * 1 ms = 8 ms.
75
 The maximum duration of all channels at maximum value is  8 * 2 ms = 16 ms.
76
 The remaining time of (22.5 - 8 ms) ms = 14.5 ms  to (22.5 - 16 ms) ms = 6.5 ms is
77
 the syncronization gap.
78
 */
79
ISR(TIMER1_CAPT_vect) { // typical rate of 1 ms to 2 ms
2109 - 80
  int16_t signal, tmp;
2108 - 81
  static int16_t index;
82
  static uint16_t oldICR1 = 0;
83
 
84
  // 16bit Input Capture Register ICR1 contains the timer value TCNT1
85
  // at the time the edge was detected
86
 
87
  // calculate the time delay to the previous event time which is stored in oldICR1
88
  // calculatiing the difference of the two uint16_t and converting the result to an int16_t
89
  // implicit handles a timer overflow 65535 -> 0 the right way.
90
  signal = (uint16_t) ICR1 - oldICR1;
91
  oldICR1 = ICR1;
92
 
2109 - 93
  //sync gap? (3.5 ms < signal < 25.6 ms)
94
  if (signal > TIME(3.5)) {
95
    // never happens...
2108 - 96
    index = 0;
97
  } else { // within the PPM frame
98
    if (index < MAX_CHANNELS) { // PPM24 supports 12 channels
2109 - 99
      // check for valid signal length (0.8 ms < signal < 2.2 ms)
100
      if ((signal >= TIME(0.8)) && (signal < TIME(2.2))) {
2108 - 101
        // shift signal to zero symmetric range  -154 to 159
2109 - 102
        //signal -= 3750; // theoretical value
103
        signal -= (TIME(1.5) + RC_TRIM); // best value with my Futaba in zero trim.
2108 - 104
        // check for stable signal
2109 - 105
        if (abs(signal - PPM_in[index]) < TIME(0.05)) {
2108 - 106
          if (RCQuality < 200)
107
            RCQuality += 10;
108
          else
109
            RCQuality = 200;
110
        }
111
        // If signal is the same as before +/- 1, just keep it there. Naah lets get rid of this slimy sticy stuff.
112
        // if (signal >= PPM_in[index] - 1 && signal <= PPM_in[index] + 1) {
113
          // In addition, if the signal is very close to 0, just set it to 0.
114
        if (signal >= -1 && signal <= 1) {
115
          tmp = 0;
116
        //} else {
117
        //  tmp = PPM_in[index];
118
        //  }
119
        } else
120
          tmp = signal;
121
        PPM_in[index] = tmp; // update channel value
122
      }
123
      index++; // next channel
124
      // demux sum signal for channels 5 to 7 to J3, J4, J5
125
      // TODO: General configurability of this R/C channel forwarding. Or remove it completely - the
126
      // channels are usually available at the receiver anyway.
127
      // if(index == 5) J3HIGH; else J3LOW;
128
      // if(index == 6) J4HIGH; else J4LOW;
129
      // if(CPUType != ATMEGA644P) // not used as TXD1
130
      //  {
131
      //    if(index == 7) J5HIGH; else J5LOW;
132
      //  }
133
    }
134
  }
135
}
136
 
137
#define RCChannel(dimension) PPM_in[channelMap.channels[dimension]]
138
#define COMMAND_CHANNEL_VERTICAL CH_THROTTLE
139
#define COMMAND_CHANNEL_HORIZONTAL CH_YAW
140
 
141
uint8_t getControlModeSwitch(void) {
2109 - 142
        int16_t channel = RCChannel(CH_MODESWITCH);
143
        uint8_t flightMode = channel < -TIME(0.17) ? FLIGHT_MODE_MANUAL : (channel > TIME(0.17) ? FLIGHT_MODE_ANGLES : FLIGHT_MODE_RATE);
2108 - 144
        return flightMode;
145
}
146
 
147
// Gyro calibration is performed as.... well mode switch with no throttle and no airspeed would be nice.
148
// Maybe simply: Very very low throttle.
149
// Throttle xlow for COMMAND_TIMER: GYROCAL (once).
150
// mode switched: CHMOD
151
 
152
uint8_t RC_getCommand(void) {
153
        uint8_t flightMode = getControlModeSwitch();
154
 
155
        if (lastFlightMode != flightMode) {
156
                lastFlightMode = flightMode;
157
                lastRCCommand = COMMAND_CHMOD;
158
                return lastRCCommand;
159
        }
160
 
161
        int16_t channel = RCChannel(CH_THROTTLE);
162
 
2109 - 163
        if (channel <= -TIME(0.55)) {
164
          lastRCCommand = COMMAND_GYROCAL;
165
          debugOut.analog[17] = 1;
2108 - 166
        } else {
167
          lastRCCommand = COMMAND_NONE;
2109 - 168
          debugOut.analog[17] = 0;
2108 - 169
        }
170
        return lastRCCommand;
171
}
172
 
173
uint8_t RC_getArgument(void) {
174
        return lastFlightMode;
175
}
176
 
177
/*
178
 * Get Pitch, Roll, Throttle, Yaw values
179
 */
180
void RC_periodicTaskAndPRYT(int16_t* PRYT) {
181
  if (RCQuality) {
182
    RCQuality--;
183
 
184
    debugOut.analog[20] = RCChannel(CH_ELEVATOR);
185
    debugOut.analog[21] = RCChannel(CH_AILERONS);
186
    debugOut.analog[22] = RCChannel(CH_RUDDER);
187
    debugOut.analog[23] = RCChannel(CH_THROTTLE);
188
 
2109 - 189
    PRYT[CONTROL_ELEVATOR]   = RCChannel(CH_ELEVATOR) / RC_SCALING;
190
    PRYT[CONTROL_AILERONS]   = RCChannel(CH_AILERONS) / RC_SCALING;
191
    PRYT[CONTROL_RUDDER]     = RCChannel(CH_RUDDER)   / RC_SCALING;
192
    PRYT[CONTROL_THROTTLE]   = RCChannel(CH_THROTTLE) / RC_SCALING;
2108 - 193
  } // if RCQuality is no good, we just do nothing.
194
}
195
 
196
/*
197
 * Get other channel value
198
 */
199
int16_t RC_getVariable(uint8_t varNum) {
200
  if (varNum < 4)
201
    // 0th variable is 5th channel (1-based) etc.
2109 - 202
    return (RCChannel(varNum + CH_POTS) >> 2) + VARIABLE_OFFSET;
2108 - 203
  /*
204
   * Let's just say:
205
   * The RC variable i is hardwired to channel i, i>=4
206
   */
2109 - 207
  return (PPM_in[varNum] >> 2) + VARIABLE_OFFSET;
2108 - 208
}
209
 
210
uint8_t RC_getSignalQuality(void) {
211
  if (RCQuality >= 160)
212
    return SIGNAL_GOOD;
213
  if (RCQuality >= 140)
214
    return SIGNAL_OK;
215
  if (RCQuality >= 120)
216
    return SIGNAL_BAD;
217
  return SIGNAL_LOST;
218
}
219
 
220
/*
221
 * To should fired only when the right stick is in the center position.
222
 * This will cause the value of pitch and roll stick to be adjusted
223
 * to zero (not just to near zero, as per the assumption in rc.c
224
 * about the rc signal. I had values about 50..70 with a Futaba
225
 * R617 receiver.) This calibration is not strictly necessary, but
226
 * for control logic that depends on the exact (non)center position
227
 * of a stick, it may be useful.
228
 */
229
void RC_calibrate(void) {
230
  // Do nothing.
231
}