Subversion Repositories FlightCtrl

Rev

Rev 2380 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2050 holgerb 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + www.MikroKopter.com
3
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// + Software Nutzungsbedingungen (english version: see below)
5
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
6
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
7
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
8
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
9
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
12
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
13
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
14
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
15
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
16
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
17
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
18
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
19
// + des Mitverschuldens offen.
20
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
21
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
22
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
23
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
24
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
25
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
26
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
27
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Software LICENSING TERMS
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
32
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
33
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
34
// + The Software may only be used with the Licensor's products.
35
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
36
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
37
// + agreement shall be the property of the Licensor.
38
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
39
// + features that can be used to identify the program may not be altered or defaced by the customer.
40
// + The customer shall be responsible for taking reasonable precautions
41
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
42
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
43
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
44
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
45
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
46
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
47
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
48
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
49
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
50
// + #### END OF LICENSING TERMS ####
51
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
52
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53
 
1 ingob 54
#include "main.h"
1760 holgerb 55
#define MULTIPLYER 4
1 ingob 56
 
57
volatile unsigned int CountMilliseconds = 0;
2191 holgerb 58
volatile unsigned int tim_main;
1 ingob 59
volatile unsigned char UpdateMotor = 0;
60
volatile unsigned int cntKompass = 0;
61
volatile unsigned int beeptime = 0;
1760 holgerb 62
volatile unsigned char SendSPI = 0, ServoActive = 0, CalculateServoSignals = 1;
2346 holgerb 63
unsigned char JustMK3MagConnected = 0;
1760 holgerb 64
uint16_t RemainingPulse = 0;
65
volatile int16_t ServoNickOffset = (255 / 2) * MULTIPLYER * 16; // initial value near center positon
66
volatile int16_t ServoRollOffset = (255 / 2) * MULTIPLYER * 16; // initial value near center positon
2384 MartinR 67
volatile int16_t ServoPanOffset = (255 / 2) * MULTIPLYER * 16; // MartinR: für Pan-Funktion
723 hbuss 68
 
173 holgerb 69
unsigned int BeepMuster = 0xffff;
2191 holgerb 70
signed int NickServoValue = 128 * MULTIPLYER * 16;
1 ingob 71
 
1156 hbuss 72
volatile int16_t        ServoNickValue = 0;
73
volatile int16_t        ServoRollValue = 0;
2384 MartinR 74
volatile int16_t        ServoPanValue = 0; // MartinR : für PAN-Funktion
1156 hbuss 75
 
76
 
1 ingob 77
enum {
78
  STOP             = 0,
79
  CK               = 1,
80
  CK8              = 2,
81
  CK64             = 3,
82
  CK256            = 4,
83
  CK1024           = 5,
84
  T0_FALLING_EDGE  = 6,
85
  T0_RISING_EDGE   = 7
86
};
87
 
88
 
1561 killagreg 89
ISR(TIMER0_OVF_vect)    // 9,7kHz
1 ingob 90
{
2346 holgerb 91
   static unsigned char cnt_1ms = 1,cnt = 0;
1643 holgerb 92
   unsigned char pieper_ein = 0;
723 hbuss 93
   if(SendSPI) SendSPI--;
1469 killagreg 94
   if(SpektrumTimer) SpektrumTimer--;
1 ingob 95
   if(!cnt--)
96
    {
1105 killagreg 97
     cnt = 9;
1643 holgerb 98
     CountMilliseconds++;
1 ingob 99
     cnt_1ms++;
100
     cnt_1ms %= 2;
1643 holgerb 101
 
1 ingob 102
     if(!cnt_1ms) UpdateMotor = 1;
2346 holgerb 103
         if(!(PINC & 0x10)) JustMK3MagConnected = 1;
1643 holgerb 104
 
1664 holgerb 105
     if(beeptime)
1 ingob 106
        {
1664 holgerb 107
        if(beeptime > 10) beeptime -= 10; else beeptime = 0;
1105 killagreg 108
        if(beeptime & BeepMuster)
173 holgerb 109
         {
110
          pieper_ein = 1;
111
         }
112
         else pieper_ein = 0;
1 ingob 113
        }
1105 killagreg 114
     else
1651 killagreg 115
      {
173 holgerb 116
       pieper_ein = 0;
117
       BeepMuster = 0xffff;
1105 killagreg 118
      }
2309 holgerb 119
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
120
     if(pieper_ein) PORTC |= (1<<7); // Speaker an PORTC.7
121
     else           PORTC &= ~(1<<7);
122
#else
173 holgerb 123
     if(pieper_ein)
124
        {
125
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
126
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
127
        }
1105 killagreg 128
     else
173 holgerb 129
        {
130
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
131
         else                      PORTC &= ~(1<<7);
1105 killagreg 132
        }
2309 holgerb 133
#endif
1643 holgerb 134
        }
2346 holgerb 135
 if(JustMK3MagConnected && !NaviDataOkay && Parameter_GlobalConfig & CFG_KOMPASS_AKTIV)
1 ingob 136
 {
137
  if(PINC & 0x10)
138
   {
2346 holgerb 139
        if(++cntKompass > 1000) JustMK3MagConnected = 0;
1 ingob 140
   }
141
  else
142
   {
1105 killagreg 143
    if((cntKompass) && (cntKompass < 362))
144
    {
693 hbuss 145
     cntKompass += cntKompass / 41;
146
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
1941 holgerb 147
//     KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180;
1105 killagreg 148
    }
1 ingob 149
    cntKompass = 0;
1105 killagreg 150
   }
1 ingob 151
 }
152
}
153
 
