APM:Libraries
SIM_Tracker.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  antenna-tracker simulator class
17 */
18 
19 #include "SIM_Tracker.h"
20 
21 #include <stdio.h>
22 
23 namespace SITL {
24 
25 Tracker::Tracker(const char *home_str, const char *frame_str) :
26 Aircraft(home_str, frame_str)
27 {}
28 
29 
30 /*
31  update function for position (normal) servos.
32 */
33 void Tracker::update_position_servos(float delta_time, float &yaw_rate, float &pitch_rate)
34 {
35  float pitch_target = pitch_input*pitch_range;
36  float yaw_target = yaw_input*yaw_range;
37 
38  pitch_rate = constrain_float(pitch_target - pitch_current_relative, -pitchrate, pitchrate);
39  yaw_rate = constrain_float(yaw_target - yaw_current_relative, -yawrate, yawrate);
40 }
41 
42 /*
43  update function for onoff servos.
44  These servos either move at a constant rate or are still
45  Returns (yaw_rate,pitch_rate) tuple
46 */
47 void Tracker::update_onoff_servos(float &yaw_rate, float &pitch_rate)
48 {
49  if (fabsf(yaw_input) < 0.1) {
50  yaw_rate = 0;
51  } else if (yaw_input >= 0.1) {
52  yaw_rate = yawrate;
53  } else {
54  yaw_rate = -yawrate;
55  }
56 
57  if (fabsf(pitch_input) < 0.1) {
58  pitch_rate = 0;
59  } else if (pitch_input >= 0.1) {
60  pitch_rate = pitchrate;
61  } else {
62  pitch_rate = -pitchrate;
63  }
64 }
65 
66 /*
67  update state of tracker
68  */
69 void Tracker::update(const struct sitl_input &input)
70 {
71  // how much time has passed?
72  float delta_time = frame_time_us * 1.0e-6f;
73 
74  float yaw_rate = 0.0f, pitch_rate = 0.0f;
75 
76  yaw_input = (input.servos[0]-1500)/500.0f;
77  pitch_input = (input.servos[1]-1500)/500.0f;
78 
79  // implement yaw and pitch limits
80  float r, p, y;
81  dcm.to_euler(&r, &p, &y);
82 
85  float roll_current = degrees(r);
86  if (yaw_current_relative > 180) {
87  yaw_current_relative -= 360;
88  }
89  if (yaw_current_relative < -180) {
90  yaw_current_relative += 360;
91  }
92  if (yaw_rate > 0 && yaw_current_relative >= yaw_range) {
93  yaw_rate = 0;
94  }
95  if (yaw_rate < 0 && yaw_current_relative <= -yaw_range) {
96  yaw_rate = 0;
97  }
98  if (pitch_rate > 0 && pitch_current_relative >= pitch_range) {
99  pitch_rate = 0;
100  }
101  if (pitch_rate < 0 && pitch_current_relative <= -pitch_range) {
102  pitch_rate = 0;
103  }
104 
105  if (onoff) {
106  update_onoff_servos(yaw_rate, pitch_rate);
107  } else {
108  update_position_servos(delta_time, yaw_rate, pitch_rate);
109  }
110 
111 
112  // keep it level
113  float roll_rate = 0 - roll_current;
114 
115  if (time_now_us - last_debug_us > 2e6f && !onoff) {
117  printf("roll=%.1f pitch=%.1f yaw=%.1f rates=%.1f/%.1f/%.1f in=%.3f,%.3f\n",
118  roll_current,
121  roll_rate, pitch_rate, yaw_rate,
123  }
124 
125  gyro = Vector3f(radians(roll_rate),radians(pitch_rate),radians(yaw_rate));
126 
127  // update attitude
128  dcm.rotate(gyro * delta_time);
129  dcm.normalize();
130 
131  Vector3f accel_earth = Vector3f(0, 0, -GRAVITY_MSS);
132  accel_body = dcm.transposed() * accel_earth;
133 
134  // new velocity vector
135  velocity_ef.zero();
136  update_position();
137  time_advance();
138 
139  // update magnetic field
141 }
142 
143 } // namespace SITL
Vector3f accel_body
Definition: SIM_Aircraft.h:142
const float zero_yaw
Definition: SIM_Tracker.h:45
void to_euler(float *roll, float *pitch, float *yaw) const
Definition: matrix3.cpp:49
int printf(const char *fmt,...)
Definition: stdio.c:113
Vector3< float > Vector3f
Definition: vector3.h:246
const float pitch_range
Definition: SIM_Tracker.h:43
void update(const struct sitl_input &input)
Definition: SIM_Tracker.cpp:69
const float pitchrate
Definition: SIM_Tracker.h:42
const bool onoff
Definition: SIM_Tracker.h:40
void update_onoff_servos(float &yaw_rate, float &pitch_rate)
Definition: SIM_Tracker.cpp:47
Matrix3< T > transposed(void) const
Definition: matrix3.cpp:189
#define GRAVITY_MSS
Definition: definitions.h:47
float yaw_current_relative
Definition: SIM_Tracker.h:51
#define f(i)
Vector3f velocity_ef
Definition: SIM_Aircraft.h:136
const float yaw_range
Definition: SIM_Tracker.h:44
uint64_t time_now_us
Definition: SIM_Aircraft.h:160
const float zero_pitch
Definition: SIM_Tracker.h:46
const float yawrate
Definition: SIM_Tracker.h:41
void update_mag_field_bf(void)
uint64_t frame_time_us
Definition: SIM_Aircraft.h:167
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
Tracker(const char *home_str, const char *frame_str)
Definition: SIM_Tracker.cpp:25
void rotate(const Vector3< T > &g)
Definition: matrix3.cpp:115
#define degrees(x)
Definition: moduletest.c:23
void normalize(void)
Definition: matrix3.cpp:135
void update_position_servos(float delta_time, float &yaw_rate, float &pitch_rate)
Definition: SIM_Tracker.cpp:33
float pitch_current_relative
Definition: SIM_Tracker.h:52
float pitch_input
Definition: SIM_Tracker.h:49
float yaw_input
Definition: SIM_Tracker.h:50
void zero()
Definition: vector3.h:182
uint64_t last_debug_us
Definition: SIM_Tracker.h:47