value_function.h
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  */
36 
38 //
39 // Defines the ValueFunction class. Templated on the tracker/planner state
40 // (TS/PS), tracker/planner control (TC/PC), tracker/planner dynamics (TD/PC),
41 // and bound (B).
42 //
44 
45 #ifndef FASTRACK_VALUE_VALUE_FUNCTION_H
46 #define FASTRACK_VALUE_VALUE_FUNCTION_H
47 
51 #include <fastrack/utils/types.h>
53 
54 #include <ros/ros.h>
55 
56 namespace fastrack {
57 namespace value {
58 
59 using dynamics::Dynamics;
60 using dynamics::RelativeDynamics;
61 using state::RelativeState;
62 
63 template <typename TS, typename TC, typename TD, typename PS, typename PC,
64  typename PD, typename B>
65 class ValueFunction : private Uncopyable {
66  public:
67  virtual ~ValueFunction() {}
68 
69  // Initialize from a ROS NodeHandle.
70  inline bool Initialize(const ros::NodeHandle& n) {
71  name_ = ros::names::append(n.getNamespace(), "ValueFunction");
72 
73  if (!LoadParameters(n)) {
74  ROS_ERROR("%s: Failed to load parameters.", name_.c_str());
75  return false;
76  }
77 
78  if (!RegisterCallbacks(n)) {
79  ROS_ERROR("%s: Failed to register callbacks.", name_.c_str());
80  return false;
81  }
82 
83  initialized_ = true;
84  return true;
85  }
86 
87  // Value and gradient at particular relative states.
88  virtual double Value(const TS& tracker_x, const PS& planner_x) const = 0;
89  virtual std::unique_ptr<RelativeState<TS, PS>> Gradient(
90  const TS& tracker_x, const PS& planner_x) const = 0;
91 
92  // Get the optimal control given the tracker state and planner state.
93  inline TC OptimalControl(const TS& tracker_x, const PS& planner_x) const {
94  if (!initialized_)
95  throw std::runtime_error("Uninitialized call to OptimalControl.");
96 
97  // std::cout << "Value is: " << Value(tracker_x, planner_x) << std::endl;
98 
99  return relative_dynamics_->OptimalControl(
100  tracker_x, planner_x, *Gradient(tracker_x, planner_x),
101  tracker_dynamics_.GetControlBound(),
102  planner_dynamics_.GetControlBound());
103  }
104 
105  // Accessors.
106  inline const B& TrackingBound() const { return bound_; }
107  inline const TD& TrackerDynamics() const { return tracker_dynamics_; }
108  inline const PD& PlannerDynamics() const { return planner_dynamics_; }
109  inline const RelativeDynamics<TS, TC, PS, PC>& GetRelativeDynamics() const {
110  if (!initialized_)
111  throw std::runtime_error("Uninitialized call to GetRelativeDynamics.");
112 
113  return *relative_dynamics_;
114  }
115 
116  // Priority of the optimal control at the given tracker and planner states.
117  // This is a number between 0 and 1, where 1 means the final control signal
118  // should be exactly the optimal control signal computed by this
119  // value function.
120  virtual double Priority(const TS& tracker_x, const PS& planner_x) const = 0;
121 
122  protected:
123  explicit ValueFunction() : initialized_(false) {}
124 
125  // Load parameters and register callbacks. May be overridden.
126  virtual bool LoadParameters(const ros::NodeHandle& n) { return true; }
127  virtual bool RegisterCallbacks(const ros::NodeHandle& n) { return true; }
128 
129  // Member variables to be instantiated by derived classes after
130  // reading the necessary parameters from the ROS parameter server.
131  // Keep a copy of the tracker and planner dynamics, and a pointer
132  // to the relative dynamics.
135  std::unique_ptr<RelativeDynamics<TS, TC, PS, PC>> relative_dynamics_;
136 
137  // Keep a copy of the tracking errror bound.
139 
140  // Naming and initialization.
141  std::string name_;
143 }; //\class ValueFunction
144 
145 } // namespace value
146 } // namespace fastrack
147 
148 #endif
virtual std::unique_ptr< RelativeState< TS, PS > > Gradient(const TS &tracker_x, const PS &planner_x) const =0
virtual double Value(const TS &tracker_x, const PS &planner_x) const =0
const B & TrackingBound() const
const RelativeDynamics< TS, TC, PS, PC > & GetRelativeDynamics() const
Definition: box.h:53
TC OptimalControl(const TS &tracker_x, const PS &planner_x) const
bool Initialize(const ros::NodeHandle &n)
std::unique_ptr< RelativeDynamics< TS, TC, PS, PC > > relative_dynamics_
virtual bool LoadParameters(const ros::NodeHandle &n)
const TD & TrackerDynamics() const
const PD & PlannerDynamics() const
virtual double Priority(const TS &tracker_x, const PS &planner_x) const =0
virtual bool RegisterCallbacks(const ros::NodeHandle &n)


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