Subversion Repositories FlightCtrl

Rev

Rev 2109 | Go to most recent revision | Details | 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
 
88
  // Servos
89
  staticParams.servoCount = 7;
90
  staticParams.servoManualMaxSpeed = 10;
91
  for (uint8_t i=0; i<2; i++) {
92
    staticParams.servoConfigurations[i].manualControl = 128;
93
    staticParams.servoConfigurations[i].stabilizationFactor = 0;
94
    staticParams.servoConfigurations[i].minValue = 32;
95
    staticParams.servoConfigurations[i].maxValue = 224;
96
    staticParams.servoConfigurations[i].flags = 0;
97
  }
98
 
99
  // Battery warning and emergency flight
100
  staticParams.batteryVoltageWarning = 101;  // 3.7 each for 3S
101
  staticParams.emergencyThrottle = 35;
102
  staticParams.emergencyFlightDuration = 30;
103
 
104
  for (uint8_t i=0; i<3; i++) {
105
          staticParams.gyroPID[i].P = 80;
106
          staticParams.gyroPID[i].I = 80;
107
          staticParams.gyroPID[i].D = 40;
108
  }
109
 
110
  staticParams.stickIElevator = 80;
111
  staticParams.stickIAilerons = 120;
112
  staticParams.stickIRudder = 40;
113
 
114
  // Outputs
115
  staticParams.outputFlash[0].bitmask = 1; //0b01011111;
116
  staticParams.outputFlash[0].timing = 15;
117
  staticParams.outputFlash[1].bitmask = 3; //0b11110011;
118
  staticParams.outputFlash[1].timing = 15;
119
 
120
  staticParams.outputDebugMask = 8;
121
  staticParams.outputFlags   = OUTPUTFLAGS_FLASH_0_AT_BEEP | OUTPUTFLAGS_FLASH_1_AT_BEEP | OUTPUTFLAGS_USE_ONBOARD_LEDS;
122
}
123
 
124
/***************************************************/
125
/*    Default Values for parameter set 1           */
126
/***************************************************/
127
void paramSet_default(uint8_t setnumber) {
128
  setOtherDefaults();
129
 
130
  for (uint8_t i=0; i<8; i++) {
131
    staticParams.userParams[i] = i;
132
  }
133
 
134
  staticParams.bitConfig =
135
    CFG_GYRO_SATURATION_PREVENTION;
136
 
137
  memcpy(staticParams.name, "Default\0", 6);
138
}
139
 
140
void IMUConfig_default(void) {
141
  IMUConfig.gyroPIDFilterConstant = 1;
142
  IMUConfig.gyroDFilterConstant = 1;
143
  IMUConfig.rateTolerance = 120;
144
  IMUConfig.gyroDWindowLength = 3;
145
  IMUConfig.gyroQuadrant = 0;
146
  IMUConfig.imuReversedFlags = 0;
147
}
148
 
149
/***************************************************/
150
/*    Default Values for R/C Channels              */
151
/***************************************************/
152
void channelMap_default(void) {
153
  channelMap.channels[CH_ELEVATOR] = 1;
154
  channelMap.channels[CH_AILERONS] = 0;
155
  channelMap.channels[CH_THROTTLE] = 2;
156
  channelMap.channels[CH_RUDDER]   = 3;
157
  channelMap.channels[CH_POTS + 0] = 4;
158
  channelMap.channels[CH_POTS + 1] = 5;
159
  channelMap.channels[CH_POTS + 2] = 6;
160
  channelMap.channels[CH_POTS + 3] = 7;
161
}