Subversion Repositories FlightCtrl

Rev

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

Rev 1603 Rev 1622
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
 
53
 
54
 
54
 
55
unsigned char PlatinenVersion = 10;
55
unsigned char PlatinenVersion = 10;
56
unsigned char SendVersionToNavi = 1;
56
unsigned char SendVersionToNavi = 1;
57
unsigned char BattLowVoltageWarning = 94;
57
unsigned char BattLowVoltageWarning = 94;
58
unsigned int FlugMinuten = 0,FlugMinutenGesamt = 0;
58
unsigned int FlugMinuten = 0,FlugMinutenGesamt = 0;
59
unsigned int FlugSekunden = 0;
59
unsigned int FlugSekunden = 0;
60
pVoidFnct_pVoidFnctChar_const_fmt _printf_P;
60
pVoidFnct_pVoidFnctChar_const_fmt _printf_P;
61
 
-
 
62
// -- Parametersatz aus EEPROM lesen ---
-
 
63
// number [1..5]
-
 
64
void ReadParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
-
 
65
{
-
 
66
   if((number > 5)||(number < 1)) number = 3;
-
 
67
   eeprom_read_block(buffer, (void*)(EEPROM_ADR_PARAM_BEGIN + length * (number - 1)), length);
-
 
68
   LED_Init();
-
 
69
}
-
 
70
 
-
 
71
// -- Parametersatz ins EEPROM schreiben ---
-
 
72
// number [1..5]
-
 
73
void WriteParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
-
 
74
{
-
 
75
   if(number > 5) number = 5;
-
 
76
   if(number < 1) return;
-
 
77
   eeprom_write_block(buffer, (void*)(EEPROM_ADR_PARAM_BEGIN + length * (number - 1)), length);
-
 
78
   eeprom_write_byte((unsigned char*)(EEPROM_ADR_PARAM_LENGTH), length); // Länge der Datensätze merken
-
 
79
   eeprom_write_block(buffer, (void*)(EEPROM_ADR_CHANNELS), 12); // 12 Kanäle merken
-
 
80
   SetActiveParamSetNumber(number);
-
 
81
   LED_Init();
-
 
82
}
-
 
83
 
-
 
84
unsigned char GetActiveParamSetNumber(void)
-
 
85
{
-
 
86
        unsigned char set;
-
 
87
        set = eeprom_read_byte((unsigned char*)(EEPROM_ADR_ACTIVE_SET));
-
 
88
        if((set > 5) || (set < 1))
-
 
89
        {
-
 
90
                set = 3;
-
 
91
                SetActiveParamSetNumber(set);                           // diesen Parametersatz als aktuell merken
-
 
92
        }
-
 
93
        return(set);
-
 
94
}
-
 
95
 
-
 
96
void SetActiveParamSetNumber(unsigned char number)
-
 
97
{
-
 
98
        if(number > 5) number = 5;
-
 
99
        if(number < 1) return;
-
 
100
        eeprom_write_byte((unsigned char*)(EEPROM_ADR_ACTIVE_SET), number);                             // diesen Parametersatz als aktuell merken
-
 
101
}
-
 
102
 
61
 
103
 
62
 
104
void CalMk3Mag(void)
63
void CalMk3Mag(void)
105
{
64
{
106
 static unsigned char stick = 1;
65
 static unsigned char stick = 1;
107
 
66
 
108
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
67
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
109
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
68
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
110
  {
69
  {
111
   stick = 1;
70
   stick = 1;
112
   WinkelOut.CalcState++;
71
   WinkelOut.CalcState++;
113
   if(WinkelOut.CalcState > 4)
72
   if(WinkelOut.CalcState > 4)
114
    {
73
    {
115
//     WinkelOut.CalcState = 0; // in Uart.c
74
//     WinkelOut.CalcState = 0; // in Uart.c
116
     beeptime = 1000;
75
     beeptime = 1000;
117
    }
76
    }
118
   else Piep(WinkelOut.CalcState,150);
77
   else Piep(WinkelOut.CalcState,150);
119
  }
78
  }
120
  DebugOut.Analog[19] = WinkelOut.CalcState;
79
  DebugOut.Analog[19] = WinkelOut.CalcState;
121
}
80
}
122
 
