Subversion Repositories FlightCtrl

Rev

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

Rev 2011 Rev 2012
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) Holger Buss, Ingo Busker
2
// + Copyright (c) Holger Buss, Ingo Busker
3
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
3
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
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.
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
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
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
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
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"
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
18
// + eindeutig als Ursprung verlinkt und genannt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
24
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
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
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.
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
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
38
// +     clearly linked as origin
39
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
39
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
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
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
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
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
#include "main.h"
52
#include "main.h"
53
unsigned char DisableRcOffBeeping = 0;
53
unsigned char DisableRcOffBeeping = 0;
54
unsigned char PlatinenVersion = 10;
54
unsigned char PlatinenVersion = 10;
55
unsigned char BattLowVoltageWarning = 94;
55
unsigned char BattLowVoltageWarning = 94;
56
unsigned int FlugMinuten = 0,FlugMinutenGesamt = 0;
56
unsigned int FlugMinuten = 0,FlugMinutenGesamt = 0;
57
unsigned int FlugSekunden = 0;
57
unsigned int FlugSekunden = 0;
58
pVoidFnct_pVoidFnctChar_const_fmt _printf_P;
58
pVoidFnct_pVoidFnctChar_const_fmt _printf_P;
59
unsigned char FoundMotors = 0;
59
unsigned char FoundMotors = 0;
60
unsigned char JetiBeep = 0; // to allow any Morse-Beeping of the Jeti-Box
60
unsigned char JetiBeep = 0; // to allow any Morse-Beeping of the Jeti-Box
61
 
61
 
62
void CalMk3Mag(void)
62
void CalMk3Mag(void)
63
{
63
{
64
 static unsigned char stick = 1;
64
 static unsigned char stick = 1;
65
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
65
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
66
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
66
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
67
  {
67
  {
68
   stick = 1;
68
   stick = 1;
69
   WinkelOut.CalcState++;
69
   WinkelOut.CalcState++;
70
   if(WinkelOut.CalcState > 4)
70
   if(WinkelOut.CalcState > 4)
71
    {
71
    {
72
//     WinkelOut.CalcState = 0; // in Uart.c
72
//     WinkelOut.CalcState = 0; // in Uart.c
73
     beeptime = 1000;
73
     beeptime = 1000;
74
    }
74
    }
75
   else Piep(WinkelOut.CalcState,150);
75
   else Piep(WinkelOut.CalcState,150);
76
  }
76
  }
77
  DebugOut.Analog[19] = WinkelOut.CalcState;
77
  DebugOut.Analog[19] = WinkelOut.CalcState;
78
}
78
}
79
 
79
 
80
 
80
 
81
void LipoDetection(unsigned char print)
81
void LipoDetection(unsigned char print)
82
{
82
{
83
        #define MAX_CELL_VOLTAGE 43 // max cell volatage for LiPO
83
        #define MAX_CELL_VOLTAGE 43 // max cell volatage for LiPO
84
        unsigned int timer, cells;
84
        unsigned int timer, cells;
85
        if(print) printf("\n\rBatt:");
85
        if(print) printf("\n\rBatt:");
86
        if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung
86
        if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung
87
        {
87
        {
88
                timer = SetDelay(500);
88
                timer = SetDelay(500);
89
                if(print) while (!CheckDelay(timer));
89
                if(print) while (!CheckDelay(timer));
90
                // up to 6s LiPo, less than 2s is technical impossible
90
                // up to 6s LiPo, less than 2s is technical impossible
91
                for(cells = 2; cells < 7; cells++)
91
                for(cells = 2; cells < 7; cells++)
92
                {
92
                {
93
                        if(UBat < cells * MAX_CELL_VOLTAGE) break;
93
                        if(UBat < cells * MAX_CELL_VOLTAGE) break;
94
                }
94
                }
95
 
95
 
96
                BattLowVoltageWarning = cells * EE_Parameter.UnterspannungsWarnung;
96
                BattLowVoltageWarning = cells * EE_Parameter.UnterspannungsWarnung;
97
                if(print)
97
                if(print)
98
                {
98
                {
99
                        Piep(cells, 200);
99
                        Piep(cells, 200);
100
                        printf(" %d Cells ", cells);
100
                        printf(" %d Cells ", cells);
101
                }
101
                }
102
        }
102
        }
103
        else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung;
103
        else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung;
104
        if(print) printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10);
104
        if(print) printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10);
