Subversion Repositories FlightCtrl

Rev

Rev 1961 | Rev 1964 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
1868 - 3
// + Nur für den privaten Gebrauch
1612 dongfang 4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1868 - 6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
1963 - 7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist.
1612 dongfang 8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1868 - 10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
1612 dongfang 11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1868 - 13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
1612 dongfang 15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
1868 - 17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
1612 dongfang 18
// + eindeutig als Ursprung verlinkt und genannt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1868 - 20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
1612 dongfang 21
// + Benutzung auf eigene Gefahr
1868 - 22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
1612 dongfang 23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1868 - 25
// + mit unserer Zustimmung zulässig
1612 dongfang 26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
1868 - 35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1612 dongfang 36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 52
 
1612 dongfang 53
#include <avr/boot.h>
54
#include <avr/io.h>
55
#include <avr/interrupt.h>
56
#include <util/delay.h>
57
 
58
#include "timer0.h"
59
#include "timer2.h"
60
#include "uart0.h"
61
#include "uart1.h"
62
#include "output.h"
63
#include "menu.h"
64
#include "attitude.h"
65
#include "flight.h"
66
#include "rc.h"
67
#include "analog.h"
68
#include "configuration.h"
69
#include "printf_P.h"
70
#include "twimaster.h"
71
#ifdef USE_NAVICTRL
72
#include "spi.h"
73
#endif
74
#ifdef USE_MK3MAG
75
#include "mk3mag.h"
76
#endif
77
#include "eeprom.h"
78
 
