Subversion Repositories FlightCtrl

Rev

Rev 950 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
886 killagreg 6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1 ingob 10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
886 killagreg 13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
1 ingob 14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
886 killagreg 24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1 ingob 25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
886 killagreg 29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
1 ingob 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.
886 killagreg 33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
1 ingob 34
// +     for non-commercial use (directly or indirectly)
886 killagreg 35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1 ingob 36
// +     with our written permission
886 killagreg 37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
1 ingob 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
886 killagreg 50
// +  POSSIBILITY OF SUCH DAMAGE.
1 ingob 51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
886 killagreg 52
#include <avr/boot.h>
1 ingob 53
 
886 killagreg 54
#include <avr/io.h>
55
#include <avr/interrupt.h>
1 ingob 56
 
886 killagreg 57
#include "main.h"
58
#include "timer0.h"
59
#include "timer2.h"
60
#include "uart.h"
61
#if defined (__AVR_ATmega644P__)
62
#include "uart1.h"
63
#endif
64
#include "led.h"
65
#include "menu.h"
66
#include "fc.h"
67
#include "rc.h"
68
#include "analog.h"
69
#include "printf_P.h"
70
#ifdef USE_KILLAGREG
71
#include "mm3.h"
72
#endif
73
#ifdef USE_NAVICTRL
74
#include "spi.h"
75
#endif
908 killagreg 76
#ifdef USE_MK3MAG
886 killagreg 77
#include "mk3mag.h"
78
#endif
79
#include "twimaster.h"
80
#include "eeprom.h"
81
#include "_Settings.h"
1 ingob 82
 
83
 
886 killagreg 84
uint8_t BoardRelease = 10;
1 ingob 85
 
819 hbuss 86
 