81
 
123
 
82
 
124
 
83
 
125
void LipoDetection(unsigned char print)
84
void LipoDetection(unsigned char print)
126
{
85
{
127
   unsigned int timer;
86
   unsigned int timer;
128
   if(print) printf("\n\rBatt:");
87
   if(print) printf("\n\rBatt:");
129
   if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung
88
   if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung
130
    {
89
    {
131
         timer = SetDelay(500);
90
         timer = SetDelay(500);
132
         if(print) while (!CheckDelay(timer));
91
         if(print) while (!CheckDelay(timer));
133
         if(UBat < 130)
92
         if(UBat < 130)
134
          {
93
          {
135
           BattLowVoltageWarning = 3 * EE_Parameter.UnterspannungsWarnung;
94
           BattLowVoltageWarning = 3 * EE_Parameter.UnterspannungsWarnung;
136
           if(print)
95
           if(print)
137
            {
96
            {
138
                 Piep(3,200);
97
                 Piep(3,200);
139
             printf(" 3 Cells  ");
98
             printf(" 3 Cells  ");
140
                }
99
                }
141
          }
100
          }
142
         else
101
         else
143
          {
102
          {
144
           BattLowVoltageWarning = 4 * EE_Parameter.UnterspannungsWarnung;
103
           BattLowVoltageWarning = 4 * EE_Parameter.UnterspannungsWarnung;
145
           if(print)
104
           if(print)
146
            {
105
            {
147
                 Piep(4,200);
106
                 Piep(4,200);
148
             printf(" 4 Cells  ");
107
             printf(" 4 Cells  ");
149
                }
108
                }
150
          }
109
          }
151
    }
110
    }
152
    else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung;
111
    else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung;
153
//      if(BattLowVoltageWarning < 93) BattLowVoltageWarning = 93;
112
//      if(BattLowVoltageWarning < 93) BattLowVoltageWarning = 93;
154
   if(print) printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10);
113
   if(print) printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10);
155
}
114
}
156
 
115
 
