Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2751 - 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
 
54
#include "main.h"
55
#define MULTIPLYER 4
56
 
57
volatile unsigned int CountMilliseconds = 0;
58
volatile unsigned int tim_main;
59
volatile unsigned char UpdateMotor = 0;
60
volatile unsigned int cntKompass = 0;
61
volatile unsigned int beeptime = 0;
62
volatile unsigned char BytegapSPI = 0, ServoActive = 0, CalculateServoSignals = 1;
63
unsigned char JustMK3MagConnected = 0;
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
67
 
68
unsigned int BeepMuster = 0xffff;
69
signed int NickServoValue = 128 * MULTIPLYER * 16;
70
 
71
volatile int16_t        ServoNickValue = 0;
72
volatile int16_t        ServoRollValue = 0;
73
 
74
 
75
enum {
76
  STOP             = 0,
77
  CK               = 1,
78
  CK8              = 2,
79
  CK64             = 3,
80
  CK256            = 4,
81
  CK1024           = 5,
82
  T0_FALLING_EDGE  = 6,
83
  T0_RISING_EDGE   = 7
84
};
85
 
86
 
87
ISR(TIMER0_OVF_vect)    // 9,7kHz
88
{
89
   static unsigned char cnt_1ms = 1,cnt = 0;
90
   unsigned char pieper_ein = 0;
91
   if(BytegapSPI) BytegapSPI--;
92
   if(SpektrumTimer) SpektrumTimer--;
93
   if(!cnt--)
94
    {
95
     cnt = 9;
96
     CountMilliseconds++;
97
     cnt_1ms++;
98
     cnt_1ms %= 2;
99
 
100
     if(!cnt_1ms) if(UpdateMotor < 4) UpdateMotor++;
101
         if(!(PINC & 0x10)) JustMK3MagConnected = 1;
102
 
103
     if(beeptime)
104
        {
105
        if(beeptime > 10) beeptime -= 10; else beeptime = 0;
106
        if(beeptime & BeepMuster)
107
         {
108
          pieper_ein = 1;
109
         }
110
         else pieper_ein = 0;
111
        }
112
     else
113
      {
114
       pieper_ein = 0;
115
       BeepMuster = 0xffff;
116
      }
117
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
118
     if(pieper_ein) PORTC |= (1<<7); // Speaker an PORTC.7
119
     else           PORTC &= ~(1<<7);
120
#else
121
     if(pieper_ein)
122
        {
123
//          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
124
//          else                      
125
                  PORTC |= (1<<7); // Speaker an PORTC.7
126
        }
127
     else
128
        {
129
//         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
130
//         else                      
131
                 PORTC &= ~(1<<7);
132
        }
133
#endif
134
        }
135
 if(JustMK3MagConnected && !NaviDataOkay && Parameter_GlobalConfig & CFG_KOMPASS_AKTIV)
136
 {
137
  if(PINC & 0x10)
138
   {
139
        if(++cntKompass > 1000) JustMK3MagConnected = 0;
140
   }
141
  else
142
   {
143
    if((cntKompass) && (cntKompass < 362))
144
    {
145
     cntKompass += cntKompass / 41;
146
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
147
    }
148
    cntKompass = 0;
149
   }
150
 }
151
}
152
 
153
 
154
// -----------------------------------------------------------------------
155
unsigned int SetDelay(unsigned int t)
156
{
157
//  TIMSK0 &= ~_BV(TOIE0);
158
  return(CountMilliseconds + t + 1);
159
//  TIMSK0 |= _BV(TOIE0);
160
}
161
 
162
// -----------------------------------------------------------------------
163
char CheckDelay(unsigned int t)
164
{
165
//  TIMSK0 &= ~_BV(TOIE0);
166
  return(((t - CountMilliseconds) & 0x8000) >> 9);
167
//  TIMSK0 |= _BV(TOIE0);
168
}
169
 
170
// -----------------------------------------------------------------------
171
void Delay_ms(unsigned int w)
172
{
173
 unsigned int akt;
174
 akt = SetDelay(w);
175
 while (!CheckDelay(akt));
176
}
177
 