105
}
105
}
106
 
106
 
107
//############################################################################
107
//############################################################################
108
//Hauptprogramm
108
//Hauptprogramm
109
int main (void)
109
int main (void)
110
//############################################################################
110
//############################################################################
111
{
111
{
112
        unsigned int timer,i,timer2 = 0, timerPolling;
112
        unsigned int timer,i,timer2 = 0, timerPolling;
113
 
113
 
114
    DDRB  = 0x00;
114
    DDRB  = 0x00;
115
    PORTB = 0x00;
115
    PORTB = 0x00;
116
    for(timer = 0; timer < 1000; timer++); // verzögern
116
    for(timer = 0; timer < 1000; timer++); // verzögern
117
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
117
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
118
        PlatinenVersion = 21;
118
        PlatinenVersion = 21;
119
#else
119
#else
120
        if(PINB & 0x01)
120
        if(PINB & 0x01)
121
     {
121
     {
122
      if(PINB & 0x02) PlatinenVersion = 13;
122
      if(PINB & 0x02) PlatinenVersion = 13;
123
       else           PlatinenVersion = 11;
123
       else           PlatinenVersion = 11;
124
     }
124
     }
125
    else
125
    else
126
     {
126
     {
127
      if(PINB & 0x02) PlatinenVersion = 20;
127
      if(PINB & 0x02) PlatinenVersion = 20;
128
       else           PlatinenVersion = 10;
128
       else           PlatinenVersion = 10;
129
     }
129
     }
130
#endif
130
#endif
131
    DDRC  = 0x81; // SCL
131
    DDRC  = 0x81; // SCL
132
    DDRC  |=0x40; // HEF4017 Reset
132
    DDRC  |=0x40; // HEF4017 Reset
133
    PORTC = 0xff; // Pullup SDA
133
    PORTC = 0xff; // Pullup SDA
134
    DDRB  = 0x1B; // LEDs und Druckoffset
134
    DDRB  = 0x1B; // LEDs und Druckoffset
135
    PORTB = 0x01; // LED_Rot
135
    PORTB = 0x01; // LED_Rot
136
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
136
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
137
        PORTD = 0x47; // LED
137
        PORTD = 0x47; // LED
138
    HEF4017R_ON;
138
    HEF4017R_ON;
139
    MCUSR &=~(1<<WDRF);
139
    MCUSR &=~(1<<WDRF);
140
    WDTCSR |= (1<<WDCE)|(1<<WDE);
140
    WDTCSR |= (1<<WDCE)|(1<<WDE);
141
    WDTCSR = 0;
141
    WDTCSR = 0;
142
 
142
 
143
    beeptime = 2500;
143
    beeptime = 2500;
144
        StickGier = 0; PPM_in[K_GAS] = 0; StickRoll = 0; StickNick = 0;
144
        StickGier = 0; PPM_in[K_GAS] = 0; StickRoll = 0; StickNick = 0;
145
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1220; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
145
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1220; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
146
    ROT_OFF;
146
    ROT_OFF;
147
 
147
 
148
    Timer_Init();
148
    Timer_Init();
149
        TIMER2_Init();
149
        TIMER2_Init();
150
        UART_Init();
150
        UART_Init();
151
    rc_sum_init();
151
    rc_sum_init();
152
        ADC_Init();
152
        ADC_Init();
153
        I2C_Init(1);
153
        I2C_Init(1);
154
        SPI_MasterInit();
154
        SPI_MasterInit();
155
        Capacity_Init();
155
        Capacity_Init();
156
        LIBFC_Init();
156
        LIBFC_Init();
157
        GRN_ON;
157
        GRN_ON;
158
    sei();
158
    sei();
159
        ParamSet_Init();
159
        ParamSet_Init();
160
 
160
 
161
 
161
 
162
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
162
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
163
// + Check connected BL-Ctrls
163
// + Check connected BL-Ctrls
164
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
164
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
165
        // Check connected BL-Ctrls
165
        // Check connected BL-Ctrls
166
        BLFlags |= BLFLAG_READ_VERSION;
166
        BLFlags |= BLFLAG_READ_VERSION;
167
        motor_read = 0;  // read the first I2C-Data
167
        motor_read = 0;  // read the first I2C-Data
168
        SendMotorData();
168
        SendMotorData();
169
        timer = SetDelay(500);
169
        timer = SetDelay(500);
170
        while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer
170
        while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer
171
 
171
 
172
    printf("\n\rFound BL-Ctrl: ");
172
    printf("\n\rFound BL-Ctrl: ");
173
    timer = SetDelay(4000);
173
    timer = SetDelay(4000);
174
        for(i=0; i < MAX_MOTORS; i++)
174
        for(i=0; i < MAX_MOTORS; i++)
175
        {
175
        {
176
                SendMotorData();
176
                SendMotorData();
177
                while(!(BLFlags & BLFLAG_TX_COMPLETE)  && !CheckDelay(timer)); //wait for complete transfer
177
                while(!(BLFlags & BLFLAG_TX_COMPLETE)  && !CheckDelay(timer)); //wait for complete transfer
178
                if(Mixer.Motor[i][0] > 0) // wait max 4 sec for the BL-Ctrls to wake up
178
                if(Mixer.Motor[i][0] > 0) // wait max 4 sec for the BL-Ctrls to wake up
179
                {
179
                {
180
                        while(!CheckDelay(timer) && !(Motor[i].State & MOTOR_STATE_PRESENT_MASK) )
180
                        while(!CheckDelay(timer) && !(Motor[i].State & MOTOR_STATE_PRESENT_MASK) )
181
                        {
181
                        {
182
                                SendMotorData();
182
                                SendMotorData();
183
                                while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer
183
                                while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer
184
                        }
184
                        }
185
                }
185
                }
186
                if(Motor[i].State & MOTOR_STATE_PRESENT_MASK)
186
                if(Motor[i].State & MOTOR_STATE_PRESENT_MASK)
187
                {
187
                {
188
                        printf("%d",i+1);
188
                        printf("%d",i+1);
189
                        FoundMotors++;
189
                        FoundMotors++;
190
//                      if(Motor[i].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) printf("(new) ");
190
//                      if(Motor[i].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) printf("(new) ");
191
                }
191
                }
192
        }
192
        }
193
        for(i=0; i < MAX_MOTORS; i++)
193
        for(i=0; i < MAX_MOTORS; i++)
194
        {
194
        {
195
                if(!(Motor[i].State & MOTOR_STATE_PRESENT_MASK) && Mixer.Motor[i][0] > 0)
195
                if(!(Motor[i].State & MOTOR_STATE_PRESENT_MASK) && Mixer.Motor[i][0] > 0)
196
                {
196
                {
197
                        printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1);
197
                        printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1);
198
                        ServoActive = 2; // just in case the FC would be used as camera-stabilizer
198
                        ServoActive = 2; // just in case the FC would be used as camera-stabilizer
199
                }
199
                }
200
                Motor[i].State &= ~MOTOR_STATE_ERROR_MASK; // clear error counter
200
                Motor[i].State &= ~MOTOR_STATE_ERROR_MASK; // clear error counter
201
        }
201
        }
