Subversion Repositories FlightCtrl

Rev

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

Rev 1166 Rev 1171
1
#include "main.h"
1
#include "main.h"
2
 
2
 
3
volatile unsigned int CountMilliseconds = 0;
3
volatile unsigned int CountMilliseconds = 0;
4
volatile static unsigned int tim_main;
4
volatile static unsigned int tim_main;
5
volatile unsigned char UpdateMotor = 0;
5
volatile unsigned char UpdateMotor = 0;
6
volatile unsigned int cntKompass = 0;
6
volatile unsigned int cntKompass = 0;
7
volatile unsigned int beeptime = 0;
7
volatile unsigned int beeptime = 0;
8
volatile unsigned char SendSPI = 0;
8
volatile unsigned char SendSPI = 0, ServoActive = 0;
9
volatile unsigned int ServoState = 40;
-
 
10
 
9
 
11
unsigned int BeepMuster = 0xffff;
10
unsigned int BeepMuster = 0xffff;
12
int ServoValue = 0;
11
int ServoValue = 0;
13
 
12
 
14
volatile int16_t        ServoNickValue = 0;
13
volatile int16_t        ServoNickValue = 0;
15
volatile int16_t        ServoRollValue = 0;
14
volatile int16_t        ServoRollValue = 0;
16
 
-
 
17
#define HEF4017R_ON     PORTC |=  (1<<PORTC6)
-
 
18
#define HEF4017R_OFF    PORTC &= ~(1<<PORTC6)
-
 
19
 
15
 
20
 
16
 
21
enum {
17
enum {
22
  STOP             = 0,
18
  STOP             = 0,
23
  CK               = 1,
19
  CK               = 1,
24
  CK8              = 2,
20
  CK8              = 2,
25
  CK64             = 3,
21
  CK64             = 3,
26
  CK256            = 4,
22
  CK256            = 4,
27
  CK1024           = 5,
23
  CK1024           = 5,
28
  T0_FALLING_EDGE  = 6,
24
  T0_FALLING_EDGE  = 6,
29
  T0_RISING_EDGE   = 7
25
  T0_RISING_EDGE   = 7
30
};
26
};
31
 
27
 
32
 
28
 
33
SIGNAL (SIG_OVERFLOW0)    // 8kHz
29
SIGNAL (SIG_OVERFLOW0)    // 8kHz
34
{
30
{
35
    static unsigned char cnt_1ms = 1,cnt = 0;
31
    static unsigned char cnt_1ms = 1,cnt = 0;
36
    unsigned char pieper_ein = 0;
32
    unsigned char pieper_ein = 0;
37
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
33
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
38
   if(SendSPI) SendSPI--;
34
   if(SendSPI) SendSPI--;
39
   if(!cnt--)
35
   if(!cnt--)
40
    {
36
    {
41
     cnt = 9;
37
     cnt = 9;
42
     cnt_1ms++;
38
     cnt_1ms++;
43
     cnt_1ms %= 2;
39
     cnt_1ms %= 2;
44
     if(!cnt_1ms) UpdateMotor = 1;
40
     if(!cnt_1ms) UpdateMotor = 1;
45
     CountMilliseconds++;
41
     CountMilliseconds++;
46
    }
42
    }
47
 
43
 
48
     if(beeptime > 1)
44
     if(beeptime > 1)
49
        {
45
        {
50
        beeptime--;
46
        beeptime--;
51
        if(beeptime & BeepMuster)
47
        if(beeptime & BeepMuster)
52
         {
48
         {
53
          pieper_ein = 1;
49
          pieper_ein = 1;
54
         }
50
         }
55
         else pieper_ein = 0;
51
         else pieper_ein = 0;
56
        }
52
        }
57
     else
53
     else
58
      {
54
      {
59
       pieper_ein = 0;
55
       pieper_ein = 0;
60
       BeepMuster = 0xffff;
56
       BeepMuster = 0xffff;
61
      }
57
      }
62
 
58
 
63
 
59
 
64
     if(pieper_ein)
60
     if(pieper_ein)
65
        {
61
        {
66
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
62
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
67
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
63
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
68
        }
64
        }
69
     else
65
     else
70
        {
66
        {
71
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
67
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
72
         else                      PORTC &= ~(1<<7);
68
         else                      PORTC &= ~(1<<7);
73
        }
69
        }
74
 
70
 
75
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
71
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
76
 {
72
 {
77
  if(PINC & 0x10)
73
  if(PINC & 0x10)
78
   {
74
   {
79
    cntKompass++;
75
    cntKompass++;
80
   }
76
   }
81
  else
77
  else
82
   {
78
   {
83
    if((cntKompass) && (cntKompass < 362))
79
    if((cntKompass) && (cntKompass < 362))
84
    {
80
    {
85
     cntKompass += cntKompass / 41;
81
     cntKompass += cntKompass / 41;
86
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
82
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
87
    }
83
    }
88
//     if(cntKompass < 10) cntKompass = 10;
84
//     if(cntKompass < 10) cntKompass = 10;
89
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
85
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
90
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
86
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
91
    cntKompass = 0;
87
    cntKompass = 0;
92
   }
88
   }
93
 }
89
 }
94
}
90
}
95
 
