Subversion Repositories FlightCtrl

Rev

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

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