157
//############################################################################
116
//############################################################################
158
//Hauptprogramm
117
//Hauptprogramm
159
int main (void)
118
int main (void)
160
//############################################################################
119
//############################################################################
161
{
120
{
162
        unsigned int timer,i,timer2 = 0, timerPolling;
121
        unsigned int timer,i,timer2 = 0, timerPolling;
163
        unsigned char RequiredMotors = 0;
-
 
164
 
122
 
165
    DDRB  = 0x00;
123
    DDRB  = 0x00;
166
    PORTB = 0x00;
124
    PORTB = 0x00;
167
    for(timer = 0; timer < 1000; timer++); // verzögern
125
    for(timer = 0; timer < 1000; timer++); // verzögern
168
    if(PINB & 0x01)
126
    if(PINB & 0x01)
169
     {
127
     {
170
      if(PINB & 0x02) PlatinenVersion = 13;
128
      if(PINB & 0x02) PlatinenVersion = 13;
171
       else           PlatinenVersion = 11;
129
       else           PlatinenVersion = 11;
172
     }
130
     }
173
    else
131
    else
174
     {
132
     {
175
      if(PINB & 0x02) PlatinenVersion = 20;
133
      if(PINB & 0x02) PlatinenVersion = 20;
176
       else           PlatinenVersion = 10;
134
       else           PlatinenVersion = 10;
177
     }
135
     }
178
 
136
 
179
    DDRC  = 0x81; // SCL
137
    DDRC  = 0x81; // SCL
180
    DDRC  |=0x40; // HEF4017 Reset
138
    DDRC  |=0x40; // HEF4017 Reset
181
    PORTC = 0xff; // Pullup SDA
139
    PORTC = 0xff; // Pullup SDA
182
    DDRB  = 0x1B; // LEDs und Druckoffset
140
    DDRB  = 0x1B; // LEDs und Druckoffset
183
    PORTB = 0x01; // LED_Rot
141
    PORTB = 0x01; // LED_Rot
184
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
142
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
185
        PORTD = 0x47; // LED
143
        PORTD = 0x47; // LED
186
    HEF4017R_ON;
144
    HEF4017R_ON;
187
    MCUSR &=~(1<<WDRF);
145
    MCUSR &=~(1<<WDRF);
188
    WDTCSR |= (1<<WDCE)|(1<<WDE);
146
    WDTCSR |= (1<<WDCE)|(1<<WDE);
189
    WDTCSR = 0;
147
    WDTCSR = 0;
190
 
148
 
191
    beeptime = 2000;
149
    beeptime = 2000;
192
        StickGier = 0; PPM_in[K_GAS] = 0;StickRoll = 0; StickNick = 0;
150
        StickGier = 0; PPM_in[K_GAS] = 0;StickRoll = 0; StickNick = 0;
193
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1160; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
151
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1160; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
194
    ROT_OFF;
152
    ROT_OFF;
195
 
153
 
196
    Timer_Init();
154
    Timer_Init();
197
        TIMER2_Init();
155
        TIMER2_Init();
198
        UART_Init();
156
        UART_Init();
199
    rc_sum_init();
157
    rc_sum_init();
200
        ADC_Init();
158
        ADC_Init();
201
        i2c_init();
159
        i2c_init();
202
        SPI_MasterInit();
160
        SPI_MasterInit();
203
        Capacity_Init();
161
        Capacity_Init();
204
        LIBFC_Init();
162
        LIBFC_Init();
205
        GRN_ON;
163
        GRN_ON;
206
    sei();
164
    sei();
207
 
-
 
208
        ReadParameterSet(3, (unsigned char *) &EE_Parameter.Kanalbelegung[0], 13); // read only the first bytes
165
        ParamSet_Init();
209
 
-
 
210
 
-
 
211
    if((eeprom_read_byte((unsigned char*)(EEPROM_ADR_MIXER_TABLE)) == MIXER_REVISION) && // Check Revision in the first Byte
-
 
212
       (eeprom_read_byte((unsigned char*)(EEPROM_ADR_VALID)) != 0xff))                   // Settings reset via Koptertool
-
 
213
        {
-
 
214
     unsigned char i;
-
 
215
     eeprom_read_block(&Mixer, (unsigned char*)(EEPROM_ADR_MIXER_TABLE), sizeof(Mixer));
-
 
216
     for(i=0; i<16;i++) { if(Mixer.Motor[i][0] > 0) RequiredMotors++;}
-
 
217
        }
-
 
218
        else // default
-
 
219
        {
-
 
220
     unsigned char i;
-
 
221
         printf("\n\rGenerating default Mixer Table");
-
 
222
         for(i=0; i<16;i++) { Mixer.Motor[i][0] = 0;Mixer.Motor[i][1] = 0;Mixer.Motor[i][2] = 0;Mixer.Motor[i][3] = 0;};
-
 
223
     // default = Quadro
-
 
224
     Mixer.Motor[0][0] = 64; Mixer.Motor[0][1] = +64; Mixer.Motor[0][2] =   0; Mixer.Motor[0][3] = +64;
-
 
225
     Mixer.Motor[1][0] = 64; Mixer.Motor[1][1] = -64; Mixer.Motor[1][2] =   0; Mixer.Motor[1][3] = +64;
-
 
226
     Mixer.Motor[2][0] = 64; Mixer.Motor[2][1] =   0; Mixer.Motor[2][2] = -64; Mixer.Motor[2][3] = -64;
-
 
227
     Mixer.Motor[3][0] = 64; Mixer.Motor[3][1] =   0; Mixer.Motor[3][2] = +64; Mixer.Motor[3][3] = -64;
-
 
228
         Mixer.Revision = MIXER_REVISION;
-
 
229
     memcpy(Mixer.Name, "Quadro\0", 11);
-
 
230
     eeprom_write_block(&Mixer, (void*)(EEPROM_ADR_MIXER_TABLE), sizeof(Mixer));
-
 
231
    }
-
 
232
    printf("\n\rMixer-Config: '%s' (%u Motors)",Mixer.Name,RequiredMotors);
166
 
233
 
167
 
234
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
168
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
235
// + Check connected BL-Ctrls
169
// + Check connected BL-Ctrls
236
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
170
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
237
    motorread = 0;   UpdateMotor = 0;   SendMotorData(); while(!UpdateMotor); motorread = 0;  // read the first I2C-Data
171
    motorread = 0;   UpdateMotor = 0;   SendMotorData(); while(!UpdateMotor); motorread = 0;  // read the first I2C-Data
238
        printf("\n\rFound BL-Ctrl: ");
172
        printf("\n\rFound BL-Ctrl: ");
239
    timer = SetDelay(4000);
173
    timer = SetDelay(4000);
240
 
174
 
241
        for(i=0; i < MAX_MOTORS; i++)
175
        for(i=0; i < MAX_MOTORS; i++)
242
         {
176
        {
243
          UpdateMotor = 0;
177
                UpdateMotor = 0;
244
      SendMotorData();
178
                SendMotorData();
245
          while(!UpdateMotor);
179
                while(!UpdateMotor);
246
      if(Mixer.Motor[i][0] > 0) // wait max 4 sec for the BL-Ctrls to wake up
180
                if(Mixer.Motor[i][0] > 0) // wait max 4 sec for the BL-Ctrls to wake up
247
        {
181
                {
248
             while(!CheckDelay(timer) && !(Motor[i].State & MOTOR_STATE_PRESENT_MASK) ) {UpdateMotor = 0; SendMotorData(); while(!UpdateMotor);};
182
                        while(!CheckDelay(timer) && !(Motor[i].State & MOTOR_STATE_PRESENT_MASK) ) {UpdateMotor = 0; SendMotorData(); while(!UpdateMotor);};
249
                }
183
                }
250
          if(Motor[i].State & MOTOR_STATE_PRESENT_MASK) printf("%d ",i+1);
184
                if(Motor[i].State & MOTOR_STATE_PRESENT_MASK) printf("%d ",i+1);
251
     }
185
        }
252
        for(i=0; i < MAX_MOTORS; i++)
186
        for(i=0; i < MAX_MOTORS; i++)
253
         {
-
 
254
          if(!(Motor[i].State & MOTOR_STATE_PRESENT_MASK) && Mixer.Motor[i][0] > 0)
-
 
255
           {
-
 
256
            printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1);
-
 
257
                ServoActive = 1; // just in case the FC would be used as camera-stabilizer
-
 
258
           }
-
 
259
          Motor[i].State &= ~MOTOR_STATE_ERROR_MASK; // clear error counter
-
 
260
     }
-
 
261
        printf("\n\r===================================");
-
 
262
    SendMotorData();
-
 
263
//printf("\n size: %u",STRUCT_PARAM_LAENGE);
-
 
264
 
-
 
265
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
266
// + Check Settings
-
 
267
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
268
    if(eeprom_read_byte((unsigned char*)(EEPROM_ADR_VALID)) != EE_DATENREVISION)
-
 
269
        {
187
        {
270
          DefaultKonstanten1();
-
 
271
          printf("\n\rInit. EEPROM");
-
 
272
          for (unsigned char i=1;i<6;i++)
-
 
273
      {
-
 
274
       if(i==2) DefaultKonstanten2(); // Kamera
-
 
275
       if(i==3) DefaultKonstanten3(); // Beginner
188
                if(!(Motor[i].State & MOTOR_STATE_PRESENT_MASK) && Mixer.Motor[i][0] > 0)
276
       if(i>3)  DefaultKonstanten2(); // Kamera
-
 
277
           if(PlatinenVersion >= 20)
-
 
278
            {
189
                {
279
                 EE_Parameter.Gyro_D = 5;
-
 
280
                 EE_Parameter.Driftkomp = 0;
-
 
281
                 EE_Parameter.GyroAccFaktor = 27;
-
 
282
         EE_Parameter.WinkelUmschlagNick = 78;
190
                        printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1);
283
         EE_Parameter.WinkelUmschlagRoll = 78;
191
                        ServoActive = 1; // just in case the FC would be used as camera-stabilizer
284
                }
192
                }
285
    // valid Stick-Settings?
-
 
286
           if(eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS)) < 12 && eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+1)) < 12 && eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+2)) < 12 && eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+3)) < 12)
