aboutsummaryrefslogtreecommitdiff
path: root/src/modules/mc_att_control/mc_att_control_sim.cpp
blob: 61a4237fc56b840e401d46336fbfd1e3995f3d20 (plain) (blame)
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
/* Copyright (c) 2014 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 mc_att_control_base.h
 *
 * @author Roman Bapst <bapstr@ethz.ch>
 *
 */

#include "mc_att_control_sim.h"
#include <geo/geo.h>
#include <math.h>

#ifdef CONFIG_ARCH_ARM
#else
#include <cmath>
using namespace std;
#endif

void MulticopterAttitudeControlSim::set_attitude(const Eigen::Quaternion<double> attitude) {
	math::Quaternion quat;
	quat(0) = (float)attitude.w();
	quat(1) = (float)attitude.x();
	quat(2) = (float)attitude.y();
	quat(3) = (float)attitude.z();

	_v_att.q[0] = quat(0);
	_v_att.q[1] = quat(1);
	_v_att.q[2] = quat(2);
	_v_att.q[3] = quat(3);

	math::Matrix<3,3> Rot = quat.to_dcm();
	_v_att.R[0][0] = Rot(0,0);
	_v_att.R[1][0] = Rot(1,0);
	_v_att.R[2][0] = Rot(2,0);
	_v_att.R[0][1] = Rot(0,1);
	_v_att.R[1][1] = Rot(1,1);
	_v_att.R[2][1] = Rot(2,1);
	_v_att.R[0][2] = Rot(0,2);
	_v_att.R[1][2] = Rot(1,2);
	_v_att.R[2][2] = Rot(2,2);

	_v_att.R_valid = true;
}

void MulticopterAttitudeControlSim::set_attitude_rates(const Eigen::Vector3d& angular_rate) {
	// check if this is consistent !!!
	_v_att.rollspeed  = angular_rate(0);
	_v_att.pitchspeed = angular_rate(1);
	_v_att.yawspeed   = angular_rate(2);
}

void MulticopterAttitudeControlSim::set_attitude_reference(const Eigen::Vector4d& control_attitude_thrust_reference) {
	_v_att_sp.roll_body  = control_attitude_thrust_reference(0);
	_v_att_sp.pitch_body = control_attitude_thrust_reference(1);
	_v_att_sp.yaw_body   = control_attitude_thrust_reference(2);
	_v_att_sp.thrust     = (control_attitude_thrust_reference(3) -30)*(-1)/30;

	// setup rotation matrix
	math::Matrix<3,3> Rot_sp;
	Rot_sp.from_euler(_v_att_sp.roll_body,_v_att_sp.pitch_body,_v_att_sp.yaw_body);
	_v_att_sp.R_body[0][0] = Rot_sp(0,0);
	_v_att_sp.R_body[1][0] = Rot_sp(1,0);
	_v_att_sp.R_body[2][0] = Rot_sp(2,0);
	_v_att_sp.R_body[0][1] = Rot_sp(0,1);
	_v_att_sp.R_body[1][1] = Rot_sp(1,1);
	_v_att_sp.R_body[2][1] = Rot_sp(2,1);
	_v_att_sp.R_body[0][2] = Rot_sp(0,2);
	_v_att_sp.R_body[1][2] = Rot_sp(1,2);
	_v_att_sp.R_body[2][2] = Rot_sp(2,2);
}

void MulticopterAttitudeControlSim::get_mixer_input(Eigen::Vector4d& motor_inputs) {
	motor_inputs(0) = _actuators.control[0];
	motor_inputs(1) = _actuators.control[1];
	motor_inputs(2) = _actuators.control[2];
	motor_inputs(3) = _actuators.control[3];
}