202
        printf("\n\r===================================");
202
        printf("\n\r===================================");
203
 
203
 
204
    if(RequiredMotors < FoundMotors) VersionInfo.HardwareError[1] |= FC_ERROR1_MIXER;
204
    if(RequiredMotors < FoundMotors) VersionInfo.HardwareError[1] |= FC_ERROR1_MIXER;
205
 
205
 
206
        //if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
206
        //if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
207
        {
207
        {
208
                printf("\n\rCalibrating pressure sensor..");
208
                printf("\n\rCalibrating pressure sensor..");
209
                timer = SetDelay(1000);
209
                timer = SetDelay(1000);
210
                SucheLuftruckOffset();
210
                SucheLuftruckOffset();
211
                while (!CheckDelay(timer));
211
                while (!CheckDelay(timer));
212
                printf("OK\n\r");
212
                printf("OK\n\r");
213
        }
213
        }
214
 
214
 
215
        SetNeutral(0);
215
        SetNeutral(0);
216
 
216
 
217
        ROT_OFF;
217
        ROT_OFF;
218
 
218
 
219
    beeptime = 2000;
219
    beeptime = 2000;
220
    ExternControl.Digital[0] = 0x55;
220
    ExternControl.Digital[0] = 0x55;
221
 
221
 
222
 
