Subversion Repositories FlightCtrl

Rev

Rev 1424 | Rev 1481 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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