Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1968 - 1
#include <stdlib.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
1962 - 4
 
1968 - 5
#include "rc.h"
6
#include "controlMixer.h"
7
#include "configuration.h"
8
#include "commands.h"
2052 - 9
#include "output.h"
1968 - 10
 
2048 - 11
// The channel array is 0-based!
1968 - 12
volatile int16_t PPM_in[MAX_CHANNELS];
13
volatile int16_t PPM_diff[MAX_CHANNELS];
2026 - 14
volatile uint8_t RCQuality;
1968 - 15
uint8_t lastRCCommand = COMMAND_NONE;
16
uint8_t commandTimer = 0;
17
 
18
/***************************************************************
19
 *  16bit timer 1 is used to decode the PPM-Signal            
20
 ***************************************************************/
21
void RC_Init(void) {
22
  uint8_t sreg = SREG;
23
 
24
  // disable all interrupts before reconfiguration
25
  cli();
26
 
27
  // PPM-signal is connected to the Input Capture Pin (PD6) of timer 1
28
  DDRD &= ~(1<<6);
29
  PORTD |= (1<<PORTD6);
30
 
31
  // Channel 5,6,7 is decoded to servo signals at pin PD5 (J3), PD4(J4), PD3(J5)
32
  // set as output
33
  DDRD |= (1<<DDD5) | (1<<DDD4) | (1<<DDD3);
34
  // low level
35
  PORTD &= ~((1<<PORTD5) | (1<<PORTD4) | (1<<PORTD3));
36
 
37
  // PD3 can't be used if 2nd UART is activated
38
  // because TXD1 is at that port
39
  if (CPUType != ATMEGA644P) {
40
    DDRD |= (1<<PORTD3);
41
    PORTD &= ~(1<<PORTD3);
42
  }
43
 
44
  // Timer/Counter1 Control Register A, B, C
45
 
46
  // Normal Mode (bits: WGM13=0, WGM12=0, WGM11=0, WGM10=0)
47
  // Compare output pin A & B is disabled (bits: COM1A1=0, COM1A0=0, COM1B1=0, COM1B0=0)
48
  // Set clock source to SYSCLK/64 (bit: CS12=0, CS11=1, CS10=1)
49
  // Enable input capture noise cancler (bit: ICNC1=1)
50
  // Trigger on positive edge of the input capture pin (bit: ICES1=1),
51
  // Therefore the counter incremets at a clock of 20 MHz/64 = 312.5 kHz or 3.2�s
52
  // The longest period is 0xFFFF / 312.5 kHz = 0.209712 s.
53
  TCCR1A &= ~((1 << COM1A1) | (1 << COM1A0) | (1 << COM1B1) | (1 << COM1B0) | (1 << WGM11) | (1 << WGM10));
54
  TCCR1B &= ~((1 << WGM13) | (1 << WGM12) | (1 << CS12));
55
  TCCR1B |= (1 << CS11) | (1 << CS10) | (1 << ICES1) | (1 << ICNC1);
56
  TCCR1C &= ~((1 << FOC1A) | (1 << FOC1B));
57
 
58
  // Timer/Counter1 Interrupt Mask Register
59
  // Enable Input Capture Interrupt (bit: ICIE1=1)
60
  // Disable Output Compare A & B Match Interrupts (bit: OCIE1B=0, OICIE1A=0)
61
  // Enable Overflow Interrupt (bit: TOIE1=0)
62
  TIMSK1 &= ~((1<<OCIE1B) | (1<<OCIE1A) | (1<<TOIE1));
63
  TIMSK1 |= (1<<ICIE1);
64
 
2019 - 65
  RCQuality = 0;
1968 - 66
 
67
  SREG = sreg;
68
}
69
 
