Firmware
follow_target.h
1 /***************************************************************************
2  *
3  * Copyright (c) 2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
41 #pragma once
42 
43 #include "navigator_mode.h"
44 #include "mission_block.h"
45 
46 #include <mathlib/mathlib.h>
47 #include <matrix/math.hpp>
48 
49 #include <px4_module_params.h>
50 #include <uORB/topics/follow_target.h>
51 
52 class FollowTarget : public MissionBlock, public ModuleParams
53 {
54 
55 public:
57  ~FollowTarget() = default;
58 
59  void on_inactive() override;
60  void on_activation() override;
61  void on_active() override;
62 
63 private:
64 
65  static constexpr int TARGET_TIMEOUT_MS = 2500;
66  static constexpr int TARGET_ACCEPTANCE_RADIUS_M = 5;
67  static constexpr int INTERPOLATION_PNTS = 20;
68  static constexpr float FF_K = .25F;
69  static constexpr float OFFSET_M = 8;
70 
71  enum FollowTargetState {
72  TRACK_POSITION,
73  TRACK_VELOCITY,
74  SET_WAIT_FOR_TARGET_POSITION,
75  WAIT_FOR_TARGET_POSITION
76  };
77 
78  enum {
79  FOLLOW_FROM_RIGHT,
80  FOLLOW_FROM_BEHIND,
81  FOLLOW_FROM_FRONT,
82  FOLLOW_FROM_LEFT
83  };
84 
85  static constexpr float _follow_position_matricies[4][9] = {
86  { 1.0F, -1.0F, 0.0F, 1.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F}, // follow right
87  {-1.0F, 0.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F, 0.0F, 1.0F}, // follow behind
88  { 1.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F}, // follow front
89  { 1.0F, 1.0F, 0.0F, -1.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F} // follow left side
90  };
91 
92  DEFINE_PARAMETERS(
93  (ParamFloat<px4::params::NAV_MIN_FT_HT>) _param_nav_min_ft_ht,
94  (ParamFloat<px4::params::NAV_FT_DST>) _param_nav_ft_dst,
95  (ParamInt<px4::params::NAV_FT_FS>) _param_nav_ft_fs,
96  (ParamFloat<px4::params::NAV_FT_RS>) _param_nav_ft_rs
97  )
98 
99  FollowTargetState _follow_target_state{SET_WAIT_FOR_TARGET_POSITION};
100  int _follow_target_position{FOLLOW_FROM_BEHIND};
101 
102  int _follow_target_sub{-1};
103  float _step_time_in_ms{0.0f};
104  float _follow_offset{OFFSET_M};
105 
106  uint64_t _target_updates{0};
107  uint64_t _last_update_time{0};
108 
109  matrix::Vector3f _current_vel;
110  matrix::Vector3f _step_vel;
111  matrix::Vector3f _est_target_vel;
112  matrix::Vector3f _target_distance;
113  matrix::Vector3f _target_position_offset;
114  matrix::Vector3f _target_position_delta;
115  matrix::Vector3f _filtered_target_position_delta;
116 
117  follow_target_s _current_target_motion{};
118  follow_target_s _previous_target_motion{};
119 
120  float _yaw_rate{0.0f};
121  float _responsiveness{0.0f};
122  float _yaw_angle{0.0f};
123 
124  // Mavlink defined motion reporting capabilities
125  enum {
126  POS = 0,
127  VEL = 1,
128  ACCEL = 2,
129  ATT_RATES = 3
130  };
131 
132  matrix::Dcmf _rot_matrix;
133 
134  void track_target_position();
135  void track_target_velocity();
136  bool target_velocity_valid();
137  bool target_position_valid();
138  void reset_target_validity();
139  void update_position_sp(bool velocity_valid, bool position_valid, float yaw_rate);
140  void update_target_motion();
141  void update_target_velocity();
142 
146  void set_follow_target_item(struct mission_item_s *item, float min_clearance, follow_target_s &target, float yaw);
147 };
Global position setpoint in WGS84 coordinates.
Definition: navigation.h:145
Definition: px4_param.h:313
Helper class to use mission items.
Definition: mission_block.h:56
Definition: navigator_main.cpp:80
Definition: px4_param.h:318
void on_active() override
This function is called while the mode is active.
Definition: follow_target.cpp:99
C++ base class for modules/classes using configuration parameters.
Definition: px4_module_params.h:46
void on_inactive() override
This function is called while the mode is inactive.
Definition: follow_target.cpp:75
Common header for mathlib exports.
Definition: navigator.h:83
void on_activation() override
This function is called one time when mode becomes active, pos_sp_triplet must be initialized here...
Definition: follow_target.cpp:80
Definition: follow_target.h:52