178
void Delay_ms_Mess(unsigned int w)
179
{
180
 unsigned int akt;
181
 akt = SetDelay(w);
182
 while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;}
183
}
184
 
185
/*****************************************************/
186
/*              Initialize Timer 2                   */
187
/*****************************************************/
188
// The timer 2 is used to generate the PWM at PD7 (J7)
189
// to control a camera servo for nick compensation.
190
void TIMER2_Init(void)
191
{
192
        uint8_t sreg = SREG;
193
 
194
        // disable all interrupts before reconfiguration
195
        cli();
196
 
197
        PORTD &= ~(1<<PORTD7);  // set PD7 to low
198
 
199
        DDRC  |= (1<<DDC6);     // set PC6 as output (Reset for HEF4017)
200
    HEF4017Reset_ON;
201
        // Timer/Counter 2 Control Register A
202
 
203
        // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1)
204
    // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
205
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
206
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
207
    TCCR2A |= (1<<WGM21)|(1<<WGM20);
208
 
209
    // Timer/Counter 2 Control Register B
210
 
211
        // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz
212
        // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us
213
        // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms
214
 
215
    // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1)
216
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22));
217
    TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22);
218
 
219
        // Initialize the Timer/Counter 2 Register
220
    TCNT2 = 0;
221
 
222
        // Initialize the Output Compare Register A used for PWM generation on port PD7.
223
        OCR2A = 255;
224
        TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0
225
 
226
        // Timer/Counter 2 Interrupt Mask Register
227
        // Enable timer output compare match A Interrupt only
228
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
229
        TIMSK2 |= (1<<OCIE2A);
230
 
231
    SREG = sreg;
232
}
233
 
234
//----------------------------
235
void Timer_Init(void)
236
{
237
    tim_main = SetDelay(10);
238
    TCCR0B = CK8;
239
//    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
240
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|(1<<COM0B0)|3;//fast PWM
241
    OCR0B =  255;
242
    OCR0A = 180;
243
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
244
    //OCR1  = 0x00;
245
    TIMSK0 |= _BV(TOIE0);
246
}
247
 
248
 
249
/*****************************************************/
250
/*              Control Servo Position               */
251
/*****************************************************/
252
void CalcNickServoValue(void)
253
{
254
 signed int max, min;
255
 
256
 if(EE_Parameter.ServoCompInvert & SERVO_RELATIVE) // relative moving of the servo value
257
  {
258
        max = ((unsigned int) EE_Parameter.ServoNickMax * MULTIPLYER * 15);
259
        min = ((unsigned int) EE_Parameter.ServoNickMin * MULTIPLYER * 20);
260
        NickServoValue -= ((signed char) (Parameter_ServoNickControl - 128) / 4) * 6;
261
        LIMIT_MIN_MAX(NickServoValue,min, max);
262
  }
263
  else NickServoValue = (int16_t)Parameter_ServoNickControl * (MULTIPLYER*16);  // direct poti control
264
}
265
 
266
void CalculateServo(void)
267
{
268
 signed char cosinus, sinus;
269
 signed long nick, roll;
270
 
271
        cosinus = sintab[EE_Parameter.CamOrientation + 6];
272
        sinus = sintab[EE_Parameter.CamOrientation];
273
 
274
 
275
  if(CalculateServoSignals == 1)
276
   {
277
            if(EE_Parameter.GlobalConfig3 & CFG3_SERVO_NICK_COMP_OFF) nick = 0;
278
            else nick = (cosinus * IntegralNick) / 128L - (sinus * IntegralRoll) / 128L;
279
        nick -= POI_KameraNick * 7;
280
                nick = ((long)Parameter_ServoNickComp * nick) / 512L;
281
                // offset (Range from 0 to 255 * 3 = 765)
282
                if(EE_Parameter.ServoCompInvert & SERVO_RELATIVE) ServoNickOffset = NickServoValue;
283
                else ServoNickOffset += (NickServoValue - ServoNickOffset) / EE_Parameter.ServoManualControlSpeed;
284
 
285
                if(EE_Parameter.ServoCompInvert & SERVO_NICK_INV) // inverting movement of servo
286
                {      
287
                        nick = ServoNickOffset / 16 + nick;
288
                }
289
                else
290
                {       // inverting movement of servo
291
                        nick = ServoNickOffset / 16 - nick;
292
                }
293
                if(EE_Parameter.ServoFilterNick) ServoNickValue = ((ServoNickValue * EE_Parameter.ServoFilterNick) + nick) / (EE_Parameter.ServoFilterNick + 1);
294
                else                     ServoNickValue = nick;
295
                // limit servo value to its parameter range definition
296
                if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER))