154
 
155
// -----------------------------------------------------------------------
2090 holgerb 156
unsigned int SetDelay(unsigned int t)
1 ingob 157
{
158
//  TIMSK0 &= ~_BV(TOIE0);
1105 killagreg 159
  return(CountMilliseconds + t + 1);
1 ingob 160
//  TIMSK0 |= _BV(TOIE0);
161
}
162
 
163
// -----------------------------------------------------------------------
164
char CheckDelay(unsigned int t)
165
{
166
//  TIMSK0 &= ~_BV(TOIE0);
167
  return(((t - CountMilliseconds) & 0x8000) >> 9);
168
//  TIMSK0 |= _BV(TOIE0);
169
}
170
 
171
// -----------------------------------------------------------------------
172
void Delay_ms(unsigned int w)
173
{
174
 unsigned int akt;
175
 akt = SetDelay(w);
176
 while (!CheckDelay(akt));
177
}
178
 
395 hbuss 179
void Delay_ms_Mess(unsigned int w)
180
{
181
 unsigned int akt;
182
 akt = SetDelay(w);
1166 hbuss 183
 while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;}
395 hbuss 184
}
185
 
1156 hbuss 186
/*****************************************************/
187
/*              Initialize Timer 2                   */
188
/*****************************************************/
189
// The timer 2 is used to generate the PWM at PD7 (J7)
190
// to control a camera servo for nick compensation.
191
void TIMER2_Init(void)
910 hbuss 192
{
1156 hbuss 193
        uint8_t sreg = SREG;
194
 
195
        // disable all interrupts before reconfiguration
196
        cli();
1469 killagreg 197
 
1156 hbuss 198
        PORTD &= ~(1<<PORTD7);  // set PD7 to low
199
 
200
        DDRC  |= (1<<DDC6);     // set PC6 as output (Reset for HEF4017)
2146 holgerb 201
    HEF4017Reset_ON;
1156 hbuss 202
        // Timer/Counter 2 Control Register A
203
 
204
        // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1)
205
    // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
206
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
207
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
208
    TCCR2A |= (1<<WGM21)|(1<<WGM20);
209
 
210
    // Timer/Counter 2 Control Register B
211
 
212
        // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz
213
        // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us
214
        // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms
215
 
216
    // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1)
217
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22));
218
    TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22);
219
 
220
        // Initialize the Timer/Counter 2 Register
221
    TCNT2 = 0;
222
 
223
        // Initialize the Output Compare Register A used for PWM generation on port PD7.
224
        OCR2A = 255;
225
        TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0
226
 
227
        // Timer/Counter 2 Interrupt Mask Register
228
        // Enable timer output compare match A Interrupt only
229
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
230
        TIMSK2 |= (1<<OCIE2A);
231
 
232
    SREG = sreg;
910 hbuss 233
}
234
 
1156 hbuss 235
//----------------------------
236
void Timer_Init(void)
1 ingob 237
{
1156 hbuss 238
    tim_main = SetDelay(10);
239
    TCCR0B = CK8;
2367 holgerb 240
//    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
241
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|(1<<COM0B0)|3;//fast PWM
242
    OCR0B =  255;
243
    OCR0A = 180;
1156 hbuss 244
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
245
    //OCR1  = 0x00;
246
    TIMSK0 |= _BV(TOIE0);
247
}
248
 
249
 