193
                Motor[i].State &= ~MOTOR_STATE_ERROR_MASK; // clear error counter
287
            {
-
 
288
         EE_Parameter.Kanalbelegung[0] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+0));
-
 
289
         EE_Parameter.Kanalbelegung[1] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+1));
-
 
290
         EE_Parameter.Kanalbelegung[2] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+2));
-
 
291
         EE_Parameter.Kanalbelegung[3] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+3));
-
 
292
         EE_Parameter.Kanalbelegung[4] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+4));
-
 
293
         EE_Parameter.Kanalbelegung[5] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+5));
-
 
294
         EE_Parameter.Kanalbelegung[6] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+6));
-
 
295
         EE_Parameter.Kanalbelegung[7] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+7));
-
 
296
 
-
 
297
         if(eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+8)) < 0xFF)
-
 
298
                  {
-
 
299
                   EE_Parameter.Kanalbelegung[K_POTI5] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+8));
-
 
300
           EE_Parameter.Kanalbelegung[K_POTI6] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+9));
-
 
301
           EE_Parameter.Kanalbelegung[K_POTI7] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+10));
-
 
302
           EE_Parameter.Kanalbelegung[K_POTI8] = eeprom_read_byte((unsigned char*)(EEPROM_ADR_CHANNELS+11));
