Subversion Repositories FlightCtrl

Rev

Rev 1910 | Rev 2025 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1910 - 1
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Contant Values
3
// + 0-250 -> normale Values
4
// + 251 -> Poti1
5
// + 252 -> Poti2
6
// + 253 -> Poti3
7
// + 254 -> Poti4
8
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
 
10
#ifndef EEMEM
11
#define EEMEM __attribute__ ((section (".eeprom")))
12
#endif
13
 
14
#include <avr/eeprom.h>
15
#include <string.h>
16
#include "eeprom.h"
17
#include "output.h"
18
// TODO: Get rid of these. They have nothing to do with eeprom.
19
#include "flight.h"
20
#include "rc.h"
21
#include "sensors.h"
22
 
23
// byte array in eeprom
24
uint8_t EEPromArray[E2END + 1] EEMEM;
25
 
26
paramset_t staticParams;
27
MixerTable_t Mixer;
28
 
29
/*
30
 * Default for your own experiments here, so you don't have to reset them
31
 * from MK-Tool all the time.
32
 */
33
void setDefaultUserParams(void) {
34
        uint8_t i;
1927 - 35
        for (i = 0; i < sizeof(staticParams.UserParams); i++) {
36
                staticParams.UserParams[i] = 0;
1910 - 37
        }
38
        /*
39
         * While we are still using userparams for flight parameters, do set
40
         * some safe & meaningful default values.
41
         */
42
}
43
 
44
void setOtherDefaults(void) {
45
        /* Channel assignments were changed to the normal:
46
         * Aileron/roll=1, elevator/pitch=2, throttle=3, yaw/rudder=4
47
         */
48
        staticParams.ChannelAssignment[CH_ELEVATOR] = 2;
49
        staticParams.ChannelAssignment[CH_AILERONS] = 1;
50
        staticParams.ChannelAssignment[CH_THROTTLE] = 3;
51
        staticParams.ChannelAssignment[CH_RUDDER] = 4;
52
        staticParams.ChannelAssignment[CH_POTS + 0] = 5;
53
        staticParams.ChannelAssignment[CH_POTS + 1] = 6;
54
        staticParams.ChannelAssignment[CH_POTS + 2] = 7;
55
        staticParams.ChannelAssignment[CH_POTS + 3] = 8;
56
        staticParams.GlobalConfig = /* CFG_AXIS_COUPLING_ACTIVE | */ CFG_HEADING_HOLD; // CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE;//CFG_HEIGHT_CONTROL | CFG_HEIGHT_SWITCH | CFG_COMPASS_FIX;
57
        staticParams.HeightMinGas = 30;
58
        staticParams.MaxHeight = 251;
59
        staticParams.HeightP = 10;
60
        staticParams.HeightD = 30;
61
        staticParams.Height_ACC_Effect = 30;
62
        staticParams.Height_Gain = 4;
63
        staticParams.CompassYawEffect = 128;
64
 
65
        staticParams.GyroPitchP = 0;
66
        staticParams.GyroRollP = 0;
67
        staticParams.GyroYawP = 0;
68
 
69
        staticParams.GyroPitchD = 0;
70
        staticParams.GyroRollD = 0;
71
        staticParams.GyroYawD = 0;
72
 
73
        staticParams.StickElevatorP = 10;
74
        staticParams.StickAileronsP = 10;
75
        staticParams.StickRudderP = 10;
76
 
77
        staticParams.LowVoltageWarning = 105;
78
        staticParams.ServoRefresh = 7;
79
        staticParams.BitConfig = 0;
80
        staticParams.J16Bitmask = 95;
81
        staticParams.J17Bitmask = 243;
82
        staticParams.J16Timing = 15;
83
        staticParams.J17Timing = 15;
84
        staticParams.ControlSigns = 2;
85
}
86
 
1927 - 87
void setDefaults(void) {
1910 - 88
        setOtherDefaults();
89
        gyro_setDefaults();
90
        setDefaultUserParams();
91
        staticParams.J16Timing = 10;
92
        staticParams.J17Timing = 10;
93
}
94
 
95
/***************************************************/
96
/*       Read Parameter from EEPROM as byte        */
97
/***************************************************/
98
uint8_t GetParamByte(uint16_t param_id) {
99
        return eeprom_read_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id]);
100
}
101
 
102
/***************************************************/
103
/*       Write Parameter to EEPROM as byte         */
104
/***************************************************/
105
void SetParamByte(uint16_t param_id, uint8_t value) {
106
        eeprom_write_byte(&EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
107
}
108
 
109
/***************************************************/
110
/*       Read Parameter from EEPROM as word        */
111
/***************************************************/
112
uint16_t GetParamWord(uint16_t param_id) {
113
        return eeprom_read_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN
114
                        + param_id]);
115
}
116
 
117
/***************************************************/
118
/*       Write Parameter to EEPROM as word         */
119
/***************************************************/
120
void SetParamWord(uint16_t param_id, uint16_t value) {
121
        eeprom_write_word(
122
                        (uint16_t *) &EEPromArray[EEPROM_ADR_PARAM_BEGIN + param_id], value);
123
}
124
 
125
/***************************************************/
126
/*       Read Parameter Set from EEPROM            */
127
/***************************************************/
128
// number [1..5]
1927 - 129
void ParamSet_ReadFromEEProm() {
1910 - 130
        eeprom_read_block((uint8_t *) &staticParams.ChannelAssignment[0],
1927 - 131
                        &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN], PARAMSET_STRUCT_LEN);
