Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
2108 - 1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include "eeprom.h"
4
#include "output.h"
5
#include "flight.h"
6
#include "attitude.h"
7
#include "timer2.h"
8
 
9
// #define COARSERESOLUTION 1
10
 
11
#ifdef COARSERESOLUTION
2135 - 12
#define NEUTRAL_PULSELENGTH ((int16_t)(F_CPU/32000*1.5f + 0.5f))
2139 - 13
#define SERVO_NORMAL_LIMIT ((int16_t)(F_CPU/32000*0.5f + 0.5f))
14
#define SERVO_ABS_LIMIT ((int16_t)(F_CPU/32000*0.8f + 0.5f))
15
//#define SCALE_FACTOR 4
2108 - 16
#define CS2 ((1<<CS21)|(1<<CS20))
17
 
18
#else
2135 - 19
#define NEUTRAL_PULSELENGTH ((int16_t)(F_CPU/8000.0f * 1.5f + 0.5f))
2139 - 20
#define SERVO_NORMAL_LIMIT ((int16_t)(F_CPU/8000.0f * 0.5f + 0.5f))
21
#define SERVO_ABS_LIMIT ((int16_t)(F_CPU/8000.0f * 0.8f + 0.5f))
22
//#define SCALE_FACTOR 16
2108 - 23
#define CS2 (1<<CS21)
24
#endif
25
 
2139 - 26
#define FRAMELENGTH ((uint16_t)(NEUTRAL_PULSELENGTH + SERVO_ABS_LIMIT) * (uint16_t)staticParams.servoCount + 128)
2108 - 27
 
28
volatile uint16_t servoValues[MAX_SERVOS];
2139 - 29
//volatile uint8_t recalculateServoTimes = 0;
30
//volatile uint16_t previousManualValues[2];
2108 - 31
 
2133 - 32
#define HEF4017R_ON     PORTD |=  (1<<PORTD3)
33
#define HEF4017R_OFF    PORTD &= ~(1<<PORTD3)
2108 - 34
 
35
/*****************************************************
36
 *              Initialize Timer 2
37
 *****************************************************/
38
void timer2_init(void) {
39
    uint8_t sreg = SREG;
40
 
41
    // disable all interrupts before reconfiguration
42
    cli();
43
 
2135 - 44
    // set PD7 as output of the 4017 clk
2109 - 45
    DDRB |= (1 << DDB3);
46
    PORTB &= ~(1 << PORTB3); // set PD7 to low
2108 - 47
 
2109 - 48
//  oc2b  DDRD |= (1 << DDD4); // set PC6 as output (Reset for HEF4017)
2133 - 49
    DDRD |= (1 << DDD3); // set PC6 as output (Reset for HEF4017)
50
    HEF4017R_ON; // reset
2108 - 51
 
52
    // Timer/Counter 2 Control Register A
53
    // Timer Mode is CTC (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 0)
54
    // PD3: Output OCR2 match, (Bits: COM2B1 = 1, COM2B0 = 0)
55
    // PB3: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
2109 - 56
    // ardu TCCR2A &= ~((1 << COM2B0) | (1 << COM2A1) | (1 << COM2A0) | (1 << WGM20) | (1 << WGM22));
57
    // ardu TCCR2A |= (1 << COM2B1) | (1 << WGM21);
58
    TCCR2A &= ~((1 << COM2A0) | (1 << COM2B1) | (1 << COM2B0) | (1 << WGM20) | (1 << WGM22));
59
    TCCR2A |= (1 << COM2A1) | (1 << WGM21);
2108 - 60
 
61
    // Timer/Counter 2 Control Register B
62
 
63
    // Set clock divider for timer 2 to 20MHz / 8 = 2.5 MHz
64
    // The timer increments from 0x00 to 0xFF with an update rate of 2.5 kHz or 0.4 us
65
    // hence the timer overflow interrupt frequency is 625 kHz / 256 = 9.765 kHz or 0.1024ms
66
 
67
    TCCR2B &= ~((1 << FOC2A) | (1 << FOC2B) | (1 << CS20) | (1 << CS21) | (1 << CS22));
68
    TCCR2B |= CS2;
69
 
70
    // Initialize the Timer/Counter 2 Register
71
    TCNT2 = 0;
72
 
73
    // Initialize the Output Compare Register A used for signal generation on port PD7.
74
    OCR2A = 255;
75
 
76
    // Timer/Counter 2 Interrupt Mask Register
77
    // Enable timer output compare match A Interrupt only
78
    TIMSK2 &= ~((1 << OCIE2B) | (1 << TOIE2));
79
    TIMSK2 |= (1 << OCIE2A);
80
 
2139 - 81
    //for (uint8_t axis=0; axis<2; axis++)
82
    //  previousManualValues[axis] = dynamicParams.gimbalServoManualControl[axis] * SCALE_FACTOR;
2108 - 83
 
2139 - 84
    for (uint8_t i=0; i<MAX_SERVOS; i++)
85
        servoValues[i] = NEUTRAL_PULSELENGTH;
86
 
2108 - 87
    SREG = sreg;
88
}
89
 
