Subversion Repositories FlightCtrl

Rev

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

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