222
 
223
        FlugMinuten = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES + 1);
223
        FlugMinuten = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES + 1);
224
        FlugMinutenGesamt = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL + 1);
224
        FlugMinutenGesamt = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL + 1);
225
 
225
 
226
        if((FlugMinutenGesamt == 0xFFFF) || (FlugMinuten == 0xFFFF))
226
        if((FlugMinutenGesamt == 0xFFFF) || (FlugMinuten == 0xFFFF))
227
        {
227
        {
228
                FlugMinuten = 0;
228
                FlugMinuten = 0;
229
                FlugMinutenGesamt = 0;
229
                FlugMinutenGesamt = 0;
230
        }
230
        }
231
    printf("\n\rFlight-time %u min  Total:%u min", FlugMinuten, FlugMinutenGesamt);
231
    printf("\n\rFlight-time %u min  Total:%u min", FlugMinuten, FlugMinutenGesamt);
232
 
232
 
233
        printf("\n\rControl: ");
233
        printf("\n\rControl: ");
234
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
234
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
235
        else printf("Normal (ACC-Mode)");
235
        else printf("Normal (ACC-Mode)");
236
 
236
 
237
    LcdClear();
237
    LcdClear();
238
    I2CTimeout = 5000;
238
    I2CTimeout = 5000;
239
    WinkelOut.Orientation = 1;
239
    WinkelOut.Orientation = 1;
240
    LipoDetection(1);
240
    LipoDetection(1);
241
 
241
 
242
        LIBFC_ReceiverInit(EE_Parameter.Receiver);
242
        LIBFC_ReceiverInit(EE_Parameter.Receiver);
243
 
243
 
244
        printf("\n\r===================================\n\r");
244
        printf("\n\r===================================\n\r");
245
        //SpektrumBinding();
245
        //SpektrumBinding();
246
    timer = SetDelay(2000);
246
    timer = SetDelay(2000);
247
        timerPolling = SetDelay(250);
247
        timerPolling = SetDelay(250);
248
 
248
 
249
        Debug(ANSI_CLEAR "FC-Start!\n\rFlugzeit: %d min", FlugMinutenGesamt);   // Note: this won't waste flash memory, if #DEBUG is not active
249
        Debug(ANSI_CLEAR "FC-Start!\n\rFlugzeit: %d min", FlugMinutenGesamt);   // Note: this won't waste flash memory, if #DEBUG is not active
250
    DebugOut.Status[0] = 0x01 | 0x02;
250
    DebugOut.Status[0] = 0x01 | 0x02;
251
        JetiBeep = 0;
251
        JetiBeep = 0;
252
    if(EE_Parameter.ExtraConfig & CFG_NO_RCOFF_BEEPING)   DisableRcOffBeeping = 1;
252
    if(EE_Parameter.ExtraConfig & CFG_NO_RCOFF_BEEPING)   DisableRcOffBeeping = 1;
253
        while (1)
253
        while (1)
