CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/simulink/arducopter/functions/getMotorMixerFactors.m
Views: 1799
1
% Function to get the copters number of motors and there configuration to
2
% calculate the motor mixer factors for roll, pitch and yaw
3
%
4
% Fabian Bredemeier - IAV GmbH
5
% License: GPL v3
6
function [num_motors, axis_facs_motors] = getMotorMixerFactors(frame_class, frame_type)
7
d2r = pi/180;
8
axis_facs_motors = struct;
9
num_motors = 0;
10
% Preliminary cell array for the motors axis factors consisting of the
11
% roll/pitch factor in degree (first element of cells) and the yaw
12
% factor
13
axis_facs_motors_pre = cell(0,0);
14
15
%% Get configuration
16
17
switch frame_class
18
case 1 % Quad
19
num_motors = 4;
20
axis_facs_motors_pre = cell(4,1);
21
switch frame_type
22
case 0 % Plus
23
axis_facs_motors_pre = { [90, 1]; [-90, 1]; [0, -1]; [180, -1] };
24
case 1 % X
25
axis_facs_motors_pre = { [45, 1]; [-135, 1]; [-45, -1]; [135, -1] };
26
case 2 % V
27
axis_facs_motors_pre = { [45, 0.7981]; [-135, 1.0]; [-45, -0.7981]; [135, -1.0] };
28
case 3 % H
29
axis_facs_motors_pre = { [45, -1]; [-135, -1]; [-45, 1]; [135, 1] };
30
case 13 % DJIX
31
axis_facs_motors_pre = { [45, 1]; [-45, -1]; [-135, 1]; [135, -1] };
32
case 14 % Clockwise ordering
33
axis_facs_motors_pre = { [45, 1]; [135, -1]; [-135, 1]; [-45, -1] };
34
end
35
case 2 % Hexa
36
num_motors = 6;
37
case {3, 4} % Octa and OctaQuad
38
num_motors = 8;
39
axis_facs_motors_pre = cell(8,1);
40
switch frame_type
41
case 0 % Plus
42
axis_facs_motors_pre = { [0, -1]; [180, -1]; [45, 1]; [135, 1]; ...
43
[-45, 1]; [-135, 1]; [-90, -1]; [90, -1] };
44
case 1 % X
45
axis_facs_motors_pre = { [22.5, -1]; [-157.5, -1]; [67.5, 1]; [157.5, 1]; ...
46
[-22.5, 1]; [-122.5, 1]; [-67.5, -1]; [112.5, -1] };
47
case 2 % V
48
axis_facs_motors_pre = { [0.83, 0.34, -1]; [-0.67, -0.32, -1]; [0.67, -0.32, 1]; [-0.50, -1.00, 1]; ...
49
[1.00, 1.00, 1]; [-0.83, 0.34, 1]; [-1.00, 1.00, -1]; [0.5, -1.00, -1] };
50
end
51
case 5 % Y6
52
num_motors = 6;
53
case 12 % DodecaHexa
54
num_motors = 12;
55
case 14 % Deca
56
num_motors = 14;
57
end
58
59
%% Check if configuration is defined
60
% Leave function if frame class is not configured yet
61
if num_motors == 0
62
disp("Frame class unknown. Please add the configuration to the function.");
63
return;
64
end
65
% Leave function if axis factors are not yet defined for frame
66
if isempty(axis_facs_motors_pre)
67
disp("Motor axis factors are not yet defined for frame class!");
68
disp("The factors can be found in the setup_motors function within AP_MotorsMatrix.cpp.");
69
return;
70
end
71
72
%% Calculate axis factors (roll/pitch) from preliminary array
73
% Create the output struct that stores the factors for the different
74
% axis in separate arrays. Each column of the subarrays represents one motor.
75
axis_facs_motors = struct('roll', zeros(1,num_motors), ...
76
'pitch', zeros(1,num_motors), ...
77
'yaw', zeros(1,num_motors) , ...
78
'throttle', zeros(1, num_motors) );
79
for i=1:num_motors
80
% Check if factors for roll and pitch are defined separately and
81
% therefore in radian -> dimension of a cell would be 3
82
if ( length(axis_facs_motors_pre{i}) >= 3 )
83
axis_facs_motors.roll(1,i) = axis_facs_motors_pre{i}(1);
84
axis_facs_motors.pitch(1,i) = axis_facs_motors_pre{i}(2);
85
else
86
axis_facs_motors.roll(1,i) = cos( (axis_facs_motors_pre{i}(1)+90)*d2r );
87
axis_facs_motors.pitch(1,i) = cos( (axis_facs_motors_pre{i}(1))*d2r );
88
end
89
% The factors for yaw can be acquired directly from the preliminary
90
% array
91
axis_facs_motors.yaw(1,i) = axis_facs_motors_pre{i}(2);
92
% The factors for throttle are 1.0 by default (see AP_MotorsMatrix.h,
93
% line 87)
94
axis_facs_motors.throttle(1,i) = 1.0;
95
end
96
97
%% Normalization of factors (AP_MotorsMatrix.cpp, line 1218)
98
roll_fac_max = 0.0;
99
pitch_fac_max = 0.0;
100
yaw_fac_max = 0.0;
101
throttle_fac_max = 0.0;
102
103
% Find maximum factor
104
for i=1:num_motors
105
roll_fac_max = max(roll_fac_max, axis_facs_motors.roll(1,i));
106
pitch_fac_max = max(pitch_fac_max, axis_facs_motors.pitch(1,i));
107
yaw_fac_max = max(yaw_fac_max, axis_facs_motors.yaw(1,i));
108
throttle_fac_max = max(throttle_fac_max, axis_facs_motors.throttle(1,i));
109
end
110
111
% Scale factors back to -0.5 to +0.5 for each axis
112
for i=1:num_motors
113
if(roll_fac_max ~= 0)
114
axis_facs_motors.roll(1,i) = 0.5 * axis_facs_motors.roll(1,i) / roll_fac_max;
115
end
116
if(pitch_fac_max ~= 0)
117
axis_facs_motors.pitch(1,i) = 0.5 * axis_facs_motors.pitch(1,i) / pitch_fac_max;
118
end
119
if(yaw_fac_max ~= 0)
120
axis_facs_motors.yaw(1,i) = 0.5 * axis_facs_motors.yaw(1,i) / yaw_fac_max;
121
end
122
if(throttle_fac_max ~= 0)
123
axis_facs_motors.throttle(1,i) = 0.5 * axis_facs_motors.throttle(1,i) / throttle_fac_max;
124
end
125
end
126
end
127