70
/********************************************************************/
71
/*         Every time a positive edge is detected at PD6            */
72
/********************************************************************/
73
/*                               t-Frame
74
    <----------------------------------------------------------------------->
75
     ____   ______   _____   ________                ______    sync gap      ____
76
    |    | |      | |     | |        |              |      |                |
77
    |    | |      | |     | |        |              |      |                |
78
 ___|    |_|      |_|     |_|        |_.............|      |________________|
79
    <-----><-------><------><-----------            <------>                <---
80
 t0       t1      t2       t4                     tn                     t0
81
 
1962 - 82
 The PPM-Frame length is 22.5 ms.
83
 Channel high pulse width range is 0.7 ms to 1.7 ms completed by an 0.3 ms low pulse.
84
 The mininimum time delay of two events coding a channel is ( 0.7 + 0.3) ms = 1 ms.
85
 The maximum time delay of two events coding a channel is ( 1.7 + 0.3) ms = 2 ms.
86
 The minimum duration of all channels at minimum value is  8 * 1 ms = 8 ms.
87
 The maximum duration of all channels at maximum value is  8 * 2 ms = 16 ms.
88
 The remaining time of (22.5 - 8 ms) ms = 14.5 ms  to (22.5 - 16 ms) ms = 6.5 ms is
89
 the syncronization gap.
90
 */
2048 - 91
ISR(TIMER1_CAPT_vect) { // typical rate of 1 ms to 2 ms
1962 - 92
  int16_t signal = 0, tmp;
93
  static int16_t index;
94
  static uint16_t oldICR1 = 0;
95
 
96
  // 16bit Input Capture Register ICR1 contains the timer value TCNT1
97
  // at the time the edge was detected
98
 
99
  // calculate the time delay to the previous event time which is stored in oldICR1
100
  // calculatiing the difference of the two uint16_t and converting the result to an int16_t
101
  // implicit handles a timer overflow 65535 -> 0 the right way.
102
  signal = (uint16_t) ICR1 - oldICR1;
103
  oldICR1 = ICR1;
104
 
105
  //sync gap? (3.52 ms < signal < 25.6 ms)
106
  if ((signal > 1100) && (signal < 8000)) {
1968 - 107
    index = 0;
1962 - 108
  } else { // within the PPM frame
1968 - 109
    if (index < MAX_CHANNELS) { // PPM24 supports 12 channels
1962 - 110
      // check for valid signal length (0.8 ms < signal < 2.1984 ms)
111
      // signal range is from 1.0ms/3.2us = 312 to 2.0ms/3.2us = 625
112
      if ((signal > 250) && (signal < 687)) {
113
        // shift signal to zero symmetric range  -154 to 159
2017 - 114
        signal -= 470; // offset of 1.4912 ms ??? (469 * 3.2us = 1.5008 ms)
1962 - 115
        // check for stable signal
116
        if (abs(signal - PPM_in[index]) < 6) {
2019 - 117
          if (RCQuality < 200)
118
            RCQuality += 10;
1962 - 119
          else
2019 - 120
            RCQuality = 200;
1962 - 121
        }
2051 - 122
        // If signal is the same as before +/- 1, just keep it there. Naah lets get rid of this slimy sticy stuff.
123
        // if (signal >= PPM_in[index] - 1 && signal <= PPM_in[index] + 1) {
1962 - 124
          // In addition, if the signal is very close to 0, just set it to 0.
2051 - 125
        if (signal >= -1 && signal <= 1) {
126
          tmp = 0;
127
        //} else {
128
        //  tmp = PPM_in[index];
129
        //  }
1962 - 130
        } else
131
          tmp = signal;
132
        // calculate signal difference on good signal level
2019 - 133
        if (RCQuality >= 195)
1962 - 134
          PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; // cut off lower 3 bit for nois reduction
135
        else
136
          PPM_diff[index] = 0;
137
        PPM_in[index] = tmp; // update channel value
138
      }
139
      index++; // next channel
140
      // demux sum signal for channels 5 to 7 to J3, J4, J5
141
      // TODO: General configurability of this R/C channel forwarding. Or remove it completely - the
142
      // channels are usually available at the receiver anyway.
143
      // if(index == 5) J3HIGH; else J3LOW;
144
      // if(index == 6) J4HIGH; else J4LOW;
145
      // if(CPUType != ATMEGA644P) // not used as TXD1
146
      //  {
147
      //    if(index == 7) J5HIGH; else J5LOW;
148
      //  }
149
    }
150
  }
151
}
152
 
153
#define RCChannel(dimension) PPM_in[channelMap.channels[dimension]]
154
#define RCDiff(dimension) PPM_diff[channelMap.channels[dimension]]
155
#define COMMAND_THRESHOLD 85
156
#define COMMAND_CHANNEL_VERTICAL CH_THROTTLE
157
#define COMMAND_CHANNEL_HORIZONTAL CH_YAW
158
 
