Subversion Repositories FlightCtrl

Rev

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