90
/*****************************************************
91
 * Control (camera gimbal etc.) servos
2139 - 92
 * Makes no sense as long as we have no acc. reference.
2108 - 93
 *****************************************************/
2139 - 94
/*
2108 - 95
int16_t calculateStabilizedServoAxis(uint8_t axis) {
96
  int32_t value = attitude[axis] >> STABILIZATION_LOG_DIVIDER; // between -500000 to 500000 extreme limits. Just about
97
  // With full blast on stabilization gain (255) we want to convert a delta of, say, 125000 to 2000.
98
  // That is a divisor of about 1<<14. Same conclusion as H&I.
2132 - 99
  value *= staticParams.gimbalServoConfigurations[axis].stabilizationFactor;
2108 - 100
  value = value >> 8;
2132 - 101
  if (staticParams.gimbalServoConfigurations[axis].flags & SERVO_STABILIZATION_REVERSE)
2108 - 102
    return -value;
103
  return value;
104
}
105
 
106
// With constant-speed limitation.
107
uint16_t calculateManualServoAxis(uint8_t axis, uint16_t manualValue) {
108
  int16_t diff = manualValue - previousManualValues[axis];
2132 - 109
  uint8_t maxSpeed = staticParams.gimbalServoMaxManualSpeed;
2108 - 110
  if (diff > maxSpeed) diff = maxSpeed;
111
  else if (diff < -maxSpeed) diff = -maxSpeed;
112
  manualValue = previousManualValues[axis] + diff;
113
  previousManualValues[axis] = manualValue;
114
  return manualValue;
115
}
2139 - 116
*/
2108 - 117
 
2139 - 118
/*
2108 - 119
// add stabilization and manual, apply soft position limits.
120
// All in a [0..255*SCALE_FACTOR] space (despite signed types used internally)
121
int16_t featuredServoValue(uint8_t axis) {
2132 - 122
  int16_t value = calculateManualServoAxis(axis, dynamicParams.gimbalServoManualControl[axis] * SCALE_FACTOR);
2108 - 123
  value += calculateStabilizedServoAxis(axis);
2132 - 124
  int16_t limit = staticParams.gimbalServoConfigurations[axis].minValue * SCALE_FACTOR;
2108 - 125
  if (value < limit) value = limit;
2132 - 126
  limit = staticParams.gimbalServoConfigurations[axis].maxValue * SCALE_FACTOR;
2108 - 127
  if (value > limit) value = limit;
128
  value -= (128 * SCALE_FACTOR);
129
  if (value < -SERVOLIMIT) value = -SERVOLIMIT;
130
  else if (value > SERVOLIMIT) value = SERVOLIMIT;
131
  // Shift into the [NEUTRAL_PULSELENGTH-SERVOLIMIT..NEUTRAL_PULSELENGTH+SERVOLIMIT] space.
132
  return value + NEUTRAL_PULSELENGTH;
133
}
2139 - 134
*/
2108 - 135
 
