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