Rev 2102 | Rev 2104 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2102 | Rev 2103 | ||
---|---|---|---|
1 | #include <util/delay.h> |
1 | #include <util/delay.h> |
2 | #include <stddef.h> |
2 | #include <stddef.h> |
3 | #include <string.h> |
3 | #include <string.h> |
4 | #include "configuration.h" |
4 | #include "configuration.h" |
5 | #include "sensors.h" |
5 | #include "sensors.h" |
6 | #include "rc.h" |
6 | #include "rc.h" |
7 | #include "output.h" |
7 | #include "output.h" |
8 | #include "flight.h" |
8 | #include "flight.h" |
9 | 9 | ||
10 | int16_t variables[VARIABLE_COUNT]; |
10 | int16_t variables[VARIABLE_COUNT]; |
11 | ParamSet_t staticParams; |
11 | ParamSet_t staticParams; |
12 | channelMap_t channelMap; |
12 | channelMap_t channelMap; |
13 | IMUConfig_t IMUConfig; |
13 | IMUConfig_t IMUConfig; |
14 | volatile DynamicParams_t dynamicParams; |
14 | volatile DynamicParams_t dynamicParams; |
15 | 15 | ||
16 | uint8_t CPUType; |
16 | uint8_t CPUType; |
17 | uint8_t boardRelease; |
17 | uint8_t boardRelease; |
18 | uint8_t requiredMotors; |
18 | uint8_t requiredMotors; |
19 | 19 | ||
20 | VersionInfo_t versionInfo; |
20 | VersionInfo_t versionInfo; |
21 | 21 | ||
22 | // MK flags. TODO: Replace by enum. State machine. |
22 | // MK flags. TODO: Replace by enum. State machine. |
23 | FlightMode_t currentFlightMode = FLIGHT_MODE_MANUAL; |
23 | FlightMode_t currentFlightMode = FLIGHT_MODE_MANUAL; |
24 | volatile uint8_t isMotorRunning = 0; |
24 | volatile uint8_t isMotorRunning = 0; |
25 | 25 | ||
26 | const MMXLATION XLATIONS[] = { |
26 | const MMXLATION XLATIONS[] = { |
27 | {offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255}, |
27 | {offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255}, |
28 | {offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255}, |
28 | {offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255}, |
29 | {offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255}, |
29 | {offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255}, |
30 | {offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255}, |
30 | {offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255}, |
31 | {offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255}, |
31 | {offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255}, |
32 | }; |
32 | }; |
33 | 33 | ||
34 | uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) { |
34 | uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) { |
35 | uint8_t result; |
35 | uint8_t result; |
36 | if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)]; |
36 | if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)]; |
37 | else result = src; |
37 | else result = src; |
38 | if (result < min) result = min; |
38 | if (result < min) result = min; |
39 | else if (result > max) result = max; |
39 | else if (result > max) result = max; |
40 | return result; |
40 | return result; |
41 | } |
41 | } |
42 | 42 | ||
43 | void configuration_applyVariablesToParams(void) { |
43 | void configuration_applyVariablesToParams(void) { |
44 | uint8_t i, src; |
44 | uint8_t i, src; |
45 | uint8_t* pointerToTgt; |
45 | uint8_t* pointerToTgt; |
46 | 46 | ||
47 | for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) { |
47 | for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) { |
48 | src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx); |
48 | src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx); |
49 | pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx; |
49 | pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx; |
50 | *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max); |
50 | *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max); |
51 | } |
51 | } |
52 | 52 | ||
53 | // User parameters are always variable. |
53 | // User parameters are always variable. |
54 | for (i=0; i<sizeof(staticParams.userParams); i++) { |
54 | for (i=0; i<sizeof(staticParams.userParams); i++) { |
55 | src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i); |
55 | src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i); |
56 | pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i; |
56 | pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i; |
57 | *pointerToTgt = configuration_applyVariableToParam(src, 0, 255); |
57 | *pointerToTgt = configuration_applyVariableToParam(src, 0, 255); |
58 | } |
58 | } |
59 | } |
59 | } |
60 | 60 | ||
61 | void setCPUType(void) { // works only after reset or power on when the registers have default values |
61 | void setCPUType(void) { // works only after reset or power on when the registers have default values |
62 | if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P; // initial Values for 644P after reset |
62 | if((UCSR1A == 0x20) && (UCSR1C == 0x06)) CPUType = ATMEGA644P; // initial Values for 644P after reset |
63 | else CPUType = ATMEGA644; |
63 | else CPUType = ATMEGA644; |
64 | } |
64 | } |
65 | 65 | ||
66 | /* |
66 | /* |
67 | * Automatic detection of hardware components is not supported in this development-oriented |
67 | * Automatic detection of hardware components is not supported in this development-oriented |
68 | * FC firmware. It would go against the point of it: To enable alternative hardware |
68 | * FC firmware. It would go against the point of it: To enable alternative hardware |
69 | * configurations with otherwise unsupported components. Instead, one should write |
69 | * configurations with otherwise unsupported components. Instead, one should write |
70 | * custom code + adjust constants for the new hardware, and include the relevant code |
70 | * custom code + adjust constants for the new hardware, and include the relevant code |
71 | * from the makefile. |
71 | * from the makefile. |
72 | * However - we still do detect the board release. Reason: Otherwise it would be too |
72 | * However - we still do detect the board release. Reason: Otherwise it would be too |
73 | * tedious to have to modify the code for how to turn on and off LEDs when deploying |
73 | * tedious to have to modify the code for how to turn on and off LEDs when deploying |
74 | * on different HW version.... |
74 | * on different HW version.... |
75 | */ |
75 | */ |
76 | void setBoardRelease(void) { |
76 | void setBoardRelease(void) { |
77 | // the board release is coded via the pull up or down the 2 status LED |
77 | // the board release is coded via the pull up or down the 2 status LED |
78 | 78 | ||
79 | PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate |
79 | PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate |
80 | DDRB &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input |
80 | DDRB &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input |
81 | 81 | ||
82 | _delay_loop_2(1000); // make some delay |
82 | _delay_loop_2(1000); // make some delay |
83 | 83 | ||
84 | switch( PINB & ((1<<PINB1)|(1<<PINB0))) { |
84 | switch( PINB & ((1<<PINB1)|(1<<PINB0))) { |
85 | case 0x00: |
85 | case 0x00: |
86 | boardRelease = 10; // 1.0 |
86 | boardRelease = 10; // 1.0 |
87 | break; |
87 | break; |
88 | case 0x01: |
88 | case 0x01: |
89 | boardRelease = 11; // 1.1 or 1.2 |
89 | boardRelease = 11; // 1.1 or 1.2 |
90 | break; |
90 | break; |
91 | case 0x02: |
91 | case 0x02: |
92 | boardRelease = 20; // 2.0 |
92 | boardRelease = 20; // 2.0 |
93 | break; |
93 | break; |
94 | case 0x03: |
94 | case 0x03: |
95 | boardRelease = 13; // 1.3 |
95 | boardRelease = 13; // 1.3 |
96 | break; |
96 | break; |
97 | default: |
97 | default: |
98 | break; |
98 | break; |
99 | } |
99 | } |
100 | // set LED ports as output |
100 | // set LED ports as output |
101 | DDRB |= (1<<DDB1)|(1<<DDB0); |
101 | DDRB |= (1<<DDB1)|(1<<DDB0); |
102 | RED_OFF; |
102 | RED_OFF; |
103 | GRN_OFF; |
103 | GRN_OFF; |
104 | } |
104 | } |
105 | 105 | ||
106 | void configuration_setFlightParameters(uint8_t newFlightMode) { |
106 | void configuration_setFlightParameters(uint8_t newFlightMode) { |
107 | updateFlightParametersToFlightMode(currentFlightMode = newFlightMode); |
107 | flight_updateFlightParametersToFlightMode(currentFlightMode = newFlightMode); |
108 | } |
108 | } |
109 | 109 | ||
110 | // Called after a change in configuration parameters, as a hook for modules to take over changes. |
110 | // Called after a change in configuration parameters, as a hook for modules to take over changes. |
111 | void configuration_paramSetDidChange(void) { |
111 | void configuration_paramSetDidChange(void) { |
112 | // This should be OK to do here as long as we don't change parameters during emergency flight. We don't. |
112 | // This should be OK to do here as long as we don't change parameters during emergency flight. We don't. |
113 | configuration_setFlightParameters(currentFlightMode); |
113 | configuration_setFlightParameters(currentFlightMode); |
114 | // Immediately load changes to output, and also signal the paramset change. |
114 | // Immediately load changes to output, and also signal the paramset change. |
115 | output_init(); |
115 | output_init(); |
116 | } |
116 | } |
117 | 117 | ||
118 | void setOtherDefaults(void) { |
118 | void setOtherDefaults(void) { |
119 | // Control |
119 | // Control |
120 | staticParams.externalControl = 0; |
120 | staticParams.externalControl = 0; |
121 | staticParams.IFactor = 52; |
121 | staticParams.IFactor = 52; |
122 | 122 | ||
123 | // Servos |
123 | // Servos |
124 | staticParams.servoCount = 7; |
124 | staticParams.servoCount = 7; |
125 | staticParams.servoManualMaxSpeed = 10; |
125 | staticParams.servoManualMaxSpeed = 10; |
126 | for (uint8_t i=0; i<2; i++) { |
126 | for (uint8_t i=0; i<2; i++) { |
127 | staticParams.servoConfigurations[i].manualControl = 128; |
127 | staticParams.servoConfigurations[i].manualControl = 128; |
128 | staticParams.servoConfigurations[i].stabilizationFactor = 0; |
128 | staticParams.servoConfigurations[i].stabilizationFactor = 0; |
129 | staticParams.servoConfigurations[i].minValue = 32; |
129 | staticParams.servoConfigurations[i].minValue = 32; |
130 | staticParams.servoConfigurations[i].maxValue = 224; |
130 | staticParams.servoConfigurations[i].maxValue = 224; |
131 | staticParams.servoConfigurations[i].flags = 0; |
131 | staticParams.servoConfigurations[i].flags = 0; |
132 | } |
132 | } |
133 | 133 | ||
134 | // Battery warning and emergency flight |
134 | // Battery warning and emergency flight |
135 | staticParams.batteryVoltageWarning = 101; // 3.7 each for 3S |
135 | staticParams.batteryVoltageWarning = 101; // 3.7 each for 3S |
136 | staticParams.emergencyThrottle = 35; |
136 | staticParams.emergencyThrottle = 35; |
137 | staticParams.emergencyFlightDuration = 30; |
137 | staticParams.emergencyFlightDuration = 30; |
- | 138 | ||
- | 139 | staticParams.stickIElevator = 80; |
|
- | 140 | staticParams.stickIAilerons = 120; |
|
- | 141 | staticParams.stickIRudder = 40; |
|
138 | 142 | ||
139 | // Outputs |
143 | // Outputs |
140 | staticParams.outputFlash[0].bitmask = 1; //0b01011111; |
144 | staticParams.outputFlash[0].bitmask = 1; //0b01011111; |
141 | staticParams.outputFlash[0].timing = 15; |
145 | staticParams.outputFlash[0].timing = 15; |
142 | staticParams.outputFlash[1].bitmask = 3; //0b11110011; |
146 | staticParams.outputFlash[1].bitmask = 3; //0b11110011; |
143 | staticParams.outputFlash[1].timing = 15; |
147 | staticParams.outputFlash[1].timing = 15; |
144 | 148 | ||
145 | staticParams.outputDebugMask = 8; |
149 | staticParams.outputDebugMask = 8; |
146 | staticParams.outputFlags = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS; |
150 | staticParams.outputFlags = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS; |
147 | } |
151 | } |
148 | 152 | ||
149 | /***************************************************/ |
153 | /***************************************************/ |
150 | /* Default Values for parameter set 1 */ |
154 | /* Default Values for parameter set 1 */ |
151 | /***************************************************/ |
155 | /***************************************************/ |
152 | void paramSet_default(uint8_t setnumber) { |
156 | void paramSet_default(uint8_t setnumber) { |
153 | setOtherDefaults(); |
157 | setOtherDefaults(); |
154 | 158 | ||
155 | for (uint8_t i=0; i<8; i++) { |
159 | for (uint8_t i=0; i<8; i++) { |
156 | staticParams.userParams[i] = i; |
160 | staticParams.userParams[i] = i; |
157 | } |
161 | } |
158 | 162 | ||
159 | staticParams.bitConfig = |
163 | staticParams.bitConfig = |
160 | CFG_GYRO_SATURATION_PREVENTION | CFG_COMPASS_ENABLED; |
164 | CFG_GYRO_SATURATION_PREVENTION | CFG_COMPASS_ENABLED; |
161 | 165 | ||
162 | memcpy(staticParams.name, "Default\0", 6); |
166 | memcpy(staticParams.name, "Default\0", 6); |
163 | } |
167 | } |
164 | 168 | ||
165 | void IMUConfig_default(void) { |
169 | void IMUConfig_default(void) { |
166 | IMUConfig.gyroPIDFilterConstant = 1; |
170 | IMUConfig.gyroPIDFilterConstant = 1; |
167 | IMUConfig.gyroDFilterConstant = 1; |
171 | IMUConfig.gyroDFilterConstant = 1; |
168 | IMUConfig.accFilterConstant = 10; |
- | |
169 | IMUConfig.rateTolerance = 120; |
172 | IMUConfig.rateTolerance = 120; |
- | 173 | IMUConfig.gyroDWindowLength = 3; |
|
- | 174 | IMUConfig.gyroQuadrant = 0; |
|
- | 175 | IMUConfig.imuReversedFlags = 0; |
|
170 | gyro_setDefaultParameters(); |
176 | gyro_setDefaultParameters(); |
171 | } |
177 | } |
172 | 178 | ||
173 | /***************************************************/ |
179 | /***************************************************/ |
174 | /* Default Values for R/C Channels */ |
180 | /* Default Values for R/C Channels */ |
175 | /***************************************************/ |
181 | /***************************************************/ |
176 | void channelMap_default(void) { |
182 | void channelMap_default(void) { |
177 | channelMap.channels[CH_ELEVATOR] = 1; |
183 | channelMap.channels[CH_ELEVATOR] = 1; |
178 | channelMap.channels[CH_AILERONS] = 0; |
184 | channelMap.channels[CH_AILERONS] = 0; |
179 | channelMap.channels[CH_THROTTLE] = 2; |
185 | channelMap.channels[CH_THROTTLE] = 2; |
180 | channelMap.channels[CH_RUDDER] = 3; |
186 | channelMap.channels[CH_RUDDER] = 3; |
181 | channelMap.channels[CH_POTS + 0] = 4; |
187 | channelMap.channels[CH_POTS + 0] = 4; |
182 | channelMap.channels[CH_POTS + 1] = 5; |
188 | channelMap.channels[CH_POTS + 1] = 5; |
183 | channelMap.channels[CH_POTS + 2] = 6; |
189 | channelMap.channels[CH_POTS + 2] = 6; |
184 | channelMap.channels[CH_POTS + 3] = 7; |
190 | channelMap.channels[CH_POTS + 3] = 7; |
185 | } |
191 | } |
186 | 192 |