91
 
96
 
92
 
97
// -----------------------------------------------------------------------
93
// -----------------------------------------------------------------------
98
 
94
 
99
unsigned int SetDelay (unsigned int t)
95
unsigned int SetDelay (unsigned int t)
100
{
96
{
101
//  TIMSK0 &= ~_BV(TOIE0);
97
//  TIMSK0 &= ~_BV(TOIE0);
102
  return(CountMilliseconds + t + 1);
98
  return(CountMilliseconds + t + 1);
103
//  TIMSK0 |= _BV(TOIE0);
99
//  TIMSK0 |= _BV(TOIE0);
104
}
100
}
105
 
101
 
106
// -----------------------------------------------------------------------
102
// -----------------------------------------------------------------------
107
char CheckDelay(unsigned int t)
103
char CheckDelay(unsigned int t)
108
{
104
{
109
//  TIMSK0 &= ~_BV(TOIE0);
105
//  TIMSK0 &= ~_BV(TOIE0);
110
  return(((t - CountMilliseconds) & 0x8000) >> 9);
106
  return(((t - CountMilliseconds) & 0x8000) >> 9);
111
//  TIMSK0 |= _BV(TOIE0);
107
//  TIMSK0 |= _BV(TOIE0);
112
}
108
}
113
 
109
 
114
// -----------------------------------------------------------------------
110
// -----------------------------------------------------------------------
115
void Delay_ms(unsigned int w)
111
void Delay_ms(unsigned int w)
116
{
112
{
117
 unsigned int akt;
113
 unsigned int akt;
118
 akt = SetDelay(w);
114
 akt = SetDelay(w);
119
 while (!CheckDelay(akt));
115
 while (!CheckDelay(akt));
120
}
116
}
121
 
117
 
122
void Delay_ms_Mess(unsigned int w)
118
void Delay_ms_Mess(unsigned int w)
123
{
119
{
124
 unsigned int akt;
120
 unsigned int akt;
125
 akt = SetDelay(w);
121
 akt = SetDelay(w);
126
 while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;}
122
 while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;}
127
}
123
}
128
 
124
 
