Subversion Repositories FlightCtrl

Rev

Rev 1179 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1180 killagreg 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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"
18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
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
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
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
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"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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)
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
886 killagreg 51
#include <avr/io.h>
52
#include <avr/interrupt.h>
53
#include "fc.h"
54
#include "eeprom.h"
1180 killagreg 55
#include "uart0.h"
1078 killagreg 56
#include "main.h"
1180 killagreg 57
#include "rc.h"
886 killagreg 58
 
1180 killagreg 59
volatile int16_t        ServoNickValue = 0;
60
volatile int16_t        ServoRollValue = 0;
61
volatile uint8_t        ServoActive = 0;
886 killagreg 62
 
1078 killagreg 63
#define HEF4017R_ON     PORTC |=  (1<<PORTC6)
64
#define HEF4017R_OFF    PORTC &= ~(1<<PORTC6)
886 killagreg 65
 
936 killagreg 66
 
886 killagreg 67
/*****************************************************/
68
/*              Initialize Timer 2                   */
69
/*****************************************************/
70
// The timer 2 is used to generate the PWM at PD7 (J7)
911 killagreg 71
// to control a camera servo for nick compensation.
886 killagreg 72
void TIMER2_Init(void)
73
{
74
        uint8_t sreg = SREG;
75
 
76
        // disable all interrupts before reconfiguration
77
        cli();
78
 
911 killagreg 79
        // set PD7 as output of the PWM for nick servo
1078 killagreg 80
        DDRD  |= (1<<DDD7);
936 killagreg 81
        PORTD &= ~(1<<PORTD7);  // set PD7 to low
886 killagreg 82
 
1078 killagreg 83
        DDRC  |= (1<<DDC6);     // set PC6 as output (Reset for HEF4017)
1180 killagreg 84
        //PORTC &= ~(1<<PORTC6);        // set PC6 to low
85
        HEF4017R_ON; // enable reset
886 killagreg 86
 
87
        // Timer/Counter 2 Control Register A
88
 
1180 killagreg 89
        // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1)
936 killagreg 90
    // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
886 killagreg 91
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
936 killagreg 92
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
93
    TCCR2A |= (1<<WGM21)|(1<<WGM20);
886 killagreg 94
 
95
    // Timer/Counter 2 Control Register B
96
 
1180 killagreg 97
        // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz
98
        // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us
99
        // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms
886 killagreg 100
 
1180 killagreg 101
    // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1)
102
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22));
103
    TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22);
886 killagreg 104
 
105
        // Initialize the Timer/Counter 2 Register
106
    TCNT2 = 0;
107
 
936 killagreg 108
        // Initialize the Output Compare Register A used for PWM generation on port PD7.
1180 killagreg 109
        OCR2A = 255;
110
        TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0
936 killagreg 111
 
886 killagreg 112
        // Timer/Counter 2 Interrupt Mask Register
113
        // Enable timer output compare match A Interrupt only
114
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
115
        TIMSK2 |= (1<<OCIE2A);
116
 
117
    SREG = sreg;
118
}
119
 
120
 
1180 killagreg 121
void Servo_On(void)
122
{
123
        ServoActive = 1;
124
}
125
 
126
void Servo_Off(void)
127
{
128
        ServoActive = 0;
129
        HEF4017R_ON; // enable reset
130
}
131
 
886 killagreg 132
/*****************************************************/
133
/*              Control Servo Position               */
134
/*****************************************************/
936 killagreg 135
 
