Subversion Repositories FlightCtrl

Rev

Rev 2142 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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