-
 
303
                  }
-
 
304
                  else
-
 
305
                  {
-
 
306
           EE_Parameter.Kanalbelegung[K_POTI5] = 9;
-
 
307
           EE_Parameter.Kanalbelegung[K_POTI6] = 10;
-
 
308
           EE_Parameter.Kanalbelegung[K_POTI7] = 11;
-
 
309
           EE_Parameter.Kanalbelegung[K_POTI8] = 12;
-
 
310
                  }
-
 
311
         if(i==1) printf(": Generating Default-Parameter using old Stick Settings");
-
 
312
                } else DefaultStickMapping();
-
 
313
       WriteParameterSet(i, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
-
 
314
      }
-
 
315
          SetActiveParamSetNumber(3); // default-Setting
-
 
316
          eeprom_write_byte((unsigned char*)(EEPROM_ADR_VALID), EE_DATENREVISION);
-
 
317
        }
194
        }
318
 
-
 
319
    FlugMinuten = (int)eeprom_read_byte((unsigned char*)(EEPROM_ADR_MINUTES2)) * 256 + (int)eeprom_read_byte((unsigned char*)(EEPROM_ADR_MINUTES2+1));
-
 
320
    FlugMinutenGesamt = (int)eeprom_read_byte((unsigned char*)(EEPROM_ADR_MINUTES)) * 256 + (int)eeprom_read_byte((unsigned char*)(EEPROM_ADR_MINUTES+1));
-
 
321
    if(FlugMinutenGesamt == 0xffff || FlugMinuten == 0xffff)
195
        printf("\n\r===================================");
322
         {
-
 
323
          FlugMinuten = 0;
196
    SendMotorData();
324
          FlugMinutenGesamt = 0;
-
 
325
         }
-
 
326
    printf("\n\rFlight-time %u min  Total:%u min" ,FlugMinuten,FlugMinutenGesamt);
-
 
327
 
-
 
