test_stability.cpp
Go to the documentation of this file.
00001 /*
00002  * hrl_kinematics - a kinematics library for humanoid robots based on KDL
00003  *
00004  * Copyright 2011-2012 Armin Hornung, University of Freiburg
00005  * License: BSD
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the University of Freiburg nor the names of its
00016  *       contributors may be used to endorse or promote products derived from
00017  *       this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include <hrl_kinematics/TestStability.h>
00033 #include <ros/ros.h>
00034 
00035 
00036 namespace hrl_kinematics {
00037   
00043 class TestStabilityNode {
00044 public:
00045   TestStabilityNode(Kinematics::FootSupport support = Kinematics::SUPPORT_DOUBLE);
00046   TestStabilityNode(ros::NodeHandle _nh_, TestStability _test_stability_, Kinematics::FootSupport support);
00047   virtual ~TestStabilityNode();
00048   void initialize();
00049   void jointStateCb(const sensor_msgs::JointStateConstPtr& state);
00050 
00051 protected:
00052   ros::NodeHandle nh_;
00053   ros::Subscriber joint_state_sub_;
00054   ros::Publisher visualization_pub_, support_polygon_pub_, pcom_pub_;
00055   TestStability test_stability_;
00056   Kinematics::FootSupport support_mode_;
00057 };
00058 
00059 TestStabilityNode::TestStabilityNode(ros::NodeHandle _nh_, TestStability _test_stability_, Kinematics::FootSupport support)
00060   : nh_(_nh_), test_stability_(_test_stability_), support_mode_ (support)
00061 {
00062   initialize();
00063 }
00064 
00065 TestStabilityNode::TestStabilityNode(Kinematics::FootSupport support)
00066 : support_mode_(support)
00067 {
00068   initialize();
00069 }
00070 
00071 TestStabilityNode::~TestStabilityNode(){
00072 
00073 }
00074 
00075 void TestStabilityNode::initialize()
00076 {
00077   ros::NodeHandle nh_private("~");
00078   visualization_pub_ = nh_private.advertise<visualization_msgs::MarkerArray>("stability_visualization", 1);
00079   support_polygon_pub_ = nh_private.advertise<geometry_msgs::PolygonStamped>("support_polygon", 10);
00080   pcom_pub_ = nh_private.advertise<visualization_msgs::Marker>("projected_com", 10);
00081 
00082   joint_state_sub_ = nh_.subscribe("joint_states", 1, &TestStabilityNode::jointStateCb, this);
00083 }
00084 
00085 void TestStabilityNode::jointStateCb(const sensor_msgs::JointStateConstPtr& state){
00086   size_t num_joints = state->position.size();
00087   ROS_DEBUG("Received JointState with %zu joints", num_joints);
00088 
00089   // TODO: need to obtain current support mode
00090 
00091   // get joint positions from state message
00092   std::map<std::string, double> joint_positions;
00093   for (unsigned int i=0; i<state->name.size(); i++)
00094     joint_positions.insert(make_pair(state->name[i], state->position[i]));
00095 
00096   tf::Vector3 normal_vector(0.0, 0.0, 1.0); // planar support
00097   // for testing, normal vector of arbitrary plane:
00098   //tf::Vector3 normal_vector(0.5, 0.0, 1.0);
00099   normal_vector.normalize();
00100 
00101   bool stable = test_stability_.isPoseStable(joint_positions, support_mode_, normal_vector);
00102 
00103   // print info
00104   tf::Point com = test_stability_.getCOM();
00105   if (stable)
00106     ROS_INFO("Pose is stable, pCOM at %f %f", com.x(), com.y());
00107   else
00108     ROS_INFO("Pose is NOT stable, pCOM at %f %f", com.x(), com.y());
00109 
00110   // publish visualization:
00111 
00112   // TODO link coms:
00113 //  visualization_msgs::MarkerArray com_vis_markers;
00114 //
00115 //  visualization_pub_.publish(vis_markers);
00116 //  visualization_msgs::Marker marker;
00117 //  createCoGMarker(kdl_tree_.getRootSegment()->first, "torso", 0.04, com, marker);
00118 //
00119 //  com_vis_markers_.markers.push_back(marker);
00120 //
00121 //
00122 //  visualization_pub_.publish(com_vis_markers_);
00123 
00124   support_polygon_pub_.publish(test_stability_.getSupportPolygon());
00125   pcom_pub_.publish(test_stability_.getCOMMarker());
00126 
00127 }
00128 
00129 
00130 }
00131 
00132 using namespace hrl_kinematics;
00133 
00134 int main(int argc, char** argv)
00135 {
00136   ros::init(argc, argv, "test_stability");
00137 
00138   Kinematics::FootSupport support = Kinematics::SUPPORT_DOUBLE;
00139 
00140   if (argc == 2){
00141     int i = atoi(argv[1]);
00142     if (i >= 0 && i <= 2)
00143       support = Kinematics::FootSupport(i);
00144 
00145   }
00146 
00147   try
00148   {
00149     ros::spinOnce();
00150 
00151     ros::NodeHandle nh_;
00152     std::string root_link_name_, rfoot_link_name_, lfoot_link_name_, rfoot_mesh_link_name_;
00153     if (!nh_.getParam("test_stability/root_link_name", root_link_name_))
00154       root_link_name_ = "base_link";
00155     if (!nh_.getParam("test_stability/rfoot_link_name", rfoot_link_name_))
00156       rfoot_link_name_ = "r_sole";
00157     if (!nh_.getParam("test_stability/lfoot_link_name", lfoot_link_name_))
00158       lfoot_link_name_ = "l_sole";
00159     if (!nh_.getParam("test_stability/rfoot_mesh_link_name", rfoot_mesh_link_name_))
00160       rfoot_mesh_link_name_ = "RAnkleRoll_link";
00161 
00162     hrl_kinematics::TestStability test_stability_(root_link_name_, rfoot_link_name_, lfoot_link_name_, rfoot_mesh_link_name_);
00163     hrl_kinematics::TestStabilityNode StabilityNode(nh_, test_stability_, support);
00164     // hrl_kinematics::TestStabilityNode StabilityNode(support);
00165 
00166     ros::spin();
00167   }
00168   catch(hrl_kinematics::Kinematics::InitFailed &e)
00169   {
00170     std::cerr << "Could not initialize kinematics node: " << e.what() << std::endl;
00171     // TODO: does not work?
00172     //ROS_INFO("Could not initialize kinematics node: %s\n", e.what());
00173     return 1;
00174   }
00175 
00176   return 0;
00177 
00178 }
00179 


hrl_kinematics
Author(s): Armin Hornung
autogenerated on Wed Aug 26 2015 11:51:09