Subversion Repositories FlightCtrl

Rev

Rev 885 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 885 Rev 886
Line 47... Line 47...
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 <avr/boot.h>
53
 
-
 
54
unsigned char EEPromArray[E2END+1] EEMEM;
-
 
55
unsigned char PlatinenVersion = 10;
-
 
56
unsigned char SendVersionToNavi = 1;
-
 
57
// -- Parametersatz aus EEPROM lesen ---
-
 
58
// number [0..5]   
-
 
59
void ReadParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
-
 
60
{
-
 
61
   if (number > 5) number = 5;
-
 
62
   eeprom_read_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * number], length);
-
 
63
}
-
 
Line -... Line 53...
-
 
53
 
-
 
54
#include <avr/io.h>
Line 64... Line 55...
64
 
55
#include <avr/interrupt.h>
65
 
56
 
66
// -- Parametersatz ins EEPROM schreiben ---
57
#include "main.h"
67
// number [0..5]   
-
 
68
void WriteParameterSet(unsigned char number, unsigned char *buffer, unsigned char length)
58
#include "timer0.h"
69
{
59
#include "timer2.h"
70
   if(number > 5) number = 5;  
60
#include "uart.h"
71
   eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * number], length);
61
#if defined (__AVR_ATmega644P__)
72
   eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], number);                              // diesen Parametersatz als aktuell merken
62
#include "uart1.h"
73
}
63
#endif
-
 
64
#include "led.h"
74
 
65
#include "menu.h"
75
unsigned char GetActiveParamSetNumber(void)
66
#include "fc.h"
-
 
67
#include "rc.h"
76
{
68
#include "analog.h"
77
 unsigned char set;
69
#include "printf_P.h"
78
 set = eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET]);
70
#ifdef USE_KILLAGREG
-
 
71
#include "mm3.h"
-
 
72
#endif
79
 if(set > 5)
73
#ifdef USE_NAVICTRL
80
  {
74
#include "spi.h"
-
 
75
#endif
81
   set = 2;  
76
#if !defined (USE_KILLAGREG) && !defined (USE_NAVICTRL)
82
   eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], set);                                 // diesen Parametersatz als aktuell merken
77
#include "mk3mag.h"
83
  }
78
#endif
-
 
79
#include "twimaster.h"
Line 84... Line -...
84
 return(set);
-
 
85
}
-
 
86
 
-
 
Line 87... Line -...
87
void CalMk3Mag(void)
-
 
88
{
-
 
89
 static unsigned char stick = 1;
-
 
90
 
-
 
91
 if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0;
-
 
92
 if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick)
-
 
93
  {
-
 
94
   stick = 1;
-
 
95
   WinkelOut.CalcState++;
80
#include "eeprom.h"
96
   if(WinkelOut.CalcState > 4)
-
 
97
    {
-
 
98
//     WinkelOut.CalcState = 0; // in Uart.c
-
 
99
     beeptime = 1000;
-
 
100
    }
-
 
Line 101... Line 81...
101
   else Piep(WinkelOut.CalcState);
81
#include "_Settings.h"
102
  }
82
 
103
  DebugOut.Analog[19] = WinkelOut.CalcState;
83
 
104
}
84
uint8_t BoardRelease = 10;
105
 
85
 
106
 
86
 
Line 107... Line 87...
107
//############################################################################
87
//############################################################################
-
 
88
//Hauptprogramm
-
 
89
int main (void)
-
 
