Subversion Repositories FlightCtrl

Rev

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

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