1821 - 79
int16_t main(void) {
1864 - 80
  uint16_t timer;
1612 dongfang 81
 
1864 - 82
  // disable interrupts global
83
  cli();
1612 dongfang 84
 
1864 - 85
  // analyze hardware environment
86
  CPUType = getCPUType();
1960 - 87
  boardRelease = getBoardRelease();
1612 dongfang 88
 
1864 - 89
  // disable watchdog
90
  MCUSR &= ~(1 << WDRF);
91
  WDTCSR |= (1 << WDCE) | (1 << WDE);
92
  WDTCSR = 0;
1612 dongfang 93
 
1864 - 94
  // PPM_in[CH_THROTTLE] = 0;
95
  // Why??? They are already initialized to 0.
96
  // stickPitch = stickRoll = stickYaw = 0;
1612 dongfang 97
 
1864 - 98
  RED_OFF;
1612 dongfang 99
 
1864 - 100
  // initalize modules
101
  output_init();
102
  timer0_init();
103
  timer2_init();
104
  usart0_Init();
1961 - 105
  if (CPUType == ATMEGA644P);// usart1_Init();
1864 - 106
  RC_Init();
107
  analog_init();
108
  I2C_init();
1612 dongfang 109
#ifdef USE_NAVICTRL
1864 - 110
  SPI_MasterInit();
1612 dongfang 111
#endif
112
#ifdef USE_MK3MAG
1864 - 113
  MK3MAG_Init();
1612 dongfang 114
#endif
115
 
1864 - 116
  // enable interrupts global
117
  sei();
1612 dongfang 118
 
1864 - 119
  printf("\n\r===================================");
120
  printf("\n\rFlightControl");
121
  printf("\n\rHardware: Custom");
122
  printf("\r\n     CPU: Atmega644");
123
  if (CPUType == ATMEGA644P)
124
    printf("p");
125
  printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a');
126
  printf("\n\r===================================");
1612 dongfang 127
 
1864 - 128
  // Parameter Set handling
1960 - 129
  channelMap_readOrDefault();
130
  paramSet_readOrDefault();
1634 - 131
 
1864 - 132
  // Wait for a short time (otherwise the RC channel check won't work below)
133
  // timer = SetDelay(500);
134
  // while(!CheckDelay(timer));
1612 dongfang 135
 
1864 - 136
  // Instead, while away the time by flashing the 2 outputs:
137
  // First J16, then J17. Makes it easier to see which is which.
1887 - 138
  timer = setDelay(200);
1864 - 139
  OUTPUT_SET(0,1);
140
  GRN_OFF;
141
  RED_ON;
1887 - 142
  while (!checkDelay(timer))
1864 - 143
    ;
1612 dongfang 144
 
1887 - 145
  timer = setDelay(200);
1864 - 146
  OUTPUT_SET(0,0);
147
  OUTPUT_SET(1,1);
148
  RED_OFF;
149
  GRN_ON;
1887 - 150
  while (!checkDelay(timer))
1864 - 151
    ;
1849 - 152
 
1887 - 153
  timer = setDelay(200);
154
  while (!checkDelay(timer))
1864 - 155
    ;
156
  OUTPUT_SET(1,0);
1612 dongfang 157
 
1864 - 158
  twi_diagnostics();
1634 - 159
 
1864 - 160
  printf("\n\r===================================");
1612 dongfang 161
 
162
#ifdef USE_NAVICTRL
1864 - 163
  printf("\n\rSupport for NaviCtrl");
1612 dongfang 164
#ifdef USE_RC_DSL
1864 - 165
  printf("\r\nSupport for DSL RC at 2nd UART");
1612 dongfang 166
#endif
167
#ifdef USE_RC_SPECTRUM
1864 - 168
  printf("\r\nSupport for SPECTRUM RC at 2nd UART");
1612 dongfang 169
#endif
170
#endif
171
 
172
#ifdef USE_MK3MAG
1864 - 173
  printf("\n\rSupport for MK3MAG Compass");
1612 dongfang 174
#endif
175
 
176
#if (defined (USE_MK3MAG))
1887 - 177
  if(CPUType == ATMEGA644P) printf("\n\r2 UART");
178
  else printf("\n\r1 UART");
1612 dongfang 179
#endif
180
 
1864 - 181
  controlMixer_setNeutral();
1612 dongfang 182
 
1864 - 183
  // Cal. attitude sensors and reset integrals.
184
  attitude_setNeutral();
1612 dongfang 185
 
1864 - 186
  Servo_On();
1612 dongfang 187
 
1864 - 188
  // Init flight parameters
189
  flight_setNeutral();
1612 dongfang 190
 
1864 - 191
  // RED_OFF;
1612 dongfang 192
 
1864 - 193
  beep(2000);
1612 dongfang 194
 
1864 - 195
  printf("\n\rControl: ");
1960 - 196
  if (staticParams.bitConfig & CFG_HEADING_HOLD)
1864 - 197
    printf("HeadingHold");
198
    else printf("Neutral (ACC-Mode)");
1612 dongfang 199
 
1864 - 200
  printf("\n\n\r");
1612 dongfang 201
 
1864 - 202
  LCD_Clear();
1612 dongfang 203
 
1864 - 204
  I2CTimeout = 5000;
1821 - 205
 
1864 - 206
  while (1) {
1869 - 207
    if (runFlightControl) { // control interval
1864 - 208
      runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0
1872 - 209
      if (!analogDataReady) {
1955 - 210
        debugOut.digital[0] |= DEBUG_MAINLOOP_TIMER;
1872 - 211
      } else {
1955 - 212
        debugOut.digital[0] &= ~DEBUG_MAINLOOP_TIMER;
1821 - 213
 
1872 - 214
        J4HIGH;
1869 - 215
        flight_control();
1872 - 216
        J4LOW;
1821 - 217
 
1872 - 218
        if (!--I2CTimeout || missingMotor) { // try to reset the i2c if motor is missing or timeout
1869 - 219
          RED_ON;
220
          if (!I2CTimeout) {
221
            I2C_Reset();
222
            I2CTimeout = 5;
223
          }
224
        } else {
225
          RED_OFF;
1864 - 226
        }
1821 - 227
 
1869 - 228
        // Allow Serial Data Transmit if motors must not updated or motors are not running
229
        if (!runFlightControl || !(MKFlags & MKFLAG_MOTOR_RUN)) {
230
          usart0_TransmitTxData();
231
        }
1821 - 232
 
1869 - 233
        usart0_ProcessRxData();
1821 - 234
 
1887 - 235
        if (checkDelay(timer)) {
1869 - 236
          if (UBat <= UBAT_AT_5V) {
237
            // Do nothing. The voltage on the input side of the regulator is <5V;
238
            // we must be running off USB power. Keep it quiet.
1960 - 239
          } else if (UBat < staticParams.batteryVoltageWarning) {
1869 - 240
            beepBatteryAlarm();
241
          }
1821 - 242
 
1612 dongfang 243
#ifdef USE_NAVICTRL
1869 - 244
          SPI_StartTransmitPacket();
245
          SendSPI = 4;
1612 dongfang 246
#endif
1887 - 247
          timer = setDelay(20); // every 20 ms
1869 - 248
        }
249
        output_update();
1864 - 250
      }
1821 - 251
 
1612 dongfang 252
#ifdef USE_NAVICTRL
1869 - 253
      if(!SendSPI) {
254
        // SendSPI is decremented in timer0.c with a rate of 9.765 kHz.
255
        // within the SPI_TransmitByte() routine the value is set to 4.
256
        // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz,
257
        // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms.
258
        SPI_TransmitByte();
259
      }
260
#endif
1864 - 261
    }
262
  }
263
  return (1);
1612 dongfang 264
}