Subversion Repositories FlightCtrl

Rev

Rev 1821 | Rev 2039 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1821 Rev 1868
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 excample: 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 <avr/io.h>
51
#include <avr/io.h>
52
#include <avr/interrupt.h>
52
#include <avr/interrupt.h>
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"
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;
60
volatile uint8_t ServoActive = 0;
60
volatile uint8_t ServoActive = 0;
61
 
61
 
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)
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) {
71
        uint8_t sreg = SREG;
71
        uint8_t sreg = SREG;
72
 
72
 
73
        // disable all interrupts before reconfiguration
73
        // disable all interrupts before reconfiguration
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
77
        DDRD |= (1 << DDD7);
77
        DDRD |= (1 << DDD7);
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
83
 
83
 
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)
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));
90
        TCCR2A |= (1 << WGM21) | (1 << WGM20);
90
        TCCR2A |= (1 << WGM21) | (1 << WGM20);
91
 
91
 
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
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)
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);
101
 
101
 
102
        // Initialize the Timer/Counter 2 Register
102
        // Initialize the Timer/Counter 2 Register
103
        TCNT2 = 0;
103
        TCNT2 = 0;
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
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
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
{
131
        // frame len 22.5 ms = 14063 * 1.6 us
131
        // frame len 22.5 ms = 14063 * 1.6 us
132
        // stop pulse: 0.3 ms = 188 * 1.6 us
132
        // stop pulse: 0.3 ms = 188 * 1.6 us
133
        // min servo pulse: 0.6 ms =  375 * 1.6 us
133
        // min servo pulse: 0.6 ms =  375 * 1.6 us
134
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
134
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
135
        // resolution: 1500 - 375 = 1125 steps
135
        // resolution: 1500 - 375 = 1125 steps
136
 
136
 
137
#define PPM_STOPPULSE 188
137
#define PPM_STOPPULSE 188
138
#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
139
#define MINSERVOPULSE 375
139
#define MINSERVOPULSE 375
140
#define MAXSERVOPULSE 1500
140
#define MAXSERVOPULSE 1500
141
#define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
141
#define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
142
 
142
 
143
#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)
144
        static uint8_t isGeneratingPulse = 0;
144
        static uint8_t isGeneratingPulse = 0;
145
        static uint16_t remainingPulseLength = 0;
145
        static uint16_t remainingPulseLength = 0;
146
        static uint16_t ServoFrameTime = 0;
146
        static uint16_t ServoFrameTime = 0;
147
        static uint8_t ServoIndex = 0;
147
        static uint8_t ServoIndex = 0;
148
 
148
 
149
#define MULTIPLIER 4
149
#define MULTIPLIER 4
150
        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
151
        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
152
#endif
152
#endif
153
#ifdef USE_NON_4017_SERVO_OUTPUTS 
153
#ifdef USE_NON_4017_SERVO_OUTPUTS 
154
        //---------------------------
154
        //---------------------------
155
        // Pitch servo state machine
155
        // Pitch servo state machine
156
        //---------------------------
156
        //---------------------------
157
        if (!isGeneratingPulse) { // pulse output complete on _next_ interrupt
157
        if (!isGeneratingPulse) { // pulse output complete on _next_ interrupt
158
                if(TCCR2A & (1<<COM2A0)) { // we are still outputting a high pulse
158
                if(TCCR2A & (1<<COM2A0)) { // we are still outputting a high pulse
159
                        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
160
                        remainingPulseLength = MINSERVOPULSE + SERVORANGE / 2; // center position ~ 1.5ms
160
                        remainingPulseLength = MINSERVOPULSE + SERVORANGE / 2; // center position ~ 1.5ms
161
                        ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl) / 4; // lowpass offset
161
                        ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl) / 4; // lowpass offset
162
                        if(staticParams.ServoPitchCompInvert & 0x01) {
162
                        if(staticParams.ServoPitchCompInvert & 0x01) {
163
                                // inverting movement of servo
163
                                // inverting movement of servo
164
                                // todo: function.
164
                                // todo: function.
165
                                ServoPitchValue = ServoPitchOffset + (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L));
165
                                ServoPitchValue = ServoPitchOffset + (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L));
166
                        } else {
166
                        } else {
167
                                // todo: function.
167
                                // todo: function.
168
                                // non inverting movement of servo
168
                                // non inverting movement of servo
169
                                ServoPitchValue = ServoPitchOffset - (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L));
169
                                ServoPitchValue = ServoPitchOffset - (int16_t)(((int32_t)staticParams.ServoPitchComp (integralGyroPitch / 128L )) / (256L));
170
                        }
170
                        }
