Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1612 dongfang 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
1963 - 7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist.
1612 dongfang 8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
1963 - 17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
1612 dongfang 19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
1868 - 35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1612 dongfang 36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1963 - 47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1612 dongfang 49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
#include <stdlib.h>
53
#include "controlMixer.h"
54
#include "rc.h"
1775 - 55
#include "heightControl.h"
56
#include "attitudeControl.h"
1612 dongfang 57
#include "externalControl.h"
58
#include "configuration.h"
59
#include "attitude.h"
1775 - 60
#include "commands.h"
61
#include "output.h"
1612 dongfang 62
 
1908 - 63
// uint16_t maxControl[2] = { 0, 0 };
64
uint16_t controlActivity = 0;
65
int16_t controls[4] = { 0, 0, 0, 0 };
1612 dongfang 66
 
67
// Internal variables for reading commands made with an R/C stick.
1821 - 68
uint8_t lastCommand = COMMAND_NONE;
1775 - 69
uint8_t lastArgument;
1612 dongfang 70
 
1775 - 71
uint8_t isCommandRepeated = 0;
1963 - 72
uint8_t controlMixer_didReceiveSignal = 0;
1775 - 73
 
1612 dongfang 74
/*
75
 * This could be expanded to take arguments from ohter sources than the RC
76
 * (read: Custom MK RC project)
77
 */
78
uint8_t controlMixer_getArgument(void) {
1961 - 79
  return lastArgument;
1612 dongfang 80
}
81
 
82
/*
83
 * This could be expanded to take calibrate / start / stop commands from ohter sources
84
 * than the R/C (read: Custom MK R/C project)
85
 */
86
uint8_t controlMixer_getCommand(void) {
1961 - 87
  return lastCommand;
1612 dongfang 88
}
89
 
90
uint8_t controlMixer_isCommandRepeated(void) {
1961 - 91
  return isCommandRepeated;
1612 dongfang 92
}
93
 
1775 - 94
void controlMixer_setNeutral() {
1961 - 95
  for (uint8_t i=0; i<VARIABLE_COUNT; i++) {
96
    variables[i] = 0;
97
  }
98
  EC_setNeutral();
99
  HC_setGround();
1612 dongfang 100
}
101
 
102
/*
103
 * Set the potientiometer values to the momentary values of the respective R/C channels.
104
 * No slew rate limitation.
105
 */
106
void controlMixer_initVariables(void) {
1961 - 107
  uint8_t i;
108
  for (i=0; i < VARIABLE_COUNT; i++) {
109
    variables[i] = RC_getVariable(i);
110
  }
1612 dongfang 111
}
112
 
113
/*
114
 * Update potentiometer values with limited slew rate. Could be made faster if desired.
1775 - 115
 * TODO: It assumes R/C as source. Not necessarily true.
1612 dongfang 116
 */
117
void controlMixer_updateVariables(void) {
1961 - 118
  uint8_t i;
119
  int16_t targetvalue;
120
  for (i=0; i < VARIABLE_COUNT; i++) {
121
    targetvalue = RC_getVariable(i);
122
    if (targetvalue < 0)
123
      targetvalue = 0;
124
    if (variables[i] < targetvalue && variables[i] < 255)
125
      variables[i]++;
126
    else if (variables[i] > 0 && variables[i] > targetvalue)
127
      variables[i]--;
128
  }
1612 dongfang 129
}
130
 
131
uint8_t controlMixer_getSignalQuality(void) {
1961 - 132
  uint8_t rcQ = RC_getSignalQuality();
133
  uint8_t ecQ = EC_getSignalQuality();
134
  // This needs not be the only correct solution...
135
  return rcQ > ecQ ? rcQ : ecQ;
1612 dongfang 136
}
137
 
1908 - 138
void updateControlAndMeasureControlActivity(uint8_t index, int16_t newValue) {
1961 - 139
  int16_t tmp = controls[index];
140
  controls[index] = newValue;
141
  tmp -= newValue;
142
  tmp /= 2;
143
  tmp = tmp * tmp;
144
  // tmp += (newValue >= 0) ? newValue : -newValue;
145
  if (controlActivity + (uint16_t)tmp >= controlActivity)
146
    controlActivity += tmp;
147
  else controlActivity = 0xffff;
1908 - 148
}
149
 
150
#define CADAMPING 10
151
void dampenControlActivity(void) {
1961 - 152
  uint32_t tmp = controlActivity;
153
  tmp *= ((1<<CADAMPING)-1);
154
  tmp >>= CADAMPING;
155
  controlActivity = tmp;
1908 - 156
}
157
 
1612 dongfang 158
/*
159
 * Update the variables indicating stick position from the sum of R/C, GPS and external control.
160
 */
