Rev 1868 | Rev 2039 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1868 | Rev 1980 | ||
---|---|---|---|
Line 53... | Line 53... | ||
53 | #include "eeprom.h" |
53 | #include "eeprom.h" |
54 | #include "uart0.h" |
54 | #include "uart0.h" |
55 | #include "rc.h" |
55 | #include "rc.h" |
56 | #include "attitude.h" |
56 | #include "attitude.h" |
Line -... | Line 57... | ||
- | 57 | ||
- | 58 | #define SLOW 1 |
|
- | 59 | ||
- | 60 | #ifdef SLOW |
|
- | 61 | #define NEUTRAL_PULSELENGTH 938 |
|
- | 62 | #define SERVOLIMIT 500 |
|
- | 63 | #define SCALE_FACTOR 4 |
|
- | 64 | #define CS2 ((1<<CS21)|(1<<CS20)) |
|
- | 65 | #else |
|
- | 66 | #define NEUTRAL_PULSELENGTH 3750 |
|
- | 67 | #define SERVOLIMIT 2000 |
|
- | 68 | #define SCALE_FACTOR 16 |
|
- | 69 | #define CS2 (<<CS21) |
|
- | 70 | #endif |
|
- | 71 | ||
- | 72 | #define MAX_SERVOS 8 |
|
- | 73 | #define FRAMELEN ((NEUTRAL_PULSELENGTH + SERVOLIMIT) * staticParams.servoCount + 128) |
|
- | 74 | #define MIN_PULSELENGTH (NEUTRAL_PULSELENGTH - SERVOLIMIT) |
|
- | 75 | #define MAX_PULSELENGTH (NEUTRAL_PULSELENGTH + SERVOLIMIT) |
|
57 | 76 | ||
58 | volatile int16_t ServoPitchValue = 0; |
77 | //volatile uint8_t servoActive = 0; |
59 | volatile int16_t ServoRollValue = 0; |
78 | volatile uint8_t recalculateServoTimes = 0; |
- | 79 | volatile uint16_t servoValues[MAX_SERVOS]; |
|
Line 60... | Line 80... | ||
60 | volatile uint8_t ServoActive = 0; |
80 | volatile uint16_t previousManualValues[2]; |
61 | 81 | ||
Line 62... | Line 82... | ||
62 | #define HEF4017R_ON PORTC |= (1<<PORTC6) |
82 | #define HEF4017R_ON PORTC |= (1<<PORTC6) |
63 | #define HEF4017R_OFF PORTC &= ~(1<<PORTC6) |
83 | #define HEF4017R_OFF PORTC &= ~(1<<PORTC6) |
64 | 84 | ||
65 | /***************************************************** |
- | |
66 | * Initialize Timer 2 |
- | |
67 | *****************************************************/ |
85 | /***************************************************** |
68 | // The timer 2 is used to generate the PWM at PD7 (J7) |
86 | * Initialize Timer 2 |
Line 69... | Line 87... | ||
69 | // to control a camera servo for pitch compensation. |
87 | *****************************************************/ |
70 | void timer2_init(void) { |
88 | void timer2_init(void) { |
Line 76... | Line 94... | ||
76 | // set PD7 as output of the PWM for pitch servo |
94 | // set PD7 as output of the PWM for pitch servo |
77 | DDRD |= (1 << DDD7); |
95 | DDRD |= (1 << DDD7); |
78 | PORTD &= ~(1 << PORTD7); // set PD7 to low |
96 | PORTD &= ~(1 << PORTD7); // set PD7 to low |
Line 79... | Line 97... | ||
79 | 97 | ||
80 | DDRC |= (1 << DDC6); // set PC6 as output (Reset for HEF4017) |
- | |
81 | //PORTC &= ~(1<<PORTC6); // set PC6 to low |
98 | DDRC |= (1 << DDC6); // set PC6 as output (Reset for HEF4017) |
Line 82... | Line 99... | ||
82 | HEF4017R_ON; // enable reset |
99 | HEF4017R_ON; // enable reset |
83 | - | ||
84 | // Timer/Counter 2 Control Register A |
100 | |
85 | 101 | // Timer/Counter 2 Control Register A |
|
86 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
102 | // Timer Mode is CTC (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 0) |
87 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
103 | // PD7: Output OCR2 match, (Bits: COM2A1 = 1, COM2A0 = 0) |
88 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
104 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
89 | TCCR2A &= ~((1 << COM2A1) | (1 << COM2A0) | (1 << COM2B1) | (1 << COM2B0)); |
105 | TCCR2A &= ~((1 << COM2A0) | (1 << COM2B1) | (1 << COM2B0) | (1 << WGM20) | (1 << WGM22)); |
90 | TCCR2A |= (1 << WGM21) | (1 << WGM20); |
106 | TCCR2A |= (1 << COM2A1) | (1 << WGM21); |
91 | 107 | ||
92 | // Timer/Counter 2 Control Register B |
108 | // Timer/Counter 2 Control Register B |
93 | 109 | ||
94 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
110 | // Set clock divider for timer 2 to 20MHz / 8 = 2.5 MHz |
95 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
111 | // The timer increments from 0x00 to 0xFF with an update rate of 2.5 kHz or 0.4 us |
96 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
- | |
97 | 112 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 9.765 kHz or 0.1024ms |
|
98 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
113 | |
99 | TCCR2B &= ~((1 << FOC2A) | (1 << FOC2B) | (1 << CS22)); |
114 | TCCR2B &= ~((1 << FOC2A) | (1 << FOC2B) | (1 << CS20) | (1 << CS21) | (1 << CS22)); |
100 | TCCR2B |= (1 << CS21) | (1 << CS20) | (1 << WGM22); |
115 | TCCR2B |= CS2; |
101 | 116 | ||
102 | // Initialize the Timer/Counter 2 Register |
117 | // Initialize the Timer/Counter 2 Register |
103 | TCNT2 = 0; |
118 | TCNT2 = 0; |
104 | 119 | ||
105 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
- | |
Line 106... | Line 120... | ||
106 | OCR2A = 255; |
120 | // Initialize the Output Compare Register A used for signal generation on port PD7. |
107 | TCCR2A |= (1 << COM2A1); // set or clear at compare match depends on value of COM2A0 |
121 | OCR2A = 255; |
108 | 122 | ||
109 | // Timer/Counter 2 Interrupt Mask Register |
123 | // Timer/Counter 2 Interrupt Mask Register |
Line -... | Line 124... | ||
- | 124 | // Enable timer output compare match A Interrupt only |
|
- | 125 | TIMSK2 &= ~((1 << OCIE2B) | (1 << TOIE2)); |
|
- | 126 | TIMSK2 |= (1 << OCIE2A); |
|
110 | // Enable timer output compare match A Interrupt only |
127 | |
111 | TIMSK2 &= ~((1 << OCIE2B) | (1 << TOIE2)); |
128 | for (uint8_t axis=0; axis<2; axis++) |
Line -... | Line 129... | ||
- | 129 | previousManualValues[axis] = dynamicParams.servoManualControl[axis] * SCALE_FACTOR; |
|
112 | TIMSK2 |= (1 << OCIE2A); |
130 | |
113 | 131 | SREG = sreg; |
|
114 | SREG = sreg; |
132 | } |
115 | } |
- | |
116 | 133 | ||
117 | void Servo_On(void) { |
134 | /* |
118 | ServoActive = 1; |
135 | void servo_On(void) { |
119 | } |
136 | servoActive = 1; |
- | 137 | } |
|
Line 120... | Line 138... | ||
120 | 138 | void servo_Off(void) { |
|
121 | void Servo_Off(void) { |
139 | servoActive = 0; |
122 | ServoActive = 0; |
140 | HEF4017R_ON; // enable reset |
123 | HEF4017R_ON; // enable reset |
- | |
124 | } |
- | |
125 | - | ||
126 | /***************************************************** |
- | |
127 | * Control Servo Position |
- | |
128 | *****************************************************/ |
- | |
129 | ISR(TIMER2_COMPA_vect) |
- | |
130 | { |
- | |
131 | // frame len 22.5 ms = 14063 * 1.6 us |
- | |
132 | // stop pulse: 0.3 ms = 188 * 1.6 us |
- | |
133 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
- | |
134 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
- | |
135 | // resolution: 1500 - 375 = 1125 steps |
- | |
136 | - | ||
137 | #define PPM_STOPPULSE 188 |
- | |
138 | #define PPM_FRAMELEN (1757 * .ServoRefresh) // 22.5 ms / 8 Channels = 2.8125ms per Servo Channel |
- | |
139 | #define MINSERVOPULSE 375 |
- | |
140 | #define MAXSERVOPULSE 1500 |
- | |
141 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
- | |
142 | - | ||
143 | #if defined(USE_NON_4017_SERVO_OUTPUTS) || defined(USE_4017_SERVO_OUTPUTS) |
- | |
144 | static uint8_t isGeneratingPulse = 0; |
- | |
145 | static uint16_t remainingPulseLength = 0; |
- | |
146 | static uint16_t ServoFrameTime = 0; |
- | |
147 | static uint8_t ServoIndex = 0; |
- | |
148 | - | ||
149 | #define MULTIPLIER 4 |
- | |
150 | static int16_t ServoPitchOffset = (255 / 2) * MULTIPLIER; // initial value near center position |
- | |
151 | static int16_t ServoRollOffset = (255 / 2) * MULTIPLIER; // initial value near center position |
- | |
152 | #endif |
- | |
153 | #ifdef USE_NON_4017_SERVO_OUTPUTS |
- | |
154 | //--------------------------- |
- | |
155 | // Pitch servo state machine |
- | |
156 | //--------------------------- |
- | |
157 | if (!isGeneratingPulse) { // pulse output complete on _next_ interrupt |
- | |
158 | if(TCCR2A & (1<<COM2A0)) { // we are still outputting a high pulse |
- | |
159 | TCCR2A &= ~(1<<COM2A0); // make a low pulse on _next_ interrupt, and now |
- | |
160 | remainingPulseLength = MINSERVOPULSE + SERVORANGE / 2; // center position ~ 1.5ms |
- | |
161 | ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl) / 4; // lowpass offset |
- | |
162 | if(staticParams.ServoPitchCompInvert & 0x01) { |
- | |
163 | // inverting movement of servo |
- | |
164 | // todo: function. |
- | |
165 | ServoPitchValue = ServoPitchOffset + (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L)); |
- | |
166 | } else { |
- | |
167 | // todo: function. |
- | |
168 | // non inverting movement of servo |
- | |
169 | ServoPitchValue = ServoPitchOffset - (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L)); |
- | |
170 | } |
- | |
171 | // limit servo value to its parameter range definition |
- | |
172 | if(ServoPitchValue < (int16_t)staticParams.ServoPitchMin) { |
- | |
173 | ServoPitchValue = (int16_t)staticParams.ServoPitchMin; |
- | |
174 | } else if(ServoPitchValue > (int16_t)staticParams.ServoPitchMax) { |
- | |
175 | ServoPitchValue = (int16_t)staticParams.ServoPitchMax; |
- | |
176 | } |
- | |
177 | - | ||
178 | remainingPulseLength = (ServoPitchValue - 256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position |
- | |
179 | - | ||
180 | // range servo pulse width |
- | |
181 | if(remainingPulseLength > MAXSERVOPULSE ) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit |
- | |
182 | else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit |
- | |
183 | - | ||
184 | // accumulate time for correct update rate |
- | |
185 | ServoFrameTime = remainingPulseLength; |
- | |
186 | } else { // we had a high pulse |
- | |
187 | TCCR2A |= (1<<COM2A0); // make a low pulse |
- | |
188 | remainingPulseLength = PPM_FRAMELEN - ServoFrameTime; |
- | |
189 | } |
- | |
190 | // set pulse output active |
- | |
191 | isGeneratingPulse = 1; |
- | |
192 | } // EOF Pitch servo state machine |
- | |
193 | - | ||
194 | #elseif defined(USE_4017_SERVOS) |
- | |
195 | //----------------------------------------------------- |
- | |
196 | // PPM state machine, onboard demultiplexed by HEF4017 |
- | |
197 | //----------------------------------------------------- |
- | |
198 | if(!isGeneratingPulse) { // pulse output complete |
- | |
199 | if(TCCR2A & (1<<COM2A0)) { // we had a low pulse |
- | |
200 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
- | |
201 | - | ||
202 | if(ServoIndex == 0) { // if we are at the sync gap |
- | |
203 | remainingPulseLength = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
- | |
204 | ServoFrameTime = 0; // reset servo frame time |
- | |
205 | HEF4017R_ON; // enable HEF4017 reset |
- | |
206 | } else { // servo channels |
- | |
207 | remainingPulseLength = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
- | |
208 | switch(ServoIndex) { // map servo channels |
- | |
209 | case 1: // Pitch Compensation Servo |
- | |
210 | ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl * MULTIPLIER) / 4; // lowpass offset |
- | |
211 | ServoPitchValue = ServoPitchOffset; // offset (Range from 0 to 255 * 3 = 765) |
- | |
212 | if(staticParams.ServoPitchCompInvert & 0x01) { |
- | |
213 | // inverting movement of servo |
- | |
214 | ServoPitchValue += (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) ); |
- | |
215 | } else { // non inverting movement of servo |
- | |
216 | ServoPitchValue -= (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) ); |
- | |
217 | } |
- | |
218 | // limit servo value to its parameter range definition |
- | |
219 | if(ServoPitchValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) { |
- | |
220 | ServoPitchValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER; |
- | |
221 | } else if(ServoPitchValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) { |
- | |
222 | ServoPitchValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER; |
- | |
223 | } |
- | |
224 | remainingPulseLength += ServoPitchValue - (256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position |
- | |
225 | ServoPitchValue /= MULTIPLIER; |
- | |
226 | break; |
- | |
227 | - | ||
228 | case 2: // Roll Compensation Servo |
- | |
229 | ServoRollOffset = (ServoRollOffset * 3 + (int16_t)80 * MULTIPLIER) / 4; // lowpass offset |
- | |
230 | ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765) |
- | |
231 | //if(staticParams.ServoRollCompInvert & 0x01) |
- | |
232 | { // inverting movement of servo |
- | |
233 | ServoRollValue += (int16_t)( ( (int32_t) 50 * MULTIPLIER * (integralGyroRoll / 128L ) ) / (256L) ); |
- | |
234 | } |
- | |
235 | /* else |
- | |
236 | { // non inverting movement of servo |
- | |
237 | ServoRollValue -= (int16_t)( ( (int32_t) 40 * MULTIPLIER * (IntegralGyroRoll / 128L ) ) / (256L) ); |
- | |
238 | } |
- | |
239 | */// limit servo value to its parameter range definition |
- | |
240 | if(ServoRollValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) { |
- | |
241 | ServoRollValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER; |
- | |
242 | } else if(ServoRollValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) { |
- | |
243 | ServoRollValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER; |
- | |
244 | } |
- | |
245 | remainingPulseLength += ServoRollValue - (256 / 2) * MULTIPLIER; // shift ServoRollValue to center position |
- | |
246 | ServoRollValue /= MULTIPLIER; |
- | |
247 | break; |
- | |
248 | - | ||
249 | default: // other servo channels |
- | |
250 | remainingPulseLength += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
- | |
251 | break; |
- | |
252 | } |
- | |
253 | // range servo pulse width |
- | |
254 | if(remainingPulseLength > MAXSERVOPULSE) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit |
- | |
255 | else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit |
- | |
256 | // substract stop pulse width |
- | |
257 | remainingPulseLength -= PPM_STOPPULSE; |
- | |
258 | // accumulate time for correct sync gap |
- | |
259 | ServoFrameTime += remainingPulseLength; |
- | |
260 | } |
- | |
261 | } else { // we had a high pulse |
- | |
262 | TCCR2A |= (1<<COM2A0); // make a low pulse |
- | |
263 | // set pulsewidth to stop pulse width |
- | |
264 | remainingPulseLength = PPM_STOPPULSE; |
- | |
265 | // accumulate time for correct sync gap |
- | |
266 | ServoFrameTime += remainingPulseLength; |
- | |
267 | if(ServoActive && RC_Quality > 180) HEF4017R_OFF; // disable HEF4017 reset |
- | |
268 | ServoIndex++; // change to next servo channel |
- | |
Line -... | Line 141... | ||
- | 141 | } |
|
- | 142 | */ |
|
- | 143 | ||
- | 144 | /***************************************************** |
|
- | 145 | * Control Servo Position |
|
- | 146 | *****************************************************/ |
|
- | 147 | ||
269 | if(ServoIndex > staticParams.ServoRefresh) ServoIndex = 0; // reset to the sync gap |
148 | /*typedef struct { |
- | 149 | uint8_t manualControl; |
|
- | 150 | uint8_t compensationFactor; |
|
- | 151 | uint8_t minValue; |
|
- | 152 | uint8_t maxValue; |
|
- | 153 | uint8_t flags; |
|
- | 154 | } servo_t;*/ |
|
- | 155 | ||
- | 156 | int16_t calculateStabilizedServoAxis(uint8_t axis) { |
|
270 | } |
157 | int32_t value = angle[axis] / 64L; // between -500000 to 500000 extreme limits. Just about |
- | 158 | // With full blast on stabilization gain (255) we want to convert a delta of, say, 125000 to 2000. |
|
- | 159 | // That is a divisor of about 1<<14. Same conclusion as H&I. |
|
- | 160 | value *= staticParams.servoConfigurations[axis].stabilizationFactor; |
|
271 | // set pulse output active |
161 | value /= 256L; |
272 | isGeneratingPulse = 1; |
162 | if (staticParams.servoConfigurations[axis].flags & SERVO_STABILIZATION_REVERSE) |
273 | } |
163 | return -value; |
- | 164 | return value; |
|
- | 165 | } |
|
274 | #endif |
166 | |
275 | 167 | // With constant-speed limitation. |
|
- | 168 | uint16_t calculateManualServoAxis(uint8_t axis, uint16_t manualValue) { |
|
276 | /* |
169 | int16_t diff = manualValue - previousManualValues[axis]; |
- | 170 | uint8_t maxSpeed = staticParams.servoManualMaxSpeed; |
|
- | 171 | if (diff > maxSpeed) diff = maxSpeed; |
|
277 | * Cases: |
172 | else if (diff < -maxSpeed) diff = -maxSpeed; |
- | 173 | manualValue = previousManualValues[axis] + diff; |
|
- | 174 | previousManualValues[axis] = manualValue; |
|
- | 175 | return manualValue; |
|
- | 176 | } |
|
- | 177 | ||
- | 178 | // add stabilization and manual, apply soft position limits. |
|
- | 179 | // All in a [0..255*SCALE_FACTOR] space (despite signed types used internally) |
|
- | 180 | int16_t featuredServoValue(uint8_t axis) { |
|
- | 181 | int16_t value = calculateManualServoAxis(axis, dynamicParams.servoManualControl[axis] * SCALE_FACTOR); |
|
- | 182 | value += calculateStabilizedServoAxis(axis); |
|
- | 183 | int16_t limit = staticParams.servoConfigurations[axis].minValue * SCALE_FACTOR; |
|
278 | * 1) 255 + 128 <= remainingPulseLength --> delta = 255 |
184 | if (value < limit) value = limit; |
- | 185 | limit = staticParams.servoConfigurations[axis].maxValue * SCALE_FACTOR; |
|
- | 186 | if (value > limit) value = limit; |
|
- | 187 | return value; |
|
- | 188 | } |
|
- | 189 | ||
- | 190 | uint16_t servoValue(uint8_t axis) { |
|
- | 191 | int16_t value; |
|
- | 192 | if (axis<2) value = featuredServoValue(axis); |
|
- | 193 | else value = 128 * SCALE_FACTOR; // dummy. Replace by something useful for servos 3..8. |
|
- | 194 | // Shift out of the [0..255*SCALE_FACTOR] space |
|
- | 195 | value -= (128 * SCALE_FACTOR); |
|
279 | * 2) 255 <= remainingPulseLength < 255 + 128 --> delta = 255 - 128 |
196 | if (value < -SERVOLIMIT) value = -SERVOLIMIT; |
- | 197 | else if (value > SERVOLIMIT) value = SERVOLIMIT; |
|
- | 198 | // Shift into the [NEUTRAL_PULSELENGTH-SERVOLIMIT..NEUTRAL_PULSELENGTH+SERVOLIMIT] space. |
|
- | 199 | return value + NEUTRAL_PULSELENGTH; |
|
280 | * this is to avoid a too short delta on the last cycle, which would cause |
200 | } |
- | 201 | ||
- | 202 | void calculateServoValues(void) { |
|
- | 203 | if (!recalculateServoTimes) return; |
|
- | 204 | for (uint8_t axis=0; axis<MAX_SERVOS; axis++) { |
|
- | 205 | servoValues[axis] = servoValue(axis); |
|
- | 206 | } |
|
- | 207 | recalculateServoTimes = 0; |
|
281 | * an interupt-on-interrupt condition and the loss of the last interrupt. |
208 | } |
- | 209 | ||
- | 210 | ISR(TIMER2_COMPA_vect) { |
|
- | 211 | static uint16_t remainingPulseTime; |
|
- | 212 | static uint8_t servoIndex = 0; |
|
282 | * 3) remainingPulseLength < 255 --> delta = remainingPulseLength |
213 | static uint16_t sumOfPulseTimes = 0; |
283 | */ |
214 | |
- | 215 | if (!remainingPulseTime) { |
|
284 | #if defined(USE_NON_4017_SERVO_OUTPUTS) || defined(USE_4017_SERVO_OUTPUTS) |
216 | // Pulse is over, and the next pulse has already just started. Calculate length of next pulse. |
285 | uint8_t delta; |
217 | if (servoIndex < staticParams.servoCount) { |
- | 218 | // There are more signals to output. |
|
- | 219 | sumOfPulseTimes += (remainingPulseTime = servoValues[servoIndex]); |
|
- | 220 | servoIndex++; |
|
286 | if (remainingPulseLength >= (255 + 128)) { |
221 | } else { |
- | 222 | // There are no more signals. Reset the counter and make this pulse cover the missing frame time. |
|
- | 223 | remainingPulseTime = FRAMELEN - sumOfPulseTimes; |
|
- | 224 | sumOfPulseTimes = servoIndex = 0; |
|
- | 225 | recalculateServoTimes = 1; |
|
- | 226 | HEF4017R_ON; |
|
287 | delta = 255; |
227 | } |
- | 228 | } |
|
- | 229 | ||
- | 230 | // Schedule the next OCR2A event. The counter is already reset at this time. |
|
- | 231 | if (remainingPulseTime > 256+128) { |
|
- | 232 | // Set output to reset to zero at next OCR match. It does not really matter when the output is set low again, |
|
- | 233 | // as long as it happens once per pulse. This will, because all pulses are > 255+128 long. |
|
288 | } else if (remainingPulseLength >= 255) { |
234 | OCR2A = 255; |
- | 235 | TCCR2A &= ~(1<<COM2A0); |
|
289 | delta = 255- 128; |
236 | remainingPulseTime-=256; |
- | 237 | } else if (remainingPulseTime > 256) { |
|
- | 238 | // Remaining pulse lengths in the range [256..256+128] might cause trouble if handled the standard |
|
- | 239 | // way, which is in chunks of 256. The remainder would be very small, possibly causing an interrupt on interrupt |
|
- | 240 | // condition. Instead we now make a chunk of 128. The remaining chunk will then be in [128..255] which is OK. |
|
- | 241 | remainingPulseTime-=128; |
|
- | 242 | OCR2A=127; |
|
290 | } else { |
243 | } else { |