Skip to content

Commit 67f0c86

Browse files
EmilsPaschugabe
authored andcommitted
New functions for Fixed wing
Apa, Cruise Mode and fix for Vector trust. Moved Motor code Moved Motor code Syntax Syntax Formatting Formatting Formatting Formatting Formatting Update fw_nav.c Update mw.c Cleaning up mixerTrustVector Update mixer.c Update fw_nav.c Update mw.c Removed Hex file Renaming vector Thrust Update mw.c Update mw.c
1 parent 978aa62 commit 67f0c86

8 files changed

Lines changed: 110 additions & 38 deletions

File tree

src/cli.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,19 @@ const clivalue_t valueTable[] = {
262262
{ "p_vel", VAR_UINT8, &cfg.P8[PIDVEL], 0, 200 },
263263
{ "i_vel", VAR_UINT8, &cfg.I8[PIDVEL], 0, 200 },
264264
{ "d_vel", VAR_UINT8, &cfg.D8[PIDVEL], 0, 200 },
265+
{ "fw_vector_thrust", VAR_UINT8, &cfg.fw_vector_thrust, 0, 1},
265266
{ "fw_gps_maxcorr", VAR_INT16, &cfg.fw_gps_maxcorr, -45, 45 },
266267
{ "fw_gps_rudder", VAR_INT16, &cfg.fw_gps_rudder, -45, 45 },
267268
{ "fw_gps_maxclimb", VAR_INT16, &cfg.fw_gps_maxclimb, -45, 45 },
268269
{ "fw_gps_maxdive", VAR_INT16, &cfg.fw_gps_maxdive, -45, 45 },
270+
{ "fw_glide_angle", VAR_UINT8, &cfg.fw_glide_angle, 0, 100 },
269271
{ "fw_climb_throttle", VAR_UINT16, &cfg.fw_climb_throttle, 1000, 2000 },
270272
{ "fw_cruise_throttle", VAR_UINT16, &cfg.fw_cruise_throttle, 1000, 2000 },
271273
{ "fw_idle_throttle", VAR_UINT16, &cfg.fw_idle_throttle, 1000, 2000 },
272274
{ "fw_scaler_throttle", VAR_UINT16, &cfg.fw_scaler_throttle, 0, 15 },
273-
{ "fw_roll_comp", VAR_FLOAT, &cfg.fw_roll_comp, 0, 2 },
274-
{ "fw_rth_alt", VAR_UINT8, &cfg.fw_rth_alt, 0, 200 },
275+
{ "fw_roll_comp", VAR_UINT8, &cfg.fw_roll_comp, 0, 250 },
276+
{ "fw_rth_alt", VAR_UINT8, &cfg.fw_rth_alt, 0, 250 },
277+
{ "fw_cruise_distance", VAR_UINT16, &cfg.fw_cruise_distance, 0, 2000},
275278
};
276279

