Rev 2108 | Rev 2119 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2108 | - | 1 | #include <util/delay.h> |
2 | #include <stddef.h> |
||
3 | #include <string.h> |
||
4 | #include "configuration.h" |
||
5 | #include "sensors.h" |
||
6 | #include "rc.h" |
||
7 | #include "output.h" |
||
8 | #include "flight.h" |
||
9 | |||
10 | int16_t variables[VARIABLE_COUNT]; |
||
11 | ParamSet_t staticParams; |
||
12 | channelMap_t channelMap; |
||
13 | IMUConfig_t IMUConfig; |
||
14 | volatile DynamicParams_t dynamicParams; |
||
15 | |||
16 | VersionInfo_t versionInfo; |
||
17 | |||
18 | FlightMode_t currentFlightMode = FLIGHT_MODE_MANUAL; |
||
19 | // updated by considering airspeed.. |
||
20 | volatile uint16_t isFlying = 0; |
||
21 | |||
22 | const MMXLATION XLATIONS[] = { |
||
23 | {offsetof(ParamSet_t, externalControl), offsetof(DynamicParams_t, externalControl),0,255}, |
||
24 | |||
25 | {offsetof(ParamSet_t, gyroPID[0].P), offsetof(DynamicParams_t, gyroPID[0].P),0,255}, |
||
26 | {offsetof(ParamSet_t, gyroPID[0].I), offsetof(DynamicParams_t, gyroPID[0].I),0,255}, |
||
27 | {offsetof(ParamSet_t, gyroPID[0].D), offsetof(DynamicParams_t, gyroPID[0].D),0,255}, |
||
28 | |||
29 | {offsetof(ParamSet_t, gyroPID[1].P), offsetof(DynamicParams_t, gyroPID[1].P),0,255}, |
||
30 | {offsetof(ParamSet_t, gyroPID[1].I), offsetof(DynamicParams_t, gyroPID[1].I),0,255}, |
||
31 | {offsetof(ParamSet_t, gyroPID[1].D), offsetof(DynamicParams_t, gyroPID[1].D),0,255}, |
||
32 | |||
33 | {offsetof(ParamSet_t, gyroPID[2].P), offsetof(DynamicParams_t, gyroPID[2].P),0,255}, |
||
34 | {offsetof(ParamSet_t, gyroPID[2].I), offsetof(DynamicParams_t, gyroPID[2].I),0,255}, |
||
35 | {offsetof(ParamSet_t, gyroPID[2].D), offsetof(DynamicParams_t, gyroPID[2].D),0,255}, |
||
36 | |||
37 | {offsetof(ParamSet_t, servoConfigurations[0].manualControl), offsetof(DynamicParams_t, servoManualControl[0]),0,255}, |
||
38 | {offsetof(ParamSet_t, servoConfigurations[1].manualControl), offsetof(DynamicParams_t, servoManualControl[1]),0,255}, |
||
39 | {offsetof(ParamSet_t, outputFlash[0].timing), offsetof(DynamicParams_t, output0Timing),0,255}, |
||
40 | {offsetof(ParamSet_t, outputFlash[1].timing), offsetof(DynamicParams_t, output1Timing),0,255}, |
||
41 | }; |
||
42 | |||
43 | uint8_t configuration_applyVariableToParam(uint8_t src, uint8_t min, uint8_t max) { |
||
44 | uint8_t result; |
||
45 | if (src>=(256-VARIABLE_COUNT)) result = variables[src-(256-VARIABLE_COUNT)]; |
||
46 | else result = src; |
||
47 | if (result < min) result = min; |
||
48 | else if (result > max) result = max; |
||
49 | return result; |
||
50 | } |
||
51 | |||
52 | void configuration_applyVariablesToParams(void) { |
||
53 | uint8_t i, src; |
||
54 | uint8_t* pointerToTgt; |
||
55 | |||
56 | for(i=0; i<sizeof(XLATIONS)/sizeof(MMXLATION); i++) { |
||
57 | src = *((uint8_t*)(&staticParams) + XLATIONS[i].sourceIdx); |
||
58 | pointerToTgt = (uint8_t*)(&dynamicParams) + XLATIONS[i].targetIdx; |
||
59 | *pointerToTgt = configuration_applyVariableToParam(src, XLATIONS[i].min, XLATIONS[i].max); |
||
60 | } |
||
61 | |||
62 | // User parameters are always variable. |
||
63 | for (i=0; i<sizeof(staticParams.userParams); i++) { |
||
64 | src = *((uint8_t*)(&staticParams) + offsetof(ParamSet_t, userParams) + i); |
||
65 | pointerToTgt = (uint8_t*)(&dynamicParams) + offsetof(DynamicParams_t, userParams) + i; |
||
66 | *pointerToTgt = configuration_applyVariableToParam(src, 0, 255); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | void configuration_setFlightParameters(uint8_t newFlightMode) { |
||
71 | currentFlightMode = newFlightMode; |
||
72 | flight_updateFlightParametersToFlightMode(); |
||
73 | } |
||
74 | |||
75 | // Called after a change in configuration parameters, as a hook for modules to take over changes. |
||
76 | void configuration_paramSetDidChange(void) { |
||
77 | // This should be OK to do here as long as we don't change parameters during emergency flight. We don't. |
||
78 | configuration_setFlightParameters(currentFlightMode); |
||
79 | // Immediately load changes to output, and also signal the paramset change. |
||
80 | output_init(); |
||
81 | } |
||
82 | |||
83 | void setOtherDefaults(void) { |
||
84 | // Control |
||
85 | staticParams.externalControl = 0; |
||
86 | staticParams.IFactor = 52; |
||
87 | |||
2109 | - | 88 | staticParams.airspeedCorrection = 100; |
89 | staticParams.isFlyingThreshold = 100; |
||
90 | |||
2108 | - | 91 | // Servos |
92 | staticParams.servoCount = 7; |
||
93 | staticParams.servoManualMaxSpeed = 10; |
||
94 | for (uint8_t i=0; i<2; i++) { |
||
95 | staticParams.servoConfigurations[i].manualControl = 128; |
||
96 | staticParams.servoConfigurations[i].stabilizationFactor = 0; |
||
97 | staticParams.servoConfigurations[i].minValue = 32; |
||
98 | staticParams.servoConfigurations[i].maxValue = 224; |
||
99 | staticParams.servoConfigurations[i].flags = 0; |
||
100 | } |
||
101 | |||
102 | // Battery warning and emergency flight |
||
103 | staticParams.batteryVoltageWarning = 101; // 3.7 each for 3S |
||
104 | staticParams.emergencyThrottle = 35; |
||
105 | staticParams.emergencyFlightDuration = 30; |
||
106 | |||
107 | for (uint8_t i=0; i<3; i++) { |
||
108 | staticParams.gyroPID[i].P = 80; |
||
109 | staticParams.gyroPID[i].I = 80; |
||
110 | staticParams.gyroPID[i].D = 40; |
||
2109 | - | 111 | staticParams.gyroPID[i].iMax = 45; |
2108 | - | 112 | } |
113 | |||
114 | staticParams.stickIElevator = 80; |
||
115 | staticParams.stickIAilerons = 120; |
||
116 | staticParams.stickIRudder = 40; |
||
117 | |||
118 | // Outputs |
||
119 | staticParams.outputFlash[0].bitmask = 1; //0b01011111; |
||
120 | staticParams.outputFlash[0].timing = 15; |
||
121 | staticParams.outputFlash[1].bitmask = 3; //0b11110011; |
||
122 | staticParams.outputFlash[1].timing = 15; |
||
123 | |||
124 | staticParams.outputDebugMask = 8; |
||
125 | staticParams.outputFlags = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS; |
||
126 | } |
||
127 | |||
128 | /***************************************************/ |
||
129 | /* Default Values for parameter set 1 */ |
||
130 | /***************************************************/ |
||
131 | void paramSet_default(uint8_t setnumber) { |
||
132 | setOtherDefaults(); |
||
133 | |||
134 | for (uint8_t i=0; i<8; i++) { |
||
135 | staticParams.userParams[i] = i; |
||
136 | } |
||
137 | |||
138 | staticParams.bitConfig = |
||
139 | CFG_GYRO_SATURATION_PREVENTION; |
||
140 | |||
141 | memcpy(staticParams.name, "Default\0", 6); |
||
142 | } |
||
143 | |||
144 | void IMUConfig_default(void) { |
||
145 | IMUConfig.gyroPIDFilterConstant = 1; |
||
146 | IMUConfig.gyroDFilterConstant = 1; |
||
147 | IMUConfig.rateTolerance = 120; |
||
148 | IMUConfig.gyroDWindowLength = 3; |
||
149 | IMUConfig.gyroQuadrant = 0; |
||
150 | IMUConfig.imuReversedFlags = 0; |
||
151 | } |
||
152 | |||
153 | /***************************************************/ |
||
154 | /* Default Values for R/C Channels */ |
||
155 | /***************************************************/ |
||
156 | void channelMap_default(void) { |
||
157 | channelMap.channels[CH_ELEVATOR] = 1; |
||
158 | channelMap.channels[CH_AILERONS] = 0; |
||
159 | channelMap.channels[CH_THROTTLE] = 2; |
||
160 | channelMap.channels[CH_RUDDER] = 3; |
||
161 | channelMap.channels[CH_POTS + 0] = 4; |
||
162 | channelMap.channels[CH_POTS + 1] = 5; |
||
163 | channelMap.channels[CH_POTS + 2] = 6; |
||
164 | channelMap.channels[CH_POTS + 3] = 7; |
||
165 | } |