254
        {
254
        {
255
        if(ReceiverUpdateModeActive) while (1) PORTC &= ~(1<<7); // Beeper off
255
        if(ReceiverUpdateModeActive) while (1) PORTC &= ~(1<<7); // Beeper off
256
 
256
 
257
//GRN_ON;
257
//GRN_ON;
258
        if(UpdateMotor && AdReady)      // ReglerIntervall
258
        if(UpdateMotor && AdReady)      // ReglerIntervall
259
            {
259
            {
260
//GRN_OFF;
260
//GRN_OFF;
261
                        UpdateMotor=0;    
261
                        UpdateMotor=0;    
262
            if(WinkelOut.CalcState) CalMk3Mag();
262
            if(WinkelOut.CalcState) CalMk3Mag();
263
            else  MotorRegler();
263
            else  MotorRegler();
264
                        SendMotorData();
264
                        SendMotorData();
265
            ROT_OFF;
265
            ROT_OFF;
266
            if(SenderOkay)  { SenderOkay--; /*VersionInfo.HardwareError[1] &= ~FC_ERROR1_PPM;*/ }
266
            if(SenderOkay)  { SenderOkay--; /*VersionInfo.HardwareError[1] &= ~FC_ERROR1_PPM;*/ }
267
                        else
267
                        else
268
                        {
268
                        {
269
                                TIMSK1 |= _BV(ICIE1); // enable PPM-Input
269
                                TIMSK1 |= _BV(ICIE1); // enable PPM-Input
270
                                PPM_in[0] = 0; // set RSSI to zero on data timeout
270
                                PPM_in[0] = 0; // set RSSI to zero on data timeout
271
                                VersionInfo.HardwareError[1] |= FC_ERROR1_PPM;
271
                                VersionInfo.HardwareError[1] |= FC_ERROR1_PPM;
272
                        }
272
                        }
273
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
273
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
274
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160;
274
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160;
275
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101;
275
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101;
276
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
276
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
277
            if(!--I2CTimeout || MissingMotor)
277
            if(!--I2CTimeout || MissingMotor)
278
                {
278
                {
279
                  if(!I2CTimeout)
279
                  if(!I2CTimeout)
280
                                   {
280
                                   {
281
                                    I2C_Reset();
281
                                    I2C_Reset();
282
                    I2CTimeout = 5;
282
                    I2CTimeout = 5;
283
                                        DebugOut.Analog[28]++; // I2C-Error
283
                                        DebugOut.Analog[28]++; // I2C-Error
284
                                        VersionInfo.HardwareError[1] |= FC_ERROR1_I2C;
284
                                        VersionInfo.HardwareError[1] |= FC_ERROR1_I2C;
285
                                        DebugOut.Status[1] |= 0x02; // BL-Error-Status
285
                                        DebugOut.Status[1] |= 0x02; // BL-Error-Status
286
                                   }
286
                                   }
287
                  if((BeepMuster == 0xffff) && MotorenEin)
287
                  if((BeepMuster == 0xffff) && MotorenEin)
288
                   {
288
                   {
289
                    beeptime = 25000;
289
                    beeptime = 25000;
290
                    BeepMuster = 0x0080;
290
                    BeepMuster = 0x0080;
291
                   }
291
                   }
292
                }
292
                }
293
            else
293
            else
294
                {
294
                {
295
                 ROT_OFF;
295
                 ROT_OFF;
296
                }
296
                }
297
                  LIBFC_Polling();
297
                  LIBFC_Polling();
298
 
298
 
299
          if(!UpdateMotor)
299
          if(!UpdateMotor)
-
 
300
                  {
-
 
301
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
-
 
302
                   if(NewSBusData) ProcessSBus();  
-
 
303
                   else
-
 
304
#endif
300
                   {
305
                   {
301
                        if(CalculateServoSignals) CalculateServo();
306
                        if(CalculateServoSignals) CalculateServo();
302
                        DatenUebertragung();
307
                        DatenUebertragung();
303
                        BearbeiteRxDaten();
308
                        BearbeiteRxDaten();
304
                        if(CheckDelay(timer))
309
                        if(CheckDelay(timer))
305
                        {
310
                        {
306
                                static unsigned char second;
311
                                static unsigned char second;
307
                                timer += 20; // 20 ms interval
312
                                timer += 20; // 20 ms interval
308
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
313
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
309
                                if(EE_Parameter.Receiver == RECEIVER_HOTT) HoTT_Menu();
314
                                if(EE_Parameter.Receiver == RECEIVER_HOTT) HoTT_Menu();
310
#endif
315
#endif
311
                                if(MissingMotor)
316
                                if(MissingMotor)
312
                                 {
317
                                 {
313
                                  VersionInfo.HardwareError[1] |= FC_ERROR1_BL_MISSING;
318
                                  VersionInfo.HardwareError[1] |= FC_ERROR1_BL_MISSING;
314
                                  DebugOut.Status[1] |= 0x02; // BL-Error-Status
319
                                  DebugOut.Status[1] |= 0x02; // BL-Error-Status
315
                                 }
320
                                 }
316
                                 else
321
                                 else
317
                                 {
322
                                 {
318
                                   if(!beeptime)
323
                                   if(!beeptime)
319
                                    {
324
                                    {
320
                                     if(I2CTimeout > 6) DebugOut.Status[1] &= ~0x02; // BL-Error-Status
325
                                     if(I2CTimeout > 6) DebugOut.Status[1] &= ~0x02; // BL-Error-Status
321
                                        }
326
                                        }
322
                                 }
327
                                 }
323
                                if(SenderOkay && DisableRcOffBeeping) { DisableRcOffBeeping = 0; beeptime = 5000;};
328
                                if(SenderOkay && DisableRcOffBeeping) { DisableRcOffBeeping = 0; beeptime = 5000;};
324
                                if(PcZugriff) PcZugriff--;
329
                                if(PcZugriff) PcZugriff--;
325
                                else
330
                                else
326
                                {
331
                                {
327
                                        ExternControl.Config = 0;
332
                                        ExternControl.Config = 0;
328
                                        ExternStickNick = 0;
333
                                        ExternStickNick = 0;
329
                                        ExternStickRoll = 0;
334
                                        ExternStickRoll = 0;
330
                                        ExternStickGier = 0;
335
                                        ExternStickGier = 0;
331
                                        if(!SenderOkay)
336
                                        if(!SenderOkay)
332
                                        {
337
                                        {
333
                                          if(BeepMuster == 0xffff && DisableRcOffBeeping != 2)  
338
                                          if(BeepMuster == 0xffff && DisableRcOffBeeping != 2)  
334
                                          {
339
                                          {
335
                                                  beeptime = 15000;
340
                                                  beeptime = 15000;
336
                                                  BeepMuster = 0x0c00;
341
                                                  BeepMuster = 0x0c00;
337
                                                  if(DisableRcOffBeeping) DisableRcOffBeeping = 2;
342
                                                  if(DisableRcOffBeeping) DisableRcOffBeeping = 2;
338
                                          }
343
                                          }
339
                                        }
344
                                        }
340
                                }
345
                                }
341
                                if(NaviDataOkay > 200)
346
                                if(NaviDataOkay > 200)
342
                                {
347
                                {
343
                                        NaviDataOkay--;
348
                                        NaviDataOkay--;
344
                                        VersionInfo.HardwareError[1] &= ~FC_ERROR1_SPI_RX;
349
                                        VersionInfo.HardwareError[1] &= ~FC_ERROR1_SPI_RX;
345
                                }
350
                                }
346
                                else
351
                                else
347
                                {
352
                                {
348
                                        if(NC_Version.Compatible)
353
                                        if(NC_Version.Compatible)
349
                                         {
354
                                         {
350
                                           VersionInfo.HardwareError[1] |= FC_ERROR1_SPI_RX;
355
                                           VersionInfo.HardwareError[1] |= FC_ERROR1_SPI_RX;
351
                                           NC_ErrorCode = 9; // "ERR: no NC communication"
356
                                           NC_ErrorCode = 9; // "ERR: no NC communication"
352
                       if(BeepMuster == 0xffff && MotorenEin)
357
                       if(BeepMuster == 0xffff && MotorenEin)
353
                                                {
358
                                                {
354
                                                        beeptime = 15000;
359
                                                        beeptime = 15000;
355
                                                        BeepMuster = 0xA800;
360
                                                        BeepMuster = 0xA800;
356
                                                }
361
                                                }
357
                                         }
362
                                         }
358
                                        GPS_Nick = 0;
363
                                        GPS_Nick = 0;
359
                                        GPS_Roll = 0;
364
                                        GPS_Roll = 0;
360
                                        GPS_Aid_StickMultiplikator = 0;
365
                                        GPS_Aid_StickMultiplikator = 0;
361
                                        GPSInfo.Flags = 0;
366
                                        GPSInfo.Flags = 0;
362
                                        FromNaviCtrl.AccErrorN = 0;
367
                                        FromNaviCtrl.AccErrorN = 0;
363
                                        FromNaviCtrl.AccErrorR = 0;
368
                                        FromNaviCtrl.AccErrorR = 0;
364
                    FromNaviCtrl.CompassValue = -1;
369
                    FromNaviCtrl.CompassValue = -1;
365
                    NaviDataOkay = 0;
370
                    NaviDataOkay = 0;
366
                                }
371
                                }
367
                           if(UBat < BattLowVoltageWarning)
372
                           if(UBat < BattLowVoltageWarning)
368
                                {
373
                                {
369
                                        FC_StatusFlags |= FC_STATUS_LOWBAT;
374
                                        FC_StatusFlags |= FC_STATUS_LOWBAT;
370
                                        if(BeepMuster == 0xffff)
375
                                        if(BeepMuster == 0xffff)
371
                                        {
376
                                        {
372
                                                beeptime = 6000;
377
                                                beeptime = 6000;
373
                                                BeepMuster = 0x0300;
378
                                                BeepMuster = 0x0300;
374
                                        }
379
                                        }
375
                                }
380
                                }
376
                                else if(!beeptime) FC_StatusFlags &= ~FC_STATUS_LOWBAT;
381
                                else if(!beeptime) FC_StatusFlags &= ~FC_STATUS_LOWBAT;
377
 
382
 
378
                                SPI_StartTransmitPacket();
383
                                SPI_StartTransmitPacket();
379
                                SendSPI = 4;
384
                                SendSPI = 4;
380
                                // +++++++++++++++++++++++++++++++++
385
                                // +++++++++++++++++++++++++++++++++
381
                                // Sekundentakt
386
                                // Sekundentakt
382
                if(++second == 49)
387
                if(++second == 49)
383
                                 {
388
                                 {
384
                                   second = 0;
389
                                   second = 0;
385
                                   if(FC_StatusFlags & FC_STATUS_FLY) FlugSekunden++;
390
                                   if(FC_StatusFlags & FC_STATUS_FLY) FlugSekunden++;
386
                                   else
391
                                   else
387
                                    {
392
                                    {
388
                                         timer2 = 1450; // 0,5 Minuten aufrunden
393
                                         timer2 = 1450; // 0,5 Minuten aufrunden
389
                                         if(StartLuftdruck < Luftdruck) StartLuftdruck++;
394
                                         if(StartLuftdruck < Luftdruck) StartLuftdruck++;
390
                                         else
395
                                         else
391
                                         if(StartLuftdruck > Luftdruck) StartLuftdruck--;
396
                                         if(StartLuftdruck > Luftdruck) StartLuftdruck--;
392
                                        }
397
                                        }
393
                                 }
398
                                 }
394
                                // +++++++++++++++++++++++++++++++++
399
                                // +++++++++++++++++++++++++++++++++
395
                                if(++timer2 == 2930)  // eine Minute
400
                                if(++timer2 == 2930)  // eine Minute
396
                                 {
401
                                 {
397
                                   timer2 = 0;
402
                                   timer2 = 0;
398
                   FlugMinuten++;
403
                   FlugMinuten++;
399
                       FlugMinutenGesamt++;
404
                       FlugMinutenGesamt++;
400
                   SetParamByte(PID_FLIGHT_MINUTES,FlugMinuten / 256);
405
                   SetParamByte(PID_FLIGHT_MINUTES,FlugMinuten / 256);
401
                   SetParamByte(PID_FLIGHT_MINUTES+1,FlugMinuten % 256);
406
                   SetParamByte(PID_FLIGHT_MINUTES+1,FlugMinuten % 256);
402
                   SetParamByte(PID_FLIGHT_MINUTES_TOTAL,FlugMinutenGesamt / 256);
407
                   SetParamByte(PID_FLIGHT_MINUTES_TOTAL,FlugMinutenGesamt / 256);
403
                   SetParamByte(PID_FLIGHT_MINUTES_TOTAL+1,FlugMinutenGesamt % 256);
408
                   SetParamByte(PID_FLIGHT_MINUTES_TOTAL+1,FlugMinutenGesamt % 256);
404
                                   timer = SetDelay(20); // falls "timer += 20;" mal nicht geht
409
                                   timer = SetDelay(20); // falls "timer += 20;" mal nicht geht
405
                             }
410
                             }
406
                        }
411
                        }
407
           LED_Update();
412
           LED_Update();
408
           Capacity_Update();
413
           Capacity_Update();
409
           } //else DebugOut.Analog[26]++;
414
           } //else DebugOut.Analog[26]++;
410
          }
415
          }
-
 
416
         }
411
     if(!SendSPI) { SPI_TransmitByte(); }
417
         if(!SendSPI) { SPI_TransmitByte(); }
412
    }
418
    }
413
 return (1);
419
 return (1);
414
}
420
}
415
//DebugOut.Analog[16]
421
//DebugOut.Analog[16]
416
 
422
 
417
 
423