Subversion Repositories FlightCtrl

Rev

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

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