Rev 1960 | Rev 1963 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1960 | Rev 1962 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
2 | // + Copyright (c) 04.2007 Holger Buss |
3 | // + Nur für den privaten Gebrauch |
3 | // + Nur für den privaten Gebrauch |
4 | // + www.MikroKopter.com |
4 | // + www.MikroKopter.com |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + eindeutig als Ursprung verlinkt werden |
18 | // + eindeutig als Ursprung verlinkt werden |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
21 | // + Benutzung auf eigene Gefahr |
21 | // + Benutzung auf eigene Gefahr |
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
25 | // + mit unserer Zustimmung zulässig |
25 | // + mit unserer Zustimmung zulässig |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
30 | // + this list of conditions and the following disclaimer. |
30 | // + this list of conditions and the following disclaimer. |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
32 | // + from this software without specific prior written permission. |
32 | // + from this software without specific prior written permission. |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
34 | // + for non-commercial use (directly or indirectly) |
34 | // + for non-commercial use (directly or indirectly) |
35 | // + Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
35 | // + Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
36 | // + with our written permission |
36 | // + with our written permission |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
38 | // + clearly linked as origin |
38 | // + clearly linked as origin |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | #include <stdlib.h> |
51 | #include <stdlib.h> |
52 | #include <avr/io.h> |
52 | #include <avr/io.h> |
53 | #include <avr/interrupt.h> |
53 | #include <avr/interrupt.h> |
54 | 54 | ||
55 | #include "rc.h" |
55 | #include "rc.h" |
56 | #include "uart0.h" |
56 | #include "uart0.h" |
57 | #include "controlMixer.h" |
57 | #include "controlMixer.h" |
58 | #include "configuration.h" |
58 | #include "configuration.h" |
59 | #include "commands.h" |
59 | #include "commands.h" |
60 | 60 | ||
61 | // The channel array is 1-based. The 0th entry is not used. |
61 | // The channel array is 1-based. The 0th entry is not used. |
62 | volatile int16_t PPM_in[MAX_CHANNELS]; |
62 | volatile int16_t PPM_in[MAX_CHANNELS]; |
63 | volatile int16_t PPM_diff[MAX_CHANNELS]; |
63 | volatile int16_t PPM_diff[MAX_CHANNELS]; |
64 | volatile uint8_t NewPpmData = 1; |
64 | volatile uint8_t NewPpmData = 1; |
65 | volatile int16_t RC_Quality = 0; |
65 | volatile int16_t RC_Quality = 0; |
66 | int16_t RC_PRTY[4]; |
66 | int16_t RC_PRTY[4]; |
67 | uint8_t lastRCCommand = COMMAND_NONE; |
67 | uint8_t lastRCCommand = COMMAND_NONE; |
68 | uint8_t commandTimer = 0; |
68 | uint8_t commandTimer = 0; |
69 | 69 | ||
70 | // Useless. Just trim on the R/C instead. |
70 | // Useless. Just trim on the R/C instead. |
71 | // int16_t stickOffsetPitch = 0, stickOffsetRoll = 0; |
71 | // int16_t stickOffsetPitch = 0, stickOffsetRoll = 0; |
72 | 72 | ||
73 | /*************************************************************** |
73 | /*************************************************************** |
74 | * 16bit timer 1 is used to decode the PPM-Signal |
74 | * 16bit timer 1 is used to decode the PPM-Signal |
75 | ***************************************************************/ |
75 | ***************************************************************/ |
76 | void RC_Init(void) { |
76 | void RC_Init(void) { |
77 | uint8_t sreg = SREG; |
77 | uint8_t sreg = SREG; |
78 | 78 | ||
79 | // disable all interrupts before reconfiguration |
79 | // disable all interrupts before reconfiguration |
80 | cli(); |
80 | cli(); |
81 | 81 | ||
82 | // PPM-signal is connected to the Input Capture Pin (PD6) of timer 1 |
82 | // PPM-signal is connected to the Input Capture Pin (PD6) of timer 1 |
83 | DDRD &= ~(1 << DDD6); |
83 | DDRD &= ~(1<<1DDD6); |
84 | PORTD |= (1 << PORTD6); |
84 | PORTD |= (1<<1PORTD6); |
85 | 85 | ||
86 | // Channel 5,6,7 is decoded to servo signals at pin PD5 (J3), PD4(J4), PD3(J5) |
86 | // Channel 5,6,7 is decoded to servo signals at pin PD5 (J3), PD4(J4), PD3(J5) |
87 | // set as output |
87 | // set as output |
88 | DDRD |= (1 << DDD5) | (1 << DDD4) | (1 << DDD3); |
88 | DDRD |= (1<<DDD5) | (1<<DDD4) | (1<<DDD3); |
89 | // low level |
89 | // low level |
90 | PORTD &= ~((1 << PORTD5) | (1 << PORTD4) | (1 << PORTD3)); |
90 | PORTD &= ~((1<<PORTD5) | (1<<PORTD4) | (1<<PORTD3)); |
91 | 91 | ||
92 | // PD3 can't be used if 2nd UART is activated |
92 | // PD3 can't be used if 2nd UART is activated |
93 | // because TXD1 is at that port |
93 | // because TXD1 is at that port |
94 | if (CPUType != ATMEGA644P) { |
94 | if (CPUType != ATMEGA644P) { |
95 | DDRD |= (1 << PORTD3); |
95 | DDRD |= (1<<PORTD3); |
96 | PORTD &= ~(1 << PORTD3); |
96 | PORTD &= ~(1<<PORTD3); |
97 | } |
97 | } |
98 | 98 | ||
99 | // Timer/Counter1 Control Register A, B, C |
99 | // Timer/Counter1 Control Register A, B, C |
100 | 100 | ||
101 | // Normal Mode (bits: WGM13=0, WGM12=0, WGM11=0, WGM10=0) |
101 | // Normal Mode (bits: WGM13=0, WGM12=0, WGM11=0, WGM10=0) |
102 | // Compare output pin A & B is disabled (bits: COM1A1=0, COM1A0=0, COM1B1=0, COM1B0=0) |
102 | // Compare output pin A & B is disabled (bits: COM1A1=0, COM1A0=0, COM1B1=0, COM1B0=0) |
103 | // Set clock source to SYSCLK/64 (bit: CS12=0, CS11=1, CS10=1) |
103 | // Set clock source to SYSCLK/64 (bit: CS12=0, CS11=1, CS10=1) |
104 | // Enable input capture noise cancler (bit: ICNC1=1) |
104 | // Enable input capture noise cancler (bit: ICNC1=1) |
105 | // Trigger on positive edge of the input capture pin (bit: ICES1=1), |
105 | // Trigger on positive edge of the input capture pin (bit: ICES1=1), |
106 | // Therefore the counter incremets at a clock of 20 MHz/64 = 312.5 kHz or 3.2µs |
106 | // Therefore the counter incremets at a clock of 20 MHz/64 = 312.5 kHz or 3.2µs |
107 | // The longest period is 0xFFFF / 312.5 kHz = 0.209712 s. |
107 | // The longest period is 0xFFFF / 312.5 kHz = 0.209712 s. |
108 | TCCR1A &= ~((1 << COM1A1) | (1 << COM1A0) | (1 << COM1B1) | (1 << COM1B0) |
108 | TCCR1A &= ~((1 << COM1A1) | (1 << COM1A0) | (1 << COM1B1) | (1 << COM1B0) |
109 | | (1 << WGM11) | (1 << WGM10)); |
109 | | (1 << WGM11) | (1 << WGM10)); |
110 | TCCR1B &= ~((1 << WGM13) | (1 << WGM12) | (1 << CS12)); |
110 | TCCR1B &= ~((1 << WGM13) | (1 << WGM12) | (1 << CS12)); |
111 | TCCR1B |= (1 << CS11) | (1 << CS10) | (1 << ICES1) | (1 << ICNC1); |
111 | TCCR1B |= (1 << CS11) | (1 << CS10) | (1 << ICES1) | (1 << ICNC1); |
112 | TCCR1C &= ~((1 << FOC1A) | (1 << FOC1B)); |
112 | TCCR1C &= ~((1 << FOC1A) | (1 << FOC1B)); |
113 | 113 | ||
114 | // Timer/Counter1 Interrupt Mask Register |
114 | // Timer/Counter1 Interrupt Mask Register |
115 | 115 | ||
116 | // Enable Input Capture Interrupt (bit: ICIE1=1) |
116 | // Enable Input Capture Interrupt (bit: ICIE1=1) |
117 | // Disable Output Compare A & B Match Interrupts (bit: OCIE1B=0, OICIE1A=0) |
117 | // Disable Output Compare A & B Match Interrupts (bit: OCIE1B=0, OICIE1A=0) |
118 | // Enable Overflow Interrupt (bit: TOIE1=0) |
118 | // Enable Overflow Interrupt (bit: TOIE1=0) |
119 | TIMSK1 &= ~((1 << OCIE1B) | (1 << OCIE1A) | (1 << TOIE1)); |
119 | TIMSK1 &= ~((1<<OCIE1B) | (1<<OCIE1A) | (1<<TOIE1)); |
120 | TIMSK1 |= (1 << ICIE1); |
120 | TIMSK1 |= (1<<ICIE1); |
121 | 121 | ||
122 | RC_Quality = 0; |
122 | RC_Quality = 0; |
123 | 123 | ||
124 | SREG = sreg; |
124 | SREG = sreg; |
125 | } |
125 | } |
126 | 126 | ||
127 | /********************************************************************/ |
127 | /********************************************************************/ |
128 | /* Every time a positive edge is detected at PD6 */ |
128 | /* Every time a positive edge is detected at PD6 */ |
129 | /********************************************************************/ |
129 | /********************************************************************/ |
130 | /* t-Frame |
130 | /* t-Frame |
131 | <-----------------------------------------------------------------------> |
131 | <-----------------------------------------------------------------------> |
132 | ____ ______ _____ ________ ______ sync gap ____ |
132 | ____ ______ _____ ________ ______ sync gap ____ |
133 | | | | | | | | | | | | |
133 | | | | | | | | | | | | |
134 | | | | | | | | | | | | |
134 | | | | | | | | | | | | |
135 | ___| |_| |_| |_| |_.............| |________________| |
135 | ___| |_| |_| |_| |_.............| |________________| |
136 | <-----><-------><------><--------> <------> <--- |
136 | <-----><-------><------><--------> <------> <--- |
137 | t0 t1 t2 t4 tn t0 |
137 | t0 t1 t2 t4 tn t0 |
458 | 458 |