motion.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 SoftBank Robotics Europe
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 // ROS Headers
00019 #include <ros/ros.h>
00020 
00021 #include "naoqi_dcm_driver/motion.hpp"
00022 #include "naoqi_dcm_driver/tools.hpp"
00023 
00024 Motion::Motion(const qi::SessionPtr& session)
00025 {
00026   try
00027   {
00028     motion_proxy_ = session->service("ALMotion");
00029   }
00030   catch (const std::exception& e)
00031   {
00032     ROS_ERROR("Motion: Failed to connect to Motion Proxy!\n\tTrace: %s", e.what());
00033   }
00034 
00035   //set walk arms enabled / disabled
00036   try
00037   {
00038     motion_proxy_.call<void>("setMoveArmsEnabled", 0, 0);
00039   }
00040   catch (const std::exception& e)
00041   {
00042     ROS_WARN("Motion: Failed to set move arms enabled!\n\tTrace: %s", e.what());
00043   }
00044 
00045   //set external collision protection of the robot
00046   try
00047   {
00048     motion_proxy_.call<void>("setExternalCollisionProtectionEnabled", "Arms", 0);
00049   }
00050   catch (const std::exception& e)
00051   {
00052     ROS_WARN("Motion: Failed to set external collision protection!\n\tTrace: %s", e.what());
00053   }
00054 
00055   //set Smart Stiffness
00056   try
00057   {
00058     motion_proxy_.call<void>("setSmartStiffnessEnabled", 1);
00059   }
00060   catch (const std::exception& e)
00061   {
00062     ROS_WARN("Motion: Failed to set smart stiffness!\n\tTrace: %s", e.what());
00063   }
00064 }
00065 
00066 void Motion::init(const std::vector <std::string> &joints_names)
00067 {
00068   joints_names_ = joints_names;
00069 }
00070 
00071 bool Motion::robotIsWakeUp()
00072 {
00073   bool res(false);
00074   try
00075   {
00076     if (motion_proxy_.call<bool>("robotIsWakeUp"))
00077       res = true;
00078   }
00079   catch (const std::exception& e)
00080   {
00081     ROS_WARN("Motion: Failed to check if the robot is wakedup!\n\tTrace: %s", e.what());
00082   }
00083   return res;
00084 }
00085 
00086 void Motion::wakeUp()
00087 {
00088   try
00089   {
00090     if (!robotIsWakeUp())
00091     {
00092       ROS_INFO_STREAM("Going to wakeup ...");
00093       motion_proxy_.call<void>("wakeUp");
00094       ros::Duration(3.0).sleep();
00095     }
00096   }
00097   catch (const std::exception& e)
00098   {
00099     ROS_WARN("Motion: Failed to wake up the robot!\n\tTrace: %s", e.what());
00100   }
00101 }
00102 
00103 void Motion::rest()
00104 {
00105   try
00106   {
00107     if (motion_proxy_.call<bool>("robotIsWakeUp"))
00108     {
00109       ROS_INFO_STREAM("Going to rest ...");
00110       motion_proxy_.call<void>("rest");
00111       sleep(4);
00112     }
00113   }
00114   catch (const std::exception& e)
00115   {
00116     ROS_WARN("Motion: Failed to go to rest!\n\tTrace: %s", e.what());
00117   }
00118 }
00119 
00120 std::vector <std::string> Motion::getBodyNames(const std::string &robot_part)
00121 {
00122   std::vector <std::string> joints;
00123   try
00124   {
00125     joints = motion_proxy_.call< std::vector<std::string> >("getBodyNames", robot_part);
00126   }
00127   catch (const std::exception& e)
00128   {
00129     ROS_ERROR("Motion: Failed to get robot body names!\n\tTrace: %s", e.what());
00130   }
00131 
00132   return joints;
00133 }
00134 
00135 std::vector <std::string> Motion::getBodyNamesFromGroup(const std::vector<std::string> &motor_groups)
00136 {
00137   std::vector <std::string> res;
00138 
00139   for (std::vector<std::string>::const_iterator it=motor_groups.begin(); it!=motor_groups.end(); ++it)
00140   {
00141     std::vector <std::string> joint_names = getBodyNames(*it);
00142     res.insert(res.end(), joint_names.begin(), joint_names.end());
00143   }
00144   return res;
00145 }
00146 
00147 void Motion::manageConcurrence()
00148 {
00149   //set Smart Stiffness
00150   try
00151   {
00152     motion_proxy_.call<void>("setSmartStiffnessEnabled", 0);
00153   }
00154   catch (const std::exception& e)
00155   {
00156     ROS_WARN("Motion: Failed to set external collision protection!\n\tTrace: %s", e.what());
00157   }
00158 
00159   //set PushRecoveryEnabled
00160   try
00161   {
00162     motion_proxy_.call<void>("setPushRecoveryEnabled", 0);
00163   }
00164   catch (const std::exception& e)
00165   {
00166     ROS_WARN("Motion: Failed to set Push Recovery Enabled!\n\tTrace: %s", e.what());
00167   }
00168 }
00169 
00170 void Motion::moveTo(const float& vel_x, const float& vel_y, const float& vel_th)
00171 {
00172   ROS_INFO_STREAM("going to move x: " << vel_x << " y: " << vel_y << " th: " << vel_th);
00173 
00174   try
00175   {
00176     motion_proxy_.call<void>("moveTo", vel_x, vel_y, vel_th);
00177   }
00178   catch (const std::exception& e)
00179   {
00180     ROS_WARN("Motion: Failed to execute MoveTo!\n\tTrace: %s", e.what());
00181   }
00182 }
00183 
00184 std::vector<double> Motion::getAngles(const std::string &robot_part)
00185 {
00186   std::vector<double> res;
00187 
00188   try
00189   {
00190     res = motion_proxy_.call< std::vector<double> >("getAngles", robot_part, 1);
00191   }
00192   catch (const std::exception& e)
00193   {
00194     ROS_WARN("Motion: Failed to get joints angles!\n\tTrace: %s", e.what());
00195   }
00196 
00197   return res;
00198 }
00199 
00200 void Motion::writeJoints(const std::vector <double> &joint_commands)
00201 {
00202   //prepare the list of joints
00203   qi::AnyValue names_qi = fromStringVectorToAnyValue(joints_names_);
00204 
00205   //prepare the list of joint angles
00206   qi::AnyValue angles_qi = fromDoubleVectorToAnyValue(joint_commands);
00207 
00208   try
00209   {
00210     motion_proxy_.async<void>("setAngles", names_qi, angles_qi, 0.2f);
00211   }
00212   catch(const std::exception& e)
00213   {
00214     ROS_ERROR("Motion: Failed to set joints nagles! \n\tTrace: %s", e.what());
00215   }
00216 }
00217 
00218 bool Motion::stiffnessInterpolation(const std::vector<std::string> &motor_groups,
00219                                     const float &stiffness,
00220                                     const float &time)
00221 {
00222   std::vector<std::string>::const_iterator it=motor_groups.begin();
00223   for (; it!=motor_groups.end(); ++it)
00224     if (!stiffnessInterpolation(*it, stiffness, time))
00225       return false;
00226   return true;
00227 }
00228 
00229 bool Motion::stiffnessInterpolation(const std::string &motor_group,
00230                                     const float &stiffness,
00231                                     const float &time)
00232 {
00233   try
00234   {
00235     motion_proxy_.call<void>("stiffnessInterpolation", motor_group, stiffness, time);
00236   }
00237   catch (const std::exception &e)
00238   {
00239     ROS_ERROR("Motion: Failed to set stiffness \n\tTrace: %s", e.what());
00240     return false;
00241   }
00242 
00243   ROS_INFO_STREAM("Stiffness is updated to " << stiffness << " for " << motor_group);
00244 
00245   return true;
00246 }
00247 
00248 bool Motion::setStiffnessArms(const float &stiffness, const float &time)
00249 {
00250   if (!stiffnessInterpolation("LArm", stiffness, time))
00251     return false;
00252   if (!stiffnessInterpolation("RArm", stiffness, time))
00253     return false;
00254 
00255   return true;
00256 }


naoqi_dcm_driver
Author(s): Konstantinos Chatzilygeroudis , Mikael Arguedas , Karsten Knese , Natalia Lyubova
autogenerated on Thu Jun 6 2019 20:50:50