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