136
void calculateControlServoValues(void) {
137
  int16_t value;
138
  for (uint8_t axis=0; axis<4; axis++) {
139
        value = controlServos[axis];
2139 - 140
 
141
        // Apply configurable limits. These are signed: +-128 is twice the normal +- 0.5 ms limit and +- 64 is normal.
2142 - 142
        int16_t min = (int16_t)staticParams.servos[axis].minValue * (SERVO_NORMAL_LIMIT >> 6);
143
        int16_t max = (int16_t)staticParams.servos[axis].maxValue * (SERVO_NORMAL_LIMIT >> 6);
2139 - 144
 
2142 - 145
        if (value < -min) value = -min;
2139 - 146
        else if (value > max) value = max;
147
 
148
        if (value < -SERVO_ABS_LIMIT) value = -SERVO_ABS_LIMIT;
149
        else if (value > SERVO_ABS_LIMIT) value = SERVO_ABS_LIMIT;
150
 
2142 - 151
        debugOut.analog[24+axis] = value;
152
 
2108 - 153
        servoValues[axis] = value + NEUTRAL_PULSELENGTH;
154
  }
155
}
156
 
2139 - 157
/*
2108 - 158
void calculateFeaturedServoValues(void) {
159
  int16_t value;
160
  uint8_t axis;
161
 
162
  // Save the computation cost of computing a new value before the old one is used.
163
  if (!recalculateServoTimes) return;
164
 
165
  for (axis= MAX_CONTROL_SERVOS; axis<MAX_CONTROL_SERVOS+2; axis++) {
166
        value = featuredServoValue(axis-MAX_CONTROL_SERVOS);
167
        servoValues[axis] = value;
168
  }
169
  for (axis=MAX_CONTROL_SERVOS+2; axis<MAX_SERVOS; axis++) {
170
        value = 128 * SCALE_FACTOR;
171
        servoValues[axis] = value;
172
  }
173
 
174
  recalculateServoTimes = 0;
175
}
2139 - 176
*/
2108 - 177
 
2142 - 178
uint8_t finalServoMap[] = {1,0,0,2,4,5,6,7};
179
 
2108 - 180
ISR(TIMER2_COMPA_vect) {
181
  static uint16_t remainingPulseTime;
182
  static uint8_t servoIndex = 0;
183
  static uint16_t sumOfPulseTimes = 0;
184
 
185
  if (!remainingPulseTime) {
186
    // Pulse is over, and the next pulse has already just started. Calculate length of next pulse.
187
    if (servoIndex < staticParams.servoCount) {
188
      // There are more signals to output.
2142 - 189
      sumOfPulseTimes += (remainingPulseTime = servoValues[finalServoMap[servoIndex]]);
2108 - 190
      servoIndex++;
191
    } else {
192
      // There are no more signals. Reset the counter and make this pulse cover the missing frame time.
2135 - 193
      remainingPulseTime = FRAMELENGTH - sumOfPulseTimes;
2108 - 194
      sumOfPulseTimes = servoIndex = 0;
2139 - 195
      //recalculateServoTimes = 1;
2108 - 196
      HEF4017R_ON;
197
    }
198
  }
199
 
200
  // Schedule the next OCR2A event. The counter is already reset at this time.
201
  if (remainingPulseTime > 256+128) {
202
    // Set output to reset to zero at next OCR match. It does not really matter when the output is set low again,
203
    // as long as it happens once per pulse. This will, because all pulses are > 255+128 long.
204
    OCR2A = 255;
205
    TCCR2A &= ~(1<<COM2A0);
206
    remainingPulseTime-=256;
207
  } else if (remainingPulseTime > 256) {
208
    // Remaining pulse lengths in the range [256..256+128] might cause trouble if handled the standard
209
    // way, which is in chunks of 256. The remainder would be very small, possibly causing an interrupt on interrupt
210
    // condition. Instead we now make a chunk of 128. The remaining chunk will then be in [128..255] which is OK.
211
    remainingPulseTime-=128;
212
    OCR2A=127;
213
  } else {
214
    // Set output to high at next OCR match. This is when the 4017 counter will advance by one. Also set reset low
215
    TCCR2A |= (1<<COM2A0);
216
    OCR2A = remainingPulseTime-1;
217
    remainingPulseTime=0;
218
    HEF4017R_OFF; // implement servo-disable here, by only removing the reset signal if ServoEnabled!=0.
219
  }
220
}