277280
#define VALUE_COUNT (sizeof(valueTable) / sizeof(clivalue_t))
@@ -1044,12 +1047,11 @@ static void cliMixer(char *cmdline)
10441047
// Presets for planes. Not functional with current reset
10451048
// Really Ugly Hack
10461049
if (mcfg.mixerConfiguration == MULTITYPE_FLYING_WING || mcfg.mixerConfiguration == MULTITYPE_AIRPLANE) {
1047-
cfg.dynThrPID = 50;
1050+
cfg.dynThrPID = 90;
10481051
cfg.rcExpo8 = 0;
10491052
cfg.P8[PIDALT] = 30;
10501053
cfg.I8[PIDALT] = 20;
10511054
cfg.D8[PIDALT] = 45;
1052-
cfg.D8[PIDPOSR] = 50; // RTH Alt
10531055
cfg.P8[PIDNAVR] = 30;
10541056
cfg.I8[PIDNAVR] = 20;
10551057
cfg.D8[PIDNAVR] = 45;

src/config.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ master_t mcfg; // master config struct with data independent from profiles
2424
config_t cfg; // profile config struct
2525
const char rcChannelLetters[] = "AERT123456789LMNOP"; // hack for the char-based channel mapping stuff, 18 channels hard max
2626

27-
static const uint8_t EEPROM_CONF_VERSION = 75;
27+
static const uint8_t EEPROM_CONF_VERSION = 76;
2828
static uint32_t enabledSensors = 0;
2929
static void resetConf(void);
3030
static const uint32_t FLASH_WRITE_ADDR = 0x08000000 + (FLASH_PAGE_SIZE * (FLASH_PAGE_COUNT - (CONFIG_SIZE / 1024)));
@@ -355,7 +355,9 @@ static void resetConf(void)
355355
cfg.fw_cruise_throttle = 1500;
356356
cfg.fw_idle_throttle = 1300;
357357
cfg.fw_scaler_throttle = 8;
358-
cfg.fw_roll_comp = 1;
358+
cfg.fw_roll_comp = 100;
359+
cfg.fw_cruise_distance = 500;
360+
cfg.fw_rth_alt = 50;
359361
// control stuff
360362
mcfg.reboot_character = 'R';
361363

src/fw_nav.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
// from gps.c
55
extern int32_t nav_bearing;
66
extern int32_t wp_distance;
7+
extern int32_t GPS_WP[2];
8+
extern float GPS_scaleLonDown;
9+
710
extern PID_PARAM navPID_PARAM;
811
extern PID_PARAM altPID_PARAM;
912

1013
#define GPS_UPD_HZ 5 // Set loop time for NavUpdate 5 Hz is enough
1114
#define PITCH_COMP 0.5f // Compensate throttle relative angle of attack
1215
// Candidates for CLI
13-
#define SAFE_NAV_ALT 20 // Safe Altitude during climbouts Wings Level below this Alt. (ex. trees & buildings..)
16+
#define SAFE_NAV_ALT 25 // Safe Altitude during climbouts Wings Level below this Alt. (ex. trees & buildings..)
1417
#define SAFE_DECSCEND_ZONE 50 // Radius around home where descending is OK
1518
// For speedBoost
1619
#define GPS_MINSPEED 500 // 500= ~18km/h
1720
#define I_TERM 0.1f
21+
#define GEO_SKALEFACT 89.832f // Scale to match meters
1822

1923
float navErrorI;
2024
float altErrorI;
@@ -40,6 +44,19 @@ void fw_nav_reset(void)
4044
}
4145
}
4246

47+
void fw_FlyTo(void) // PatrikE CruiseMode version
48+
{
49+
float wp_lat_diff, wp_lon_diff, scaler;
50+
int32_t holdHeading = GPS_ground_course / 10;
51+
if (holdHeading > 180)
52+
holdHeading -= 360;
53+
scaler = (GEO_SKALEFACT / GPS_scaleLonDown) * cfg.fw_cruise_distance;
54+
wp_lat_diff = cos(holdHeading * 0.0174532925f);
55+
wp_lon_diff = sin(holdHeading * 0.0174532925f) * GPS_scaleLonDown;
56+
GPS_WP[LAT] += wp_lat_diff * scaler;
57+
GPS_WP[LON] += wp_lon_diff * scaler;
58+
}
59+
4360
void fw_nav(void)
4461
{
4562
int16_t GPS_Heading = GPS_ground_course; // Store current bearing
@@ -130,7 +147,6 @@ void fw_nav(void)
130147
if (abs(navDiff) > 170)
131148
navDiff = 175; // Forced turn.
132149

133-
134150
// PID for Navigating planes.
135151
navDT = (float) (millis() - nav_loopT) / 1000;
136152
nav_loopT = millis();
@@ -201,16 +217,15 @@ void fw_nav(void)
201217
// Elevator compensation depending on behaviour.
202218
// Prevent stall with Disarmed motor
203219
if (f.MOTORS_STOPPED)
204-
GPS_angle[PITCH] = constrain(GPS_angle[PITCH], 0, cfg.fw_gps_maxdive * 10);
220+
GPS_angle[PITCH] = constrain(GPS_angle[PITCH], -cfg.fw_glide_angle, cfg.fw_gps_maxdive * 10);
205221

206222
// Add elevator compared with rollAngle
207223
if (!f.CLIMBOUT_FW)
208-
GPS_angle[PITCH] -= (abs(angle[ROLL]) * cfg.fw_roll_comp);
224+
GPS_angle[PITCH] -= (abs(angle[ROLL]) * (cfg.fw_roll_comp / 100));
209225

210226
// Throttle compensation depending on behaviour.
211227
// Compensate throttle with pitch Angle
212228
NAV_Thro -= constrain(angle[PITCH] * PITCH_COMP, 0, 450);
213-
NAV_Thro = constrain(NAV_Thro, cfg.fw_idle_throttle, cfg.fw_climb_throttle);
214229

215230
// Force the Plane move forward in headwind with speedBoost
216231
groundSpeed = GPS_speed;
@@ -221,6 +236,9 @@ void fw_nav(void)
221236

222237
speedBoost = constrain(speedBoost, 0, 500);
223238
NAV_Thro += speedBoost;
239+
240+
// constrain throttle to Max climb.
241+
NAV_Thro = constrain(NAV_Thro, cfg.fw_idle_throttle, cfg.fw_climb_throttle);
224242
}
225243
// End of NavTimer
226244

