quadrotor_decoupled_6d_rel_planar_dubins_3d.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, The Regents of the University of California (Regents).
3  * 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 are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  * Jaime F. Fisac ( jfisac@eecs.berkeley.edu )
36  */
37 
39 //
40 // Defines the relative dynamics between a 6D decoupled quadrotor model and a
41 // 3D planar Dubins model.
42 //
44 
53 #include <fastrack/utils/types.h>
54 
55 #include <math.h>
56 
57 namespace fastrack {
58 namespace dynamics {
59 
60 using control::QuadrotorControl;
61 using control::QuadrotorControlBoundCylinder;
62 using state::PlanarDubins3D;
63 using state::PositionVelocity;
64 using state::PositionVelocityRelPlanarDubins3D;
65 
66 // Derived classes must be able to give the time derivative of relative state
67 // as a function of current state and control of each system.
68 std::unique_ptr<RelativeState<PositionVelocity, PlanarDubins3D>>
70  const PositionVelocity& tracker_x, const QuadrotorControl& tracker_u,
71  const PlanarDubins3D& planner_x, const double& planner_u) const {
72  // Compute relative state.
73  const PositionVelocityRelPlanarDubins3D relative_x(tracker_x, planner_x);
74 
75  // Net instantaneous tangent velocity (PositionVelocity minus Dubins).
76  // This is used in the derivatives of relative position (distance, bearing).
77  // It is NOT used in the velocity derivatives because velocity states are
78  // absolute (even though they are expressed in the changing Dubins frame).
79  const auto net_tangent_velocity =
80  relative_x.TangentVelocity() - planner_x.V();
81 
82  // Relative distance derivative.
83  const double distance_dot =
84  net_tangent_velocity * std::cos(relative_x.Bearing()) +
85  relative_x.NormalVelocity() * std::sin(relative_x.Bearing());
86 
87  // Relative bearing derivative.
88  const double bearing_dot =
89  -planner_u +
90  (-net_tangent_velocity * std::sin(relative_x.Bearing()) +
91  relative_x.NormalVelocity() * std::cos(relative_x.Bearing())) /
92  relative_x.Distance(); // omega_circ = v_circ / R
93 
94  // Tracker accelerations expressed in inertial world frame.
95  const double tracker_accel_x = constants::G * std::tan(tracker_u.pitch);
96  const double tracker_accel_y = -constants::G * std::tan(tracker_u.roll);
97 
98  // Relative tangent and normal velocity derivatives.
99  // NOTE! Must rotate roll/pitch into planner frame.
100  const double c = std::cos(planner_x.Theta());
101  const double s = std::sin(planner_x.Theta());
102 
103  const double tangent_velocity_dot = tracker_accel_x * c +
104  tracker_accel_y * s +
105  planner_u * relative_x.NormalVelocity();
106  const double normal_velocity_dot = -tracker_accel_x * s +
107  tracker_accel_y * c -
108  planner_u * relative_x.TangentVelocity();
109 
110  return std::unique_ptr<PositionVelocityRelPlanarDubins3D>(
111  new PositionVelocityRelPlanarDubins3D(distance_dot, bearing_dot,
112  tangent_velocity_dot,
113  normal_velocity_dot));
114 }
115 
116 // Derived classes must be able to compute an optimal control given
117 // the gradient of the value function at the relative state specified
118 // by the given system states, provided abstract control bounds.
120  const PositionVelocity& tracker_x, const PlanarDubins3D& planner_x,
121  const RelativeState<PositionVelocity, PlanarDubins3D>& value_gradient,
122  const ControlBound<QuadrotorControl>& tracker_u_bound,
123  const ControlBound<double>& planner_u_bound) const {
124  // Get internal state of value gradient and map tracker control (negative)
125  // coefficients to QuadrotorControl, so we get a negative gradient.
126  const auto& grad =
127  static_cast<const PositionVelocityRelPlanarDubins3D&>(value_gradient);
128 
129  // Translate gradient into (negative) control-affine terms for pitch and roll.
130  const double c = std::cos(planner_x.Theta());
131  const double s = std::sin(planner_x.Theta());
132 
133  QuadrotorControl negative_grad;
134  negative_grad.pitch =
135  -(grad.TangentVelocity() * c - grad.NormalVelocity() * s);
136  negative_grad.roll =
137  -(-grad.TangentVelocity() * s - grad.NormalVelocity() * c);
138  negative_grad.thrust = 0.0; // Vertical position controlled externally.
139  negative_grad.yaw_rate = 0.0; // Yaw controlled externally.
140 
141  // Project onto tracker control bound and make sure to zero out yaw_rate.
142  QuadrotorControl u = tracker_u_bound.ProjectToSurface(negative_grad);
143 
144  // Adjust non-bang-bang control inputs.
145  u.yaw_rate = 0.0; // Yaw controlled externally.
146  constexpr double k_p = 1.5; // HACK! PD constants are hard-coded.
147  constexpr double k_d = 1.0; // (from k_manual.txt in crazyflie_clean).
148  u.thrust =
149  constants::G + k_p * (planner_x.Z() - tracker_x.Z()) +
150  k_d * (planner_x.Vz() - tracker_x.Vz()); // Vertical PD controller.
151 
152  return u;
153 }
154 
155 } // namespace dynamics
156 } // namespace fastrack
std::unique_ptr< RelativeState< PositionVelocity, PlanarDubins3D > > Evaluate(const PositionVelocity &tracker_x, const QuadrotorControl &tracker_u, const PlanarDubins3D &planner_x, const double &planner_u) const
Definition: box.h:53
QuadrotorControl OptimalControl(const PositionVelocity &tracker_x, const PlanarDubins3D &planner_x, const RelativeState< PositionVelocity, PlanarDubins3D > &value_gradient, const ControlBound< QuadrotorControl > &tracker_u_bound, const ControlBound< double > &planner_u_bound) const
static constexpr double G
Definition: types.h:67


fastrack
Author(s): David Fridovich-Keil
autogenerated on Mon Aug 3 2020 21:28:37