159
// Internal.
160
uint8_t RC_getStickCommand(void) {
161
  if (RCChannel(COMMAND_CHANNEL_VERTICAL) > COMMAND_THRESHOLD) {
162
    // vertical is up
163
    if (RCChannel(COMMAND_CHANNEL_HORIZONTAL) > COMMAND_THRESHOLD)
164
      return COMMAND_GYROCAL;
165
    if (RCChannel(COMMAND_CHANNEL_HORIZONTAL) < -COMMAND_THRESHOLD)
166
      return COMMAND_ACCCAL;
167
    return COMMAND_NONE;
168
  } else if (RCChannel(COMMAND_CHANNEL_VERTICAL) < -COMMAND_THRESHOLD) {
169
    // vertical is down
170
    if (RCChannel(COMMAND_CHANNEL_HORIZONTAL) > COMMAND_THRESHOLD)
171
      return COMMAND_STOP;
172
    if (RCChannel(COMMAND_CHANNEL_HORIZONTAL) < -COMMAND_THRESHOLD)
173
      return COMMAND_START;
174
    return COMMAND_NONE;
175
  }
176
  // vertical is around center
177
  return COMMAND_NONE;
178
}
179
 
180
/*
2048 - 181
 * Get Pitch, Roll, Throttle, Yaw values
1962 - 182
 */
2048 - 183
void RC_periodicTaskAndPRTY(int16_t* PRTY) {
1962 - 184
  int16_t tmp1, tmp2;
2019 - 185
  if (RCQuality) {
186
    RCQuality--;
2048 - 187
    PRTY[CONTROL_PITCH]     = RCChannel(CH_PITCH) * staticParams.stickP + RCDiff(CH_PITCH) * staticParams.stickD;
188
    PRTY[CONTROL_ROLL]      = RCChannel(CH_ROLL) * staticParams.stickP + RCDiff(CH_ROLL) * staticParams.stickD;
189
    PRTY[CONTROL_THROTTLE]  = RCChannel(CH_THROTTLE) + RCDiff(CH_THROTTLE) * staticParams.stickThrottleD + 120;
190
    if (PRTY[CONTROL_THROTTLE] < 0) PRTY[CONTROL_THROTTLE] = 0; // Throttle is non negative.
191
    tmp1 = -RCChannel(CH_YAW) - RCDiff(CH_YAW);
192
    // exponential stick sensitivity in yawing rate
2052 - 193
    tmp2 = (int32_t)staticParams.stickYawP * ((int32_t)tmp1 * abs(tmp1)) >> 9; // expo  y = ax + bx^2
2048 - 194
    tmp2 += (staticParams.stickYawP * tmp1) >> 2;
195
    PRTY[CONTROL_YAW] = tmp2;
196
 
1962 - 197
    uint8_t command = RC_getStickCommand();
198
    if (lastRCCommand == command) {
199
      // Keep timer from overrunning.
200
      if (commandTimer < COMMAND_TIMER)
201
        commandTimer++;
202
    } else {
203
      // There was a change.
204
      lastRCCommand = command;
205
      commandTimer = 0;
206
    }
207
  }
2045 - 208
  debugOut.analog[18] = RCQuality;
1962 - 209
}
210
 
211
/*
212
 * Get other channel value
213
 */
214
int16_t RC_getVariable(uint8_t varNum) {
215
  if (varNum < 4)
216
    // 0th variable is 5th channel (1-based) etc.
1986 - 217
    return RCChannel(varNum + CH_POTS) + POT_OFFSET;
1962 - 218
  /*
219
   * Let's just say:
1986 - 220
   * The RC variable i is hardwired to channel i, i>=4
1962 - 221
   */
1986 - 222
  return PPM_in[varNum] + POT_OFFSET;
1962 - 223
}
224
 
225
uint8_t RC_getSignalQuality(void) {
2019 - 226
  if (RCQuality >= 160)
1962 - 227
    return SIGNAL_GOOD;
2019 - 228
  if (RCQuality >= 140)
1962 - 229
    return SIGNAL_OK;
2019 - 230
  if (RCQuality >= 120)
1962 - 231
    return SIGNAL_BAD;
232
  return SIGNAL_LOST;
233
}
234
 
