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