Subversion Repositories FlightCtrl

Rev

Rev 1813 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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