235
/*
236
 * To should fired only when the right stick is in the center position.
237
 * This will cause the value of pitch and roll stick to be adjusted
238
 * to zero (not just to near zero, as per the assumption in rc.c
239
 * about the rc signal. I had values about 50..70 with a Futaba
240
 * R617 receiver.) This calibration is not strictly necessary, but
241
 * for control logic that depends on the exact (non)center position
242
 * of a stick, it may be useful.
243
 */
244
void RC_calibrate(void) {
245
  // Do nothing.
246
}
247
 
248
/*
249
 if (staticParams.GlobalConfig & CFG_HEADING_HOLD) {
250
 // In HH, it s OK to trim the R/C. The effect should not be conteracted here.
251
 stickOffsetPitch = stickOffsetRoll = 0;
252
 } else {
253
 stickOffsetPitch = RCChannel(CH_PITCH) * staticParams.StickP;
254
 stickOffsetRoll = RCChannel(CH_ROLL)   * staticParams.StickP;
255
 }
256
 }
257
 */
258
 
259
uint8_t RC_getCommand(void) {
260
  if (commandTimer == COMMAND_TIMER) {
261
    // Stick has been held long enough; command committed.
262
    return lastRCCommand;
263
  }
264
  // Not yet sure what the command is.
265
  return COMMAND_NONE;
266
}
267
 
268
/*
269
 * Command arguments on R/C:
270
 * 2--3--4
271
 * |     |  +
272
 * 1  0  5  ^ 0
273
 * |     |  |  
274
 * 8--7--6
275
 *    
276
 * + <--
277
 *    0
278
 *
279
 * Not in any of these positions: 0
280
 */
281
 
282
#define ARGUMENT_THRESHOLD 70
283
#define ARGUMENT_CHANNEL_VERTICAL CH_PITCH
284
#define ARGUMENT_CHANNEL_HORIZONTAL CH_ROLL
285
 
286
uint8_t RC_getArgument(void) {
287
  if (RCChannel(ARGUMENT_CHANNEL_VERTICAL) > ARGUMENT_THRESHOLD) {
288
    // vertical is up
289
    if (RCChannel(ARGUMENT_CHANNEL_HORIZONTAL) > ARGUMENT_THRESHOLD)
290
      return 2;
291
    if (RCChannel(ARGUMENT_CHANNEL_HORIZONTAL) < -ARGUMENT_THRESHOLD)
292
      return 4;
293
    return 3;
294
  } else if (RCChannel(ARGUMENT_CHANNEL_VERTICAL) < -ARGUMENT_THRESHOLD) {
295
    // vertical is down
296
    if (RCChannel(ARGUMENT_CHANNEL_HORIZONTAL) > ARGUMENT_THRESHOLD)
297
      return 8;
298
    if (RCChannel(ARGUMENT_CHANNEL_HORIZONTAL) < -ARGUMENT_THRESHOLD)
299
      return 6;
300
    return 7;
301
  } else {
302
    // vertical is around center
303
    if (RCChannel(ARGUMENT_CHANNEL_HORIZONTAL) > ARGUMENT_THRESHOLD)
304
      return 1;
305
    if (RCChannel(ARGUMENT_CHANNEL_HORIZONTAL) < -ARGUMENT_THRESHOLD)
306
      return 5;
307
    return 0;
308
  }
309
}
310
 
2052 - 311
#ifdef USE_MK3MAG
1962 - 312
/*
2048 - 313
 * For each time the stick is pulled, returns true.
314
 */
1962 - 315
uint8_t RC_testCompassCalState(void) {
2048 - 316
  static uint8_t stickPulled = 1;
1962 - 317
  // if pitch is centered or top set stick to zero
318
  if (RCChannel(CH_PITCH) > -20)
2048 - 319
    stickPulled = 0;
1962 - 320
  // if pitch is down trigger to next cal state
2048 - 321
  if ((RCChannel(CH_PITCH) < -70) && !stickPulled) {
322
    stickPulled = 1;
1962 - 323
    return 1;
324
  }
325
  return 0;
326
}
2052 - 327
#endif
328
 
1962 - 329
/*
330
 * Abstract controls are not used at the moment.
331
 t_control rc_control = {
332
 RC_getPitch,
333
 RC_getRoll,
334
 RC_getYaw,
335
 RC_getThrottle,
336
 RC_getSignalQuality,
337
 RC_calibrate
338
 };
339
 */