Subversion Repositories FlightCtrl

Rev

Rev 1849 | Rev 1869 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1849 Rev 1864
Line 76... Line 76...
76
#include "mk3mag.h"
76
#include "mk3mag.h"
77
#endif
77
#endif
78
#include "eeprom.h"
78
#include "eeprom.h"
Line 79... Line 79...
79
 
79
 
80
int16_t main(void) {
80
int16_t main(void) {
Line 81... Line 81...
81
        uint16_t timer;
81
  uint16_t timer;
82
 
82
 
Line 83... Line 83...
83
        // disable interrupts global
83
  // disable interrupts global
84
        cli();
84
  cli();
85
 
85
 
86
        // analyze hardware environment
86
  // analyze hardware environment
87
        CPUType = getCPUType();
87
  CPUType = getCPUType();
88
        BoardRelease = getBoardRelease();
88
  BoardRelease = getBoardRelease();
89
 
89
 
90
        // disable watchdog
90
  // disable watchdog
91
        MCUSR &= ~(1 << WDRF);
91
  MCUSR &= ~(1 << WDRF);
92
        WDTCSR |= (1 << WDCE) | (1 << WDE);
92
  WDTCSR |= (1 << WDCE) | (1 << WDE);
93
        WDTCSR = 0;
93
  WDTCSR = 0;
94
 
94
 
95
        // PPM_in[CH_THROTTLE] = 0;
95
  // PPM_in[CH_THROTTLE] = 0;
96
        // Why??? They are already initialized to 0.
96
  // Why??? They are already initialized to 0.
97
        // stickPitch = stickRoll = stickYaw = 0;
97
  // stickPitch = stickRoll = stickYaw = 0;
98
 
98
 
99
        RED_OFF;
99
  RED_OFF;
100
 
100
 
101
        // initalize modules
101
  // initalize modules
102
        output_init();
102
  output_init();
103
        timer0_init();
103
  timer0_init();
104
        timer2_init();
104
  timer2_init();
105
        usart0_Init();
105
  usart0_Init();
106
        if (CPUType == ATMEGA644P)
106
  if (CPUType == ATMEGA644P)
107
                usart1_Init();
107
    usart1_Init();
108
        RC_Init();
108
  RC_Init();
109
        analog_init();
109
  analog_init();
110
        I2C_init();
110
  I2C_init();
111
#ifdef USE_NAVICTRL
111
#ifdef USE_NAVICTRL
112
        SPI_MasterInit();
112
  SPI_MasterInit();
113
#endif
113
#endif
Line 114... Line 114...
114
#ifdef USE_MK3MAG
114
#ifdef USE_MK3MAG
115
        MK3MAG_Init();
115
  MK3MAG_Init();
Line 116... Line 116...
116
#endif
116
#endif
117
 
117
 
118
        // enable interrupts global
118
  // enable interrupts global
119
        sei();
119
  sei();
120
 
120
 
121
        printf("\n\r===================================");
121
  printf("\n\r===================================");
122
        printf("\n\rFlightControl");
122
  printf("\n\rFlightControl");
123
        printf("\n\rHardware: Custom");
123
  printf("\n\rHardware: Custom");
124
        printf("\r\n     CPU: Atmega644");
124
  printf("\r\n     CPU: Atmega644");
125
        if (CPUType == ATMEGA644P)
125
  if (CPUType == ATMEGA644P)
126
                printf("p");
126
    printf("p");
127
        printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a');
127
  printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a');
128
        printf("\n\r===================================");
128
  printf("\n\r===================================");
129
 
129
 
130
        // Parameter Set handling
130
  // Parameter Set handling
131
        ParamSet_Init();
131
  ParamSet_Init();
132
 
132
 
133
        // Wait for a short time (otherwise the RC channel check won't work below)
133
  // Wait for a short time (otherwise the RC channel check won't work below)
134
        // timer = SetDelay(500);
134
  // timer = SetDelay(500);
135
        // while(!CheckDelay(timer));
135
  // while(!CheckDelay(timer));
136
 
136
 
137
        // Instead, while away the time by flashing the 2 outputs:
137
  // Instead, while away the time by flashing the 2 outputs:
138
        // First J16, then J17. Makes it easier to see which is which.
138
  // First J16, then J17. Makes it easier to see which is which.
139
        timer = SetDelay(250);
139
  timer = SetDelay(200);
140
        OUTPUT_SET(0,1);
140
  OUTPUT_SET(0,1);
141
        GRN_OFF;
-
 
142
        RED_ON;
141
  GRN_OFF;
143
        while (!CheckDelay(timer))
-
 
144
                ;
-
 
145
 
-
 
146
        OUTPUT_SET(0,0);
142
  RED_ON;
147
        timer = SetDelay(250);
143
  while (!CheckDelay(timer))
148
        while (!CheckDelay(timer))
144
    ;
149
                ;
145
 
150
 
146
  timer = SetDelay(200);
151
        timer = SetDelay(250);
147
  OUTPUT_SET(0,0);
152
        OUTPUT_SET(1,1);
148
  OUTPUT_SET(1,1);
153
        RED_OFF;
149
  RED_OFF;
154
        GRN_ON;
150
  GRN_ON;
155
        while (!CheckDelay(timer))
151
  while (!CheckDelay(timer))
156
                ;
152
    ;
157
 
153
 
158
        timer = SetDelay(250);
154
  timer = SetDelay(200);
159
        while (!CheckDelay(timer))
155
  while (!CheckDelay(timer))
160
                ;
156
    ;
161
        OUTPUT_SET(1,0);
157
  OUTPUT_SET(1,0);
162
 
158
 
163
        twi_diagnostics();
159
  twi_diagnostics();
164
 
160
 
165
        printf("\n\r===================================");
161
  printf("\n\r===================================");
166
 
162
 
167
        /*
163
  /*
168
         if(staticParams.GlobalConfig & CFG_HEIGHT_CONTROL)
164
   if(staticParams.GlobalConfig & CFG_HEIGHT_CONTROL)
169
         {
165
   {
170
         printf("\n\rCalibrating air pressure sensor..");
166
   printf("\n\rCalibrating air pressure sensor..");
171
         timer = SetDelay(1000);
167
   timer = SetDelay(1000);
Line 172... Line 168...
172
         SearchAirPressureOffset();
168
   SearchAirPressureOffset();
173
         while (!CheckDelay(timer));
169
   while (!CheckDelay(timer));
174
         printf("OK\n\r");
170
   printf("OK\n\r");
175
         }
171
   }
176
         */
172
   */
177
 
173
 
178
#ifdef USE_NAVICTRL
174
#ifdef USE_NAVICTRL
179
        printf("\n\rSupport for NaviCtrl");
175
  printf("\n\rSupport for NaviCtrl");
180
#ifdef USE_RC_DSL
176
#ifdef USE_RC_DSL
Line 181... Line 177...
181
        printf("\r\nSupport for DSL RC at 2nd UART");
177
  printf("\r\nSupport for DSL RC at 2nd UART");
182
#endif
178
#endif
183
#ifdef USE_RC_SPECTRUM
179
#ifdef USE_RC_SPECTRUM
Line 184... Line 180...
184
        printf("\r\nSupport for SPECTRUM RC at 2nd UART");
180
  printf("\r\nSupport for SPECTRUM RC at 2nd UART");
185
#endif
181
#endif
186
#endif
182
#endif
187
 
183
 
Line 188... Line 184...
188
#ifdef USE_MK3MAG
184
#ifdef USE_MK3MAG
Line 189... Line 185...
189
        printf("\n\rSupport for MK3MAG Compass");
185
  printf("\n\rSupport for MK3MAG Compass");
190
#endif
186
#endif
Line 191... Line 187...
191
 
187
 
192
#if (defined (USE_MK3MAG))
188
#if (defined (USE_MK3MAG))
193
        if(CPUType == ATMEGA644P) printf("\n\rSupport for GPS at 2nd UART");
189
  if(CPUType == ATMEGA644P) printf("\n\rSupport for GPS at 2nd UART");
194
        else printf("\n\rSupport for GPS at 1st UART");
190
  else printf("\n\rSupport for GPS at 1st UART");
195
#endif
191
#endif
196
 
192
 
197
        controlMixer_setNeutral();
193
  controlMixer_setNeutral();
198
 
194
 
199
        // Cal. attitude sensors and reset integrals.
195
  // Cal. attitude sensors and reset integrals.
200
        attitude_setNeutral();
196
  attitude_setNeutral();
201
 
197
 
202
        Servo_On();
198
  Servo_On();
203
 
199
 
204
        // Init flight parameters
200
  // Init flight parameters
205
        flight_setNeutral();
201
  flight_setNeutral();
206
 
202
 
207
        // RED_OFF;
203
  // RED_OFF;
208
 
204
 
209
        beep(2000);
205
  beep(2000);
210
 
206
 
211
        printf("\n\rControl: ");
207
  printf("\n\rControl: ");
212
        if (staticParams.GlobalConfig & CFG_HEADING_HOLD)
208
  if (staticParams.GlobalConfig & CFG_HEADING_HOLD)
213
                printf("HeadingHold");
209
    printf("HeadingHold");
214
                else printf("Neutral (ACC-Mode)");
210
    else printf("Neutral (ACC-Mode)");
215
 
211
 
216
        printf("\n\n\r");
212
  printf("\n\n\r");
217
 
213
 
218
        LCD_Clear();
214
  LCD_Clear();
219
 
215
 
220
        I2CTimeout = 5000;
216
  I2CTimeout = 5000;
221
 
217
 
222
        while (1) {
218
  while (1) {
223
                if (runFlightControl && analogDataReady) { // control interval
219
    if (runFlightControl && analogDataReady) { // control interval
224
                        runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0
220
      runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0
225
 
221
 
226
                        J4HIGH;
222
      //J4HIGH;
227
                        flight_control();
223
      flight_control();
228
                        J4LOW;
224
      //J4LOW;
229
 
225
 
230
                        /*
226
      /*
231
                         * If the motors are running (MKFlags & MKFLAG_MOTOR_RUN in flight.c), transmit
227
       * If the motors are running (MKFlags & MKFLAG_MOTOR_RUN in flight.c), transmit
232
                         * the throttle vector just computed. Otherwise, if motor test is engaged, transmit
228
       * the throttle vector just computed. Otherwise, if motor test is engaged, transmit
233
                         * the test throttle vector. If no testing, stop all motors.
229
       * the test throttle vector. If no testing, stop all motors.
234
                         */
230
       */
235
                        // Obsoleted.
231
      // Obsoleted.
236
                        // transmitMotorThrottleData();
232
      // transmitMotorThrottleData();
237
 
233
 
238
                        RED_OFF;
234
      RED_OFF;
239
 
235
 
240
                        /*
236
      /*
241
                         Does not belong here. Instead, external control should be ignored in
237
       Does not belong here. Instead, external control should be ignored in
242
                         controlMixer if there was no new data from there for some time.
238
       controlMixer if there was no new data from there for some time.
243
                         if(externalControlActive) externalControlActive--;
239
       if(externalControlActive) externalControlActive--;
244
                         else {
240
       else {
245
                         externalControl.config = 0;
241
       externalControl.config = 0;
246
                         externalStickPitch = 0;
242
       externalStickPitch = 0;
247
                         externalStickRoll = 0;
243
       externalStickRoll = 0;
248
                         externalStickYaw = 0;
244
       externalStickYaw = 0;
249
                         }
245
       }
250
                         */
246
       */
251
 
247
 
252
                        /*
248
      /*
253
                         Does not belong here.
249
       Does not belong here.
254
                         if(RC_Quality)  RC_Quality--;
250
       if(RC_Quality)  RC_Quality--;
255
                         */
251
       */
256
 
252
 
257
                        /* Does not belong here. Well since we are not supporting navi right now anyway, leave out.
253
      /* Does not belong here. Well since we are not supporting navi right now anyway, leave out.
258
                         #ifdef USE_NAVICTRL
254
       #ifdef USE_NAVICTRL
259
                         if(NCDataOkay) {
255
       if(NCDataOkay) {
260
                         if(--NCDataOkay == 0) // no data from NC
256
       if(--NCDataOkay == 0) // no data from NC
261
                         {  // set gps control sticks neutral
257
       {  // set gps control sticks neutral
262
                         GPSStickPitch = 0;
258
       GPSStickPitch = 0;
263
                         GPSStickRoll = 0;
259
       GPSStickRoll = 0;
264
                         NCSerialDataOkay = 0;
260
       NCSerialDataOkay = 0;
265
                         }
261
       }
266
                         }
262
       }
267
                         #endif
263
       #endif
268
                         */
264
       */
269
                        if (!--I2CTimeout || missingMotor) { // try to reset the i2c if motor is missing ot timeout
265
      if (!--I2CTimeout || missingMotor) { // try to reset the i2c if motor is missing ot timeout
270
                                RED_ON;
266
        RED_ON;
271
                                if (!I2CTimeout) {
267
        if (!I2CTimeout) {
272
                                        I2C_Reset();
268
          I2C_Reset();
273
                                        I2CTimeout = 5;
269
          I2CTimeout = 5;
274
                                }
270
        }
275
                        } else {
271
      } else {
276
                                RED_OFF;
272
        RED_OFF;
277
                        }
273
      }
278
 
274
 
279
                        // Allow Serial Data Transmit if motors must not updated or motors are not running
275
      // Allow Serial Data Transmit if motors must not updated or motors are not running
280
                        if (!runFlightControl || !(MKFlags & MKFLAG_MOTOR_RUN)) {
276
      if (!runFlightControl || !(MKFlags & MKFLAG_MOTOR_RUN)) {
281
                                usart0_TransmitTxData();
277
        usart0_TransmitTxData();
Line 282... Line 278...
282
                        }
278
      }
283
 
279
 
284
                        usart0_ProcessRxData();
280
      usart0_ProcessRxData();
285
 
281
 
286
                        if (CheckDelay(timer)) {
282
      if (CheckDelay(timer)) {
287
                                if (UBat <= UBAT_AT_5V) {
283
        if (UBat <= UBAT_AT_5V) {
288
                                        // Do nothing. The voltage on the input side of the regulator is <5V;
284
          // Do nothing. The voltage on the input side of the regulator is <5V;
289
                                        // we must be running off USB power. Keep it quiet.
285
          // we must be running off USB power. Keep it quiet.
Line 290... Line 286...
290
                                } else if (UBat < staticParams.LowVoltageWarning) {
286
        } else if (UBat < staticParams.LowVoltageWarning) {
291
                                        beepBatteryAlarm();
287
          beepBatteryAlarm();
292
                                }
288
        }
293
 
289
 
294
#ifdef USE_NAVICTRL
290
#ifdef USE_NAVICTRL
295
                                SPI_StartTransmitPacket();
291
        SPI_StartTransmitPacket();
296
                                SendSPI = 4;
292
        SendSPI = 4;
297
#endif
293
#endif
298
                                timer = SetDelay(20); // every 20 ms
294
        timer = SetDelay(20); // every 20 ms
299
                        }
295
      }
300
                        output_update();
296
      output_update();
301
                }
297
    }