src/gps.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,15 @@ static void reset_PID(PID *pid)
505505

506506
static float dTnav; // Delta Time in milliseconds for navigation computations, updated with every good GPS read
507507
static int16_t actual_speed[2] = { 0, 0 };
508-
static float GPS_scaleLonDown = 1.0f; // this is used to offset the shrinking longitude as we go towards the poles
508+
float GPS_scaleLonDown = 1.0f; // this is used to offset the shrinking longitude as we go towards the poles
509509

510510
// The difference between the desired rate of travel and the actual rate of travel
511511
// updated after GPS read - 5-10hz
512512
static int16_t rate_error[2];
513513
static int32_t error[2];
514514

515515
// Currently used WP
516-
static int32_t GPS_WP[2];
516+
int32_t GPS_WP[2];
517517

518518
////////////////////////////////////////////////////////////////////////////////
519519
// Location & Navigation
@@ -768,6 +768,9 @@ void GPS_set_next_wp(int32_t *lat, int32_t *lon)
768768
GPS_WP[LON] = *lon;
769769

770770
GPS_calc_longitude_scaling(*lat);
771+
if (f.CRUISE_MODE)
772+
fw_FlyTo(); // PatrikE CruiseMode version
773+
771774
GPS_distance_cm_bearing(&GPS_coord[LAT], &GPS_coord[LON], &GPS_WP[LAT], &GPS_WP[LON], &wp_distance, &target_bearing);
772775

773776
nav_bearing = target_bearing;

src/mixer.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static const motorMixer_t mixerDualcopter[] = {
134134
{ 1.0f, 0.0f, 0.0f, 1.0f }, // RIGHT
135135
};
136136

137-
static const motorMixer_t mixerTrustVector[] = {
137+
static const motorMixer_t mixerVectorThrust[] = {
138138
{ 1.0f, 0.0f, 0.0f, -0.5f }, // LEFT
139139
{ 1.0f, 0.0f, 0.0f, 0.5f }, // RIGHT
140140
};
@@ -150,13 +150,13 @@ const mixer_t mixers[] = {
150150
{ 0, 1, NULL }, // * MULTITYPE_GIMBAL
151151
{ 6, 0, mixerY6 }, // MULTITYPE_Y6
152152
{ 6, 0, mixerHex6P }, // MULTITYPE_HEX6
153-
{ 2, 1, mixerTrustVector }, // * MULTITYPE_FLYING_WING
153+
{ 2, 1, mixerVectorThrust }, // * MULTITYPE_FLYING_WING
154154
{ 4, 0, mixerY4 }, // MULTITYPE_Y4
155155
{ 6, 0, mixerHex6X }, // MULTITYPE_HEX6X
156156
{ 8, 0, mixerOctoX8 }, // MULTITYPE_OCTOX8
157157
{ 8, 0, mixerOctoFlatP }, // MULTITYPE_OCTOFLATP
158158
{ 8, 0, mixerOctoFlatX }, // MULTITYPE_OCTOFLATX
159-
{ 1, 1, NULL }, // * MULTITYPE_AIRPLANE
159+
{ 2, 1, mixerVectorThrust }, // * MULTITYPE_AIRPLANE
160160
{ 0, 1, NULL }, // * MULTITYPE_HELI_120_CCPM
161161
{ 0, 1, NULL }, // * MULTITYPE_HELI_90_DEG
162162
{ 4, 0, mixerVtail4 }, // MULTITYPE_VTAIL4
@@ -166,7 +166,7 @@ const mixer_t mixers[] = {
166166
{ 1, 1, NULL }, // MULTITYPE_SINGLECOPTER
167167
{ 4, 0, mixerAtail4 }, // MULTITYPE_ATAIL4
168168
{ 0, 0, NULL }, // MULTITYPE_CUSTOM
169-
{ 1, 1, NULL }, // MULTITYPE_CUSTOM_PLANE
169+
{ 2, 1, mixerVectorThrust }, // MULTITYPE_CUSTOM_PLANE
170170
};
171171

172172
// mixer rule format servo, input, rate, speed, min, max, box
@@ -545,15 +545,18 @@ void mixTable(void)
545545
}
546546

547547
// motors for non-servo mixes
548-
if (numberMotor > 1)
549-
for (i = 0; i < numberMotor; i++)
548+
if (numberMotor > 1) {
549+
for (i = 0; i < numberMotor; i++) {
550550
motor[i] = rcCommand[THROTTLE] * currentMixer[i].throttle + axisPID[PITCH] * currentMixer[i].pitch + axisPID[ROLL] * currentMixer[i].roll + -cfg.yaw_direction * axisPID[YAW] * currentMixer[i].yaw;
551-
552-
if (f.FIXED_WING) {
553-
if (!f.ARMED)
554-
motor[0] = mcfg.mincommand; // Kill throttle when disarmed
555-
else
556-
motor[0] = constrain(rcCommand[THROTTLE], mcfg.minthrottle, mcfg.maxthrottle);
551+
if (f.FIXED_WING) { // vector_thrust handeling
552+
if (cfg.fw_vector_thrust) {
553+
if (f.PASSTHRU_MODE)
554+
motor[i] = rcCommand[THROTTLE] - rcCommand[YAW] * (i - 0.5f);
555+
} else { // Override mixerVectorThrust
556+
motor[i] = rcCommand[THROTTLE];
557+
}
558+
}
559+
}
557560
}
558561

559562
// airplane / servo mixes
@@ -639,6 +642,7 @@ void mixTable(void)
639642
}
640643
if (!f.ARMED) {
641644
motor[i] = motor_disarmed[i];
645+
f.MOTORS_STOPPED = 1;
642646
}
643647
}
644648
}