250
/*****************************************************/
251
/*              Control Servo Position               */
252
/*****************************************************/
2191 holgerb 253
void CalcNickServoValue(void)
254
{
2232 holgerb 255
 signed int max, min;
1156 hbuss 256
 
2191 holgerb 257
 if(EE_Parameter.ServoCompInvert & SERVO_RELATIVE) // relative moving of the servo value
258
  {
259
        max = ((unsigned int) EE_Parameter.ServoNickMax * MULTIPLYER * 15);
260
        min = ((unsigned int) EE_Parameter.ServoNickMin * MULTIPLYER * 20);
2192 holgerb 261
        NickServoValue -= ((signed char) (Parameter_ServoNickControl - 128) / 4) * 6;
2191 holgerb 262
        LIMIT_MIN_MAX(NickServoValue,min, max);
263
  }
264
  else NickServoValue = (int16_t)Parameter_ServoNickControl * (MULTIPLYER*16);  // direct poti control
265
}
1760 holgerb 266
 
267
void CalculateServo(void)
268
{
2384 MartinR 269
 //signed char cosinus, sinus; // MartinR : so war es
270
 extern signed char cosinus, sinus; // MartinR : extern für PAN-Funktion
1771 holgerb 271
 signed long nick, roll;
1760 holgerb 272
 
2384 MartinR 273
        nick = 0; // MartinR : StartWert bei abgeschalteten Nick/ Roll ausgleich
274
        roll = 0; // MartinR : StartWert bei abgeschalteten Nick/ Roll ausgleich
275
        int tmp; // MartinR : für PAN-Funktion // Wert : 0-24 -> 0-360 -> 15° steps
276
        /* // MartinR: bisher
277
        tmp = EE_Parameter.CamOrientation + ((Parameter_Servo4 - 125) * (Parameter_UserParam8 - 125)) / 400 ; //MartinR : für PAN-Funktion
278
        if (tmp < 0) tmp = 24- (abs(tmp)) % 24 ; // MartinR :Modulo 24
279
        else tmp = tmp % 24 ; // MartinR :Modulo 24
280
        */
281
        tmp = EE_Parameter.CamOrientation + ((Parameter_Servo4 - 125) * (Parameter_UserParam8 - 125)) / 200 ; //MartinR : für PAN-Funktion
282
        if (tmp < 0) tmp = 48- (abs(tmp)) % 48 ; // MartinR :Modulo 48
283
        else tmp = tmp % 48 ; // MartinR :Modulo 48
284
 
285
        // cosinus = sintab[EE_Parameter.CamOrientation + 6];  // MartinR : so war es
286
        // sinus = sintab[EE_Parameter.CamOrientation];  // MartinR : so war es
287
        //cosinus = sintab[tmp + 6];  // MartinR : für PAN-Funktion
288
        cosinus += (2*sintab[tmp + 12]- cosinus + 1) / 2;  // MartinR : für PAN-Funktion
289
        sinus += (2*sintab[tmp] - sinus + 1) / 2;  // MartinR : für PAN-Funktion
1760 holgerb 290
 
291
  if(CalculateServoSignals == 1)
292
   {
2380 holgerb 293
            if(EE_Parameter.GlobalConfig3 & CFG3_SERVO_NICK_COMP_OFF) nick = 0;
2384 MartinR 294
            //else nick = (cosinus * IntegralNick) / 128L - (sinus * IntegralRoll) / 128L; // MartinR: so war es
295
                else nick = (cosinus * IntegralNick) / 512L - (sinus * IntegralRoll) / 512L; // MartinR: bessere Auflösung
1848 holgerb 296
        nick -= POI_KameraNick * 7;
2296 holgerb 297
                nick = ((long)Parameter_ServoNickComp * nick) / 512L;
2010 holgerb 298
                // offset (Range from 0 to 255 * 3 = 765)
2191 holgerb 299
                if(EE_Parameter.ServoCompInvert & SERVO_RELATIVE) ServoNickOffset = NickServoValue;
300
                else ServoNickOffset += (NickServoValue - ServoNickOffset) / EE_Parameter.ServoManualControlSpeed;
301
 
302
                if(EE_Parameter.ServoCompInvert & SERVO_NICK_INV) // inverting movement of servo
2010 holgerb 303
                {      
304
                        nick = ServoNickOffset / 16 + nick;
1763 killagreg 305
                }
306
                else
2010 holgerb 307
                {       // inverting movement of servo
308
                        nick = ServoNickOffset / 16 - nick;
1763 killagreg 309
                }
2012 holgerb 310
                if(EE_Parameter.ServoFilterNick) ServoNickValue = ((ServoNickValue * EE_Parameter.ServoFilterNick) + nick) / (EE_Parameter.ServoFilterNick + 1);
2010 holgerb 311
                else                     ServoNickValue = nick;
1763 killagreg 312
                // limit servo value to its parameter range definition
2040 holgerb 313
                if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER))