171
                        // limit servo value to its parameter range definition
171
                        // limit servo value to its parameter range definition
172
                        if(ServoPitchValue < (int16_t)staticParams.ServoPitchMin) {
172
                        if(ServoPitchValue < (int16_t)staticParams.ServoPitchMin) {
173
                                ServoPitchValue = (int16_t)staticParams.ServoPitchMin;
173
                                ServoPitchValue = (int16_t)staticParams.ServoPitchMin;
174
                        } else if(ServoPitchValue > (int16_t)staticParams.ServoPitchMax) {
174
                        } else if(ServoPitchValue > (int16_t)staticParams.ServoPitchMax) {
175
                                ServoPitchValue = (int16_t)staticParams.ServoPitchMax;
175
                                ServoPitchValue = (int16_t)staticParams.ServoPitchMax;
176
                        }
176
                        }
177
 
177
 
178
                        remainingPulseLength = (ServoPitchValue - 256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position
178
                        remainingPulseLength = (ServoPitchValue - 256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position
179
 
179
 
180
                        // range servo pulse width
180
                        // range servo pulse width
181
                        if(remainingPulseLength > MAXSERVOPULSE ) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit
181
                        if(remainingPulseLength > MAXSERVOPULSE ) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit
182
                        else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit
182
                        else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit
183
 
183
 
184
                        // accumulate time for correct update rate
184
                        // accumulate time for correct update rate
185
                        ServoFrameTime = remainingPulseLength;
185
                        ServoFrameTime = remainingPulseLength;
186
                } else { // we had a high pulse
186
                } else { // we had a high pulse
187
                        TCCR2A |= (1<<COM2A0); // make a low pulse
187
                        TCCR2A |= (1<<COM2A0); // make a low pulse
188
                        remainingPulseLength = PPM_FRAMELEN - ServoFrameTime;
188
                        remainingPulseLength = PPM_FRAMELEN - ServoFrameTime;
189
                }
189
                }
190
                // set pulse output active
190
                // set pulse output active
191
                isGeneratingPulse = 1;
191
                isGeneratingPulse = 1;
192
        } // EOF Pitch servo state machine
192
        } // EOF Pitch servo state machine
193
 
193
 
194
#elseif defined(USE_4017_SERVOS)
194
#elseif defined(USE_4017_SERVOS)
195
        //-----------------------------------------------------
195
        //-----------------------------------------------------
196
        // PPM state machine, onboard demultiplexed by HEF4017
196
        // PPM state machine, onboard demultiplexed by HEF4017
197
        //-----------------------------------------------------
197
        //-----------------------------------------------------
