Subversion Repositories FlightCtrl

Rev

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

Rev 1229 Rev 1242
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
 
-
 
16
// Arthur P: Added two variables for control of the shutter servo cycle.
-
 
17
 
-
 
18
volatile static unsigned int CameraShutterCycleCounter = 0;
-
 
19
volatile static unsigned int CameraShutterCycle = 0;
15
 
20
 
16
 
21
 
17
enum {
22
enum {
18
  STOP             = 0,
23
  STOP             = 0,
19
  CK               = 1,
24
  CK               = 1,
20
  CK8              = 2,
25
  CK8              = 2,
21
  CK64             = 3,
26
  CK64             = 3,
22
  CK256            = 4,
27
  CK256            = 4,
23
  CK1024           = 5,
28
  CK1024           = 5,
24
  T0_FALLING_EDGE  = 6,
29
  T0_FALLING_EDGE  = 6,
25
  T0_RISING_EDGE   = 7
30
  T0_RISING_EDGE   = 7
26
};
31
};
27
 
32
 
28
 
33
 
29
SIGNAL (SIG_OVERFLOW0)    // 8kHz
34
SIGNAL (SIG_OVERFLOW0)    // 8kHz
30
{
35
{
31
    static unsigned char cnt_1ms = 1,cnt = 0;
36
    static unsigned char cnt_1ms = 1,cnt = 0;
32
    unsigned char pieper_ein = 0;
37
    unsigned char pieper_ein = 0;
33
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
38
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
34
   if(SendSPI) SendSPI--;
39
   if(SendSPI) SendSPI--;
35
   if(!cnt--)
40
   if(!cnt--)
36
    {
41
    {
37
     cnt = 9;
42
     cnt = 9;
38
     cnt_1ms++;
43
     cnt_1ms++;
39
     cnt_1ms %= 2;
44
     cnt_1ms %= 2;
40
     if(!cnt_1ms) UpdateMotor = 1;
45
     if(!cnt_1ms) UpdateMotor = 1;
41
     CountMilliseconds++;
46
     CountMilliseconds++;
42
    }
47
    }
43
 
48
 
44
     if(beeptime > 1)
49
     if(beeptime > 1)
45
        {
50
        {
46
        beeptime--;
51
        beeptime--;
47
        if(beeptime & BeepMuster)
52
        if(beeptime & BeepMuster)
48
         {
53
         {
49
          pieper_ein = 1;
54
          pieper_ein = 1;
50
         }
55
         }
51
         else pieper_ein = 0;
56
         else pieper_ein = 0;
52
        }
57
        }
53
     else
58
     else
54
      {
59
      {
55
       pieper_ein = 0;
60
       pieper_ein = 0;
56
       BeepMuster = 0xffff;
61
       BeepMuster = 0xffff;
57
      }
62
      }
58
 
63
 
59
 
64
 
60
     if(pieper_ein)
65
     if(pieper_ein)
61
        {
66
        {
62
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
67
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
63
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
68
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
64
        }
69
        }
65
     else
70
     else
66
        {
71
        {
67
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
72
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
68
         else                      PORTC &= ~(1<<7);
73
         else                      PORTC &= ~(1<<7);
69
        }
74
        }
70
 
75
 
71
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
76
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
72
 {
77
 {
73
  if(PINC & 0x10)
78
  if(PINC & 0x10)
74
   {
79
   {
75
    cntKompass++;
80
    cntKompass++;
76
   }
81
   }
77
  else
82
  else
78
   {
83
   {
79
    if((cntKompass) && (cntKompass < 362))
84
    if((cntKompass) && (cntKompass < 362))
80
    {
85
    {
81
     cntKompass += cntKompass / 41;
86
     cntKompass += cntKompass / 41;
82
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
87
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
83
    }
88
    }
84
//     if(cntKompass < 10) cntKompass = 10;
89
//     if(cntKompass < 10) cntKompass = 10;
85
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
90
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
86
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
91
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
87
    cntKompass = 0;
92
    cntKompass = 0;
88
   }
93
   }
89
 }
94
 }
90
}
95
}
91
 
