Rev 1775 | Rev 1980 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1775 | Rev 1821 | ||
---|---|---|---|
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 57... | Line 57... | ||
57 | 57 | ||
58 | volatile int16_t ServoPitchValue = 0; |
58 | volatile int16_t ServoPitchValue = 0; |
59 | volatile int16_t ServoRollValue = 0; |
59 | volatile int16_t ServoRollValue = 0; |
Line 60... | Line 60... | ||
60 | volatile uint8_t ServoActive = 0; |
60 | volatile uint8_t ServoActive = 0; |
61 | 61 | ||
Line 62... | Line 62... | ||
62 | #define HEF4017R_ON PORTC |= (1<<PORTC6) |
62 | #define HEF4017R_ON PORTC |= (1<<PORTC6) |
63 | #define HEF4017R_OFF PORTC &= ~(1<<PORTC6) |
63 | #define HEF4017R_OFF PORTC &= ~(1<<PORTC6) |
64 | 64 | ||
65 | /***************************************************** |
65 | /***************************************************** |
66 | * Initialize Timer 2 |
66 | * Initialize Timer 2 |
67 | *****************************************************/ |
67 | *****************************************************/ |
68 | // The timer 2 is used to generate the PWM at PD7 (J7) |
68 | // The timer 2 is used to generate the PWM at PD7 (J7) |
Line 69... | Line 69... | ||
69 | // to control a camera servo for pitch compensation. |
69 | // to control a camera servo for pitch compensation. |
70 | void timer2_init(void) { |
70 | void timer2_init(void) { |
Line 71... | Line 71... | ||
71 | uint8_t sreg = SREG; |
71 | uint8_t sreg = SREG; |
72 | 72 | ||
73 | // disable all interrupts before reconfiguration |
73 | // disable all interrupts before reconfiguration |
Line 74... | Line 74... | ||
74 | cli(); |
74 | cli(); |
75 | 75 | ||
76 | // set PD7 as output of the PWM for pitch servo |
76 | // set PD7 as output of the PWM for pitch servo |
Line 77... | Line 77... | ||
77 | DDRD |= (1<<DDD7); |
77 | DDRD |= (1 << DDD7); |
Line 78... | Line 78... | ||
78 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
78 | PORTD &= ~(1 << PORTD7); // set PD7 to low |
79 | 79 | ||
80 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
80 | DDRC |= (1 << DDC6); // set PC6 as output (Reset for HEF4017) |
81 | //PORTC &= ~(1<<PORTC6); // set PC6 to low |
81 | //PORTC &= ~(1<<PORTC6); // set PC6 to low |
82 | HEF4017R_ON; // enable reset |
82 | HEF4017R_ON; // enable reset |
Line 83... | Line 83... | ||
83 | 83 | ||
Line 84... | Line 84... | ||
84 | // Timer/Counter 2 Control Register A |
84 | // Timer/Counter 2 Control Register A |
85 | 85 | ||
86 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
86 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
Line 87... | Line 87... | ||
87 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
87 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
88 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
88 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
89 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
89 | TCCR2A &= ~((1 << COM2A1) | (1 << COM2A0) | (1 << COM2B1) | (1 << COM2B0)); |
Line 90... | Line 90... | ||
90 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
90 | TCCR2A |= (1 << WGM21) | (1 << WGM20); |
91 | 91 | ||
Line 92... | Line 92... | ||
92 | // Timer/Counter 2 Control Register B |
92 | // Timer/Counter 2 Control Register B |
93 | 93 | ||
94 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
94 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
Line 95... | Line 95... | ||
95 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
95 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
96 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
96 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
97 | 97 | ||
98 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
98 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
Line 99... | Line 99... | ||
99 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
99 | TCCR2B &= ~((1 << FOC2A) | (1 << FOC2B) | (1 << CS22)); |
100 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
100 | TCCR2B |= (1 << CS21) | (1 << CS20) | (1 << WGM22); |
Line 101... | Line 101... | ||
101 | 101 | ||
102 | // Initialize the Timer/Counter 2 Register |
102 | // Initialize the Timer/Counter 2 Register |
103 | TCNT2 = 0; |
103 | TCNT2 = 0; |
Line 104... | Line 104... | ||
104 | 104 | ||
105 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
105 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
106 | OCR2A = 255; |
106 | OCR2A = 255; |
107 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
107 | TCCR2A |= (1 << COM2A1); // set or clear at compare match depends on value of COM2A0 |
Line 108... | Line 108... | ||
108 | 108 | ||
109 | // Timer/Counter 2 Interrupt Mask Register |
109 | // Timer/Counter 2 Interrupt Mask Register |
110 | // Enable timer output compare match A Interrupt only |
110 | // Enable timer output compare match A Interrupt only |
111 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
111 | TIMSK2 &= ~((1 << OCIE2B) | (1 << TOIE2)); |
- | 112 | TIMSK2 |= (1 << OCIE2A); |
|
112 | TIMSK2 |= (1<<OCIE2A); |
113 | |
113 | 114 | SREG = sreg; |
|
114 | SREG = sreg; |
115 | } |
115 | } |
116 | |
116 | 117 | void Servo_On(void) { |
|
117 | void Servo_On(void) { |
118 | ServoActive = 1; |
118 | ServoActive = 1; |
119 | } |
119 | } |
120 | |
120 | 121 | void Servo_Off(void) { |
|
121 | void Servo_Off(void) { |
122 | ServoActive = 0; |
122 | ServoActive = 0; |
123 | HEF4017R_ON; // enable reset |
Line 123... | Line 124... | ||
123 | HEF4017R_ON; // enable reset |
124 | } |
124 | } |
125 | |
125 | 126 | /***************************************************** |
|
126 | /***************************************************** |
127 | * Control Servo Position |
127 | * Control Servo Position |
128 | *****************************************************/ |
128 | *****************************************************/ |
129 | ISR(TIMER2_COMPA_vect) |
129 | ISR(TIMER2_COMPA_vect) { |
130 | { |
130 | // frame len 22.5 ms = 14063 * 1.6 us |
131 | // frame len 22.5 ms = 14063 * 1.6 us |
131 | // stop pulse: 0.3 ms = 188 * 1.6 us |
132 | // stop pulse: 0.3 ms = 188 * 1.6 us |
132 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
133 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
133 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
134 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
134 | // resolution: 1500 - 375 = 1125 steps |
135 | // resolution: 1500 - 375 = 1125 steps |
135 | 136 | ||
136 | #define PPM_STOPPULSE 188 |
137 | #define PPM_STOPPULSE 188 |
137 | #define PPM_FRAMELEN (1757 * .ServoRefresh) // 22.5 ms / 8 Channels = 2.8125ms per Servo Channel |
138 | #define PPM_FRAMELEN (1757 * .ServoRefresh) // 22.5 ms / 8 Channels = 2.8125ms per Servo Channel |
138 | #define MINSERVOPULSE 375 |
139 | #define MINSERVOPULSE 375 |
139 | #define MAXSERVOPULSE 1500 |
140 | #define MAXSERVOPULSE 1500 |
140 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
141 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
141 | 142 | ||
142 | #if defined(USE_NON_4017_SERVO_OUTPUTS) || defined(USE_4017_SERVO_OUTPUTS) |
143 | #if defined(USE_NON_4017_SERVO_OUTPUTS) || defined(USE_4017_SERVO_OUTPUTS) |
143 | static uint8_t isGeneratingPulse = 0; |
144 | static uint8_t isGeneratingPulse = 0; |
144 | static uint16_t remainingPulseLength = 0; |
145 | static uint16_t remainingPulseLength = 0; |
145 | static uint16_t ServoFrameTime = 0; |
146 | static uint16_t ServoFrameTime = 0; |
146 | static uint8_t ServoIndex = 0; |
147 | static uint8_t ServoIndex = 0; |
147 | 148 | ||
148 | #define MULTIPLIER 4 |
149 | #define MULTIPLIER 4 |
149 | static int16_t ServoPitchOffset = (255 / 2) * MULTIPLIER; // initial value near center position |
150 | static int16_t ServoPitchOffset = (255 / 2) * MULTIPLIER; // initial value near center position |
150 | static int16_t ServoRollOffset = (255 / 2) * MULTIPLIER; // initial value near center position |
151 | static int16_t ServoRollOffset = (255 / 2) * MULTIPLIER; // initial value near center position |
151 | #endif |
152 | #endif |
152 | #ifdef USE_NON_4017_SERVO_OUTPUTS |
153 | #ifdef USE_NON_4017_SERVO_OUTPUTS |
153 | //--------------------------- |
154 | //--------------------------- |
154 | // Pitch servo state machine |
155 | // Pitch servo state machine |
155 | //--------------------------- |
156 | //--------------------------- |
156 | if (!isGeneratingPulse) { // pulse output complete on _next_ interrupt |
157 | if (!isGeneratingPulse) { // pulse output complete on _next_ interrupt |
157 | if(TCCR2A & (1<<COM2A0)) { // we are still outputting a high pulse |
158 | if(TCCR2A & (1<<COM2A0)) { // we are still outputting a high pulse |
158 | TCCR2A &= ~(1<<COM2A0); // make a low pulse on _next_ interrupt, and now |
159 | TCCR2A &= ~(1<<COM2A0); // make a low pulse on _next_ interrupt, and now |
159 | remainingPulseLength = MINSERVOPULSE + SERVORANGE / 2; // center position ~ 1.5ms |
160 | remainingPulseLength = MINSERVOPULSE + SERVORANGE / 2; // center position ~ 1.5ms |
160 | ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl) / 4; // lowpass offset |
161 | ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl) / 4; // lowpass offset |
161 | if(staticParams.ServoPitchCompInvert & 0x01) { |
162 | if(staticParams.ServoPitchCompInvert & 0x01) { |
162 | // inverting movement of servo |
163 | // inverting movement of servo |
163 | // todo: function. |
164 | // todo: function. |
164 | ServoPitchValue = ServoPitchOffset + (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L)); |
165 | ServoPitchValue = ServoPitchOffset + (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L)); |
165 | } else { |
166 | } else { |
166 | // todo: function. |
167 | // todo: function. |
167 | // non inverting movement of servo |
168 | // non inverting movement of servo |
168 | ServoPitchValue = ServoPitchOffset - (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L)); |
169 | ServoPitchValue = ServoPitchOffset - (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L)); |
169 | } |
170 | } |
170 | // limit servo value to its parameter range definition |
171 | // limit servo value to its parameter range definition |
171 | if(ServoPitchValue < (int16_t)staticParams.ServoPitchMin) { |
172 | if(ServoPitchValue < (int16_t)staticParams.ServoPitchMin) { |
172 | ServoPitchValue = (int16_t)staticParams.ServoPitchMin; |
173 | ServoPitchValue = (int16_t)staticParams.ServoPitchMin; |
Line 173... | Line 174... | ||
173 | } else if(ServoPitchValue > (int16_t)staticParams.ServoPitchMax) { |
174 | } else if(ServoPitchValue > (int16_t)staticParams.ServoPitchMax) { |
174 | ServoPitchValue = (int16_t)staticParams.ServoPitchMax; |
175 | ServoPitchValue = (int16_t)staticParams.ServoPitchMax; |
175 | } |
176 | } |
176 | 177 | ||
177 | remainingPulseLength = (ServoPitchValue - 256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position |
178 | remainingPulseLength = (ServoPitchValue - 256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position |
178 | 179 | ||
179 | // range servo pulse width |
180 | // range servo pulse width |
180 | if(remainingPulseLength > MAXSERVOPULSE ) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit |
181 | if(remainingPulseLength > MAXSERVOPULSE ) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit |
181 | else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit |
182 | else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit |
182 | 183 | ||
183 | // accumulate time for correct update rate |
184 | // accumulate time for correct update rate |
184 | ServoFrameTime = remainingPulseLength; |
185 | ServoFrameTime = remainingPulseLength; |
185 | } else { // we had a high pulse |
186 | } else { // we had a high pulse |
186 | TCCR2A |= (1<<COM2A0); // make a low pulse |
187 | TCCR2A |= (1<<COM2A0); // make a low pulse |
187 | remainingPulseLength = PPM_FRAMELEN - ServoFrameTime; |
188 | remainingPulseLength = PPM_FRAMELEN - ServoFrameTime; |
188 | } |
189 | } |
189 | // set pulse output active |
190 | // set pulse output active |
190 | isGeneratingPulse = 1; |
191 | isGeneratingPulse = 1; |
191 | } // EOF Pitch servo state machine |
192 | } // EOF Pitch servo state machine |
192 | 193 | ||
193 | #elseif defined(USE_4017_SERVOS) |
194 | #elseif defined(USE_4017_SERVOS) |
194 | //----------------------------------------------------- |
195 | //----------------------------------------------------- |
195 | // PPM state machine, onboard demultiplexed by HEF4017 |
196 | // PPM state machine, onboard demultiplexed by HEF4017 |
196 | //----------------------------------------------------- |
197 | //----------------------------------------------------- |
197 | if(!isGeneratingPulse) { // pulse output complete |
198 | if(!isGeneratingPulse) { // pulse output complete |
198 | if(TCCR2A & (1<<COM2A0)) { // we had a low pulse |
199 | if(TCCR2A & (1<<COM2A0)) { // we had a low pulse |
199 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
200 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
200 | 201 | ||
201 | if(ServoIndex == 0) { // if we are at the sync gap |
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 | } |
|
202 | remainingPulseLength = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
253 | // range servo pulse width |
203 | ServoFrameTime = 0; // reset servo frame time |
- | |
204 | HEF4017R_ON; // enable HEF4017 reset |
- | |
205 | } else { // servo channels |
- | |
206 | remainingPulseLength = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
- | |
207 | switch(ServoIndex) { // map servo channels |
- | |
208 | case 1: // Pitch Compensation Servo |
- | |
209 | ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl * MULTIPLIER) / 4; // lowpass offset |
- | |
210 | ServoPitchValue = ServoPitchOffset; // offset (Range from 0 to 255 * 3 = 765) |
- | |
211 | if(staticParams.ServoPitchCompInvert & 0x01) { |
- | |
212 | // inverting movement of servo |
- | |
213 | ServoPitchValue += (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) ); |
- | |
214 | } else { // non inverting movement of servo |
- | |
215 | ServoPitchValue -= (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) ); |
- | |
216 | } |
- | |
217 | // limit servo value to its parameter range definition |
- | |
218 | if(ServoPitchValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) { |
- | |
219 | ServoPitchValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER; |
- | |
220 | } else if(ServoPitchValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) { |
- | |
221 | ServoPitchValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER; |
- | |
222 | } |
- | |
223 | remainingPulseLength += ServoPitchValue - (256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position |
- | |
224 | ServoPitchValue /= MULTIPLIER; |
- | |
225 | break; |
- | |
226 | - | ||
227 | case 2: // Roll Compensation Servo |
- | |
228 | ServoRollOffset = (ServoRollOffset * 3 + (int16_t)80 * MULTIPLIER) / 4; // lowpass offset |
- | |
229 | ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765) |
- | |
230 | //if(staticParams.ServoRollCompInvert & 0x01) |
- | |
231 | { // inverting movement of servo |
- | |
232 | ServoRollValue += (int16_t)( ( (int32_t) 50 * MULTIPLIER * (integralGyroRoll / 128L ) ) / (256L) ); |
- | |
233 | } |
- | |
234 | /* else |
- | |
235 | { // non inverting movement of servo |
- | |
236 | ServoRollValue -= (int16_t)( ( (int32_t) 40 * MULTIPLIER * (IntegralGyroRoll / 128L ) ) / (256L) ); |
- | |
237 | } |
- | |
238 | */ // limit servo value to its parameter range definition |
- | |
239 | if(ServoRollValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) { |
- | |
240 | ServoRollValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER; |
- | |
241 | } else if(ServoRollValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) { |
- | |
242 | ServoRollValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER; |
- | |
243 | } |
- | |
244 | remainingPulseLength += ServoRollValue - (256 / 2) * MULTIPLIER; // shift ServoRollValue to center position |
- | |
245 | ServoRollValue /= MULTIPLIER; |
- | |
246 | break; |
- | |
247 | - | ||
248 | default: // other servo channels |
- | |
249 | remainingPulseLength += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
- | |
250 | break; |
- | |
251 | } |
- | |
252 | // range servo pulse width |
- | |
253 | if(remainingPulseLength > MAXSERVOPULSE) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit |
254 | if(remainingPulseLength > MAXSERVOPULSE) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit |
Line 254... | Line 255... | ||
254 | else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit |
255 | else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit |
255 | // substract stop pulse width |
256 | // substract stop pulse width |
256 | remainingPulseLength -= PPM_STOPPULSE; |
257 | remainingPulseLength -= PPM_STOPPULSE; |
257 | // accumulate time for correct sync gap |
258 | // accumulate time for correct sync gap |
258 | ServoFrameTime += remainingPulseLength; |
259 | ServoFrameTime += remainingPulseLength; |
259 | } |
260 | } |
260 | } else { // we had a high pulse |
261 | } else { // we had a high pulse |
261 | TCCR2A |= (1<<COM2A0); // make a low pulse |
262 | TCCR2A |= (1<<COM2A0); // make a low pulse |
262 | // set pulsewidth to stop pulse width |
263 | // set pulsewidth to stop pulse width |
263 | remainingPulseLength = PPM_STOPPULSE; |
264 | remainingPulseLength = PPM_STOPPULSE; |
264 | // accumulate time for correct sync gap |
265 | // accumulate time for correct sync gap |
265 | ServoFrameTime += remainingPulseLength; |
266 | ServoFrameTime += remainingPulseLength; |
266 | if(ServoActive && RC_Quality > 180) HEF4017R_OFF; // disable HEF4017 reset |
267 | if(ServoActive && RC_Quality > 180) HEF4017R_OFF; // disable HEF4017 reset |
267 | ServoIndex++; // change to next servo channel |
268 | ServoIndex++; // change to next servo channel |
268 | if(ServoIndex > staticParams.ServoRefresh) ServoIndex = 0; // reset to the sync gap |
269 | if(ServoIndex > staticParams.ServoRefresh) ServoIndex = 0; // reset to the sync gap |
269 | } |
270 | } |
270 | // set pulse output active |
271 | // set pulse output active |
271 | isGeneratingPulse = 1; |
272 | isGeneratingPulse = 1; |
272 | } |
273 | } |
273 | #endif |
274 | #endif |
274 | 275 | ||
275 | /* |
276 | /* |