198
        if(!isGeneratingPulse) { // pulse output complete
198
        if(!isGeneratingPulse) { // pulse output complete
199
                if(TCCR2A & (1<<COM2A0)) { // we had a low pulse
199
                if(TCCR2A & (1<<COM2A0)) { // we had a low pulse
200
                        TCCR2A &= ~(1<<COM2A0);// make a high pulse
200
                        TCCR2A &= ~(1<<COM2A0);// make a high pulse
201
 
201
 
202
                        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
203
                                remainingPulseLength = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time
204
                                ServoFrameTime = 0; // reset servo frame time
204
                                ServoFrameTime = 0; // reset servo frame time
205
                                HEF4017R_ON; // enable HEF4017 reset
205
                                HEF4017R_ON; // enable HEF4017 reset
206
                        } else { // servo channels
206
                        } else { // servo channels
207
                                remainingPulseLength = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
207
                                remainingPulseLength = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
208
                                switch(ServoIndex) { // map servo channels
208
                                switch(ServoIndex) { // map servo channels
209
                                        case 1: // Pitch Compensation Servo
209
                                        case 1: // Pitch Compensation Servo
210
                                        ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl * MULTIPLIER) / 4; // lowpass offset
210
                                        ServoPitchOffset = (ServoPitchOffset * 3 + (int16_t)dynamicParams.ServoPitchControl * MULTIPLIER) / 4; // lowpass offset
211
                                        ServoPitchValue = ServoPitchOffset; // offset (Range from 0 to 255 * 3 = 765)
211
                                        ServoPitchValue = ServoPitchOffset; // offset (Range from 0 to 255 * 3 = 765)
212
                                        if(staticParams.ServoPitchCompInvert & 0x01) {
212
                                        if(staticParams.ServoPitchCompInvert & 0x01) {
213
                                                // inverting movement of servo
213
                                                // inverting movement of servo
214
                                                ServoPitchValue += (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) );
214
                                                ServoPitchValue += (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) );
215
                                        } else { // non inverting movement of servo
215
                                        } else { // non inverting movement of servo
216
                                                ServoPitchValue -= (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) );
216
                                                ServoPitchValue -= (int16_t)( ( (int32_t)staticParams.ServoPitchComp * MULTIPLIER * (integralGyroPitch / 128L ) ) / (256L) );
217
                                        }
217
                                        }
218
                                        // limit servo value to its parameter range definition
218
                                        // limit servo value to its parameter range definition
219
                                        if(ServoPitchValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) {
219
                                        if(ServoPitchValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) {
220
                                                ServoPitchValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER;
220
                                                ServoPitchValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER;
221
                                        } else if(ServoPitchValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) {
221
                                        } else if(ServoPitchValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) {
222
                                                ServoPitchValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER;
222
                                                ServoPitchValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER;
223
                                        }
223
                                        }
224
                                        remainingPulseLength += ServoPitchValue - (256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position
224
                                        remainingPulseLength += ServoPitchValue - (256 / 2) * MULTIPLIER; // shift ServoPitchValue to center position
225
                                        ServoPitchValue /= MULTIPLIER;
225
                                        ServoPitchValue /= MULTIPLIER;
226
                                        break;
226
                                        break;
227
 
227
 
228
                                        case 2: // Roll Compensation Servo
228
                                        case 2: // Roll Compensation Servo
229
                                        ServoRollOffset = (ServoRollOffset * 3 + (int16_t)80 * MULTIPLIER) / 4; // lowpass offset
229
                                        ServoRollOffset = (ServoRollOffset * 3 + (int16_t)80 * MULTIPLIER) / 4; // lowpass offset
230
                                        ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765)
230
                                        ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765)
231
                                        //if(staticParams.ServoRollCompInvert & 0x01)
231
                                        //if(staticParams.ServoRollCompInvert & 0x01)
232
                                        { // inverting movement of servo
232
                                        { // inverting movement of servo
233
                                                ServoRollValue += (int16_t)( ( (int32_t) 50 * MULTIPLIER * (integralGyroRoll / 128L ) ) / (256L) );
233
                                                ServoRollValue += (int16_t)( ( (int32_t) 50 * MULTIPLIER * (integralGyroRoll / 128L ) ) / (256L) );
234
                                        }
234
                                        }
235
                                        /*                                                      else
235
                                        /*                                                      else
236
                                         {      // non inverting movement of servo
236
                                         {      // non inverting movement of servo
237
                                         ServoRollValue -= (int16_t)( ( (int32_t) 40 * MULTIPLIER * (IntegralGyroRoll / 128L ) ) / (256L) );
237
                                         ServoRollValue -= (int16_t)( ( (int32_t) 40 * MULTIPLIER * (IntegralGyroRoll / 128L ) ) / (256L) );
238
                                         }
238
                                         }
239
                                         */// limit servo value to its parameter range definition
239
                                         */// limit servo value to its parameter range definition
240
                                        if(ServoRollValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) {
240
                                        if(ServoRollValue < ((int16_t)staticParams.ServoPitchMin * MULTIPLIER)) {
241
                                                ServoRollValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER;
241
                                                ServoRollValue = (int16_t)staticParams.ServoPitchMin * MULTIPLIER;
242
                                        } else if(ServoRollValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) {
242
                                        } else if(ServoRollValue > ((int16_t)staticParams.ServoPitchMax * MULTIPLIER)) {
243
                                                ServoRollValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER;
243
                                                ServoRollValue = (int16_t)staticParams.ServoPitchMax * MULTIPLIER;
244
                                        }
244
                                        }
245
                                        remainingPulseLength += ServoRollValue - (256 / 2) * MULTIPLIER; // shift ServoRollValue to center position
245
                                        remainingPulseLength += ServoRollValue - (256 / 2) * MULTIPLIER; // shift ServoRollValue to center position
246
                                        ServoRollValue /= MULTIPLIER;
246
                                        ServoRollValue /= MULTIPLIER;
247
                                        break;
247
                                        break;
248
 
248
 
249
                                        default: // other servo channels
249
                                        default: // other servo channels
250
                                        remainingPulseLength += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
250
                                        remainingPulseLength += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
251
                                        break;
251
                                        break;
252
                                }