297
                {
298
                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
299
                }
300
                else
301
                if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER))
302
                {
303
                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
304
                }
305
//              if(PlatinenVersion < 20) CalculateServoSignals = 0; else 
306
                CalculateServoSignals++;
307
        }
308
        else
309
        {
310
            if(EE_Parameter.GlobalConfig3 & CFG3_SERVO_NICK_COMP_OFF) roll = 0;
311
        else roll = (cosinus * IntegralRoll) / 128L + (sinus * IntegralNick) / 128L;
312
        roll = ((long)Parameter_ServoRollComp * roll) / 512L;
313
                ServoRollOffset += ((int16_t)Parameter_ServoRollControl * (MULTIPLYER*16) - ServoRollOffset) / EE_Parameter.ServoManualControlSpeed;
314
                if(EE_Parameter.ServoCompInvert & SERVO_ROLL_INV)
315
                {       // inverting movement of servo
316
                        roll = ServoRollOffset / 16 + roll;
317
                }
318
                else
319
                {       // inverting movement of servo
320
                        roll = ServoRollOffset / 16 - roll;
321
                }
322
                if(EE_Parameter.ServoFilterRoll) ServoRollValue = ((ServoRollValue * EE_Parameter.ServoFilterRoll) + roll) / (EE_Parameter.ServoFilterRoll + 1);
323
                else                     ServoRollValue = roll;
324
                // limit servo value to its parameter range definition
325
                if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER))
326
                {
327
                        ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER;
328
                }
329
                else
330
                if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER))
331
                {
332
                        ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER;
333
                }
334
                CalculateServoSignals = 0;
335
        }
336
}
337
 