96
 
92
 
97
 
93
// -----------------------------------------------------------------------
98
// -----------------------------------------------------------------------
94
 
99
 
95
unsigned int SetDelay (unsigned int t)
100
unsigned int SetDelay (unsigned int t)
96
{
101
{
97
//  TIMSK0 &= ~_BV(TOIE0);
102
//  TIMSK0 &= ~_BV(TOIE0);
98
  return(CountMilliseconds + t + 1);
103
  return(CountMilliseconds + t + 1);
99
//  TIMSK0 |= _BV(TOIE0);
104
//  TIMSK0 |= _BV(TOIE0);
100
}
105
}
101
 
106
 
102
// -----------------------------------------------------------------------
107
// -----------------------------------------------------------------------
103
char CheckDelay(unsigned int t)
108
char CheckDelay(unsigned int t)
104
{
109
{
105
//  TIMSK0 &= ~_BV(TOIE0);
110
//  TIMSK0 &= ~_BV(TOIE0);
106
  return(((t - CountMilliseconds) & 0x8000) >> 9);
111
  return(((t - CountMilliseconds) & 0x8000) >> 9);
107
//  TIMSK0 |= _BV(TOIE0);
112
//  TIMSK0 |= _BV(TOIE0);
108
}
113
}
109
 
114
 
110
// -----------------------------------------------------------------------
115
// -----------------------------------------------------------------------
111
void Delay_ms(unsigned int w)
116
void Delay_ms(unsigned int w)
112
{
117
{
113
 unsigned int akt;
118
 unsigned int akt;
114
 akt = SetDelay(w);
119
 akt = SetDelay(w);
115
 while (!CheckDelay(akt));
120
 while (!CheckDelay(akt));
116
}
121
}
117
 
122
 
118
void Delay_ms_Mess(unsigned int w)
123
void Delay_ms_Mess(unsigned int w)
119
{
124
{
120
 unsigned int akt;
125
 unsigned int akt;
121
 akt = SetDelay(w);
126
 akt = SetDelay(w);
122
 while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;}
127
 while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;}
123
}
128
}
124
 
129
 
125
/*****************************************************/
130
/*****************************************************/
126
/*              Initialize Timer 2                   */
131
/*              Initialize Timer 2                   */
127
/*****************************************************/
132
/*****************************************************/
128
// The timer 2 is used to generate the PWM at PD7 (J7)
133
// The timer 2 is used to generate the PWM at PD7 (J7)
129
// to control a camera servo for nick compensation.
134
// to control a camera servo for nick compensation.
130
void TIMER2_Init(void)
135
void TIMER2_Init(void)
131
{
136
{
-
 
137
       
132
        uint8_t sreg = SREG;
138
        uint8_t sreg = SREG;
133
 
139
 
134
        // disable all interrupts before reconfiguration
140
        // disable all interrupts before reconfiguration
135
        cli();
141
        cli();
136
 
142
 
137
        // set PD7 as output of the PWM for nick servo
143
        // set PD7 as output of the PWM for nick servo
138
        DDRD  |= (1<<DDD7);
144
        DDRD  |= (1<<DDD7);
139
        PORTD &= ~(1<<PORTD7);  // set PD7 to low
145
        PORTD &= ~(1<<PORTD7);  // set PD7 to low
140
 
146
 
141
        DDRC  |= (1<<DDC6);     // set PC6 as output (Reset for HEF4017)
147
        DDRC  |= (1<<DDC6);     // set PC6 as output (Reset for HEF4017)
142
    HEF4017R_ON;
148
    HEF4017R_ON;
143
        // Timer/Counter 2 Control Register A
149
        // Timer/Counter 2 Control Register A
144
 
150
 
145
        // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1)
151
        // 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)
152
    // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
147
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
153
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
148
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
154
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
149
    TCCR2A |= (1<<WGM21)|(1<<WGM20);
155
    TCCR2A |= (1<<WGM21)|(1<<WGM20);
150
 
156
 
151
    // Timer/Counter 2 Control Register B
157
    // Timer/Counter 2 Control Register B
152
 
158
 
153
        // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz
159
        // 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
160
        // 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
161
        // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms
156
 
162
 
157
    // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1)
163
    // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1)
158
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22));
164
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22));
159
    TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22);