90
//############################################################################
108
//Hauptprogramm
91
{
109
int main (void)
92
        unsigned int timer;
110
//############################################################################
93
 
111
{
94
        // disable interrupts global
112
        unsigned int timer;
95
        cli();
113
 
-
 
114
        //unsigned int timer2 = 0;
-
 
115
    DDRB  = 0x00;
-
 
116
    PORTB = 0x00;
-
 
117
    for(timer = 0; timer < 1000; timer++); // verzögern
-
 
118
    if(PINB & 0x01) PlatinenVersion = 11; else PlatinenVersion = 10;
96
 
119
    DDRC  = 0x81; // SCL
97
        // get board release
-
 
98
    DDRB  = 0x00;
120
    PORTC = 0xff; // Pullup SDA
99
    PORTB = 0x00;
-
 
100
    for(timer = 0; timer < 1000; timer++); // make some delay
Line -... Line 101...
-
 
101
    if(PINB & (1<<PINB0)) BoardRelease = 11;
121
    DDRB  = 0x1B; // LEDs und Druckoffset
102
    else BoardRelease = 10;
122
    PORTB = 0x01; // LED_Rot
103
 
123
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
104
        // set LED ports as output
Line 124... Line 105...
124
    DDRD  |=0x80; // J7
105
        DDRB |= (1<<DDB1)|(1<<DDB0);
Line 125... Line 106...
125
   
106
        ROT_ON;
-
 
107
        GRN_OFF;
-
 
108
 
-
 
109
        // disable watchdog
Line 126... Line 110...
126
        PORTD = 0xF7; // LED
110
    MCUSR &=~(1<<WDRF);
Line -... Line 111...
-
 
111
    WDTCSR |= (1<<WDCE)|(1<<WDE);
-
 
112
    WDTCSR = 0;
-
 
113
 
127
 
114
    BeepTime = 2000;
128
   
115
 
-
 
116
        PPM_in[CH_THRUST] = 0;
-
 
117
        StickYaw = 0;
-
 
118
        StickRoll = 0;
-
 
119
        StickPitch = 0;
-
 
120
 
129
    MCUSR &=~(1<<WDRF);
121
    ROT_OFF;
130
    WDTCSR |= (1<<WDCE)|(1<<WDE);
122
 
131
    WDTCSR = 0;
123
        // initalize modules
-
 
124
        LED_Init();
-
 
125
    TIMER0_Init();
-
 
126
    TIMER2_Init();
-
 
127
        USART0_Init();
-
 
128
 
132
 
129
        #if defined (__AVR_ATmega644P__)
-
 
130
        if (BoardRelease == 11) USART1_Init();
-
 
131
        #endif
-
 
132
 
-
 
133
    RC_Init();
Line -... Line 134...
-
 
134
        ADC_Init();
-
 
135
        I2C_Init();
133
    beeptime = 2000;
136
 
Line 134... Line 137...
134
 
137
        #ifdef USE_KILLAGREG
135
        StickGier = 0; PPM_in[K_GAS] = 0;StickRoll = 0; StickNick = 0;
138
        MM3_Init();
136
 
139
        #endif
Line 137... Line 140...
137
    ROT_OFF;
140
        #ifdef USE_NAVICTRL
138
   
141
        SPI_MasterInit();
139
    Timer_Init();
142
        #endif
Line 140... Line -...
140
        UART_Init();
-
 
141
    rc_sum_init();
-
 
142
        ADC_Init();
-
 
143
        i2c_init();
-
 
144
        SPI_MasterInit();
143
        #if !defined (USE_KILLAGREG) && !defined (USE_NAVICTRL)
145
   
144
        MK3MAG_Init();
146
        sei();
-
 
147
 
-
 
148
    VersionInfo.Hauptversion = VERSION_HAUPTVERSION;
-
 
149
    VersionInfo.Nebenversion = VERSION_NEBENVERSION;
-
 
150
    VersionInfo.PCKompatibel = VERSION_KOMPATIBEL;
-
 
151
   
-
 
152
        printf("\n\rFlightControl\n\rHardware:%d.%d\n\rSoftware:V%d.%d%c ",PlatinenVersion/10,PlatinenVersion%10, VERSION_HAUPTVERSION, VERSION_NEBENVERSION,VERSION_INDEX + 'a');
-
 
153
        printf("\n\r==============================");
-
 
154
        GRN_ON;
-
 
Line 155... Line 145...
155
 
145
        #endif
156
#define EE_DATENREVISION 70 // wird angepasst, wenn sich die EEPROM-Daten geändert haben
146
 
157
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != EE_DATENREVISION)
147
 
158
        {
148
        // enable interrupts global
Line 159... Line 149...
159
          printf("\n\rInit. EEPROM: Generiere Default-Parameter...");
149
        sei();
-
 
150
 
160
          DefaultKonstanten1();
151
    VersionInfo.Major = VERSION_MAJOR;
-
 
152
    VersionInfo.Minor = VERSION_MINOR;
Line -... Line 153...
-
 
153
    VersionInfo.PCCompatible = VERSION_COMPATIBLE;
-
 
154
 
-
 
155
        printf("\n\rFlightControl\n\rHardware:%d.%d\n\rSoftware:V%d.%d%c ",BoardRelease/10,BoardRelease%10, VERSION_MAJOR, VERSION_MINOR,VERSION_INDEX + 'a');
Line -... Line 156...
-
 
156
        printf("\n\r==============================");
161
          for (unsigned char i=0;i<6;i++)  
157
        GRN_ON;
162
      {
158
 
163
       if(i==2) DefaultKonstanten2(); // Kamera
159
        // Parameter set handling
164
       if(i==3) DefaultKonstanten3(); // Beginner
160
        ParamSet_Init();
165
       if(i>3)  DefaultKonstanten2(); // Kamera
161
 
166
       WriteParameterSet(i, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
162
    if(GetParamWord(PID_ACC_PITCH) > 1023)
167
      }
163
     {
168
          eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], 3); // default-Setting
164
       printf("\n\rACC not calibrated!");
Line -... Line 165...
-
 
165
     }
-
 
166
 
-
 
167
        //wait for a short time (otherwise the RC channel check won't work below)
-
 
168
        timer = SetDelay(500);
-
 
169
        while(!CheckDelay(timer));
-
 
170
 
-
 
171
 
-
 
172
        #if !defined (USE_KILLAGREG) && !defined (USE_NAVICTRL)
-
 
173
        printf("\n\rSupport for MK3MAG Compass");
-
 
174
        #endif
-
 
175
 
-
 
176
 
-
 
177
        if(ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)
-
 
178
         {
-
 
179
           printf("\n\rCalibrating air pressure sensor..");
-
 
180
           timer = SetDelay(1000);
169
          eeprom_write_byte(&EEPromArray[EEPROM_ADR_VALID], EE_DATENREVISION);
181
       SearchAirPressureOffset();
Line 170... Line 182...
170
        }
182
           while (!CheckDelay(timer));
Line 171... Line 183...
171
 
183
       printf("OK\n\r");
172
    if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4)
