Subversion Repositories FlightCtrl

Rev

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