Subversion Repositories FlightCtrl

Rev

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

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