Subversion Repositories FlightCtrl

Rev

Rev 2135 | Rev 2142 | 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.
142
        int16_t min = (staticParams.servos[axis].minValue * SERVO_NORMAL_LIMIT) >> 6;
143
        int16_t max = (staticParams.servos[axis].maxValue * SERVO_NORMAL_LIMIT) >> 6;
144
 
145
        if (value < min) value = min;
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
 
2108 - 151
        servoValues[axis] = value + NEUTRAL_PULSELENGTH;
152
  }
153
}
154
 
2139 - 155
/*
2108 - 156
void calculateFeaturedServoValues(void) {
157
  int16_t value;
158
  uint8_t axis;
159
 
160
  // Save the computation cost of computing a new value before the old one is used.
161
  if (!recalculateServoTimes) return;
162
 
163
  for (axis= MAX_CONTROL_SERVOS; axis<MAX_CONTROL_SERVOS+2; axis++) {
164
        value = featuredServoValue(axis-MAX_CONTROL_SERVOS);
165
        servoValues[axis] = value;
166
  }
167
  for (axis=MAX_CONTROL_SERVOS+2; axis<MAX_SERVOS; axis++) {
168
        value = 128 * SCALE_FACTOR;
169
        servoValues[axis] = value;
170
  }
171
 
172
  recalculateServoTimes = 0;
173
}
2139 - 174
*/
2108 - 175
 
176
ISR(TIMER2_COMPA_vect) {
177
  static uint16_t remainingPulseTime;
178
  static uint8_t servoIndex = 0;
179
  static uint16_t sumOfPulseTimes = 0;
180
 
181
  if (!remainingPulseTime) {
182
    // Pulse is over, and the next pulse has already just started. Calculate length of next pulse.
183
    if (servoIndex < staticParams.servoCount) {
184
      // There are more signals to output.
185
      sumOfPulseTimes += (remainingPulseTime = servoValues[servoIndex]);
186
      servoIndex++;
187
    } else {
188
      // There are no more signals. Reset the counter and make this pulse cover the missing frame time.
2135 - 189
      remainingPulseTime = FRAMELENGTH - sumOfPulseTimes;
2108 - 190
      sumOfPulseTimes = servoIndex = 0;
2139 - 191
      //recalculateServoTimes = 1;
2108 - 192
      HEF4017R_ON;
193
    }
194
  }
195
 
196
  // Schedule the next OCR2A event. The counter is already reset at this time.
197
  if (remainingPulseTime > 256+128) {
198
    // Set output to reset to zero at next OCR match. It does not really matter when the output is set low again,
199
    // as long as it happens once per pulse. This will, because all pulses are > 255+128 long.
200
    OCR2A = 255;
201
    TCCR2A &= ~(1<<COM2A0);
202
    remainingPulseTime-=256;
203
  } else if (remainingPulseTime > 256) {
204
    // Remaining pulse lengths in the range [256..256+128] might cause trouble if handled the standard
205
    // way, which is in chunks of 256. The remainder would be very small, possibly causing an interrupt on interrupt
206
    // condition. Instead we now make a chunk of 128. The remaining chunk will then be in [128..255] which is OK.
207
    remainingPulseTime-=128;
208
    OCR2A=127;
209
  } else {
210
    // Set output to high at next OCR match. This is when the 4017 counter will advance by one. Also set reset low
211
    TCCR2A |= (1<<COM2A0);
212
    OCR2A = remainingPulseTime-1;
213
    remainingPulseTime=0;
214
    HEF4017R_OFF; // implement servo-disable here, by only removing the reset signal if ServoEnabled!=0.
215
  }
216
}