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