Subversion Repositories FlightCtrl

Rev

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