-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Expand file tree
/
Copy pathtailsitter.cpp
More file actions
373 lines (296 loc) · 12.9 KB
/
tailsitter.cpp
File metadata and controls
373 lines (296 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/****************************************************************************
*
* Copyright (c) 2015-2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file tailsitter.cpp
*
* @author Roman Bapst <bapstroman@gmail.com>
* @author David Vorsin <davidvorsin@gmail.com>
*
*/
#include "tailsitter.h"
#include "vtol_att_control_main.h"
using namespace matrix;
Tailsitter::Tailsitter(VtolAttitudeControl *attc) :
VtolType(attc)
{
}
void
Tailsitter::parameters_update()
{
VtolType::updateParams();
}
void Tailsitter::update_vtol_state()
{
/* simple logic using a two way switch to perform transitions.
* after flipping the switch the vehicle will start tilting in MC control mode, picking up
* forward speed. After the vehicle has picked up enough and sufficient pitch angle the uav will go into FW mode.
* For the backtransition the pitch is controlled in MC mode again and switches to full MC control reaching the sufficient pitch angle.
*/
if (_vtol_vehicle_status->fixed_wing_system_failure) {
// Failsafe event, switch to MC mode immediately
if (_vtol_mode != vtol_mode::MC_MODE) {
_transition_start_timestamp = hrt_absolute_time();
}
_vtol_mode = vtol_mode::MC_MODE;
} else if (!_attc->is_fixed_wing_requested()) {
switch (_vtol_mode) { // user switchig to MC mode
case vtol_mode::MC_MODE:
break;
case vtol_mode::FW_MODE:
resetTransitionStates();
_vtol_mode = vtol_mode::TRANSITION_BACK;
break;
case vtol_mode::TRANSITION_FRONT_P1:
// failsafe into multicopter mode
_vtol_mode = vtol_mode::MC_MODE;
break;
case vtol_mode::TRANSITION_BACK:
const float pitch = Eulerf(Quatf(_v_att->q)).theta();
// check if we have reached pitch angle to switch to MC mode
if (pitch >= PITCH_THRESHOLD_AUTO_TRANSITION_TO_MC || _time_since_trans_start > _param_vt_b_trans_dur.get()) {
_vtol_mode = vtol_mode::MC_MODE;
}
break;
}
} else { // user switchig to FW mode
switch (_vtol_mode) {
case vtol_mode::MC_MODE:
// initialise a front transition
_vtol_mode = vtol_mode::TRANSITION_FRONT_P1;
resetTransitionStates();
break;
case vtol_mode::FW_MODE:
break;
case vtol_mode::TRANSITION_FRONT_P1: {
if (isFrontTransitionCompleted()) {
_vtol_mode = vtol_mode::FW_MODE;
_trans_finished_ts = hrt_absolute_time();
}
break;
}
case vtol_mode::TRANSITION_BACK:
// failsafe into fixed wing mode
_vtol_mode = vtol_mode::FW_MODE;
_trans_finished_ts = hrt_absolute_time();
break;
}
}
// map tailsitter specific control phases to simple control modes
switch (_vtol_mode) {
case vtol_mode::MC_MODE:
_common_vtol_mode = mode::ROTARY_WING;
_flag_was_in_trans_mode = false;
break;
case vtol_mode::FW_MODE:
_common_vtol_mode = mode::FIXED_WING;
_flag_was_in_trans_mode = false;
break;
case vtol_mode::TRANSITION_FRONT_P1:
_common_vtol_mode = mode::TRANSITION_TO_FW;
break;
case vtol_mode::TRANSITION_BACK:
_common_vtol_mode = mode::TRANSITION_TO_MC;
break;
}
}
void Tailsitter::update_transition_state()
{
VtolType::update_transition_state();
const hrt_abstime now = hrt_absolute_time();
// we need the incoming (virtual) mc attitude setpoints to be recent, otherwise return (means the previous setpoint stays active)
if (_mc_virtual_att_sp->timestamp < (now - 1_s)) {
return;
}
if (!_flag_was_in_trans_mode) {
_flag_was_in_trans_mode = true;
if (_vtol_mode == vtol_mode::TRANSITION_BACK) {
// calculate rotation axis for transition.
_q_trans_start = Quatf(_v_att->q);
Vector3f z = -_q_trans_start.dcm_z();
_trans_rot_axis = z.cross(Vector3f(0.f, 0.f, -1.f));
// as heading setpoint we choose the heading given by the direction the vehicle points
const float yaw_sp = atan2f(z(1), z(0));
// the intial attitude setpoint for a backtransition is a combination of the current fw pitch setpoint,
// the yaw setpoint and zero roll since we want wings level transition.
// If for some reason the fw attitude setpoint is not recent then don't use it and assume 0 pitch
if (_fw_virtual_att_sp->timestamp > (now - 1_s)) {
const float pitch_body = Eulerf(Quatf(_fw_virtual_att_sp->q_d)).theta();
_q_trans_start = Eulerf(0.f, pitch_body, yaw_sp);
} else {
_q_trans_start = Eulerf(0.f, 0.f, yaw_sp);
}
// attitude during transitions are controlled by mc attitude control so rotate the desired attitude to the
// multirotor frame
_q_trans_start = _q_trans_start * Quatf(Eulerf(0, -M_PI_2_F, 0));
} else if (_vtol_mode == vtol_mode::TRANSITION_FRONT_P1) {
// initial attitude setpoint for the transition should be with wings level
const Eulerf setpoint_euler(Quatf(_mc_virtual_att_sp->q_d));
_q_trans_start = Eulerf(0.f, setpoint_euler.theta(), setpoint_euler.psi());
Vector3f x = Dcmf(Quatf(_v_att->q)) * Vector3f(1.f, 0.f, 0.f);
_trans_rot_axis = -x.cross(Vector3f(0.f, 0.f, -1.f));
}
_q_trans_sp = _q_trans_start;
}
// ensure input quaternions are exactly normalized because acosf(1.00001) == NaN
_q_trans_sp.normalize();
// tilt angle (zero if vehicle nose points up (hover))
const float cos_tilt = math::constrain(_q_trans_sp(0) * _q_trans_sp(0) - _q_trans_sp(1) * _q_trans_sp(1) -
_q_trans_sp(2) * _q_trans_sp(2) + _q_trans_sp(3) * _q_trans_sp(3), -1.f, 1.f);
const float tilt = acosf(cos_tilt);
if (_vtol_mode == vtol_mode::TRANSITION_FRONT_P1) {
// calculate pitching rate - and constrain to at least 0.1s transition time
const float trans_pitch_rate = M_PI_2_F / math::max(_param_vt_f_trans_dur.get(), 0.1f);
if (tilt < M_PI_2_F - math::radians(_param_fw_psp_off.get())) {
_q_trans_sp = Quatf(AxisAnglef(_trans_rot_axis,
_time_since_trans_start * trans_pitch_rate)) * _q_trans_start;
}
} else if (_vtol_mode == vtol_mode::TRANSITION_BACK) {
// calculate pitching rate - and constrain to at least 0.1s transition time
const float trans_pitch_rate = M_PI_2_F / math::max(_param_vt_b_trans_dur.get(), 0.1f);
if (tilt > 0.01f) {
_q_trans_sp = Quatf(AxisAnglef(_trans_rot_axis,
_time_since_trans_start * trans_pitch_rate)) * _q_trans_start;
}
}
_v_att_sp->thrust_body[2] = _mc_virtual_att_sp->thrust_body[2];
if (_vtol_mode == vtol_mode::TRANSITION_BACK) {
const float progress = math::constrain(_time_since_trans_start / B_TRANS_THRUST_BLENDING_DURATION, 0.f, 1.f);
blendThrottleBeginningBackTransition(progress);
}
_v_att_sp->timestamp = hrt_absolute_time();
const Eulerf euler_sp(_q_trans_sp);
_q_trans_sp.copyTo(_v_att_sp->q_d);
}
void Tailsitter::waiting_on_tecs()
{
// copy the last trust value from the front transition
_v_att_sp->thrust_body[0] = -_last_thr_in_mc;
}
void Tailsitter::update_fw_state()
{
VtolType::update_fw_state();
}
/**
* Write data to actuator output topic.
*/
void Tailsitter::fill_actuator_outputs()
{
_torque_setpoint_0->timestamp = hrt_absolute_time();
_torque_setpoint_0->timestamp_sample = _vehicle_torque_setpoint_virtual_mc->timestamp_sample;
_torque_setpoint_0->xyz[0] = 0.f;
_torque_setpoint_0->xyz[1] = 0.f;
_torque_setpoint_0->xyz[2] = 0.f;
_torque_setpoint_1->timestamp = hrt_absolute_time();
_torque_setpoint_1->timestamp_sample = _vehicle_torque_setpoint_virtual_fw->timestamp_sample;
_torque_setpoint_1->xyz[0] = 0.f;
_torque_setpoint_1->xyz[1] = 0.f;
_torque_setpoint_1->xyz[2] = 0.f;
_thrust_setpoint_0->timestamp = hrt_absolute_time();
_thrust_setpoint_0->timestamp_sample = _vehicle_thrust_setpoint_virtual_mc->timestamp_sample;
_thrust_setpoint_0->xyz[0] = 0.f;
_thrust_setpoint_0->xyz[1] = 0.f;
_thrust_setpoint_0->xyz[2] = 0.f;
_thrust_setpoint_1->timestamp = hrt_absolute_time();
_thrust_setpoint_1->timestamp_sample = _vehicle_thrust_setpoint_virtual_fw->timestamp_sample;
_thrust_setpoint_1->xyz[0] = 0.f;
_thrust_setpoint_1->xyz[1] = 0.f;
_thrust_setpoint_1->xyz[2] = 0.f;
// Motors: Generating force and torque (optional for FW_MODE)
if (_vtol_mode == vtol_mode::FW_MODE) {
_thrust_setpoint_0->xyz[2] = -_vehicle_thrust_setpoint_virtual_fw->xyz[0];
/* allow differential thrust if enabled */
if (_param_vt_fw_difthr_en.get() & static_cast<int32_t>(VtFwDifthrEnBits::YAW_BIT)) {
_torque_setpoint_0->xyz[0] = _vehicle_torque_setpoint_virtual_fw->xyz[0] * _param_vt_fw_difthr_s_y.get();
}
if (_param_vt_fw_difthr_en.get() & static_cast<int32_t>(VtFwDifthrEnBits::PITCH_BIT)) {
_torque_setpoint_0->xyz[1] = _vehicle_torque_setpoint_virtual_fw->xyz[1] * _param_vt_fw_difthr_s_p.get();
}
if (_param_vt_fw_difthr_en.get() & static_cast<int32_t>(VtFwDifthrEnBits::ROLL_BIT)) {
_torque_setpoint_0->xyz[2] = _vehicle_torque_setpoint_virtual_fw->xyz[2] * _param_vt_fw_difthr_s_r.get();
}
// for the short period after switching to FW where there is no thrust published yet from the FW controller,
// keep publishing the last MC thrust to keep the motors running
if (hrt_elapsed_time(&_trans_finished_ts) < 50_ms) {
_thrust_setpoint_0->xyz[2] = _last_thr_in_mc;
_torque_setpoint_0->xyz[0] = 0.f;
_torque_setpoint_0->xyz[1] = 0.f;
_torque_setpoint_0->xyz[2] = 0.f;
}
} else {
_thrust_setpoint_0->xyz[2] = _vehicle_thrust_setpoint_virtual_mc->xyz[2];
// for the short period after starting the backtransition where there is no thrust published yet from the MC controller,
// keep publishing the last FW thrust to keep the motors running
if (_vtol_mode != vtol_mode::TRANSITION_FRONT_P1 && hrt_elapsed_time(&_transition_start_timestamp) < 50_ms) {
_thrust_setpoint_0->xyz[2] = -_last_thr_in_fw_mode;
}
_torque_setpoint_0->xyz[0] = _vehicle_torque_setpoint_virtual_mc->xyz[0];
_torque_setpoint_0->xyz[1] = _vehicle_torque_setpoint_virtual_mc->xyz[1];
_torque_setpoint_0->xyz[2] = _vehicle_torque_setpoint_virtual_mc->xyz[2];
}
// Control surfaces: Generating torque (optional for MC_MODE)
// VT_ELEV_MC_LOCK=0 enables control surfaces in hover, VT_TS_CS_HVR_DIS can still
// selectively disable individual surfaces (e.g., those outside propeller slipstream).
if (!_param_vt_elev_mc_lock.get() && _vtol_mode == vtol_mode::MC_MODE) {
// In MC_MODE apply torque from multirotor controller
_torque_setpoint_1->xyz[0] = _vehicle_torque_setpoint_virtual_mc->xyz[0];
_torque_setpoint_1->xyz[1] = _vehicle_torque_setpoint_virtual_mc->xyz[1];
_torque_setpoint_1->xyz[2] = _vehicle_torque_setpoint_virtual_mc->xyz[2];
} else {
_torque_setpoint_1->xyz[0] = _vehicle_torque_setpoint_virtual_fw->xyz[0];
_torque_setpoint_1->xyz[1] = _vehicle_torque_setpoint_virtual_fw->xyz[1];
_torque_setpoint_1->xyz[2] = _vehicle_torque_setpoint_virtual_fw->xyz[2];
}
}
bool Tailsitter::isFrontTransitionCompletedBase()
{
const bool airspeed_triggers_transition = PX4_ISFINITE(_attc->get_calibrated_airspeed());
bool transition_to_fw = false;
const float pitch = Eulerf(Quatf(_v_att->q)).theta();
if (pitch <= PITCH_THRESHOLD_AUTO_TRANSITION_TO_FW) {
if (airspeed_triggers_transition) {
transition_to_fw = _attc->get_calibrated_airspeed() >= _param_vt_arsp_trans.get() ;
} else {
transition_to_fw = true;
}
}
return transition_to_fw;
}
void Tailsitter::blendThrottleAfterFrontTransition(float scale)
{
// note: MC throttle is negative (as in negative z), while FW throttle is positive (positive x)
_v_att_sp->thrust_body[0] = scale * _v_att_sp->thrust_body[0] + (1.f - scale) * (-_last_thr_in_mc);
}
void Tailsitter::blendThrottleBeginningBackTransition(float scale)
{
_v_att_sp->thrust_body[2] = scale * _v_att_sp->thrust_body[2] + (1.f - scale) * (-_last_thr_in_fw_mode);
}