328
    if(eeprom_read_byte((unsigned char*)(EEPROM_ADR_ACC_NICK)) > 4)
-
 
329
     {
-
 
330
       printf("\n\rACC not calibrated !");
-
 
331
     }
-
 
332
 
-
 
333
        ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
-
 
334
    printf("\n\rUsing parameterset %d", GetActiveParamSetNumber());
-
 
335
 
-
 
336
 
197
 
337
        //if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
198
        //if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
338
         {
199
        {
339
           printf("\n\rCalibrating pressure sensor..");
200
                printf("\n\rCalibrating pressure sensor..");
340
           timer = SetDelay(1000);
201
                timer = SetDelay(1000);
341
       SucheLuftruckOffset();
202
                SucheLuftruckOffset();
342
           while (!CheckDelay(timer));
203
                while (!CheckDelay(timer));
343
       printf("OK\n\r");
204
                printf("OK\n\r");
344
        }
205
        }
345
 
206
 
346
        SetNeutral();
207
        SetNeutral(0);
347
 
208
 
348
        ROT_OFF;
209
        ROT_OFF;
349
 
210
 
350
    beeptime = 2000;
211
    beeptime = 2000;
351
    ExternControl.Digital[0] = 0x55;
212
    ExternControl.Digital[0] = 0x55;
352
 
213
 
-
 
214
 
-
 
215
        FlugMinuten = GetParamWord(PID_FLIGHT_MINUTES);
-
 
216
        FlugMinutenGesamt = GetParamWord(PID_FLIGHT_MINUTES_TOTAL);
-
 
217
 
-
 
218
        if( (FlugMinutenGesamt == 0xFFFF) || (FlugMinuten == 0xFFFF) )
-
 
219
        {
-
 
220
                FlugMinuten = 0;
-
 
221
                FlugMinutenGesamt = 0;
-
 
222
        }
-
 
223
    printf("\n\rFlight-time %u min  Total:%u min", FlugMinuten, FlugMinutenGesamt);
-
 
224
 
353
 
225
 
354
        printf("\n\rControl: ");
226
        printf("\n\rControl: ");
355
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
227
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
356
        else printf("Normal (ACC-Mode)");
228
        else printf("Normal (ACC-Mode)");
357
 
229
 
358
    LcdClear();
230
    LcdClear();
359
    I2CTimeout = 5000;
231
    I2CTimeout = 5000;
360
    WinkelOut.Orientation = 1;
232
    WinkelOut.Orientation = 1;
361
    LipoDetection(1);
233
    LipoDetection(1);
362
 
234
 
363
        LIBFC_ReceiverInit();
235
        LIBFC_ReceiverInit();
364
 
236
 
365
        printf("\n\r===================================\n\r");
237
        printf("\n\r===================================\n\r");
366
//SpektrumBinding();
238
        //SpektrumBinding();
367
    timer = SetDelay(2000);
239
    timer = SetDelay(2000);
368
        timerPolling = SetDelay(250);
240
        timerPolling = SetDelay(250);
369
 
241
 
370
        while (1)
242
        while (1)