1910 - 132
        output_init();
133
}
134
 
135
/***************************************************/
136
/*        Write Parameter Set to EEPROM            */
137
/***************************************************/
138
// number [1..5]
1927 - 139
void ParamSet_WriteToEEProm() {
1910 - 140
        eeprom_write_block((uint8_t *) &staticParams.ChannelAssignment[0],
1927 - 141
                        &EEPromArray[EEPROM_ADR_PARAMSET_BEGIN], PARAMSET_STRUCT_LEN);
1910 - 142
        eeprom_write_word((uint16_t *) &EEPromArray[EEPROM_ADR_PARAMSET_LENGTH],
143
                        PARAMSET_STRUCT_LEN);
144
        eeprom_write_block(&staticParams.ChannelAssignment[0],
145
                        &EEPromArray[EEPROM_ADR_CHANNELS], 8); // backup the first 8 bytes that is the rc channel mapping
146
        // set this parameter set to active set
147
        output_init();
148
}
149
 
150
/***************************************************/
151
/*       Get active parameter set                  */
152
/***************************************************/
153
uint8_t getActiveParamSet(void) {
154
        uint8_t setnumber;
155
        setnumber = eeprom_read_byte(&EEPromArray[PID_ACTIVE_SET]);
156
        if (setnumber > 5) {
157
                setnumber = 3;
158
                eeprom_write_byte(&EEPromArray[PID_ACTIVE_SET], setnumber);
159
        }
160
        return (setnumber);
161
}
162
 
163
/***************************************************/
164
/*          Read MixerTable from EEPROM            */
165
/***************************************************/
166
uint8_t MixerTable_ReadFromEEProm(void) {
167
        if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE])
168
                        == EEMIXER_REVISION) {
169
                eeprom_read_block((uint8_t *) &Mixer, &EEPromArray[EEPROM_ADR_MIXER_TABLE],
170
                                sizeof(Mixer));
171
                return 1;
172
        } else
173
                return 0;
174
}
175
 
176
/***************************************************/
177
/*          Write Mixer Table to EEPROM            */
178
/***************************************************/
179
uint8_t MixerTable_WriteToEEProm(void) {
180
        if (Mixer.Revision == EEMIXER_REVISION) {
181
                eeprom_write_block((uint8_t *) &Mixer,
182
                                &EEPromArray[EEPROM_ADR_MIXER_TABLE], sizeof(Mixer));
183
                return 1;
184
        } else
185
                return 0;
186
}
187
 
188
/***************************************************/
189
/*    Default Values for Mixer Table               */
190
/***************************************************/
191
void MixerTable_Default(void) { // Quadro 
192
        uint8_t i;
193
        Mixer.Revision = EEMIXER_REVISION;
194
        // clear mixer table (but preset throttle)
195
        for (i = 0; i < 16; i++) {
196
                Mixer.Motor[i][MIX_THROTTLE] = i < 4 ? 64 : 0;
197
                Mixer.Motor[i][MIX_PITCH] = 0;
198
                Mixer.Motor[i][MIX_ROLL] = 0;
199
                Mixer.Motor[i][MIX_YAW] = 0;
200
        }
201
        // default = Quadro
202
        Mixer.Motor[0][MIX_PITCH] = +64;
203
        Mixer.Motor[0][MIX_YAW] = +64;
204
        Mixer.Motor[1][MIX_PITCH] = -64;
205
        Mixer.Motor[1][MIX_YAW] = +64;
206
        Mixer.Motor[2][MIX_ROLL] = -64;
207
        Mixer.Motor[2][MIX_YAW] = -64;
208
        Mixer.Motor[3][MIX_ROLL] = +64;
209
        Mixer.Motor[3][MIX_YAW] = -64;
210
        memcpy(Mixer.Name, "Quadro\0", 7);
211
}
212
 
213
/***************************************************/
214
/*       Initialize EEPROM Parameter Sets          */
215
/***************************************************/
216
void ParamSet_Init(void) {
1927 - 217
        uint8_t Channel_Backup = 1, j;
1910 - 218
        // parameter version  check
219
        if (eeprom_read_byte(&EEPromArray[PID_PARAM_REVISION]) != EEPARAM_REVISION) {
220
                // if version check faild
221
                eeprom_write_byte(&EEPromArray[EEPROM_ADR_MIXER_TABLE], 0xFF); // reset also mixer table
222
                // check if channel mapping backup is valid
223
                for (j = 0; j < 4 && Channel_Backup; j++) {
224
                        if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS + 0]) >= 12)
225
                                Channel_Backup = 0;
226
                }
227
                // fill all 5 parameter settings
1927 - 228
 
229
        setDefaults(); // Fill staticParams Structure to default parameter set 1 (Sport)
230
 
231
        if (Channel_Backup) { // if we have a rc channel mapping backup in eeprom
232
                        // restore it
233
                        for (j = 0; j < 8; j++) {
234
                                staticParams.ChannelAssignment[j] = eeprom_read_byte(&EEPromArray[EEPROM_ADR_CHANNELS + j]);
235
            }
236
        }
237
 
238
        ParamSet_WriteToEEProm();
239
 
1910 - 240
                // update version info
241
                SetParamByte(PID_PARAM_REVISION, EEPARAM_REVISION);
242
        }
243
        // read active parameter set to staticParams stucture
1927 - 244
        ParamSet_ReadFromEEProm();
1910 - 245
}