src/mw.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void annexCode(void)
101101
static uint32_t calibratedAccTime;
102102
int32_t tmp, tmp2;
103103
int32_t axis, prop1, prop2;
104+
static uint16_t MaxBrkpoint = 300; // Max angle of APA
104105

105106
// vbat shit
106107
static uint8_t vbatTimer = 0;
@@ -109,17 +110,39 @@ void annexCode(void)
109110
static int64_t mAhdrawnRaw = 0;
110111
static int32_t vbatCycleTime = 0;
111112

112-
// PITCH & ROLL only dynamic PID adjustemnt, depending on throttle value
113-
if (rcData[THROTTLE] < cfg.tpa_breakpoint) {
114-
prop2 = 100;
113+
if (!f.FIXED_WING) { // Baseflight original dynamic PID adjustemnt
114+
// PITCH & ROLL only dynamic PID adjustemnt, depending on throttle value
115+
if (rcData[THROTTLE] < cfg.tpa_breakpoint) {
116+
prop2 = 100;
117+
} else {
118+
if (rcData[THROTTLE] < 2000) {
119+
prop2 = 100 - (uint16_t)cfg.dynThrPID * (rcData[THROTTLE] - cfg.tpa_breakpoint) / (2000 - cfg.tpa_breakpoint);
120+
} else {
121+
prop2 = 100 - cfg.dynThrPID;
122+
}
123+
}
115124
} else {
116-
if (rcData[THROTTLE] < 2000) {
117-
prop2 = 100 - (uint16_t)cfg.dynThrPID * (rcData[THROTTLE] - cfg.tpa_breakpoint) / (2000 - cfg.tpa_breakpoint);
125+
// Throttle & Angle combined PID Attenuation
126+
// Will dampen the PID's in High speeds dive on Fixed Wing Only
127+
prop2 = 128; // prop2 was 100, is 128 now
128+
if (rcData[THROTTLE] < cfg.tpa_breakpoint) {
129+
prop2 = 128; // Higher prop2 for Fixed wing Same as used in MWii
118130
} else {
119-
prop2 = 100 - cfg.dynThrPID;
131+
if (rcCommand[THROTTLE] > cfg.dynThrPID) { // Using rcCommand() to include Tpa even in Gps modes.
132+
if (rcCommand[THROTTLE] < 2000) {
133+
prop2 -= ((uint16_t)cfg.dynThrPID * (rcCommand[THROTTLE] - cfg.dynThrPID) >> 9);
134+
} else {
135+
prop2 -= cfg.dynThrPID;
136+
}
137+
}
120138
}
139+
// APA dynamic PID adjustemnt, depending on Angle of attack
140+
if (angle[1] > 20)
141+
prop2 -= ((uint16_t)cfg.dynThrPID * (min(angle[1], MaxBrkpoint)) >> 8);
142+
prop2 = max((128 - cfg.dynThrPID), prop2);
121143
}
122144

145+
123146
for (axis = 0; axis < 3; axis++) {
124147
tmp = min(abs(rcData[axis] - mcfg.midrc), 500);
125148
if (axis != 2) { // ROLL & PITCH
@@ -692,6 +715,11 @@ void loop(void)
692715
auxState |= (rcData[AUX1 + i] < 1300) << (3 * i) | (1300 < rcData[AUX1 + i] && rcData[AUX1 + i] < 1700) << (3 * i + 1) | (rcData[AUX1 + i] > 1700) << (3 * i + 2);
693716
for (i = 0; i < CHECKBOXITEMS; i++)
694717
rcOptions[i] = (auxState & cfg.activate[i]) > 0;
718+
f.CRUISE_MODE = rcOptions[BOXGCRUISE];
719+
if (f.CRUISE_MODE) {
720+
rcOptions[BOXGPSHOLD] = true;
721+
rcOptions[BOXHORIZON] = true;
722+
}
695723

696724
// note: if FAILSAFE is disable, failsafeCnt > 5 * FAILSAVE_DELAY is always false
697725
if ((rcOptions[BOXANGLE] || (failsafeCnt > 5 * cfg.failsafe_delay)) && (sensors(SENSOR_ACC))) {
@@ -783,6 +811,9 @@ void loop(void)
783811
#ifdef GPS
784812
if (sensors(SENSOR_GPS)) {
785813
if (f.GPS_FIX && GPS_numSat >= 5) {
814+
if (nav_mode != NAV_MODE_NONE && (!f.HORIZON_MODE && !f.ANGLE_MODE))
815+
f.ANGLE_MODE = true; // Force a stable mode in GPS Mode
816+
786817
// if both GPS_HOME & GPS_HOLD are checked => GPS_HOME is the priority
787818
if (rcOptions[BOXGPSHOME] || f.FW_FAILSAFE_RTH_ENABLE ) {
788819
if (!f.GPS_HOME_MODE) {
@@ -833,7 +864,7 @@ void loop(void)
833864
f.PASSTHRU_MODE = 0;
834865
}
835866

836-
if (mcfg.mixerConfiguration == MULTITYPE_FLYING_WING || mcfg.mixerConfiguration == MULTITYPE_AIRPLANE || mcfg.mixerConfiguration == MULTITYPE_CUSTOM_PLANE) {
867+
if (f.FIXED_WING) {
837868
f.HEADFREE_MODE = 0;
838869
if (feature(FEATURE_FAILSAFE) && failsafeCnt > (6 * cfg.failsafe_delay)) {
839870
f.PASSTHRU_MODE = 0;

src/mw.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ enum {
121121
BOXSERVO1,
122122
BOXSERVO2,
123123
BOXSERVO3,
124+
BOXGCRUISE,
124125
CHECKBOXITEMS
125126
};
126127

@@ -282,15 +283,18 @@ typedef struct config_t {
282283
uint16_t ap_mode; // Temporarily Disables GPS_HOLD_MODE to be able to make it possible to adjust the Hold-position when moving the sticks, creating a deadspan for GPS
283284

284285
// fw-related stuff
286+
uint8_t fw_vector_thrust; // Enable Vector trust on Twin Engine models
285287
int16_t fw_gps_maxcorr; // Degrees banking Allowed by GPS.
286-
int16_t fw_gps_rudder; // Maximum Rudder
288+
int16_t fw_gps_rudder; // Maximum input of Rudder Allowed by GPS.
287289
int16_t fw_gps_maxclimb; // Degrees climbing . To much can stall the plane.
288290
int16_t fw_gps_maxdive; // Degrees Diving . To much can overspeed the plane.
291+
uint8_t fw_glide_angle; // Glide angle in power off
289292
uint16_t fw_climb_throttle; // Max allowed throttle in GPS modes.
290293
uint16_t fw_cruise_throttle; // Throttle to set for cruisespeed.
291294
uint16_t fw_idle_throttle; // Lowest throttleValue during Descend
292295
uint16_t fw_scaler_throttle; // Adjust to Match Power/Weight ratio of your model
293-
float fw_roll_comp; // How much Elevator compensates Roll in GPS modes
296+
uint8_t fw_roll_comp; // Adds Elevator Based on Roll Angle
297+
int16_t fw_cruise_distance; // Distance to viritual WP.
294298
uint8_t fw_rth_alt; // Min Altitude to keep during RTH. (Max 200m)
295299

296300
} config_t;
@@ -427,6 +431,7 @@ typedef struct flags_t {
427431
uint8_t MOTORS_STOPPED;
428432
uint8_t FW_FAILSAFE_RTH_ENABLE;
429433
uint8_t CLIMBOUT_FW;
434+
uint8_t CRUISE_MODE;
430435
} flags_t;
431436

432437
extern int16_t gyroZero[3];
@@ -606,4 +611,5 @@ void GPS_reset_nav(void);
606611
void GPS_set_next_wp(int32_t *lat, int32_t *lon);
607612
int32_t wrap_18000(int32_t error);
608613
void fw_nav(void);
614+
void fw_FlyTo(void);
609615

0 commit comments

Comments
 (0)