129
/*****************************************************/
125
/*****************************************************/
130
/*              Initialize Timer 2                   */
126
/*              Initialize Timer 2                   */
131
/*****************************************************/
127
/*****************************************************/
132
// The timer 2 is used to generate the PWM at PD7 (J7)
128
// The timer 2 is used to generate the PWM at PD7 (J7)
133
// to control a camera servo for nick compensation.
129
// to control a camera servo for nick compensation.
134
void TIMER2_Init(void)
130
void TIMER2_Init(void)
135
{
131
{
136
        uint8_t sreg = SREG;
132
        uint8_t sreg = SREG;
137
 
133
 
138
        // disable all interrupts before reconfiguration
134
        // disable all interrupts before reconfiguration
139
        cli();
135
        cli();
140
 
136
 
141
        // set PD7 as output of the PWM for nick servo
137
        // set PD7 as output of the PWM for nick servo
142
        DDRD  |= (1<<DDD7);
138
        DDRD  |= (1<<DDD7);
143
        PORTD &= ~(1<<PORTD7);  // set PD7 to low
139
        PORTD &= ~(1<<PORTD7);  // set PD7 to low
144
 
140
 
145
        DDRC  |= (1<<DDC6);     // set PC6 as output (Reset for HEF4017)
141
        DDRC  |= (1<<DDC6);     // set PC6 as output (Reset for HEF4017)
146
        PORTC &= ~(1<<PORTC6);  // set PC6 to low
142
    HEF4017R_ON;
147
 
-
 
148
        // Timer/Counter 2 Control Register A
143
        // Timer/Counter 2 Control Register A
149
 
144
 
150
        // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1)
145
        // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1)
151
    // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
146
    // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
152
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
147
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
153
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
148
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
154
    TCCR2A |= (1<<WGM21)|(1<<WGM20);
149
    TCCR2A |= (1<<WGM21)|(1<<WGM20);
155
 
150
 
156
    // Timer/Counter 2 Control Register B
151
    // Timer/Counter 2 Control Register B
157
 
152
 
158
        // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz
153
        // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz
159
        // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us
154
        // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us
160
        // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms
155
        // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms
161
 
156
 
162
    // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1)
157
    // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1)
163
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22));
158
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22));
164
    TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22);
159
    TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22);
165
 
160
 
166
        // Initialize the Timer/Counter 2 Register
161
        // Initialize the Timer/Counter 2 Register
167
    TCNT2 = 0;
162
    TCNT2 = 0;
168
 
163
 
169
        // Initialize the Output Compare Register A used for PWM generation on port PD7.
164
        // Initialize the Output Compare Register A used for PWM generation on port PD7.
170
        OCR2A = 255;
165
        OCR2A = 255;
171
        TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0
166
        TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0
172
 
167
 
173
        // Timer/Counter 2 Interrupt Mask Register
168
        // Timer/Counter 2 Interrupt Mask Register
174
        // Enable timer output compare match A Interrupt only
169
        // Enable timer output compare match A Interrupt only
175
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
170
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
176
        TIMSK2 |= (1<<OCIE2A);
171
        TIMSK2 |= (1<<OCIE2A);
177
 
172
 
178
    SREG = sreg;
173
    SREG = sreg;
179
}
174
}
180
 
175
 
181
//----------------------------
176
//----------------------------
182
void Timer_Init(void)
177
void Timer_Init(void)
183
{
178
{
184
    tim_main = SetDelay(10);
179
    tim_main = SetDelay(10);
185
    TCCR0B = CK8;
180
    TCCR0B = CK8;
186
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
181
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
187
    OCR0A =  0;
182
    OCR0A =  0;
188
    OCR0B = 120;
183
    OCR0B = 120;
189
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
184
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
190
    //OCR1  = 0x00;
185
    //OCR1  = 0x00;
191
 
186
 
192
    TIMSK0 |= _BV(TOIE0);
187
    TIMSK0 |= _BV(TOIE0);
193
}
188
}
194
 
189
 
195
 
190
 
196
/*****************************************************/
191
/*****************************************************/
197
/*              Control Servo Position               */
192
/*              Control Servo Position               */
198
/*****************************************************/
193
/*****************************************************/
199
 
194
 