1 ingob 87
//############################################################################
88
//Hauptprogramm
89
int main (void)
90
//############################################################################
91
{
819 hbuss 92
        unsigned int timer;
296 holgerb 93
 
886 killagreg 94
        // disable interrupts global
95
        cli();
96
 
97
        // get board release
98
    DDRB  = 0x00;
99
    PORTB = 0x00;
100
    for(timer = 0; timer < 1000; timer++); // make some delay
936 killagreg 101
    if(PINB & (1<<PINB0))
102
    {
103
                if(PINB & (1<<PINB1)) BoardRelease = 13;
104
                else BoardRelease = 11;
105
        }
886 killagreg 106
    else BoardRelease = 10;
107
 
108
        // set LED ports as output
109
        DDRB |= (1<<DDB1)|(1<<DDB0);
936 killagreg 110
        RED_ON;
886 killagreg 111
        GRN_OFF;
112
 
113
        // disable watchdog
1 ingob 114
    MCUSR &=~(1<<WDRF);
115
    WDTCSR |= (1<<WDCE)|(1<<WDE);
116
    WDTCSR = 0;
117
 
886 killagreg 118
    BeepTime = 2000;
1 ingob 119
 
911 killagreg 120
        PPM_in[CH_GAS] = 0;
886 killagreg 121
        StickYaw = 0;
122
        StickRoll = 0;
911 killagreg 123
        StickNick = 0;
1 ingob 124
 
936 killagreg 125
    RED_OFF;
886 killagreg 126
 
127
        // initalize modules
936 killagreg 128
        //LED_Init(); Is done within ParamSet_Init() below
886 killagreg 129
    TIMER0_Init();
130
    TIMER2_Init();
131
        USART0_Init();
132
 
133
        #if defined (__AVR_ATmega644P__)
950 pangu 134
        if (BoardRelease > 10) USART1_Init();
886 killagreg 135
        #endif
136
 
137
    RC_Init();
1 ingob 138
        ADC_Init();
886 killagreg 139
        I2C_Init();
140
 
908 killagreg 141
 
142
        #ifdef USE_NAVICTRL
143
        SPI_MasterInit();
144
        #endif
886 killagreg 145
        #ifdef USE_KILLAGREG
146
        MM3_Init();
147
        #endif
908 killagreg 148
        #ifdef USE_MK3MAG
886 killagreg 149
        MK3MAG_Init();
150
        #endif
151
 
152
 
153
        // enable interrupts global
1 ingob 154
        sei();
155
 
886 killagreg 156
    VersionInfo.Major = VERSION_MAJOR;
157
    VersionInfo.Minor = VERSION_MINOR;
158
    VersionInfo.PCCompatible = VERSION_COMPATIBLE;
936 killagreg 159
    VersionInfo.Hardware = 1; // Flight Control
886 killagreg 160
 
161
        printf("\n\rFlightControl\n\rHardware:%d.%d\n\rSoftware:V%d.%d%c ",BoardRelease/10,BoardRelease%10, VERSION_MAJOR, VERSION_MINOR,VERSION_INDEX + 'a');
1 ingob 162
        printf("\n\r==============================");
163
        GRN_ON;
164
 
886 killagreg 165
        // Parameter set handling
166
        ParamSet_Init();
513 hbuss 167
 
911 killagreg 168
    if(GetParamWord(PID_ACC_NICK) > 1023)
513 hbuss 169
     {
886 killagreg 170
       printf("\n\rACC not calibrated!");
513 hbuss 171
     }
1 ingob 172
 
886 killagreg 173
        //wait for a short time (otherwise the RC channel check won't work below)
174
        timer = SetDelay(500);
175
        while(!CheckDelay(timer));
176
 
177
 
178
        if(ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)
179
         {
180
           printf("\n\rCalibrating air pressure sensor..");
181
           timer = SetDelay(1000);
182
       SearchAirPressureOffset();
1 ingob 183
           while (!CheckDelay(timer));
184
       printf("OK\n\r");
185
        }
886 killagreg 186
 
908 killagreg 187
        #ifdef USE_NAVICTRL
188
        printf("\n\rSupport for NaviCtrl");
189
        #endif
190
 
886 killagreg 191
        #ifdef USE_KILLAGREG
192
        printf("\n\rSupport for MicroMag3 Compass");
908 killagreg 193
        #endif
957 pangu 194
 
908 killagreg 195
        #ifdef USE_MK3MAG
196
        printf("\n\rSupport for MK3MAG Compass");
197
        #endif
198
 
199
 
200
 
957 pangu 201
        #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG))
886 killagreg 202
        #if defined (__AVR_ATmega644P__)
203
        if(BoardRelease == 10)
204
        {
205
                printf("\n\rSupport for GPS at 1st UART");
206
        }
207
        else
208
        {
209
                printf("\n\rSupport for GPS at 2nd UART");
210
        }
211
        #else // (__AVR_ATmega644__)
908 killagreg 212
        printf("\n\rSupport for GPS at 1st UART");
886 killagreg 213
        #endif
214
        #endif
215
 
908 killagreg 216
 
217
 
1 ingob 218
        SetNeutral();
219
 
936 killagreg 220
        RED_OFF;
1 ingob 221
 
886 killagreg 222
    BeepTime = 2000;
223
    ExternControl.Digital[0] = 0x55;
224
 
225
 
226
        printf("\n\rControl: ");
227
        if (ParamSet.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
1 ingob 228
        else printf("Neutral");
886 killagreg 229
 
1 ingob 230
        printf("\n\n\r");
886 killagreg 231
 
232
    LCD_Clear();
233
 
943 pangu 234
    I2CTimeout = 2000;
886 killagreg 235
 
1 ingob 236
        while (1)
237
        {
886 killagreg 238
        if(UpdateMotor)      // control interval
239
        {
936 killagreg 240
                        UpdateMotor = 0; // reset Flag, is enabled every 2 ms by isr of timer0
886 killagreg 241
                        //PORTD |= (1<<PORTD4);
242
            MotorControl();
243
                        //PORTD &= ~(1<<PORTD4);
244
 
245
                        SendMotorData();
246
 
936 killagreg 247
            RED_OFF;
886 killagreg 248
 
249
            if(PcAccess) PcAccess--;
250
            else
251
            {
252
               DubWiseKeys[0] = 0;
492 hbuss 253
               DubWiseKeys[1] = 0;
595 hbuss 254
                           ExternControl.Config = 0;
911 killagreg 255
               ExternStickNick= 0;
492 hbuss 256
               ExternStickRoll = 0;
886 killagreg 257
               ExternStickYaw = 0;
258
            }
259
 
173 holgerb 260
            if(!I2CTimeout)
886 killagreg 261
            {
262
                                I2CTimeout = 5;
263
                                I2C_Reset();
936 killagreg 264
                                if((BeepModulation == 0xFFFF) && (MKFlags & MKFLAG_MOTOR_RUN) )
886 killagreg 265
                                {
266
                                        BeepTime = 10000; // 1 second
267
                                        BeepModulation = 0x0080;
268
                                }
269
                        }
270
                        else
271
                        {
272
                                I2CTimeout--;
936 killagreg 273
                                RED_OFF;
886 killagreg 274
                        }
275
 
936 killagreg 276
                        if(SIO_DEBUG && (!UpdateMotor || !(MKFlags & MKFLAG_MOTOR_RUN) ))
886 killagreg 277
                        {
278
                                USART0_TransmitTxData();
279
                                USART0_ProcessRxData();
280
                        }
281
                        else USART0_ProcessRxData();
282
 
283
                        if(CheckDelay(timer))
284
                        {
285
                if(UBat < ParamSet.LowVoltageWarning)
173 holgerb 286
                {
957 pangu 287
                                        BeepModulation = 0x0300;
288
                                        if(!BeepTime )
886 killagreg 289
                                        {
290
                                                BeepTime = 6000; // 0.6 seconds
291
                                        }
1 ingob 292
                }
886 killagreg 293
                                #ifdef USE_NAVICTRL
294
                                SPI_StartTransmitPacket();
295
                                SendSPI = 4;
296
                                #endif
297
                                timer = SetDelay(20); // every 20 ms
298
            }
936 killagreg 299
 
300
            LED_Update();
886 killagreg 301
                }
723 hbuss 302
 
886 killagreg 303
                #ifdef USE_NAVICTRL
304
                if(!SendSPI)
305
                {       // SendSPI is decremented in timer0.c with a rate of 9.765 kHz.
306
                        // within the SPI_TransmitByte() routine the value is set to 4.
307
                        // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz,
308
                        // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms.
309
                        SPI_TransmitByte();
310
                }
311
                #endif
1 ingob 312
    }
886 killagreg 313
        return (1);
1 ingob 314
}
315