371
        {
243
        {
372
 
244
 
373
        if(CheckDelay(timerPolling))
245
        if(CheckDelay(timerPolling))
374
        {
246
        {
375
          timerPolling = SetDelay(100);
247
          timerPolling = SetDelay(100);
376
          LIBFC_Polling();
248
          LIBFC_Polling();
377
        }
249
        }
378
 
250
 
379
        if(UpdateMotor && AdReady)      // ReglerIntervall
251
        if(UpdateMotor && AdReady)      // ReglerIntervall
380
            {
252
            {
381
                    UpdateMotor=0;
253
                    UpdateMotor=0;
382
            if(WinkelOut.CalcState) CalMk3Mag();
254
            if(WinkelOut.CalcState) CalMk3Mag();
383
            else MotorRegler();
255
            else MotorRegler();
384
            SendMotorData();
256
            SendMotorData();
385
            ROT_OFF;
257
            ROT_OFF;
386
            if(SenderOkay)  SenderOkay--;
258
            if(SenderOkay)  SenderOkay--;
387
                        else
259
                        else
388
                        {
260
                        {
389
                                TIMSK1 |= _BV(ICIE1); // enable PPM-Input
261
                                TIMSK1 |= _BV(ICIE1); // enable PPM-Input
390
                                PPM_in[0] = 0; // set RSSI to zero on data timeout
262
                                PPM_in[0] = 0; // set RSSI to zero on data timeout
391
                        }
263
                        }
392
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
264
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
393
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160;
265
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160;
394
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101;
266
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101;
395
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
267
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396
 
268
 
397
            if(!--I2CTimeout || MissingMotor)
269
            if(!--I2CTimeout || MissingMotor)
398
                {
270
                {
399
                  if(!I2CTimeout)
271
                  if(!I2CTimeout)
400
                                   {
272
                                   {
401
                                    i2c_reset();
273
                                    i2c_reset();
402
                    I2CTimeout = 5;
274
                    I2CTimeout = 5;
403
                                        DebugOut.Analog[28]++; // I2C-Error
275
                                        DebugOut.Analog[28]++; // I2C-Error
404
                                        FCFlags |= FCFLAG_I2CERR;
276
                                        FCFlags |= FCFLAG_I2CERR;
405
                                   }
277
                                   }
406
                  if((BeepMuster == 0xffff) && MotorenEin)
278
                  if((BeepMuster == 0xffff) && MotorenEin)
407
                   {
279
                   {
408
                    beeptime = 10000;
280
                    beeptime = 10000;
409
                    BeepMuster = 0x0080;
281
                    BeepMuster = 0x0080;
410
                   }
282
                   }
411
                }
283
                }
412
            else
284
            else
413
                {
285
                {
414
                 ROT_OFF;
286
                 ROT_OFF;
415
                                 if(!beeptime) FCFlags &= ~FCFLAG_I2CERR;
287
                                 if(!beeptime) FCFlags &= ~FCFLAG_I2CERR;
416
                }
288
                }
417
            if(SIO_DEBUG && (!UpdateMotor || !MotorenEin))
289
            if(SIO_DEBUG && (!UpdateMotor || !MotorenEin))
418
              {
290
              {
419
               DatenUebertragung();
291
               DatenUebertragung();
420
               BearbeiteRxDaten();
292
               BearbeiteRxDaten();
421
              }
293
              }
422
                        else BearbeiteRxDaten();
294
                        else BearbeiteRxDaten();
423
                        if(CheckDelay(timer))
295
                        if(CheckDelay(timer))
424
                        {
296
                        {
425
                                static unsigned char second;
297
                                static unsigned char second;
426
                                timer += 20; // 20 ms interval
298
                                timer += 20; // 20 ms interval
427
                                if(PcZugriff) PcZugriff--;
299
                                if(PcZugriff) PcZugriff--;
428
                                else
300
                                else
429
                                {
301
                                {
430
                                        ExternControl.Config = 0;
302
                                        ExternControl.Config = 0;
431
                                        ExternStickNick = 0;
303
                                        ExternStickNick = 0;
432
                                        ExternStickRoll = 0;
304
                                        ExternStickRoll = 0;
433
                                        ExternStickGier = 0;
305
                                        ExternStickGier = 0;
434
                                        if(BeepMuster == 0xffff && SenderOkay == 0)
306
                                        if(BeepMuster == 0xffff && SenderOkay == 0)
435
                                        {
307
                                        {
436
                                                beeptime = 15000;
308
                                                beeptime = 15000;
437
                                                BeepMuster = 0x0c00;
309
                                                BeepMuster = 0x0c00;
438
                                        }
310
                                        }
439
                                }
311
                                }
440
                                if(NaviDataOkay)
312
                                if(NaviDataOkay)
441
                                {
313
                                {
442
                                        NaviDataOkay--;
314
                                        NaviDataOkay--;
443
                                        FCFlags &= ~FCFLAG_SPI_RX_ERR;
315
                                        FCFlags &= ~FCFLAG_SPI_RX_ERR;
444
                                }
316
                                }
445
                                else
317
                                else
446
                                {
318
                                {
447
                                        GPS_Nick = 0;
319
                                        GPS_Nick = 0;
448
                                        GPS_Roll = 0;
320
                                        GPS_Roll = 0;
449
                                        if(!beeptime) FCFlags |= FCFLAG_SPI_RX_ERR;
321
                                        if(!beeptime) FCFlags |= FCFLAG_SPI_RX_ERR;
450
                                }
322
                                }
451
                           if(UBat < BattLowVoltageWarning)
323
                           if(UBat < BattLowVoltageWarning)
452
                                {
324
                                {
453
                                        FCFlags |= FCFLAG_LOWBAT;
325
                                        FCFlags |= FCFLAG_LOWBAT;
454
                                        if(BeepMuster == 0xffff)
326
                                        if(BeepMuster == 0xffff)
455
                                        {
327
                                        {
456
                                                beeptime = 6000;
328
                                                beeptime = 6000;
457
                                                BeepMuster = 0x0300;
329
                                                BeepMuster = 0x0300;
458
                                        }
330
                                        }
459
                                }
331
                                }
460
                                else if(!beeptime) FCFlags &= ~FCFLAG_LOWBAT;
332
                                else if(!beeptime) FCFlags &= ~FCFLAG_LOWBAT;
461
 
333
 
462
                                SPI_StartTransmitPacket();
334
                                SPI_StartTransmitPacket();
463
                                SendSPI = 4;
335
                                SendSPI = 4;
464
                                if(!MotorenEin) timer2 = 1450; // 0,5 Minuten aufrunden
336
                                if(!MotorenEin) timer2 = 1450; // 0,5 Minuten aufrunden
465
                                else
337
                                else
466
                if(++second == 49)
338
                if(++second == 49)
467
                                 {
339
                                 {
468
                                   second = 0;
340
                                   second = 0;
469
                                   FlugSekunden++;
341
                                   FlugSekunden++;
470
                                 }
342
                                 }
471
 
343
 
472
                                if(++timer2 == 2930)  // eine Minute
344
                                if(++timer2 == 2930)  // eine Minute
473
                                 {
345
                                 {
474
                                   timer2 = 0;
346
                                   timer2 = 0;
475
                   FlugMinuten++;
347
                   FlugMinuten++;
476
                       FlugMinutenGesamt++;
348
                       FlugMinutenGesamt++;
477
                   eeprom_write_byte((unsigned char*)(EEPROM_ADR_MINUTES2),FlugMinuten / 256);
349
                       SetParamWord(PID_FLIGHT_MINUTES_TOTAL, FlugMinutenGesamt);
478
                   eeprom_write_byte((unsigned char*)(EEPROM_ADR_MINUTES2+1),FlugMinuten % 256);
350
                                   SetParamWord(PID_FLIGHT_MINUTES, FlugMinuten);
479
                   eeprom_write_byte((unsigned char*)(EEPROM_ADR_MINUTES),FlugMinutenGesamt / 256);
-
 
480
                   eeprom_write_byte((unsigned char*)(EEPROM_ADR_MINUTES+1),FlugMinutenGesamt % 256);
-
 
481
                                   timer = SetDelay(20); // falls "timer += 20;" mal nicht geht
351
                                   timer = SetDelay(20); // falls "timer += 20;" mal nicht geht
482
                             }
352
                             }
483
                        }
353
                        }
484
           LED_Update();
354
           LED_Update();
485
           Capacity_Update();
355
           Capacity_Update();
486
          }
356
          }
487
     if(!SendSPI) { SPI_TransmitByte(); }
357
     if(!SendSPI) { SPI_TransmitByte(); }
488
    }
358
    }
489
 return (1);
359
 return (1);
490
}
360
}
491
 
361
 
492
 
362
 
493
 
363