1763 killagreg 314
                {
315
                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
316
                }
317
                else
2040 holgerb 318
                if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER))
1763 killagreg 319
                {
320
                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
321
                }
322
                if(PlatinenVersion < 20) CalculateServoSignals = 0; else CalculateServoSignals++;
1760 holgerb 323
        }
324
        else
325
        {
2384 MartinR 326
            if(EE_Parameter.GlobalConfig3 & CFG3_SERVO_NICK_COMP_OFF) roll = 0;
327
        //roll = (cosinus * IntegralRoll) / 128L + (sinus * IntegralNick) / 128L; // MartinR: so war es
328
                else roll = (cosinus * IntegralRoll) / 512L + (sinus * IntegralNick) / 512L; // MartinR: bessere Auflösung
2296 holgerb 329
        roll = ((long)Parameter_ServoRollComp * roll) / 512L;
1763 killagreg 330
                ServoRollOffset += ((int16_t)Parameter_ServoRollControl * (MULTIPLYER*16) - ServoRollOffset) / EE_Parameter.ServoManualControlSpeed;
2191 holgerb 331
                if(EE_Parameter.ServoCompInvert & SERVO_ROLL_INV)
1763 killagreg 332
                {       // inverting movement of servo
2010 holgerb 333
                        roll = ServoRollOffset / 16 + roll;
1763 killagreg 334
                }
335
                else
2010 holgerb 336
                {       // inverting movement of servo
337
                        roll = ServoRollOffset / 16 - roll;
1763 killagreg 338
                }
2012 holgerb 339
                if(EE_Parameter.ServoFilterRoll) ServoRollValue = ((ServoRollValue * EE_Parameter.ServoFilterRoll) + roll) / (EE_Parameter.ServoFilterRoll + 1);
2010 holgerb 340
                else                     ServoRollValue = roll;
1763 killagreg 341
                // limit servo value to its parameter range definition
2040 holgerb 342
                if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER))
1763 killagreg 343
                {
344
                        ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER;
345
                }
346
                else
2040 holgerb 347
                if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER))
1763 killagreg 348
                {
349
                        ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER;
350
                }
2384 MartinR 351
 
352
                // MartinR: Filterung der Pan- Funktion
353
                ServoPanOffset += ((int16_t)Parameter_Servo4 * (MULTIPLYER*16) - ServoPanOffset) / EE_Parameter.ServoManualControlSpeed;
354
                ServoPanValue = (int16_t)ServoPanOffset/16; // offset (Range from 0 to 255 * 3 = 765)
355
 
1763 killagreg 356
                CalculateServoSignals = 0;
1760 holgerb 357
        }
358
}
359
 