184
        }
Line 173... Line 185...
173
     {
185
 
174
       printf("\n\rACC nicht abgeglichen!");
186
        #ifdef USE_KILLAGREG
175
     }
187
        printf("\n\rSupport for MicroMag3 Compass");
Line 176... Line 188...
176
       
188
        #if defined (__AVR_ATmega644P__)
Line 177... Line 189...
177
        ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
189
        if(BoardRelease == 10)
-
 
190
        {
178
    printf("\n\rBenutze Parametersatz %d", GetActiveParamSetNumber());
191
                printf("\n\rSupport for GPS at 1st UART");
179
 
-
 
-
 
192
        }
180
   
193
        else
181
        if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
194
        {
182
         {
-
 
183
           printf("\n\rAbgleich Luftdrucksensor..");
195
                printf("\n\rSupport for GPS at 2nd UART");
184
           timer = SetDelay(1000);  
196
        }
185
       SucheLuftruckOffset();
197
        #else // (__AVR_ATmega644__)
186
           while (!CheckDelay(timer));
198
                printf("\n\rSupport for GPS at 1st UART");
187
       printf("OK\n\r");
-
 
188
        }
199
        #endif
189
       
200
        #endif
-
 
201
 
190
        SetNeutral();
202
        SetNeutral();
-
 
203
 
191
 
204
        ROT_OFF;
-
 
205
 
192
        ROT_OFF;
206
    BeepTime = 2000;
193
   