338
ISR(TIMER2_COMPA_vect)
339
{
340
        // frame len 22.5 ms = 14063 * 1.6 us
341
        // stop pulse: 0.3 ms = 188 * 1.6 us
342
        // min servo pulse: 0.6 ms =  375 * 1.6 us
343
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
344
        // resolution: 1500 - 375 = 1125 steps
345
 
346
        #define IRS_RUNTIME 127
347
        #define PPM_STOPPULSE 188
348
    #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh)
349
        #define MINSERVOPULSE 375
350
        #define MAXSERVOPULSE 1500
351
        #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
352
 
353
        static uint8_t  PulseOutput = 0;
354
        static uint16_t ServoFrameTime = 0;
355
        static uint8_t  ServoIndex = 0;
356
 
357
/*
358
        if(PlatinenVersion < 20)
359
        {
360
                //---------------------------
361
                // Nick servo state machine
362
                //---------------------------
363
                if(!PulseOutput) // pulse output complete
364
                {
365
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
366
                        {
367
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
368
                                RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
369
                                RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
370
                                // range servo pulse width
371
                                if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
372
                                else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
373
                                // accumulate time for correct update rate
374
                                ServoFrameTime = RemainingPulse;
375
                        }
376
                        else // we had a high pulse
377
                        {
378
                                TCCR2A |= (1<<COM2A0); // make a low pulse
379
                                RemainingPulse = PPM_FRAMELEN - ServoFrameTime;
380
                                CalculateServoSignals = 1;
381
                        }
382
                        // set pulse output active
383
                        PulseOutput = 1;
384
                }
385
        } // EOF Nick servo state machine
386
        else
387
*/
388
        {
389
                //-----------------------------------------------------
390
                // PPM state machine, onboard demultiplexed by HEF4017
391
                //-----------------------------------------------------
392
                if(!PulseOutput) // pulse output complete
393
                {
394
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
395
                        {
396
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
397
                                if(ServoIndex == 0) // if we are at the sync gap
398
                                {
399
                                        RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time
400
                                        ServoFrameTime = 0; // reset servo frame time
401
                                        HEF4017Reset_ON; // enable HEF4017 reset
402
                                }
403
                                else // servo channels 
404
                                if(ServoIndex > EE_Parameter.ServoNickRefresh)  
405
                                 {
406
                                  RemainingPulse = 10; // end it here
407
                                 }
408
                                else
409
                                {
410
                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
411
                                        if(ServoFailsafeActive && ServoIndex < 6 && EE_Parameter.ServoFS_Pos[ServoIndex-1]) RemainingPulse += ((int16_t)EE_Parameter.ServoFS_Pos[ServoIndex-1] * MULTIPLYER) - (256 / 2) * MULTIPLYER;
412
                                        else
413
                                        switch(ServoIndex) // map servo channels
414
                                        {
415
                                         case 1: // Nick Compensation Servo
416
                                                        RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER;
417
                                                        break;
418
                                         case 2: // Roll Compensation Servo
419
                                                        RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER;
420
                                                        break;
421
                                         case 3:
422
                                                        RemainingPulse += ((int16_t)Parameter_Servo3 * MULTIPLYER) - (256 / 2) * MULTIPLYER;
423
                                                        break;
424
                                         case 4:
425
                                                        RemainingPulse += ((int16_t)Parameter_Servo4 * MULTIPLYER) - (256 / 2) * MULTIPLYER;
426
                                                        break;
427
                                         case 5:
428
                                                        RemainingPulse += ((int16_t)Parameter_Servo5 * MULTIPLYER) - (256 / 2) * MULTIPLYER;
429
                                                        break;
430
                                        default: // other servo channels
431
                                                        RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
432
                                                        break;
433
                                        }
434
                                        // range servo pulse width
435
                                        if(RemainingPulse > MAXSERVOPULSE)                      RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
436
                                        else if(RemainingPulse < MINSERVOPULSE)     RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
437
                                        // substract stop pulse width
438
                                        RemainingPulse -= PPM_STOPPULSE;
439
                                        // accumulate time for correct sync gap
440
                                        ServoFrameTime += RemainingPulse;
441
                                }
442
                        }
443
                        else // we had a high pulse
444
                        {
445
                                TCCR2A |= (1<<COM2A0); // make a low pulse
446
                                // set pulsewidth to stop pulse width
447
                                RemainingPulse = PPM_STOPPULSE;
448
                                // accumulate time for correct sync gap
449
                                ServoFrameTime += RemainingPulse;
450
                                if((ServoActive/* && SenderOkay*/) || ServoActive == 2) HEF4017Reset_OFF; // disable HEF4017 reset
451
                                else HEF4017Reset_ON;
452
                                ServoIndex++;
453
                                if(ServoIndex > EE_Parameter.ServoNickRefresh+1)
454
                                  {
455
                                    CalculateServoSignals = 1;
456
                                        ServoIndex = 0; // reset to the sync gap
457
                                  }
458
                        }
459
                        // set pulse output active
460
                        PulseOutput = 1;
461
                }
462
        } // EOF PPM state machine
463
 
464
        // General pulse output generator
465
        if(RemainingPulse > (255 + IRS_RUNTIME))
466
        {
467
                OCR2A = 255;
468
                RemainingPulse -= 255;
469
        }
470
        else
471
        {
472
                if(RemainingPulse > 255) // this is the 2nd last part
473
                {
474
                        if((RemainingPulse - 255) < IRS_RUNTIME)
475
                        {
476
                                OCR2A = 255 - IRS_RUNTIME;
477
                                RemainingPulse -= 255 - IRS_RUNTIME;
478
 
479
                        }
480
                        else // last part > ISR_RUNTIME
481
                        {
482
                                OCR2A = 255;
483
                                RemainingPulse -= 255;
484
                        }
485
                }
486
                else // this is the last part
487
                {
488
                        OCR2A = RemainingPulse;
489
                        RemainingPulse = 0;
490
                        PulseOutput = 0; // trigger to stop pulse
491
                }
492
        } // EOF general pulse output generator
493
}