1156 hbuss 360
ISR(TIMER2_COMPA_vect)
361
{
362
        // frame len 22.5 ms = 14063 * 1.6 us
363
        // stop pulse: 0.3 ms = 188 * 1.6 us
364
        // min servo pulse: 0.6 ms =  375 * 1.6 us
365
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
366
        // resolution: 1500 - 375 = 1125 steps
367
 
368
        #define IRS_RUNTIME 127
369
        #define PPM_STOPPULSE 188
1171 hbuss 370
    #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh)
1156 hbuss 371
        #define MINSERVOPULSE 375
372
        #define MAXSERVOPULSE 1500
373
        #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
374
 
375
        static uint8_t  PulseOutput = 0;
376
        static uint16_t ServoFrameTime = 0;
377
        static uint8_t  ServoIndex = 0;
378
 
379
 
380
        if(PlatinenVersion < 20)
381
        {
382
                //---------------------------
383
                // Nick servo state machine
384
                //---------------------------
385
                if(!PulseOutput) // pulse output complete
386
                {
387
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
388
                        {
389
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
390
                                RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
391
                                RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
392
                                // range servo pulse width
393
                                if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
394
                                else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
395
                                // accumulate time for correct update rate
396
                                ServoFrameTime = RemainingPulse;
397
                        }
398
                        else // we had a high pulse
399
                        {
400
                                TCCR2A |= (1<<COM2A0); // make a low pulse
401
                                RemainingPulse = PPM_FRAMELEN - ServoFrameTime;
1763 killagreg 402
                                CalculateServoSignals = 1;
1156 hbuss 403
                        }
404
                        // set pulse output active
405
                        PulseOutput = 1;
406
                }
407
        } // EOF Nick servo state machine
408
        else
409
        {
410
                //-----------------------------------------------------
411
                // PPM state machine, onboard demultiplexed by HEF4017
412
                //-----------------------------------------------------
413
                if(!PulseOutput) // pulse output complete
414
                {
415
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
416
                        {
417
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
418
                                if(ServoIndex == 0) // if we are at the sync gap
419
                                {
420
                                        RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time
421
                                        ServoFrameTime = 0; // reset servo frame time
2146 holgerb 422
                                        HEF4017Reset_ON; // enable HEF4017 reset
1156 hbuss 423
                                }
2166 holgerb 424
                                else // servo channels 
425
                                if(ServoIndex > EE_Parameter.ServoNickRefresh)  
426
                                 {
427
                                  RemainingPulse = 10; // end it here
428
                                 }
429
                                else
1156 hbuss 430
                                {
431
                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
432
                                        switch(ServoIndex) // map servo channels
433
                                        {
2166 holgerb 434
                                         case 1: // Nick Compensation Servo
1156 hbuss 435
                                                        RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
436
                                                        break;
1224 hbuss 437
                                         case 2: // Roll Compensation Servo
438
                                                        RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
1232 hbuss 439
                                                        break;
1565 killagreg 440
                                         case 3:
441
                                                        RemainingPulse += ((int16_t)Parameter_Servo3 * MULTIPLYER) - (256 / 2) * MULTIPLYER;
1403 hbuss 442
                                                        break;
1543 killagreg 443
                                         case 4:
2384 MartinR 444
                                                        //RemainingPulse += ((int16_t)Parameter_Servo4 * MULTIPLYER) - (256 / 2) * MULTIPLYER; // MartinR: so war es
445
                                                        RemainingPulse += ServoPanValue - (256 / 2) * MULTIPLYER; // MartinR: zur Filterung der Pan-Funktion
1403 hbuss 446
                                                        break;
1543 killagreg 447
                                         case 5:
1565 killagreg 448
                                                        RemainingPulse += ((int16_t)Parameter_Servo5 * MULTIPLYER) - (256 / 2) * MULTIPLYER;
1403 hbuss 449
                                                        break;
1156 hbuss 450
                                                default: // other servo channels
451
                                                        RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
452
                                                        break;
453
                                        }
454
                                        // range servo pulse width
2166 holgerb 455
                                        if(RemainingPulse > MAXSERVOPULSE)                      RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
456
                                        else if(RemainingPulse < MINSERVOPULSE)     RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
1156 hbuss 457
                                        // substract stop pulse width
458
                                        RemainingPulse -= PPM_STOPPULSE;
459
                                        // accumulate time for correct sync gap
460
                                        ServoFrameTime += RemainingPulse;
461
                                }
462
                        }
463
                        else // we had a high pulse
464
                        {
465
                                TCCR2A |= (1<<COM2A0); // make a low pulse
466
                                // set pulsewidth to stop pulse width
467
                                RemainingPulse = PPM_STOPPULSE;
468
                                // accumulate time for correct sync gap
469
                                ServoFrameTime += RemainingPulse;
2146 holgerb 470
                                if((ServoActive && SenderOkay) || ServoActive == 2) HEF4017Reset_OFF; // disable HEF4017 reset
471
                                else HEF4017Reset_ON;
2166 holgerb 472
                                ServoIndex++;
473
                                if(ServoIndex > EE_Parameter.ServoNickRefresh+1)
1763 killagreg 474
                                  {
475
                                    CalculateServoSignals = 1;
1760 holgerb 476
                                        ServoIndex = 0; // reset to the sync gap
477
                                  }
1156 hbuss 478
                        }
479
                        // set pulse output active
480
                        PulseOutput = 1;
481
                }
482
        } // EOF PPM state machine
483
 
484
        // General pulse output generator
485
        if(RemainingPulse > (255 + IRS_RUNTIME))
486
        {
487
                OCR2A = 255;
488
                RemainingPulse -= 255;
910 hbuss 489
        }
1156 hbuss 490
        else
491
        {
492
                if(RemainingPulse > 255) // this is the 2nd last part
493
                {
494
                        if((RemainingPulse - 255) < IRS_RUNTIME)
495
                        {
496
                                OCR2A = 255 - IRS_RUNTIME;
497
                                RemainingPulse -= 255 - IRS_RUNTIME;
498
 
499
                        }
500
                        else // last part > ISR_RUNTIME
501
                        {
502
                                OCR2A = 255;
503
                                RemainingPulse -= 255;
504
                        }
505
                }
506
                else // this is the last part
507
                {
508
                        OCR2A = RemainingPulse;
509
                        RemainingPulse = 0;
510
                        PulseOutput = 0; // trigger to stop pulse
511
                }
512
        } // EOF general pulse output generator
1111 hbuss 513
}