207
    ExternControl.Digital[0] = 0x55;
194
    beeptime = 2000;
208
 
195
    ExternControl.Digital[0] = 0x55;   
209
 
196
 
210
        printf("\n\rControl: ");
197
   
211
        if (ParamSet.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
198
        printf("\n\rSteuerung: ");
212
        else printf("Neutral");
199
        if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
213
 
200
        else printf("Neutral");
214
        printf("\n\n\r");
201
       
215
 
202
        printf("\n\n\r");
-
 
-
 
216
    LCD_Clear();
203
               
217
 
204
    LcdClear();
218
    I2CTimeout = 5000;
205
    I2CTimeout = 5000;
219
 
206
    WinkelOut.Orientation = 1;
220
        while (1)
207
        while (1)
221
        {
208
        {
222
        if(UpdateMotor)      // control interval
209
       
223
        {
210
                if(UpdateMotor)      // ReglerIntervall
224
                        UpdateMotor=0; // reset Flag, is enabled every 2 ms by isr of timer0
211
            {
225
                        //PORTD |= (1<<PORTD4);
212
                    UpdateMotor=0;
226
            MotorControl();
213
//PORTD |= 0x08;
227
                        //PORTD &= ~(1<<PORTD4);
214
            if(WinkelOut.CalcState) CalMk3Mag();
228
 
215
            else MotorRegler();  
229
                        SendMotorData();
216
//PORTD &= ~0x08;
230
 
217
            SendMotorData();
231
            ROT_OFF;
-
 
232
 
218
            ROT_OFF;
233
            if(PcAccess) PcAccess--;
219
            if(PcZugriff) PcZugriff--;
234
            else
220
             else
235
            {
221
              {
236
               DubWiseKeys[0] = 0;
222
               DubWiseKeys[0] = 0;
237
               DubWiseKeys[1] = 0;
223
               DubWiseKeys[1] = 0;
238
                           ExternControl.Config = 0;
-
 
239
               ExternStickPitch= 0;
224
                           ExternControl.Config = 0;
240
               ExternStickRoll = 0;
225
               ExternStickNick = 0;
241
               ExternStickYaw = 0;
226
               ExternStickRoll = 0;
242
            }
227
               ExternStickGier = 0;
243
 
228
              }
244
            if(!I2CTimeout)
229
            if(SenderOkay)  SenderOkay--;
245
            {
230
            if(!I2CTimeout)
246
                                I2CTimeout = 5;
231
                {
247
                                I2C_Reset();
232
                 I2CTimeout = 5;
-
 
233
                 i2c_reset();
248
                                if((BeepModulation == 0xFFFF) && MotorsOn)
234
                  if((BeepMuster == 0xffff) && MotorenEin)
-
 
235
                   {
-
 
236
                    beeptime = 10000;
-
 
237
                    BeepMuster = 0x0080;
-
 
238
                   }
249
                                {
239
                }
250
                                        BeepTime = 10000; // 1 second
240
            else        
-
 
241
                {
251
                                        BeepModulation = 0x0080;
242
                 I2CTimeout--;
-
 
243
                 ROT_OFF;
252
                                }
-
 
253
                        }
244
                }
254
                        else
-
 
255
                        {
245
            if(SIO_DEBUG && (!UpdateMotor || !MotorenEin))
256
                                I2CTimeout--;
-
 
257
                                ROT_OFF;
-
 
258
                        }
-
 
259
 
246
              {
260
                        if(SIO_DEBUG && (!UpdateMotor || !MotorsOn))
-
 
261
                        {
-
 
262
                                USART0_TransmitTxData();
-
 
263
                                USART0_ProcessRxData();
-
 
264
                        }
247
               DatenUebertragung();
265
                        else USART0_ProcessRxData();
248
               BearbeiteRxDaten();
266
 
249
              }
267
                        if(CheckDelay(timer))
250
              else BearbeiteRxDaten();
268
                        {
251
         if(CheckDelay(timer))
269
                if(UBat < ParamSet.LowVoltageWarning)