00001 /****************************************************************************** 00002 Copyright (c) 2018, Alexander W. Winkler. All rights reserved. 00003 00004 Redistribution and use in source and binary forms, with or without 00005 modification, are permitted provided that the following conditions are met: 00006 00007 * Redistributions of source code must retain the above copyright notice, this 00008 list of conditions and the following disclaimer. 00009 00010 * Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions and the following disclaimer in the documentation 00012 and/or other materials provided with the distribution. 00013 00014 * Neither the name of the copyright holder nor the names of its 00015 contributors may be used to endorse or promote products derived from 00016 this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00028 ******************************************************************************/ 00029 00030 #ifndef TOWR_INCLUDE_TOWR_ROS_TOWR_ROS_H_ 00031 #define TOWR_INCLUDE_TOWR_ROS_TOWR_ROS_H_ 00032 00033 #include <string> 00034 00035 #include <ros/ros.h> 00036 #include <rosbag/bag.h> 00037 00038 #include <xpp_states/robot_state_cartesian.h> 00039 #include <xpp_msgs/RobotStateCartesian.h> 00040 #include <xpp_msgs/RobotParameters.h> 00041 #include <towr_ros/TowrCommand.h> 00042 00043 #include <towr/nlp_formulation.h> 00044 #include <ifopt/ipopt_solver.h> 00045 00046 00047 namespace towr { 00048 00049 00057 class TowrRosInterface { 00058 public: 00059 using XppVec = std::vector<xpp::RobotStateCartesian>; 00060 using TowrCommandMsg = towr_ros::TowrCommand; 00061 using Vector3d = Eigen::Vector3d; 00062 00063 protected: 00064 TowrRosInterface (); 00065 virtual ~TowrRosInterface () = default; 00066 00070 virtual void SetTowrInitialState() = 0; 00071 00079 virtual Parameters GetTowrParameters(int n_ee, const TowrCommandMsg& msg) const = 0; 00080 00085 virtual void SetIpoptParameters(const TowrCommandMsg& msg) = 0; 00086 00087 NlpFormulation formulation_; 00088 ifopt::IpoptSolver::Ptr solver_; 00089 00090 private: 00091 SplineHolder solution; 00092 ifopt::Problem nlp_; 00093 double visualization_dt_; 00094 00095 ::ros::Subscriber user_command_sub_; 00096 ::ros::Publisher initial_state_pub_; 00097 ::ros::Publisher robot_parameters_pub_; 00098 00099 void UserCommandCallback(const TowrCommandMsg& msg); 00100 XppVec GetTrajectory() const; 00101 virtual BaseState GetGoalState(const TowrCommandMsg& msg) const; 00102 void PublishInitialState(); 00103 std::vector<XppVec>GetIntermediateSolutions(); 00104 xpp_msgs::RobotParameters BuildRobotParametersMsg(const RobotModel& model) const; 00105 void SaveOptimizationAsRosbag(const std::string& bag_name, 00106 const xpp_msgs::RobotParameters& robot_params, 00107 const TowrCommandMsg user_command_msg, 00108 bool include_iterations=false); 00109 void SaveTrajectoryInRosbag (rosbag::Bag&, 00110 const std::vector<xpp::RobotStateCartesian>& traj, 00111 const std::string& topic) const; 00112 }; 00113 00114 } /* namespace towr */ 00115 00116 #endif /* XPP_OPT_INCLUDE_XPP_ROS_OPTIMIZER_NODE_H_ */