165
    TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22);
160
 
166
 
161
        // Initialize the Timer/Counter 2 Register
167
        // Initialize the Timer/Counter 2 Register
162
    TCNT2 = 0;
168
    TCNT2 = 0;
163
 
169
 
164
        // Initialize the Output Compare Register A used for PWM generation on port PD7.
170
        // Initialize the Output Compare Register A used for PWM generation on port PD7.
165
        OCR2A = 255;
171
        OCR2A = 255;
166
        TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0
172
        TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0
167
 
173
 
168
        // Timer/Counter 2 Interrupt Mask Register
174
        // Timer/Counter 2 Interrupt Mask Register
169
        // Enable timer output compare match A Interrupt only
175
        // Enable timer output compare match A Interrupt only
170
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
176
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
171
        TIMSK2 |= (1<<OCIE2A);
177
        TIMSK2 |= (1<<OCIE2A);
172
 
178
 
173
    SREG = sreg;
179
    SREG = sreg;
174
}
180
}
175
 
181
 
176
//----------------------------
182
//----------------------------
177
void Timer_Init(void)
183
void Timer_Init(void)
178
{
184
{
-
 
185
 
179
    tim_main = SetDelay(10);
186
    tim_main = SetDelay(10);
180
    TCCR0B = CK8;
187
    TCCR0B = CK8;
181
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
188
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
182
    OCR0A =  0;
189
    OCR0A =  0;
183
    OCR0B = 120;
190
    OCR0B = 120;
184
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
191
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
185
    //OCR1  = 0x00;
192
    //OCR1  = 0x00;
186
 
193
 
187
    TIMSK0 |= _BV(TOIE0);
194
    TIMSK0 |= _BV(TOIE0);
188
}
195
}
189
 
196
 
190
 
197
 
191
/*****************************************************/
198
/*****************************************************/
192
/*              Control Servo Position               */
199
/*              Control Servo Position               */
193
/*****************************************************/
200
/*****************************************************/
194
 
201
 
