Rev 2073 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2073 | Rev 2160 | ||
---|---|---|---|
Line 9... | Line 9... | ||
9 | #include "output.h" |
9 | #include "output.h" |
Line 10... | Line 10... | ||
10 | 10 | ||
11 | // The channel array is 0-based! |
11 | // The channel array is 0-based! |
12 | volatile int16_t PPM_in[MAX_CHANNELS]; |
12 | volatile int16_t PPM_in[MAX_CHANNELS]; |
- | 13 | volatile int16_t PPM_diff[MAX_CHANNELS]; |
|
- | 14 | volatile uint16_t RC_buffer[MAX_CHANNELS]; |
|
- | 15 | volatile uint8_t inBfrPnt = 0; |
|
13 | volatile int16_t PPM_diff[MAX_CHANNELS]; |
16 | |
14 | volatile uint8_t RCQuality; |
17 | volatile uint8_t RCQuality; |
15 | uint8_t lastRCCommand = COMMAND_NONE; |
18 | uint8_t lastRCCommand = COMMAND_NONE; |
Line -... | Line 19... | ||
- | 19 | uint8_t commandTimer = 0; |
|
- | 20 | ||
16 | uint8_t commandTimer = 0; |
21 | #define TIME(s) ((int16_t)(((long)F_CPU/(long)64000)*(float)s + 0.5f)) |
17 | 22 | ||
18 | /*************************************************************** |
23 | /*************************************************************** |
19 | * 16bit timer 1 is used to decode the PPM-Signal |
24 | * 16bit timer 1 is used to decode the PPM-Signal |
20 | ***************************************************************/ |
25 | ***************************************************************/ |
Line 39... | Line 44... | ||
39 | if (CPUType != ATMEGA644P) { |
44 | if (CPUType != ATMEGA644P) { |
40 | DDRD |= (1<<PORTD3); |
45 | DDRD |= (1<<PORTD3); |
41 | PORTD &= ~(1<<PORTD3); |
46 | PORTD &= ~(1<<PORTD3); |
42 | } |
47 | } |
Line 43... | Line -... | ||
43 | - | ||
44 | // Timer/Counter1 Control Register A, B, C |
- | |
45 | 48 | ||
46 | // Normal Mode (bits: WGM13=0, WGM12=0, WGM11=0, WGM10=0) |
49 | // Normal Mode (bits: WGM13=0, WGM12=0, WGM11=0, WGM10=0) |
47 | // Compare output pin A & B is disabled (bits: COM1A1=0, COM1A0=0, COM1B1=0, COM1B0=0) |
50 | // Compare output pin A & B is disabled (bits: COM1A1=0, COM1A0=0, COM1B1=0, COM1B0=0) |
48 | // Set clock source to SYSCLK/64 (bit: CS12=0, CS11=1, CS10=1) |
51 | // Set clock source to SYSCLK/64 (bit: CS12=0, CS11=1, CS10=1) |
49 | // Enable input capture noise cancler (bit: ICNC1=1) |
- | |
50 | // Trigger on positive edge of the input capture pin (bit: ICES1=1), |
52 | // Enable input capture noise cancler (bit: ICNC1=1) |
51 | // Therefore the counter incremets at a clock of 20 MHz/64 = 312.5 kHz or 3.2�s |
53 | // Therefore the counter incremets at a clock of 20 MHz/64 = 312.5 kHz or 3.2�s |
52 | // The longest period is 0xFFFF / 312.5 kHz = 0.209712 s. |
54 | // The longest period is 0xFFFF / 312.5 kHz = 0.209712 s. |
53 | TCCR1A &= ~((1 << COM1A1) | (1 << COM1A0) | (1 << COM1B1) | (1 << COM1B0) | (1 << WGM11) | (1 << WGM10)); |
55 | TCCR1A &= ~((1<<COM1A1)| (1<<COM1A0) | (1<<COM1B1) | (1<<COM1B0) | (1<<WGM11) | (1<<WGM10)); |
54 | TCCR1B &= ~((1 << WGM13) | (1 << WGM12) | (1 << CS12)); |
56 | TCCR1B &= ~((1<<WGM13) | (1<<WGM12) | (1<<CS12)); |
- | 57 | TCCR1B |= (1<<CS11) | (1<<CS10) | (1<<ICNC1); |
|
- | 58 | TCCR1C &= ~((1<<FOC1A) | (1<<FOC1B)); |
|
- | 59 | ||
- | 60 | if (channelMap.RCPolarity) { |
|
- | 61 | TCCR1B |= (1<<ICES1); |
|
- | 62 | } else { |
|
- | 63 | TCCR1B &= ~(1<<ICES1); |
|
- | 64 | } |
|
55 | TCCR1B |= (1 << CS11) | (1 << CS10) | (1 << ICES1) | (1 << ICNC1); |
65 | |
Line 56... | Line 66... | ||
56 | TCCR1C &= ~((1 << FOC1A) | (1 << FOC1B)); |
66 | TCCR1C &= ~((1 << FOC1A) | (1 << FOC1B)); |
57 | 67 | ||
58 | // Timer/Counter1 Interrupt Mask Register |
68 | // Timer/Counter1 Interrupt Mask Register |
Line 65... | Line 75... | ||
65 | RCQuality = 0; |
75 | RCQuality = 0; |
Line 66... | Line 76... | ||
66 | 76 | ||
67 | SREG = sreg; |
77 | SREG = sreg; |
Line -... | Line 78... | ||
- | 78 | } |
|
- | 79 | ||
- | 80 | /* |
|
- | 81 | * This new and much faster interrupt handler should reduce servo jolts. |
|
- | 82 | */ |
|
- | 83 | ISR(TIMER1_CAPT_vect) { |
|
- | 84 | static uint16_t oldICR1 = 0; |
|
- | 85 | uint16_t signal = (uint16_t)ICR1 - oldICR1; |
|
- | 86 | oldICR1 = ICR1; |
|
- | 87 | //sync gap? (3.5 ms < signal < 25.6 ms) |
|
- | 88 | if (signal > TIME(3.5)) { |
|
- | 89 | inBfrPnt = 0; |
|
- | 90 | } else if (inBfrPnt<MAX_CHANNELS) { |
|
- | 91 | RC_buffer[inBfrPnt++] = signal; |
|
- | 92 | if (RCQuality <= 200-4) RCQuality+=4; else RCQuality = 200; |
|
- | 93 | } |
|
68 | } |
94 | } |
69 | 95 | ||
70 | /********************************************************************/ |
96 | /********************************************************************/ |
71 | /* Every time a positive edge is detected at PD6 */ |
97 | /* Every time a positive edge is detected at PD6 */ |
72 | /********************************************************************/ |
98 | /********************************************************************/ |
Line 86... | Line 112... | ||
86 | The minimum duration of all channels at minimum value is 8 * 1 ms = 8 ms. |
112 | The minimum duration of all channels at minimum value is 8 * 1 ms = 8 ms. |
87 | The maximum duration of all channels at maximum value is 8 * 2 ms = 16 ms. |
113 | The maximum duration of all channels at maximum value is 8 * 2 ms = 16 ms. |
88 | The remaining time of (22.5 - 8 ms) ms = 14.5 ms to (22.5 - 16 ms) ms = 6.5 ms is |
114 | The remaining time of (22.5 - 8 ms) ms = 14.5 ms to (22.5 - 16 ms) ms = 6.5 ms is |
89 | the syncronization gap. |
115 | the syncronization gap. |
90 | */ |
116 | */ |
91 | ISR(TIMER1_CAPT_vect) { // typical rate of 1 ms to 2 ms |
- | |
92 | int16_t signal = 0, tmp; |
- | |
93 | static int16_t index; |
- | |
94 | static uint16_t oldICR1 = 0; |
- | |
95 | - | ||
96 | // 16bit Input Capture Register ICR1 contains the timer value TCNT1 |
- | |
97 | // at the time the edge was detected |
- | |
98 | - | ||
99 | // calculate the time delay to the previous event time which is stored in oldICR1 |
- | |
100 | // calculatiing the difference of the two uint16_t and converting the result to an int16_t |
- | |
101 | // implicit handles a timer overflow 65535 -> 0 the right way. |
- | |
102 | signal = (uint16_t) ICR1 - oldICR1; |
- | |
103 | oldICR1 = ICR1; |
- | |
Line 104... | Line -... | ||
104 | - | ||
105 | //sync gap? (3.52 ms < signal < 25.6 ms) |
- | |
106 | if ((signal > 1100) && (signal < 8000)) { |
117 | |
107 | index = 0; |
118 | void RC_process(void) { |
108 | } else { // within the PPM frame |
119 | if (RCQuality) RCQuality--; |
109 | if (index < MAX_CHANNELS) { // PPM24 supports 12 channels |
- | |
110 | // check for valid signal length (0.8 ms < signal < 2.1984 ms) |
- | |
111 | // signal range is from 1.0ms/3.2us = 312 to 2.0ms/3.2us = 625 |
- | |
112 | if ((signal > 250) && (signal < 687)) { |
- | |
113 | // shift signal to zero symmetric range -154 to 159 |
- | |
114 | signal -= 475; // offset of 1.4912 ms ??? (469 * 3.2us = 1.5008 ms) |
- | |
115 | // check for stable signal |
120 | for (uint8_t channel=0; channel<MAX_CHANNELS; channel++) { |
116 | if (abs(signal - PPM_in[index]) < 6) { |
- | |
117 | if (RCQuality < 200) |
- | |
118 | RCQuality += 10; |
121 | uint16_t signal = RC_buffer[channel]; |
119 | else |
- | |
120 | RCQuality = 200; |
- | |
121 | } |
- | |
122 | // If signal is the same as before +/- 1, just keep it there. Naah lets get rid of this slimy sticy stuff. |
122 | if (signal != 0) { |
123 | // if (signal >= PPM_in[index] - 1 && signal <= PPM_in[index] + 1) { |
- | |
124 | // In addition, if the signal is very close to 0, just set it to 0. |
123 | RC_buffer[channel] = 0; // reset to flag value already used. |
125 | if (signal >= -1 && signal <= 1) { |
124 | if ((signal >= TIME(0.8)) && (signal < TIME(2.2))) { |
126 | tmp = 0; |
- | |
127 | //} else { |
125 | signal -= TIME(1.5); |
128 | // tmp = PPM_in[index]; |
- | |
129 | // } |
- | |
130 | } else |
- | |
131 | tmp = signal; |
- | |
132 | // calculate signal difference on good signal level |
- | |
133 | if (RCQuality >= 195) |
- | |
134 | PPM_diff[index] = signal - PPM_in[index]; //((tmp - PPM_in[index]) / 3) * 3; // cut off lower 3 bit for nois reduction |
- | |
135 | else |
126 | PPM_diff[channel] = signal - PPM_in[channel]; |
136 | PPM_diff[index] = 0; |
- | |
137 | PPM_in[index] = tmp; // update channel value |
127 | PPM_in[channel] = signal; |
138 | } |
- | |
139 | index++; // next channel |
- | |
140 | // demux sum signal for channels 5 to 7 to J3, J4, J5 |
- | |
141 | // TODO: General configurability of this R/C channel forwarding. Or remove it completely - the |
- | |
142 | // channels are usually available at the receiver anyway. |
- | |
143 | // if(index == 5) J3HIGH; else J3LOW; |
- | |
144 | // if(index == 6) J4HIGH; else J4LOW; |
- | |
145 | // if(CPUType != ATMEGA644P) // not used as TXD1 |
- | |
146 | // { |
- | |
147 | // if(index == 7) J5HIGH; else J5LOW; |
- | |
148 | // } |
128 | } |
149 | } |
129 | } |
150 | } |
130 | } |
Line 151... | Line 131... | ||
151 | } |
131 | } |
Line 180... | Line 160... | ||
180 | /* |
160 | /* |
181 | * Get Pitch, Roll, Throttle, Yaw values |
161 | * Get Pitch, Roll, Throttle, Yaw values |
182 | */ |
162 | */ |
183 | void RC_periodicTaskAndPRTY(int16_t* PRTY) { |
163 | void RC_periodicTaskAndPRTY(int16_t* PRTY) { |
184 | int16_t tmp1, tmp2; |
164 | int16_t tmp1, tmp2; |
- | 165 | RC_process(); |
|
185 | if (RCQuality) { |
166 | if (RCQuality) { |
186 | RCQuality--; |
167 | RCQuality--; |
187 | PRTY[CONTROL_PITCH] = RCChannel(CH_PITCH) * staticParams.stickP + RCDiff(CH_PITCH) * staticParams.stickD; |
168 | PRTY[CONTROL_PITCH] = RCChannel(CH_PITCH) * staticParams.stickP + RCDiff(CH_PITCH) * staticParams.stickD; |
188 | PRTY[CONTROL_ROLL] = RCChannel(CH_ROLL) * staticParams.stickP + RCDiff(CH_ROLL) * staticParams.stickD; |
169 | PRTY[CONTROL_ROLL] = RCChannel(CH_ROLL) * staticParams.stickP + RCDiff(CH_ROLL) * staticParams.stickD; |
189 | int16_t throttle = RCChannel(CH_THROTTLE) + RCDiff(CH_THROTTLE) * staticParams.stickThrottleD + 120; |
170 | int16_t throttle = RCChannel(CH_THROTTLE) + RCDiff(CH_THROTTLE) * staticParams.stickThrottleD + 120; |