Subversion Repositories FlightCtrl

Rev

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

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