195
ISR(TIMER2_COMPA_vect)
202
ISR(TIMER2_COMPA_vect)
196
{
203
{
197
 
204
 
198
        // frame len 22.5 ms = 14063 * 1.6 us
205
        // frame len 22.5 ms = 14063 * 1.6 us
199
        // stop pulse: 0.3 ms = 188 * 1.6 us
206
        // stop pulse: 0.3 ms = 188 * 1.6 us
200
        // min servo pulse: 0.6 ms =  375 * 1.6 us
207
        // min servo pulse: 0.6 ms =  375 * 1.6 us
201
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
208
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
202
        // resolution: 1500 - 375 = 1125 steps
209
        // resolution: 1500 - 375 = 1125 steps
203
 
210
 
204
        #define IRS_RUNTIME 127
211
        #define IRS_RUNTIME 127
205
        #define PPM_STOPPULSE 188
212
        #define PPM_STOPPULSE 188
206
//      #define PPM_FRAMELEN (14063
213
//      #define PPM_FRAMELEN (14063
207
    #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh)
214
    #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh)
208
        #define MINSERVOPULSE 375
215
        #define MINSERVOPULSE 375
209
        #define MAXSERVOPULSE 1500
216
        #define MAXSERVOPULSE 1500
210
        #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
217
        #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)
211
 
218
 
212
        static uint8_t  PulseOutput = 0;
219
        static uint8_t  PulseOutput = 0;
213
        static uint16_t RemainingPulse = 0;
220
        static uint16_t RemainingPulse = 0;
214
        static uint16_t ServoFrameTime = 0;
221
        static uint16_t ServoFrameTime = 0;
215
        static uint8_t  ServoIndex = 0;
222
        static uint8_t  ServoIndex = 0;
216
 
223
 
217
        #define MULTIPLYER 4
224
        #define MULTIPLYER 4
218
        static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center positon
225
        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
226
        static int16_t ServoRollOffset = (255 / 2) * MULTIPLYER; // initial value near center positon
-
 
227
 
-
 
228
    // Arthur P: Added initialization of the CameraShutterCycle value here as this routine is only 
-
 
229
        // called once. This retains all code changes in timer0.c. If parameter 6 > 0 then the user
-
 
230
        // has set a value for the cycle. CameraShuytterCycle == 5x Para6 to get approx 0.1sec increments.
-
 
231
       
-
 
232
        // CameraShutterCycle = 5 *  Parameter_UserParam6;
-
 
233
    CameraShutterCycle = Parameter_UserParam6;
-
 
234
 
-
 
235
        // Arthur P: Modified the code to scheck the value of parameter 8. If 128 or higher then a HEF4017 is
220
 
236
        // expected and will be used. Else J7 and J9 are seen as separate normal outputs.
-
 
237
        // if((PlatinenVersion < 20)
221
        // if((PlatinenVersion < 20)
238
 
222
        if((PlatinenVersion < 20) && (Parameter_UserParam8 < 128 ))
239
        if((PlatinenVersion < 20) && (Parameter_UserParam8 < 128 ))
223
        {
240
        {
224
                //---------------------------
241
                //---------------------------
225
                // Nick servo state machine
242
                // Nick servo state machine
226
                //---------------------------
243
                //---------------------------
227
                if(!PulseOutput) // pulse output complete
244
                if(!PulseOutput) // pulse output complete
228
                {
245
                {
229
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
246
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
230
                        {
247
                        {
231
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
248
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
232
                                RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
249
                                RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
233
 
250
 
234
                                ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset
251
                                ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset
235
                                ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
252
                                ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
236
                                if(EE_Parameter.ServoNickCompInvert & 0x01)
253
                                if(EE_Parameter.ServoNickCompInvert & 0x01)
237
                                {       // inverting movement of servo
254
                                {       // inverting movement of servo
238
                                        ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
255
                                        ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
239
                                }
256
                                }
240
                                else
257
                                else
241
                                {       // non inverting movement of servo
258
                                {       // non inverting movement of servo
242
                                        ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
259
                                        ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
243
                                }
260
                                }
244
                                // limit servo value to its parameter range definition
261
                                // limit servo value to its parameter range definition
245
                                if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
262
                                if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
246
                                {
263
                                {
247
                                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
264
                                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
248
                                }
265
                                }
249
                                else
266
                                else
250
                                if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
267
                                if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
251
                                {
268
                                {
252
                                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
269
                                        ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
253
                                }
270
                                }
254
 
271
 
255
                                RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
272
                                RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
256
 
273
 
257
                                ServoNickValue /= MULTIPLYER;
274
                                ServoNickValue /= MULTIPLYER;
258
                                DebugOut.Analog[20] = ServoNickValue;
275
                                DebugOut.Analog[20] = ServoNickValue;
259
 
276
 
260
                                // range servo pulse width
277
                                // range servo pulse width
261
                                if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
278
                                if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
262
                                else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
279
                                else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
263
                                // accumulate time for correct update rate
280
                                // accumulate time for correct update rate
264
                                ServoFrameTime = RemainingPulse;
281
                                ServoFrameTime = RemainingPulse;
265
                        }
282
                        }
266
                        else // we had a high pulse
283
                        else // we had a high pulse
267
                        {
284
                        {
268
                                TCCR2A |= (1<<COM2A0); // make a low pulse
285
                                TCCR2A |= (1<<COM2A0); // make a low pulse
269
                                RemainingPulse = PPM_FRAMELEN - ServoFrameTime;
286
                                RemainingPulse = PPM_FRAMELEN - ServoFrameTime;
270
                        }
287
                        }
271
                        // set pulse output active
288
                        // set pulse output active
272
                        PulseOutput = 1;
289
                        PulseOutput = 1;
273
                }
290
                }
274
        } // EOF Nick servo state machine
291
        } // EOF Nick servo state machine
275
        else
292
        else
276
        {
293
        {
277
                //-----------------------------------------------------
294
                //-----------------------------------------------------
278
                // PPM state machine, onboard demultiplexed by HEF4017
295
                // PPM state machine, onboard demultiplexed by HEF4017
279
                //-----------------------------------------------------
296
                //-----------------------------------------------------
280
                if(!PulseOutput) // pulse output complete
297
                if(!PulseOutput) // pulse output complete
281
                {
298
                {
282
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
299
                        if(TCCR2A & (1<<COM2A0)) // we had a low pulse
283
                        {
300
                        {
284
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
301
                                TCCR2A &= ~(1<<COM2A0);// make a high pulse
285
 
302
 
286
                                if(ServoIndex == 0) // if we are at the sync gap
303
                                if(ServoIndex == 0) // if we are at the sync gap
287
                                {
304
                                {
288
                                        RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time
305
                                        RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time
289
                                        ServoFrameTime = 0; // reset servo frame time
306
                                        ServoFrameTime = 0; // reset servo frame time
290
                                        HEF4017R_ON; // enable HEF4017 reset
307
                                        HEF4017R_ON; // enable HEF4017 reset
291
                                }
308
                                }
292
                                else // servo channels
309
                                else // servo channels
293
                                {
310
                                {
294
                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
311
                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms
295
                                        switch(ServoIndex) // map servo channels
312
                                        switch(ServoIndex) // map servo channels
296
                                        {
313
                                        {
297
                                                case 1: // Nick Compensation Servo
314
                                                case 1: // Nick Compensation Servo
298
                                                        ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset
315
                                                        ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset
299
                                                        ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
316
                                                        ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765)
300
                                                        if(EE_Parameter.ServoNickCompInvert & 0x01)
317
                                                        if(EE_Parameter.ServoNickCompInvert & 0x01)
301
                                                        {       // inverting movement of servo
318
                                                        {       // inverting movement of servo
302
                                                                ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
319
                                                                ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
303
                                                        }
320
                                                        }
304
                                                        else
321
                                                        else
305
                                                        {       // non inverting movement of servo
322
                                                        {       // non inverting movement of servo
306
                                                                ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
323
                                                                ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
307
                                                        }
324
                                                        }
308
                                                        // limit servo value to its parameter range definition
325
                                                        // limit servo value to its parameter range definition
309
                                                        if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
326
                                                        if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
310
                                                        {
327
                                                        {
311
                                                                ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
328
                                                                ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
312
                                                        }
329
                                                        }
313
                                                        else
330
                                                        else
314
                                                        if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
331
                                                        if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
315
                                                        {
332
                                                        {
316
                                                                ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
333
                                                                ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
317
                                                        }
334
                                                        }
318
                                                        RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
335
                                                        RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
319
                                                        ServoNickValue /= MULTIPLYER;
336
                                                        ServoNickValue /= MULTIPLYER;
320
                                                        DebugOut.Analog[20] = ServoNickValue;
337
                                                        DebugOut.Analog[20] = ServoNickValue;
321
                                                        break;
338
                                                        break;
322
                                         case 2: // Roll Compensation Servo
339
                                         case 2: // Roll Compensation Servo
-
 
340
                                                        // Arthur P: Modified the code here to allow the user to either continue with the default
-
 
341
                                                        // offset value of 80, or set a different offset using parameter 7. I.e. if 
-
 
342
                                                        // parameter 7 == 0 then the default is used, but if a value > 0 is entered then
-
 
343
                                                        // that value is used. 
-
 
344
                                                        if(Parameter_UserParam7==0)
-
 
345
                                                        {
323
                                                        ServoRollOffset = (ServoRollOffset * 3 + (int16_t) 80 * MULTIPLYER) / 4; // lowpass offset
346
                                                                ServoRollOffset = (ServoRollOffset * 3 + (int16_t) 80 * MULTIPLYER) / 4; // lowpass offset
-
 
347
                                                        }
-
 
348
                                                        else
-
 
349
                                                        {
-
 
350
                                                                ServoRollOffset = (ServoRollOffset * 3 + (int16_t) Parameter_UserParam7 * MULTIPLYER) / 4;
-
 
351
                                                        }
324
                                                        ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765)
352
                                                        ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765)
325
                                                        //if(EE_Parameter.ServoRollCompInvert & 0x01)
353
                                                        //if(EE_Parameter.ServoRollCompInvert & 0x01)
326
                                                        if(Parameter_UserParam8 & 0x40)
354
                                                        if(Parameter_UserParam8 & 0x40)
327
                                                        {       // inverting movement of servo if 64 has been added to User Parameter8
355
                                                        {       // Arthur P: Inverting movement of servo if 64 has been added to User Parameter8
328
                                                                                                ServoRollValue += (int16_t)( ( (int32_t) 50 * MULTIPLYER * (IntegralRoll / 128L ) ) / (256L) );
356
                                                                                                ServoRollValue += (int16_t)( ( (int32_t) 50 * MULTIPLYER * (IntegralRoll / 128L ) ) / (256L) );
329
                                                        }
357
                                                        }
330
/**/                                                    else
358
/**/                                                    else
331
                                                        {       // non inverting movement of servo
359
                                                        {       // non inverting movement of servo
332
                                                                ServoRollValue -= (int16_t)( ( (int32_t) 40 * MULTIPLYER * (IntegralRoll / 128L ) ) / (256L) );
360
                                                                ServoRollValue -= (int16_t)( ( (int32_t) 40 * MULTIPLYER * (IntegralRoll / 128L ) ) / (256L) );
333
                                                        }
361
                                                        }
334
/**/                                                    // limit servo value to its parameter range definition
362
/**/                                                    // limit servo value to its parameter range definition
335
                                                        if(ServoRollValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
363
                                                        if(ServoRollValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) )
336
                                                        {
364
                                                        {
337
                                                                ServoRollValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
365
                                                                ServoRollValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER;
338
                                                        }
366
                                                        }
339
                                                        else
367
                                                        else
340
                                                        if(ServoRollValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
368
                                                        if(ServoRollValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) )
341
                                                        {
369
                                                        {
342
                                                                ServoRollValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
370
                                                                ServoRollValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER;
343
                                                        }
371
                                                        }
344
                                                        RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
372
                                                        RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
345
                                                        ServoRollValue /= MULTIPLYER;
373
                                                        ServoRollValue /= MULTIPLYER;
346
                                                        //DebugOut.Analog[20] = ServoRollValue;
374
                                                        //DebugOut.Analog[20] = ServoRollValue;
347
 
375
 
348
/*                                                      ServoRollOffset = (ServoRollOffset * 3 + (int16_t)Parameter_ServoRollControl * MULTIPLYER) / 4; // lowpass offset
376
/*                                                      ServoRollOffset = (ServoRollOffset * 3 + (int16_t)Parameter_ServoRollControl * MULTIPLYER) / 4; // lowpass offset
349
                                                        ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765)
377
                                                        ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765)
350
                                                        if(EE_Parameter.ServoRollCompInvert & 0x01)
378
                                                        if(EE_Parameter.ServoRollCompInvert & 0x01)
351
                                                        {       // inverting movement of servo
379
                                                        {       // inverting movement of servo
352
                                                                ServoRollValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
380
                                                                ServoRollValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
353
                                                        }
381
                                                        }
354
                                                        else
382
                                                        else
355
                                                        {       // non inverting movement of servo
383
                                                        {       // non inverting movement of servo
356
                                                                ServoRollValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
384
                                                                ServoRollValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) );
357
                                                        }
385
                                                        }
358
                                                        // limit servo value to its parameter range definition
386
                                                        // limit servo value to its parameter range definition
359
                                                        if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER) )
387
                                                        if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER) )
360
                                                        {
388
                                                        {
361
                                                                ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER;
389
                                                                ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER;
362
                                                        }
390
                                                        }
363
                                                        else
391
                                                        else
364
                                                        if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER) )
392
                                                        if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER) )
365
                                                        {
393
                                                        {
366
                                                                ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER;
394
                                                                ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER;
367
                                                        }
395
                                                        }
368
                                                        RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
396
                                                        RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position
369
                                                        ServoRollValue /= MULTIPLYER;
397
                                                        ServoRollValue /= MULTIPLYER;
370
                                                        //DebugOut.Analog[20] = ServoRollValue;
398
                                                        //DebugOut.Analog[20] = ServoRollValue;
371
*/                                                      break;
399
*/                                                      break;
-
 
400
                        case 3: // Arthur P: Shutter Servo including interval control over parameter 5 and 6. 
-
 
401
 
-
 
402
                                                       
-
 
403
                                                        if(PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] < -32)
-
 
404
                                                        {
-
 
405
                                                        // Set servo to null position, turning camera off.
-
 
406
                                                                RemainingPulse  = MINSERVOPULSE;
-
 
407
                                                        }
-
 
408
                                                        else
-
 
409
                                                        {
-
 
410
                                                                if(PPM_in[EE_Parameter.Kanalbelegung[K_POTI3]] > 32)
-
 
411
                                                                // Middle position on a 3 position switch which runs from -127 to +127
-
 
412
                                                                {
-
 
413
       
-
 
414
                                                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2;
-
 
415
                                                                }
-
 
416
                                                                else
-
 
417
                                                                {
-
 
418
                                                                        // Cycle shutter servo between on and off depending upon CameraShutterCycleCounter
-
 
419
                                                                        // If CameraShutterCylce < 50 then default to continuous shoot.
-
 
420
                                                                        if(CameraShutterCycle < 50 )
-
 
421
                                                                        {
-
 
422
                                                                                RemainingPulse  = MINSERVOPULSE + SERVORANGE/2;
-
 
423
                                                                        }
-
 
424
                                                                        else
-
 
425
                                                                        {
-
 
426
                                                                                if(CameraShutterCycleCounter == CameraShutterCycle)
-
 
427
                                                                                {
-
 
428
                                                                                        // Shutter on
-
 
429
                                                                                        CameraShutterCycleCounter = 0;
-
 
430
                                                                                        RemainingPulse  = MINSERVOPULSE + SERVORANGE/2;
-
 
431
                                                                                }
-
 
432
                                                                                else
-
 
433
                                                                                {
-
 
434
                                                                                        // Leave on for at least 24 cycles or 0.25 seconds to allow 
-
 
435
                                                                                        // the camera to properly trigger, turn off if past 0.25 sec.
-
 
436
                                                                                        // For now this is actually set via para5 to allow for a long enough
-
 
437
                                                                                        // shutter pulse for different cameras. Once it is clear what value
-
 
438
                                                                                        // works, this can be changed to a hardcoded value.
-
 
439
                                                                                        if(CameraShutterCycleCounter>Parameter_UserParam5)
-
 
440
                                                                                        {
-
 
441
                                                                                       
-
 
442
                                                                                                RemainingPulse = MINSERVOPULSE;
-
 
443
                                                                                        }
-
 
444
                                                                                        CameraShutterCycleCounter++;
-
 
445
                                                                                }
-
 
446
                                                                        }
-
 
447
                                                                }      
-
 
448
                                                        }
-
 
449
                                                        break;
372
 
450
                       
373
                                                default: // other servo channels
451
                                                default: // other servo channels
374
                                                        RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
452
                                                        RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs
375
                                                        break;
453
                                                        break;
376
                                        }
454
                                        }
377
                                        // range servo pulse width
455
                                        // range servo pulse width
378
                                        if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
456
                                        if(RemainingPulse > MAXSERVOPULSE )                     RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit
379
                                        else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
457
                                        else if(RemainingPulse < MINSERVOPULSE )        RemainingPulse = MINSERVOPULSE; // lower servo pulse limit
380
                                        // substract stop pulse width
458
                                        // substract stop pulse width
-
 
459
                                        // Arthur P: I think the following line, correcting for the PPM sTOPPULSE does not apply for the
-
 
460
                                        // calculated pulses, or maybe not at all. Therefore I-m commenting it out to see what happens....
-
 
461
                                        // 090624 reactivated as rollservo correction can now be done over para7.
381
                                        RemainingPulse -= PPM_STOPPULSE;
462
                                        RemainingPulse -= PPM_STOPPULSE;
382
                                        // accumulate time for correct sync gap
463
                                        // accumulate time for correct sync gap
383
                                        ServoFrameTime += RemainingPulse;
464
                                        ServoFrameTime += RemainingPulse;
384
                                }
465
                                }
385
                        }
466
                        }
386
                        else // we had a high pulse
467
                        else // we had a high pulse
387
                        {
468
                        {
388
                                TCCR2A |= (1<<COM2A0); // make a low pulse
469
                                TCCR2A |= (1<<COM2A0); // make a low pulse
389
                                // set pulsewidth to stop pulse width
470
                                // set pulsewidth to stop pulse width
390
                                RemainingPulse = PPM_STOPPULSE;
471
                                RemainingPulse = PPM_STOPPULSE;
391
                                // accumulate time for correct sync gap
472
                                // accumulate time for correct sync gap
392
                                ServoFrameTime += RemainingPulse;
473
                                ServoFrameTime += RemainingPulse;
393
                                if(ServoActive && SenderOkay > 180) HEF4017R_OFF; // disable HEF4017 reset
474
                                if(ServoActive && SenderOkay > 180) HEF4017R_OFF; // disable HEF4017 reset
394
                                ServoIndex++; // change to next servo channel
475
                                ServoIndex++; // change to next servo channel
395
                                if(ServoIndex > EE_Parameter.ServoNickRefresh) ServoIndex = 0; // reset to the sync gap
476
                                if(ServoIndex > EE_Parameter.ServoNickRefresh) ServoIndex = 0; // reset to the sync gap
396
                        }
477
                        }
397
                        // set pulse output active
478
                        // set pulse output active
398
                        PulseOutput = 1;
479
                        PulseOutput = 1;
399
                }
480
                }
