Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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