Subversion Repositories FlightCtrl

Rev

Details | 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>
1180 killagreg 56
#include <util/delay.h>
1 ingob 57
 
886 killagreg 58
#include "main.h"
59
#include "timer0.h"
60
#include "timer2.h"
1180 killagreg 61
#include "uart0.h"
886 killagreg 62
#include "uart1.h"
63
#include "led.h"
64
#include "menu.h"
65
#include "fc.h"
66
#include "rc.h"
67
#include "analog.h"
68
#include "printf_P.h"
69
#ifdef USE_KILLAGREG
70
#include "mm3.h"
71
#endif
72
#ifdef USE_NAVICTRL
73
#include "spi.h"
74
#endif
908 killagreg 75
#ifdef USE_MK3MAG
886 killagreg 76
#include "mk3mag.h"
77
#endif
78
#include "twimaster.h"
79
#include "eeprom.h"
1 ingob 80
 
81
 
886 killagreg 82
uint8_t BoardRelease = 10;
1180 killagreg 83
uint8_t CPUType = ATMEGA644;
1 ingob 84
 
819 hbuss 85
 
1180 killagreg 86
uint8_t GetCPUType(void)
87
{   // works only after reset or power on when the registers have default values
88
        uint8_t CPUType = ATMEGA644;
89
        if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P;  // initial Values for 644P after reset
90
        return CPUType;
91
}
92
 
93
 
94
uint8_t GetBoardRelease(void)
1 ingob 95
{
1180 killagreg 96
        uint8_t BoardRelease = 10;
97
        // the board release is coded via the pull up or down the 2 status LED
296 holgerb 98
 
1180 killagreg 99
    PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
100
    DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
886 killagreg 101
 
1180 killagreg 102
        _delay_loop_2(1000); // make some delay
103
 
104
    switch( PINB & ((1<<PINB1)|(1<<PINB0)) )
936 killagreg 105
    {
1180 killagreg 106
                case 0x00:
107
                        BoardRelease = 10; // 1.0
108
                        break;
109
                case 0x01:
110
                        BoardRelease = 11; // 1.1 or 1.2
111
                        break;
112
                case 0x02:
113
                        BoardRelease = 20; // 2.0
114
                        break;
115
                case 0x03:
116
                        BoardRelease = 13; // 1.3
117
                        break;
118
                default:
119
                        break;
936 killagreg 120
        }
886 killagreg 121
        // set LED ports as output
122
        DDRB |= (1<<DDB1)|(1<<DDB0);
936 killagreg 123
        RED_ON;
886 killagreg 124
        GRN_OFF;
1180 killagreg 125
        return BoardRelease;
126
}
886 killagreg 127
 
1180 killagreg 128
 