400
        } // EOF PPM state machine
481
        } // EOF PPM state machine
401
 
482
 
402
        // General pulse output generator
483
        // General pulse output generator
403
        if(RemainingPulse > (255 + IRS_RUNTIME))
484
        if(RemainingPulse > (255 + IRS_RUNTIME))
404
        {
485
        {
405
                OCR2A = 255;
486
                OCR2A = 255;
406
                RemainingPulse -= 255;
487
                RemainingPulse -= 255;
407
        }
488
        }
408
        else
489
        else
409
        {
490
        {
410
                if(RemainingPulse > 255) // this is the 2nd last part
491
                if(RemainingPulse > 255) // this is the 2nd last part
411
                {
492
                {
412
                        if((RemainingPulse - 255) < IRS_RUNTIME)
493
                        if((RemainingPulse - 255) < IRS_RUNTIME)
413
                        {
494
                        {
414
                                OCR2A = 255 - IRS_RUNTIME;
495
                                OCR2A = 255 - IRS_RUNTIME;
415
                                RemainingPulse -= 255 - IRS_RUNTIME;
496
                                RemainingPulse -= 255 - IRS_RUNTIME;
416
 
497
 
417
                        }
498
                        }
418
                        else // last part > ISR_RUNTIME
499
                        else // last part > ISR_RUNTIME
419
                        {
500
                        {
420
                                OCR2A = 255;
501
                                OCR2A = 255;
421
                                RemainingPulse -= 255;
502
                                RemainingPulse -= 255;
422
                        }
503
                        }
423
                }
504
                }
424
                else // this is the last part
505
                else // this is the last part
425
                {
506
                {
426
                        OCR2A = RemainingPulse;
507
                        OCR2A = RemainingPulse;
427
                        RemainingPulse = 0;
508
                        RemainingPulse = 0;
428
                        PulseOutput = 0; // trigger to stop pulse
509
                        PulseOutput = 0; // trigger to stop pulse
429
                }
510
                }
430
        } // EOF general pulse output generator
511
        } // EOF general pulse output generator
431
}
512
}
432
 
513