APM:Libraries
SIM_SingleCopter.cpp
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 /*
16  singlecopter simulator class
17 */
18 
19 #include "SIM_SingleCopter.h"
20 
21 #include <stdio.h>
22 
23 using namespace SITL;
24 
25 SingleCopter::SingleCopter(const char *home_str, const char *frame_str) :
26  Aircraft(home_str, frame_str)
27 {
28  mass = 2.0f;
29 
30  if (strstr(frame_str, "coax")) {
32  } else {
34  }
35 
36  /*
37  scaling from motor power to Newtons. Allows the copter
38  to hover against gravity when the motor is at hover_throttle
39  */
41  frame_height = 0.1;
42 }
43 
44 /*
45  update the copter simulation by one time step
46  */
47 void SingleCopter::update(const struct sitl_input &input)
48 {
49  // get wind vector setup
50  update_wind(input);
51 
52  float actuator[4];
53  for (uint8_t i=0; i<4; i++) {
54  actuator[i] = constrain_float((input.servos[i]-1500) / 500.0f, -1, 1);
55  }
56  float thrust;
57  float yaw_thrust;
58  float roll_thrust;
59  float pitch_thrust;
60 
61  switch (frame_type) {
62  case FRAME_SINGLE:
63  thrust = constrain_float((input.servos[4]-1000) / 1000.0f, 0, 1);
64  yaw_thrust = -(actuator[0] + actuator[1] + actuator[2] + actuator[3]) * 0.25f * thrust + thrust * rotor_rot_accel;
65  roll_thrust = (actuator[0] - actuator[2]) * 0.5f * thrust;
66  pitch_thrust = (actuator[1] - actuator[3]) * 0.5f * thrust;
67  break;
68 
69  case FRAME_COAX:
70  default: {
71  float motor1 = constrain_float((input.servos[4]-1000) / 1000.0f, 0, 1);
72  float motor2 = constrain_float((input.servos[5]-1000) / 1000.0f, 0, 1);
73  thrust = 0.5f*(motor1 + motor2);
74  yaw_thrust = -(actuator[0] + actuator[1] + actuator[2] + actuator[3]) * 0.25f * thrust + (motor2 - motor1) * rotor_rot_accel;
75  roll_thrust = (actuator[0] - actuator[2]) * 0.5f * thrust;
76  pitch_thrust = (actuator[1] - actuator[3]) * 0.5f * thrust;
77  break;
78  }
79  }
80 
81  // rotational acceleration, in rad/s/s, in body frame
82  Vector3f rot_accel(roll_thrust * roll_rate_max,
83  pitch_thrust * pitch_rate_max,
84  yaw_thrust * yaw_rate_max);
85 
86  // rotational air resistance
87  rot_accel.x -= gyro.x * radians(5000.0) / terminal_rotation_rate;
88  rot_accel.y -= gyro.y * radians(5000.0) / terminal_rotation_rate;
89  rot_accel.z -= gyro.z * radians(400.0) / terminal_rotation_rate;
90 
91  // air resistance
93 
94  // scale thrust to newtons
95  thrust *= thrust_scale;
96 
97  accel_body = Vector3f(0, 0, -thrust / mass);
98  accel_body += dcm.transposed() * air_resistance;
99 
100  update_dynamics(rot_accel);
101 
102  // update lat/lon/altitude
103  update_position();
104  time_advance();
105 
106  // update magnetic field
108 }
Vector3f accel_body
Definition: SIM_Aircraft.h:142
void update(const struct sitl_input &input)
void update_wind(const struct sitl_input &input)
Vector3< float > Vector3f
Definition: vector3.h:246
SingleCopter(const char *home_str, const char *frame_str)
Vector3f velocity_air_ef
Definition: SIM_Aircraft.h:138
enum SITL::SingleCopter::@210 frame_type
Matrix3< T > transposed(void) const
Definition: matrix3.cpp:189
#define GRAVITY_MSS
Definition: definitions.h:47
#define f(i)
T y
Definition: vector3.h:67
T z
Definition: vector3.h:67
void update_mag_field_bf(void)
float constrain_float(const float amt, const float low, const float high)
Definition: AP_Math.h:142
void update_position(void)
static constexpr float radians(float deg)
Definition: AP_Math.h:158
void update_dynamics(const Vector3f &rot_accel)
T x
Definition: vector3.h:67