Subversion Repositories FlightCtrl

Rev

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

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