200
ISR(TIMER2_COMPA_vect)
195
ISR(TIMER2_COMPA_vect)
201
{
196
{
202
 
197
 
203
        // frame len 22.5 ms = 14063 * 1.6 us
198
        // frame len 22.5 ms = 14063 * 1.6 us
204
        // stop pulse: 0.3 ms = 188 * 1.6 us
199
        // stop pulse: 0.3 ms = 188 * 1.6 us
205
        // min servo pulse: 0.6 ms =  375 * 1.6 us
200
        // min servo pulse: 0.6 ms =  375 * 1.6 us
206
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
201
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
207
        // resolution: 1500 - 375 = 1125 steps
202
        // resolution: 1500 - 375 = 1125 steps
208
 
203
 
209
        #define IRS_RUNTIME 127
204
        #define IRS_RUNTIME 127
210
        #define PPM_STOPPULSE 188
205
        #define PPM_STOPPULSE 188
211
//      #define PPM_FRAMELEN (14063
206
//      #define PPM_FRAMELEN (14063
212
#define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh)
207
    #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh)
213
        #define MINSERVOPULSE 375
208
        #define MINSERVOPULSE 375
214
        #define MAXSERVOPULSE 1500
209
        #define MAXSERVOPULSE 1500
215
        #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
210
        #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
216
 
211
 
217
        static uint8_t  PulseOutput = 0;
212
        static uint8_t  PulseOutput = 0;
218
        static uint16_t RemainingPulse = 0;
213
        static uint16_t RemainingPulse = 0;
219
        static uint16_t ServoFrameTime = 0;
214
        static uint16_t ServoFrameTime = 0;
220
        static uint8_t  ServoIndex = 0;
215
        static uint8_t  ServoIndex = 0;
221
 
216
 
222
        #define MULTIPLYER 4
217
        #define MULTIPLYER 4
223
        static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center positon
218
        static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center positon
224
 
-
 
225
 
219
 
226
        if(PlatinenVersion < 20)
220
        if(PlatinenVersion < 20)
227
        {
221
        {
228
                //---------------------------
222
                //---------------------------
229
                // Nick servo state machine
223
                // Nick servo state machine
230
                //---------------------------
224
                //---------------------------
231
                if(!PulseOutput) // pulse output complete
225
                if(!PulseOutput) // pulse output complete
232
                {
226
                {
233
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
227
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
234
                        {
228
                        {
235
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
229
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
236
                                RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
230
                                RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
237
 
231
 
238
                                ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset
232
                                ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset
239
                                ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
233
                                ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
240
                                if(EE_Parameter.ServoNickCompInvert & 0x01)
234
                                if(EE_Parameter.ServoNickCompInvert & 0x01)
241
                                {       // inverting movement of servo
235
                                {       // inverting movement of servo
242
                                        ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
236
                                        ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
243
                                }
237
                                }
244
                                else
238
                                else
245
                                {       // non inverting movement of servo
239
                                {       // non inverting movement of servo
246
                                        ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
240
                                        ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
247
                                }
241
                                }
248
                                // limit servo value to its parameter range definition
242
                                // limit servo value to its parameter range definition
249
                                if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
243
                                if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
250
                                {
244
                                {
251
                                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
245
                                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
252
                                }
246
                                }
253
                                else
247
                                else
254
                                if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
248
                                if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
255
                                {
249
                                {
256
                                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
250
                                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
257
                                }
251
                                }
258
 
252
 
259
                                RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
253
                                RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
260
 
254
 
261
                                ServoNickValue /= MULTIPLYER;
255
                                ServoNickValue /= MULTIPLYER;
262
                                DebugOut.Analog[20] = ServoNickValue;
256
                                DebugOut.Analog[20] = ServoNickValue;
263
 
257
 
264
                                // range servo pulse width
258
                                // range servo pulse width
265
                                if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
259
                                if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
266
                                else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
260
                                else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
267
                                // accumulate time for correct update rate
261
                                // accumulate time for correct update rate
268
                                ServoFrameTime = RemainingPulse;
262
                                ServoFrameTime = RemainingPulse;
269
                        }
263
                        }
270
                        else // we had a high pulse
264
                        else // we had a high pulse
271
                        {
265
                        {
272
                                TCCR2A |= (1<<COM2A0); // make a low pulse
266
                                TCCR2A |= (1<<COM2A0); // make a low pulse
273
                                RemainingPulse = PPM_FRAMELEN - ServoFrameTime;
267
                                RemainingPulse = PPM_FRAMELEN - ServoFrameTime;
274
                        }
268
                        }
275
                        // set pulse output active
269
                        // set pulse output active
276
                        PulseOutput = 1;
270
                        PulseOutput = 1;
277
                }
271
                }
278
        } // EOF Nick servo state machine
272
        } // EOF Nick servo state machine
279
        else
273
        else
280
        {
274
        {
281
                //-----------------------------------------------------
275
                //-----------------------------------------------------
282
                // PPM state machine, onboard demultiplexed by HEF4017
276
                // PPM state machine, onboard demultiplexed by HEF4017
283
                //-----------------------------------------------------
277
                //-----------------------------------------------------
284
                if(!PulseOutput) // pulse output complete
278
                if(!PulseOutput) // pulse output complete
285
                {
279
                {
286
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
280
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
287
                        {
281
                        {
288
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
282
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
289
 
283
 
290
                                if(ServoIndex == 0) // if we are at the sync gap
284
                                if(ServoIndex == 0) // if we are at the sync gap
291
                                {
285
                                {
292
                                        RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time
286
                                        RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time
293
                                        ServoFrameTime = 0; // reset servo frame time
287
                                        ServoFrameTime = 0; // reset servo frame time
294
                                        HEF4017R_ON; // enable HEF4017 reset
288
                                        HEF4017R_ON; // enable HEF4017 reset
295
                                }
289
                                }
296
                                else // servo channels
290
                                else // servo channels
297
                                {
291
                                {
298
                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
292
                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
299
                                        switch(ServoIndex) // map servo channels
293
                                        switch(ServoIndex) // map servo channels
300
                                        {
294
                                        {
301
                                                case 1: // Nick Compensation Servo
295
                                                case 1: // Nick Compensation Servo
302
                                                        ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset
296
                                                        ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset
303
                                                        ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
297
                                                        ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
304
                                                        if(EE_Parameter.ServoNickCompInvert & 0x01)
298
                                                        if(EE_Parameter.ServoNickCompInvert & 0x01)
305
                                                        {       // inverting movement of servo
299
                                                        {       // inverting movement of servo
306
                                                                ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
300
                                                                ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
307
                                                        }
301
                                                        }
308
                                                        else
302
                                                        else
309
                                                        {       // non inverting movement of servo
303
                                                        {       // non inverting movement of servo
310
                                                                ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
304
                                                                ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
311
                                                        }
305
                                                        }
312
                                                        // limit servo value to its parameter range definition
306
                                                        // limit servo value to its parameter range definition
313
                                                        if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
307
                                                        if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
314
                                                        {
308
                                                        {
315
                                                                ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
309
                                                                ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
316
                                                        }
310
                                                        }
317
                                                        else
311
                                                        else
318
                                                        if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
312
                                                        if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
319
                                                        {
313
                                                        {
320
                                                                ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
314
                                                                ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
321
                                                        }
315
                                                        }
322
                                                        RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
316
                                                        RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
323
                                                        ServoNickValue /= MULTIPLYER;
317
                                                        ServoNickValue /= MULTIPLYER;
324
                                                        DebugOut.Analog[20] = ServoNickValue;
318
                                                        DebugOut.Analog[20] = ServoNickValue;
325
                                                        break;
319
                                                        break;
326
 
320
 
327
                                                default: // other servo channels
321
                                                default: // other servo channels
328
                                                        RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
322
                                                        RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
329
                                                        break;
323
                                                        break;
330
                                        }
324
                                        }
331
                                        // range servo pulse width
325
                                        // range servo pulse width
332
                                        if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
326
                                        if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
333
                                        else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
327
                                        else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
334
                                        // substract stop pulse width
328
                                        // substract stop pulse width
335
                                        RemainingPulse -= PPM_STOPPULSE;
329
                                        RemainingPulse -= PPM_STOPPULSE;
336
                                        // accumulate time for correct sync gap
330
                                        // accumulate time for correct sync gap
337
                                        ServoFrameTime += RemainingPulse;
331
                                        ServoFrameTime += RemainingPulse;
338
                                }
332
                                }
339
                        }
333
                        }
340
                        else // we had a high pulse
334
                        else // we had a high pulse
341
                        {
335
                        {
342
                                TCCR2A |= (1<<COM2A0); // make a low pulse
336
                                TCCR2A |= (1<<COM2A0); // make a low pulse
343
                                // set pulsewidth to stop pulse width
337
                                // set pulsewidth to stop pulse width
344
                                RemainingPulse = PPM_STOPPULSE;
338
                                RemainingPulse = PPM_STOPPULSE;
345
                                // accumulate time for correct sync gap
339
                                // accumulate time for correct sync gap
346
                                ServoFrameTime += RemainingPulse;
340
                                ServoFrameTime += RemainingPulse;
347
                                HEF4017R_OFF; // disable HEF4017 reset
341
                                if(ServoActive && SenderOkay > 180) HEF4017R_OFF; // disable HEF4017 reset
348
                                ServoIndex++; // change to next servo channel
342
                                ServoIndex++; // change to next servo channel
349
                                if(ServoIndex > EE_Parameter.ServoNickRefresh) ServoIndex = 0; // reset to the sync gap
343
                                if(ServoIndex > EE_Parameter.ServoNickRefresh) ServoIndex = 0; // reset to the sync gap
350
                        }
344
                        }
351
                        // set pulse output active
345
                        // set pulse output active
352
                        PulseOutput = 1;
346
                        PulseOutput = 1;
353
                }
347
                }
354
        } // EOF PPM state machine
348
        } // EOF PPM state machine
355
 
349
 
356
        // General pulse output generator
350
        // General pulse output generator
357
        if(RemainingPulse > (255 + IRS_RUNTIME))
351
        if(RemainingPulse > (255 + IRS_RUNTIME))
358
        {
352
        {
359
                OCR2A = 255;
353
                OCR2A = 255;
360
                RemainingPulse -= 255;
354
                RemainingPulse -= 255;
361
        }
355
        }
362
        else
356
        else
363
        {
357
        {
364
                if(RemainingPulse > 255) // this is the 2nd last part
358
                if(RemainingPulse > 255) // this is the 2nd last part
365
                {
359
                {
366
                        if((RemainingPulse - 255) < IRS_RUNTIME)
360
                        if((RemainingPulse - 255) < IRS_RUNTIME)
367
                        {
361
                        {
368
                                OCR2A = 255 - IRS_RUNTIME;
362
                                OCR2A = 255 - IRS_RUNTIME;
369
                                RemainingPulse -= 255 - IRS_RUNTIME;
363
                                RemainingPulse -= 255 - IRS_RUNTIME;
370
 
364
 
371
                        }
365
                        }
372
                        else // last part > ISR_RUNTIME
366
                        else // last part > ISR_RUNTIME
373
                        {
367
                        {
374
                                OCR2A = 255;
368
                                OCR2A = 255;
375
                                RemainingPulse -= 255;
369
                                RemainingPulse -= 255;
376
                        }
370
                        }
377
                }
371
                }
378
                else // this is the last part
372
                else // this is the last part
379
                {
373
                {
380
                        OCR2A = RemainingPulse;
374
                        OCR2A = RemainingPulse;
381
                        RemainingPulse = 0;
375
                        RemainingPulse = 0;
382
                        PulseOutput = 0; // trigger to stop pulse
376
                        PulseOutput = 0; // trigger to stop pulse
383
                }
377
                }
384
        } // EOF general pulse output generator
378
        } // EOF general pulse output generator
385
}
379
}
386
 
380