129
int16_t main (void)
130
{
131
        unsigned int timer;
132
 
133
        // disable interrupts global
134
        cli();
135
 
136
        // analyze hardware environment
137
        CPUType = GetCPUType();
138
        BoardRelease = GetBoardRelease();
139
 
886 killagreg 140
        // disable watchdog
1 ingob 141
    MCUSR &=~(1<<WDRF);
142
    WDTCSR |= (1<<WDCE)|(1<<WDE);
143
    WDTCSR = 0;
144
 
886 killagreg 145
    BeepTime = 2000;
1 ingob 146
 
911 killagreg 147
        PPM_in[CH_GAS] = 0;
886 killagreg 148
        StickYaw = 0;
149
        StickRoll = 0;
911 killagreg 150
        StickNick = 0;
1 ingob 151
 
936 killagreg 152
    RED_OFF;
886 killagreg 153
 
154
        // initalize modules
1180 killagreg 155
        LED_Init();
886 killagreg 156
    TIMER0_Init();
157
    TIMER2_Init();
158
        USART0_Init();
1180 killagreg 159
        if(CPUType == ATMEGA644P) USART1_Init();
886 killagreg 160
    RC_Init();
1 ingob 161
        ADC_Init();
886 killagreg 162
        I2C_Init();
908 killagreg 163
        #ifdef USE_NAVICTRL
164
        SPI_MasterInit();
165
        #endif
886 killagreg 166
        #ifdef USE_KILLAGREG
167
        MM3_Init();
168
        #endif
908 killagreg 169
        #ifdef USE_MK3MAG
886 killagreg 170
        MK3MAG_Init();
171
        #endif
172
 
173
        // enable interrupts global
1 ingob 174
        sei();
175
 
1180 killagreg 176
        printf("\n\rFlightControl");
177
        printf("\n\rHardware: %d.%d", BoardRelease/10, BoardRelease%10);
178
        if(CPUType == ATMEGA644P)
179
        printf("\r\n     CPU: Atmega644p");
180
        else
181
        printf("\r\n     CPU: Atmega644");
182
        printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a');
1 ingob 183
        printf("\n\r==============================");
184
        GRN_ON;
185
 
1180 killagreg 186
        // Parameter Set handling
886 killagreg 187
        ParamSet_Init();
513 hbuss 188
 
1180 killagreg 189
        if(GetParamWord(PID_ACC_NICK) > 1023)
190
        {
191
                printf("\n\rACC not calibrated!");
192
        }
1 ingob 193
 
886 killagreg 194
        //wait for a short time (otherwise the RC channel check won't work below)
195
        timer = SetDelay(500);
196
        while(!CheckDelay(timer));
197
 
198
        if(ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)
1180 killagreg 199
        {
200
                printf("\n\rCalibrating air pressure sensor..");
201
                timer = SetDelay(1000);
202
                SearchAirPressureOffset();
203
                while (!CheckDelay(timer));
204
                printf("OK\n\r");
1 ingob 205
        }
886 killagreg 206
 
908 killagreg 207
        #ifdef USE_NAVICTRL
208
        printf("\n\rSupport for NaviCtrl");
1199 killagreg 209
        #ifdef USE_RC_DSL
210
        printf("\r\nSupport for DSL RC at 2nd UART");
908 killagreg 211
        #endif
1199 killagreg 212
        #ifdef USE_RC_SPECTRUM
213
        printf("\r\nSupport for SPECTRUM RC at 2nd UART");
214
        #endif
215
        #endif
908 killagreg 216
 
886 killagreg 217
        #ifdef USE_KILLAGREG
218
        printf("\n\rSupport for MicroMag3 Compass");
908 killagreg 219
        #endif
953 killagreg 220
 
908 killagreg 221
        #ifdef USE_MK3MAG
222
        printf("\n\rSupport for MK3MAG Compass");
223
        #endif
224
 
953 killagreg 225
        #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG))
1180 killagreg 226
        if(CPUType == ATMEGA644P) printf("\n\rSupport for GPS at 2nd UART");
227
        else                      printf("\n\rSupport for GPS at 1st UART");
886 killagreg 228
        #endif
229
 
908 killagreg 230
 
1180 killagreg 231
        SetNeutral(NO_ACC_CALIB);
908 killagreg 232
 
936 killagreg 233
        RED_OFF;
1 ingob 234
 
1180 killagreg 235
        BeepTime = 2000;
236
        ExternControl.Digital[0] = 0x55;
886 killagreg 237
 
238
 
239
        printf("\n\rControl: ");
240
        if (ParamSet.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
1 ingob 241
        else printf("Neutral");
886 killagreg 242
 
1 ingob 243
        printf("\n\n\r");
886 killagreg 244
 
1180 killagreg 245
        LCD_Clear();
886 killagreg 246
 
1180 killagreg 247
        I2CTimeout = 5000;
1 ingob 248
        while (1)
249
        {
1180 killagreg 250
                if(UpdateMotor && ADReady)      // control interval
251
                {
252
                        UpdateMotor = 0; // reset Flag, is enabled every 2 ms by ISR of timer0
886 killagreg 253
 
1180 killagreg 254
                        //J4HIGH;
255
                        MotorControl();
256
                        //J4LOW;
886 killagreg 257
 
1180 killagreg 258
                        SendMotorData(); // the flight control code
259
                        RED_OFF;
886 killagreg 260
 
261
 
1180 killagreg 262
 
263
                        if(PcAccess) PcAccess--;
264
                        else
265
                        {
266
                                ExternControl.Config = 0;
267
                                ExternStickNick= 0;
268
                                ExternStickRoll = 0;
269
                                ExternStickYaw = 0;
270
                        }
1214 killagreg 271
                        if(RC_Quality)  RC_Quality--;
1180 killagreg 272
                        if(!I2CTimeout)
273
                        {
886 killagreg 274
                                I2CTimeout = 5;
275
                                I2C_Reset();
936 killagreg 276
                                if((BeepModulation == 0xFFFF) && (MKFlags & MKFLAG_MOTOR_RUN) )
886 killagreg 277
                                {
278
                                        BeepTime = 10000; // 1 second
279
                                        BeepModulation = 0x0080;
280
                                }
281
                        }
282
                        else
283
                        {
284
                                I2CTimeout--;
936 killagreg 285
                                RED_OFF;
886 killagreg 286
                        }
287
 
1180 killagreg 288
                        // allow Serial Data Transmit if motors must not updated or motors are not running
289
                        if( !UpdateMotor || !(MKFlags & MKFLAG_MOTOR_RUN) )
886 killagreg 290
                        {
291
                                USART0_TransmitTxData();
292
                        }
1180 killagreg 293
                        USART0_ProcessRxData();
886 killagreg 294
 
295
                        if(CheckDelay(timer))
296
                        {
1180 killagreg 297
                                if(UBat < ParamSet.LowVoltageWarning)
298
                                {
954 killagreg 299
                                        BeepModulation = 0x0300;
952 killagreg 300
                                        if(!BeepTime )
886 killagreg 301
                                        {
302
                                                BeepTime = 6000; // 0.6 seconds
303
                                        }
1180 killagreg 304
                                }
886 killagreg 305
                                #ifdef USE_NAVICTRL
306
                                SPI_StartTransmitPacket();
307
                                SendSPI = 4;
308
                                #endif
309
                                timer = SetDelay(20); // every 20 ms
1180 killagreg 310
                        }
936 killagreg 311
 
1180 killagreg 312
                        LED_Update();
313
                        //J4LOW;
886 killagreg 314
                }
723 hbuss 315
 
886 killagreg 316
                #ifdef USE_NAVICTRL
317
                if(!SendSPI)
318
                {       // SendSPI is decremented in timer0.c with a rate of 9.765 kHz.
319
                        // within the SPI_TransmitByte() routine the value is set to 4.
320
                        // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz,
321
                        // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms.
322
                        SPI_TransmitByte();
323
                }
324
                #endif
1180 killagreg 325
        }
886 killagreg 326
        return (1);
1 ingob 327
}
328