161
void controlMixer_update(void) {
1961 - 162
  // calculate Stick inputs by rc channels (P) and changing of rc channels (D)
163
  // TODO: If no signal --> zero.
164
  int16_t tempThrottle;
165
 
166
  // takes almost no time...
167
  RC_update();
168
 
169
  // takes almost no time...
170
  EC_update();
171
 
172
  // takes about 80 usec.
173
  HC_update();
174
 
175
  int16_t* RC_PRTY = RC_getPRTY();
176
  int16_t* EC_PRTY = EC_getPRTY();
177
 
178
  updateControlAndMeasureControlActivity(CONTROL_PITCH, RC_PRTY[CONTROL_PITCH] + EC_PRTY[CONTROL_PITCH]);
179
  updateControlAndMeasureControlActivity(CONTROL_ROLL, RC_PRTY[CONTROL_ROLL] + EC_PRTY[CONTROL_ROLL]);
180
  updateControlAndMeasureControlActivity(CONTROL_YAW, RC_PRTY[CONTROL_YAW] + EC_PRTY[CONTROL_YAW]);
181
  dampenControlActivity();
1775 - 182
 
1961 - 183
  //debugOut.analog[14] = controlActivity/10;
184
 
185
  tempThrottle = HC_getThrottle(RC_PRTY[CONTROL_THROTTLE] + EC_PRTY[CONTROL_THROTTLE]);
186
  controls[CONTROL_THROTTLE] = AC_getThrottle(tempThrottle);
187
 
188
  // controlYaw = RC_PRTY[CONTROL_YAW] + EC_PRTY[CONTROL_YAW];
189
 
190
  if (controlMixer_getSignalQuality() >= SIGNAL_GOOD) {
191
    controlMixer_updateVariables();
192
    configuration_applyVariablesToParams();
1963 - 193
    controlMixer_didReceiveSignal = 1;
1961 - 194
  } else { // Signal is not OK
195
    // Could handle switch to emergency flight here.
196
    // throttle is handled elsewhere.
197
  }
198
 
199
  // part1a end.
200
 
201
  /* This is not really necessary with the dead-band feature on all sticks (see rc.c)
202
     if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) {
203
     if (controlYaw > 2) controlYaw-= 2;
204
     else if (controlYaw< -2) controlYaw += 2;
205
     else controlYaw = 0;
206
     }
207
  */
208
 
209
  /*
210
   * Record maxima
211
   for (axis = PITCH; axis <= ROLL; axis++) {
212
   if (abs(control[axis] / CONTROL_SCALING) > maxControl[axis]) {
213
   maxControl[axis] = abs(control[axis]) / CONTROL_SCALING;
214
   if (maxControl[axis] > 100)
215
   maxControl[axis] = 100;
216
   } else if (maxControl[axis])
217
   maxControl[axis]--;
218
   }
219
  */
1612 dongfang 220
 
1961 - 221
  uint8_t rcCommand = (RC_getSignalQuality() >= SIGNAL_OK) ? RC_getCommand()
222
    : COMMAND_NONE;
223
  uint8_t ecCommand = (EC_getSignalQuality() >= SIGNAL_OK) ? EC_getCommand()
224
    : COMMAND_NONE;
225
 
226
  if (rcCommand != COMMAND_NONE) {
227
    isCommandRepeated = (lastCommand == rcCommand);
228
    lastCommand = rcCommand;
229
    lastArgument = RC_getArgument();
230
  } else if (ecCommand != COMMAND_NONE) {
231
    isCommandRepeated = (lastCommand == ecCommand);
232
    lastCommand = ecCommand;
233
    lastArgument = EC_getArgument();
234
  } else {
235
    // Both sources have no command, or one or both are out.
236
    // Just set to false. There is no reason to check if the none-command was repeated anyway.
237
    isCommandRepeated = 0;
238
    lastCommand = COMMAND_NONE;
239
  }
240
 
241
  /*
242
    if (isCommandRepeated)
243
    DebugOut.Digital[0] |= DEBUG_COMMANDREPEATED;
244
    else
245
    DebugOut.Digital[0] &= ~DEBUG_COMMANDREPEATED;
246
    if (rcCommand)
247
    DebugOut.Digital[1] |= DEBUG_COMMANDREPEATED;
248
    else
249
    DebugOut.Digital[1] &= ~DEBUG_COMMANDREPEATED;
250
  */
1612 dongfang 251
}
252
 
1775 - 253
// TODO: Integrate into command system.
254
uint8_t controlMixer_testCompassCalState(void) {
1961 - 255
  return RC_testCompassCalState();
1612 dongfang 256
}