1180 killagreg 136
ISR(TIMER2_COMPA_vect)
886 killagreg 137
{
138
 
1180 killagreg 139
        // frame len 22.5 ms = 14063 * 1.6 us
140
        // stop pulse: 0.3 ms = 188 * 1.6 us
141
        // min servo pulse: 0.6 ms =  375 * 1.6 us
142
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
143
        // resolution: 1500 - 375 = 1125 steps
886 killagreg 144
 
1180 killagreg 145
        #define IRS_RUNTIME 127
146
        #define PPM_STOPPULSE 188
147
        //#define PPM_FRAMELEN 14063
148
        #define PPM_FRAMELEN (1757 * ParamSet.ServoRefresh) // 22.5 ms / 8 Channels = 2.8125ms per Servo Channel
149
        #define MINSERVOPULSE 375
150
        #define MAXSERVOPULSE 1500
151
        #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
152
 
153
        static uint8_t  PulseOutput = 0;
154
        static uint16_t RemainingPulse = 0;
155
        static uint16_t ServoFrameTime = 0;
156
        static uint8_t  ServoIndex = 0;
157
 
158
        #define MULTIPLYER 4
159
        static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center positon
160
 
161
 
162
        if(BoardRelease < 20)
1078 killagreg 163
        {
1180 killagreg 164
                //---------------------------
165
                // Nick servo state machine
166
                //---------------------------
167
                if(!PulseOutput) // pulse output complete
1078 killagreg 168
                {
1180 killagreg 169
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
170
                        {
171
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
172
                                RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
173
 
174
                                ServoNickOffset = (ServoNickOffset * 3 + (int16_t)FCParam.ServoNickControl * MULTIPLYER) / 4; // lowpass offset
175
                                ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
1078 killagreg 176
                                if(ParamSet.ServoNickCompInvert & 0x01)
177
                                {       // inverting movement of servo
1180 killagreg 178
                                        ServoNickValue += (int16_t)( ( (int32_t)ParamSet.ServoNickComp * MULTIPLYER * (IntegralGyroNick / 128L ) ) / (256L) );
1078 killagreg 179
                                }
180
                                else
181
                                {       // non inverting movement of servo
1180 killagreg 182
                                        ServoNickValue -= (int16_t)( ( (int32_t)ParamSet.ServoNickComp * MULTIPLYER * (IntegralGyroNick / 128L ) ) / (256L) );
1078 killagreg 183
                                }
184
                                // limit servo value to its parameter range definition
1180 killagreg 185
                                if(ServoNickValue < ((int16_t)ParamSet.ServoNickMin * MULTIPLYER) )
1078 killagreg 186
                                {
1180 killagreg 187
                                        ServoNickValue = (int16_t)ParamSet.ServoNickMin * MULTIPLYER;
1078 killagreg 188
                                }
189
                                else
1180 killagreg 190
                                if(ServoNickValue > ((int16_t)ParamSet.ServoNickMax * MULTIPLYER) )
1078 killagreg 191
                                {
1180 killagreg 192
                                        ServoNickValue = (int16_t)ParamSet.ServoNickMax * MULTIPLYER;
1078 killagreg 193
                                }
936 killagreg 194
 
1180 killagreg 195
                                RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
936 killagreg 196
 
1180 killagreg 197
                                ServoNickValue /= MULTIPLYER;
198
                                DebugOut.Analog[20] = ServoNickValue;
936 killagreg 199
 
1180 killagreg 200
                                // range servo pulse width
201
                                if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
202
                                else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
203
                                // accumulate time for correct update rate
204
                                ServoFrameTime = RemainingPulse;
205
                        }
206
                        else // we had a high pulse
207
                        {
208
                                TCCR2A |= (1<<COM2A0); // make a low pulse
209
                                RemainingPulse = PPM_FRAMELEN - ServoFrameTime;
210
                        }
211
                        // set pulse output active
212
                        PulseOutput = 1;
213
                }
214
        } // EOF Nick servo state machine
215
        else
216
        {
217
                //-----------------------------------------------------
218
                // PPM state machine, onboard demultiplexed by HEF4017
219
                //-----------------------------------------------------
220
                if(!PulseOutput) // pulse output complete
221
                {
222
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
223
                        {
224
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
936 killagreg 225
 
1180 killagreg 226
                                if(ServoIndex == 0) // if we are at the sync gap
1078 killagreg 227
                                {
1180 killagreg 228
                                        RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time
229
                                        ServoFrameTime = 0; // reset servo frame time
230
                                        HEF4017R_ON; // enable HEF4017 reset
1078 killagreg 231
                                }
1180 killagreg 232
                                else // servo channels
1078 killagreg 233
                                {
1180 killagreg 234
                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
235
                                        switch(ServoIndex) // map servo channels
236
                                        {
237
                                                case 1: // Nick Compensation Servo
238
                                                        ServoNickOffset = (ServoNickOffset * 3 + (int16_t)FCParam.ServoNickControl * MULTIPLYER) / 4; // lowpass offset
239
                                                        ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
240
                                                        if(ParamSet.ServoNickCompInvert & 0x01)
241
                                                        {       // inverting movement of servo
242
                                                                ServoNickValue += (int16_t)( ( (int32_t)ParamSet.ServoNickComp * MULTIPLYER * (IntegralGyroNick / 128L ) ) / (256L) );
243
                                                        }
244
                                                        else
245
                                                        {       // non inverting movement of servo
246
                                                                ServoNickValue -= (int16_t)( ( (int32_t)ParamSet.ServoNickComp * MULTIPLYER * (IntegralGyroNick / 128L ) ) / (256L) );
247
                                                        }
248
                                                        // limit servo value to its parameter range definition
249
                                                        if(ServoNickValue < ((int16_t)ParamSet.ServoNickMin * MULTIPLYER) )
250
                                                        {
251
                                                                ServoNickValue = (int16_t)ParamSet.ServoNickMin * MULTIPLYER;
252
                                                        }
253
                                                        else
254
                                                        if(ServoNickValue > ((int16_t)ParamSet.ServoNickMax * MULTIPLYER) )
255
                                                        {
256
                                                                ServoNickValue = (int16_t)ParamSet.ServoNickMax * MULTIPLYER;
257
                                                        }
936 killagreg 258
 
1180 killagreg 259
                                                        RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
936 killagreg 260
 
1180 killagreg 261
                                                        ServoNickValue /= MULTIPLYER;
262
                                                        DebugOut.Analog[20] = ServoNickValue;
263
                                                        break;
264
 
265
                                                default: // other servo channels
266
                                                        RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
267
                                                        break;
268
                                        }
269
                                        // range servo pulse width
270
                                        if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
271
                                        else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
272
                                        // substract stop pulse width
273
                                        RemainingPulse -= PPM_STOPPULSE;
274
                                        // accumulate time for correct sync gap
275
                                        ServoFrameTime += RemainingPulse;
276
                                }
277
                        }
278
                        else // we had a high pulse
279
                        {
280
                                TCCR2A |= (1<<COM2A0); // make a low pulse
281
                                // set pulsewidth to stop pulse width
282
                                RemainingPulse = PPM_STOPPULSE;
283
                                // accumulate time for correct sync gap
284
                                ServoFrameTime += RemainingPulse;
285
                                if(ServoActive && RC_Quality > 180) HEF4017R_OFF; // disable HEF4017 reset
286
                                ServoIndex++; // change to next servo channel
287
                                if(ServoIndex > ParamSet.ServoRefresh) ServoIndex = 0; // reset to the sync gap
288
                        }
289
                        // set pulse output active
290
                        PulseOutput = 1;
1078 killagreg 291
                }
1180 killagreg 292
        } // EOF PPM state machine
293
 
294
        // General pulse output generator
295
        if(RemainingPulse > (255 + IRS_RUNTIME))
1078 killagreg 296
        {
1180 killagreg 297
                OCR2A = 255;
298
                RemainingPulse -= 255;
936 killagreg 299
        }
1180 killagreg 300
        else
301
        {
302
                if(RemainingPulse > 255) // this is the 2nd last part
303
                {
304
                        if((RemainingPulse - 255) < IRS_RUNTIME)
305
                        {
306
                                OCR2A = 255 - IRS_RUNTIME;
307
                                RemainingPulse -= 255 - IRS_RUNTIME;
308
 
309
                        }
310
                        else // last part > ISR_RUNTIME
311
                        {
312
                                OCR2A = 255;
313
                                RemainingPulse -= 255;
314
                        }
315
                }
316
                else // this is the last part
317
                {
318
                        OCR2A = RemainingPulse;
319
                        RemainingPulse = 0;
320
                        PulseOutput = 0; // trigger to stop pulse
321
                }
322
        } // EOF general pulse output generator
323
 
886 killagreg 324
}