motion.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 SoftBank Robotics Europe
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 // ROS Headers
19 #include <ros/ros.h>
20 
23 
24 Motion::Motion(const qi::SessionPtr& session)
25 {
26  try
27  {
28  motion_proxy_ = session->service("ALMotion");
29  }
30  catch (const std::exception& e)
31  {
32  ROS_ERROR("Motion: Failed to connect to Motion Proxy!\n\tTrace: %s", e.what());
33  }
34 
35  //set walk arms enabled / disabled
36  try
37  {
38  motion_proxy_.call<void>("setMoveArmsEnabled", 0, 0);
39  }
40  catch (const std::exception& e)
41  {
42  ROS_WARN("Motion: Failed to set move arms enabled!\n\tTrace: %s", e.what());
43  }
44 
45  //set external collision protection of the robot
46  try
47  {
48  motion_proxy_.call<void>("setExternalCollisionProtectionEnabled", "Arms", 0);
49  }
50  catch (const std::exception& e)
51  {
52  ROS_WARN("Motion: Failed to set external collision protection!\n\tTrace: %s", e.what());
53  }
54 
55  //set Smart Stiffness
56  try
57  {
58  motion_proxy_.call<void>("setSmartStiffnessEnabled", 1);
59  }
60  catch (const std::exception& e)
61  {
62  ROS_WARN("Motion: Failed to set smart stiffness!\n\tTrace: %s", e.what());
63  }
64 }
65 
66 void Motion::init(const std::vector <std::string> &joints_names)
67 {
68  joints_names_ = joints_names;
69 }
70 
72 {
73  bool res(false);
74  try
75  {
76  if (motion_proxy_.call<bool>("robotIsWakeUp"))
77  res = true;
78  }
79  catch (const std::exception& e)
80  {
81  ROS_WARN("Motion: Failed to check if the robot is wakedup!\n\tTrace: %s", e.what());
82  }
83  return res;
84 }
85 
87 {
88  try
89  {
90  if (!robotIsWakeUp())
91  {
92  ROS_INFO_STREAM("Going to wakeup ...");
93  motion_proxy_.call<void>("wakeUp");
94  ros::Duration(3.0).sleep();
95  }
96  }
97  catch (const std::exception& e)
98  {
99  ROS_WARN("Motion: Failed to wake up the robot!\n\tTrace: %s", e.what());
100  }
101 }
102 
104 {
105  try
106  {
107  if (motion_proxy_.call<bool>("robotIsWakeUp"))
108  {
109  ROS_INFO_STREAM("Going to rest ...");
110  motion_proxy_.call<void>("rest");
111  sleep(4);
112  }
113  }
114  catch (const std::exception& e)
115  {
116  ROS_WARN("Motion: Failed to go to rest!\n\tTrace: %s", e.what());
117  }
118 }
119 
120 std::vector <std::string> Motion::getBodyNames(const std::string &robot_part)
121 {
122  std::vector <std::string> joints;
123  try
124  {
125  joints = motion_proxy_.call< std::vector<std::string> >("getBodyNames", robot_part);
126  }
127  catch (const std::exception& e)
128  {
129  ROS_ERROR("Motion: Failed to get robot body names!\n\tTrace: %s", e.what());
130  }
131 
132  return joints;
133 }
134 
135 std::vector <std::string> Motion::getBodyNamesFromGroup(const std::vector<std::string> &motor_groups)
136 {
137  std::vector <std::string> res;
138 
139  for (std::vector<std::string>::const_iterator it=motor_groups.begin(); it!=motor_groups.end(); ++it)
140  {
141  std::vector <std::string> joint_names = getBodyNames(*it);
142  res.insert(res.end(), joint_names.begin(), joint_names.end());
143  }
144  return res;
145 }
146 
148 {
149  //set Smart Stiffness
150  try
151  {
152  motion_proxy_.call<void>("setSmartStiffnessEnabled", 0);
153  }
154  catch (const std::exception& e)
155  {
156  ROS_WARN("Motion: Failed to set external collision protection!\n\tTrace: %s", e.what());
157  }
158 
159  //set PushRecoveryEnabled
160  try
161  {
162  motion_proxy_.call<void>("setPushRecoveryEnabled", 0);
163  }
164  catch (const std::exception& e)
165  {
166  ROS_WARN("Motion: Failed to set Push Recovery Enabled!\n\tTrace: %s", e.what());
167  }
168 }
169 
170 void Motion::moveTo(const float& vel_x, const float& vel_y, const float& vel_th)
171 {
172  ROS_INFO_STREAM("going to move x: " << vel_x << " y: " << vel_y << " th: " << vel_th);
173 
174  try
175  {
176  motion_proxy_.call<void>("moveTo", vel_x, vel_y, vel_th);
177  }
178  catch (const std::exception& e)
179  {
180  ROS_WARN("Motion: Failed to execute MoveTo!\n\tTrace: %s", e.what());
181  }
182 }
183 
184 std::vector<double> Motion::getAngles(const std::string &robot_part)
185 {
186  std::vector<double> res;
187 
188  try
189  {
190  res = motion_proxy_.call< std::vector<double> >("getAngles", robot_part, 1);
191  }
192  catch (const std::exception& e)
193  {
194  ROS_WARN("Motion: Failed to get joints angles!\n\tTrace: %s", e.what());
195  }
196 
197  return res;
198 }
199 
200 void Motion::writeJoints(const std::vector <double> &joint_commands)
201 {
202  //prepare the list of joints
203  qi::AnyValue names_qi = fromStringVectorToAnyValue(joints_names_);
204 
205  //prepare the list of joint angles
206  qi::AnyValue angles_qi = fromDoubleVectorToAnyValue(joint_commands);
207 
208  try
209  {
210  motion_proxy_.async<void>("setAngles", names_qi, angles_qi, 0.2f);
211  }
212  catch(const std::exception& e)
213  {
214  ROS_ERROR("Motion: Failed to set joints nagles! \n\tTrace: %s", e.what());
215  }
216 }
217 
218 bool Motion::stiffnessInterpolation(const std::vector<std::string> &motor_groups,
219  const float &stiffness,
220  const float &time)
221 {
222  std::vector<std::string>::const_iterator it=motor_groups.begin();
223  for (; it!=motor_groups.end(); ++it)
224  if (!stiffnessInterpolation(*it, stiffness, time))
225  return false;
226  return true;
227 }
228 
229 bool Motion::stiffnessInterpolation(const std::string &motor_group,
230  const float &stiffness,
231  const float &time)
232 {
233  try
234  {
235  motion_proxy_.call<void>("stiffnessInterpolation", motor_group, stiffness, time);
236  }
237  catch (const std::exception &e)
238  {
239  ROS_ERROR("Motion: Failed to set stiffness \n\tTrace: %s", e.what());
240  return false;
241  }
242 
243  ROS_INFO_STREAM("Stiffness is updated to " << stiffness << " for " << motor_group);
244 
245  return true;
246 }
247 
248 bool Motion::setStiffnessArms(const float &stiffness, const float &time)
249 {
250  if (!stiffnessInterpolation("LArm", stiffness, time))
251  return false;
252  if (!stiffnessInterpolation("RArm", stiffness, time))
253  return false;
254 
255  return true;
256 }
bool sleep() const
bool setStiffnessArms(const float &stiffness, const float &time)
set stiffness for arms
Definition: motion.cpp:248
void moveTo(const float &vel_x, const float &vel_y, const float &vel_th)
Move the robot at given velocity and angle.
Definition: motion.cpp:170
qi::AnyValue fromDoubleVectorToAnyValue(const std::vector< double > &vector)
Definition: tools.cpp:46
void manageConcurrence()
Manage concurrence of DCM and ALMotion.
Definition: motion.cpp:147
void wakeUp()
wake up the robot
Definition: motion.cpp:86
#define ROS_WARN(...)
qi::AnyValue fromStringVectorToAnyValue(const std::vector< std::string > &vector)
Definition: tools.cpp:22
std::vector< std::string > getBodyNamesFromGroup(const std::vector< std::string > &motor_groups)
get body names based on motor groups
Definition: motion.cpp:135
qi::AnyObject motion_proxy_
Definition: motion.hpp:77
std::vector< double > getAngles(const std::string &robot_part)
get joints angles
Definition: motion.cpp:184
void init(const std::vector< std::string > &joints_names)
initialize with joints names to control
Definition: motion.cpp:66
Motion(const qi::SessionPtr &session)
Definition: motion.cpp:24
std::vector< std::string > joints_names_
Definition: motion.hpp:80
void writeJoints(const std::vector< double > &joint_commands)
set joints values
Definition: motion.cpp:200
void rest()
go to the rest position
Definition: motion.cpp:103
#define ROS_INFO_STREAM(args)
bool robotIsWakeUp()
check if the robot is waked up
Definition: motion.cpp:71
std::vector< std::string > getBodyNames(const std::string &robot_part)
get body names
Definition: motion.cpp:120
bool stiffnessInterpolation(const std::string &motor_group, const float &stiffness, const float &time)
set stiffness for one motor group
Definition: motion.cpp:229
#define ROS_ERROR(...)


naoqi_dcm_driver
Author(s): Konstantinos Chatzilygeroudis , Mikael Arguedas , Karsten Knese , Natalia Lyubova
autogenerated on Thu Jul 25 2019 03:49:27