252
                                }
253
                                // range servo pulse width
253
                                // range servo pulse width
254
                                if(remainingPulseLength > MAXSERVOPULSE) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit
254
                                if(remainingPulseLength > MAXSERVOPULSE) remainingPulseLength = MAXSERVOPULSE; // upper servo pulse limit
255
                                else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit
255
                                else if(remainingPulseLength < MINSERVOPULSE) remainingPulseLength = MINSERVOPULSE; // lower servo pulse limit
256
                                // substract stop pulse width
256
                                // substract stop pulse width
257
                                remainingPulseLength -= PPM_STOPPULSE;
257
                                remainingPulseLength -= PPM_STOPPULSE;
258
                                // accumulate time for correct sync gap
258
                                // accumulate time for correct sync gap
259
                                ServoFrameTime += remainingPulseLength;
259
                                ServoFrameTime += remainingPulseLength;
260
                        }
260
                        }
261
                } else { // we had a high pulse
261
                } else { // we had a high pulse
262
                        TCCR2A |= (1<<COM2A0); // make a low pulse
262
                        TCCR2A |= (1<<COM2A0); // make a low pulse
263
                        // set pulsewidth to stop pulse width
263
                        // set pulsewidth to stop pulse width
264
                        remainingPulseLength = PPM_STOPPULSE;
264
                        remainingPulseLength = PPM_STOPPULSE;
265
                        // accumulate time for correct sync gap
265
                        // accumulate time for correct sync gap
266
                        ServoFrameTime += remainingPulseLength;
266
                        ServoFrameTime += remainingPulseLength;
267
                        if(ServoActive && RC_Quality > 180) HEF4017R_OFF; // disable HEF4017 reset
267
                        if(ServoActive && RC_Quality > 180) HEF4017R_OFF; // disable HEF4017 reset
268
                        ServoIndex++; // change to next servo channel
268
                        ServoIndex++; // change to next servo channel
269
                        if(ServoIndex > staticParams.ServoRefresh) ServoIndex = 0; // reset to the sync gap
269
                        if(ServoIndex > staticParams.ServoRefresh) ServoIndex = 0; // reset to the sync gap
270
                }
270
                }
271
                // set pulse output active
271
                // set pulse output active
272
                isGeneratingPulse = 1;
272
                isGeneratingPulse = 1;
273
        }
273
        }
274
#endif
274
#endif
275
 
275
 
276
        /*
276
        /*
277
         * Cases:
277
         * Cases:
278
         * 1) 255 + 128 <= remainingPulseLength --> delta = 255
278
         * 1) 255 + 128 <= remainingPulseLength --> delta = 255
279
         * 2) 255 <= remainingPulseLength < 255 + 128 --> delta = 255 - 128
279
         * 2) 255 <= remainingPulseLength < 255 + 128 --> delta = 255 - 128
280
         *    this is to avoid a too short delta on the last cycle, which would cause
280
         *    this is to avoid a too short delta on the last cycle, which would cause
281
         *    an interupt-on-interrupt condition and the loss of the last interrupt.
281
         *    an interupt-on-interrupt condition and the loss of the last interrupt.
282
         * 3) remainingPulseLength < 255 --> delta = remainingPulseLength
282
         * 3) remainingPulseLength < 255 --> delta = remainingPulseLength
283
         */
283
         */
284
#if defined(USE_NON_4017_SERVO_OUTPUTS) || defined(USE_4017_SERVO_OUTPUTS)
284
#if defined(USE_NON_4017_SERVO_OUTPUTS) || defined(USE_4017_SERVO_OUTPUTS)
285
        uint8_t delta;
285
        uint8_t delta;
286
        if (remainingPulseLength >= (255 + 128)) {
286
        if (remainingPulseLength >= (255 + 128)) {
287
                delta = 255;
287
                delta = 255;
288
        } else if (remainingPulseLength >= 255) {
288
        } else if (remainingPulseLength >= 255) {
289
                delta = 255- 128;
289
                delta = 255- 128;
290
        } else {
290
        } else {
291
                delta = remainingPulseLength;
291
                delta = remainingPulseLength;
292
                isGeneratingPulse = 0; // trigger to stop pulse
292
                isGeneratingPulse = 0; // trigger to stop pulse
293
        }
293
        }
294
        OCR2A = delta;
294
        OCR2A = delta;
295
        remainingPulseLength -= delta;
295
        remainingPulseLength -= delta;
296
#endif
296
#endif
297
}
297
}
298
 
298