message_callback.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2016, ROBOTIS CO., LTD.
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 met:
7  *
8  * * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of ROBOTIS nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *******************************************************************************/
30 
31 /*
32  * message_callback.cpp
33  *
34  * Created on: 2016. 6. 10.
35  * Author: Jay Song
36  */
37 
39 
42 
44 
46 
48 
52 
54 
55 thormang3_walking_module_msgs::AddStepDataArray add_step_data_array_srv;
56 
57 thormang3_foot_step_generator::FootStepCommand last_command;
59 
60 
62 
63 void initialize(void)
64 {
65  ros::NodeHandle nh;
66 
67  g_get_ref_step_data_client = nh.serviceClient<thormang3_walking_module_msgs::GetReferenceStepData>("/robotis/walking/get_reference_step_data");
68  g_add_step_data_array_client = nh.serviceClient<thormang3_walking_module_msgs::AddStepDataArray>("/robotis/walking/add_step_data");
69  g_set_balance_param_client = nh.serviceClient<thormang3_walking_module_msgs::SetBalanceParam>("/robotis/walking/set_balance_param");
70  g_is_running_client = nh.serviceClient<thormang3_walking_module_msgs::IsRunning>("/robotis/walking/is_running");
71 
72  g_walking_module_status_msg_sub = nh.subscribe("/robotis/status", 10, walkingModuleStatusMSGCallback);
73 
74  g_walking_command_sub = nh.subscribe("/robotis/thormang3_foot_step_generator/walking_command", 0, walkingCommandCallback);
75  g_footsteps_2d_sub = nh.subscribe("/robotis/thormang3_foot_step_generator/footsteps_2d", 0, step2DArrayCallback);
76 
78 }
79 
80 void walkingModuleStatusMSGCallback(const robotis_controller_msgs::StatusMsg::ConstPtr& msg)
81 {
82  if(msg->type == msg->STATUS_ERROR)
83  ROS_ERROR_STREAM("[Robot] : " << msg->status_msg);
84  else if(msg->type == msg->STATUS_INFO)
85  ROS_INFO_STREAM("[Robot] : " << msg->status_msg);
86  else if(msg->type == msg->STATUS_WARN)
87  ROS_WARN_STREAM("[Robot] : " << msg->status_msg);
88  else if(msg->type == msg->STATUS_UNKNOWN)
89  ROS_ERROR_STREAM("[Robot] : " << msg->status_msg);
90  else
91  ROS_ERROR_STREAM("[Robot] : " << msg->status_msg);
92 }
93 
94 void walkingCommandCallback(const thormang3_foot_step_generator::FootStepCommand::ConstPtr &msg)
95 {
96  double now_time = ros::Time::now().toSec();
97 
98  if((last_command.command == msg->command)
99  && (last_command.step_num == msg->step_num)
100  && (last_command.step_time == msg->step_time)
101  && (last_command.step_length == msg->step_length)
102  && (last_command.side_step_length == msg->side_step_length)
103  && (last_command.step_angle_rad == msg->step_angle_rad))
104  {
105  //prevent double click
106  if( (fabs(now_time - g_last_command_time) < last_command.step_time) )
107  {
108  ROS_ERROR("Receive same command in short time");
109  return;
110  }
111  }
112 
113  g_last_command_time = now_time;
114 
115  last_command.command = msg->command;
116  last_command.step_num = msg->step_num;
117  last_command.step_time = msg->step_time;
118  last_command.step_length = msg->step_length;
119  last_command.side_step_length = msg->side_step_length;
120  last_command.step_angle_rad = msg->step_angle_rad;
121 
122 
123  ROS_INFO("[Demo] : Walking Command");
124  ROS_INFO_STREAM(" command : " << msg->command );
125  ROS_INFO_STREAM(" step_num : " << msg->step_num );
126  ROS_INFO_STREAM(" step_time : " << msg->step_time );
127  ROS_INFO_STREAM(" step_length : " << msg->step_length);
128  ROS_INFO_STREAM(" side_step_length : " << msg->side_step_length );
129  ROS_INFO_STREAM(" step_angle_rad : " << msg->step_angle_rad );
130 
131  if((msg->step_num == 0)
132  && (msg->command != "left kick")
133  && (msg->command != "right kick")
134  && (msg->command != "stop"))
135  return;
136 
137  //set walking parameter
138  if(msg->step_length < 0)
139  {
140  g_foot_stp_generator.fb_step_length_m_ = 0;
141  ROS_ERROR_STREAM("step_length is negative.");
142  ROS_ERROR_STREAM("It will be set to zero.");
143  }
144  else
145  {
146  g_foot_stp_generator.fb_step_length_m_ = msg->step_length;
147  }
148 
149  if(msg->side_step_length < 0)
150  {
151  g_foot_stp_generator.rl_step_length_m_ = 0;
152  ROS_ERROR_STREAM("side_step_length is negative.");
153  ROS_ERROR_STREAM("It will be set to zero.");
154  }
155  else
156  {
157  g_foot_stp_generator.rl_step_length_m_ = msg->side_step_length;
158  }
159 
160  if(msg->step_angle_rad < 0)
161  {
162  g_foot_stp_generator.rotate_step_angle_rad_ = 0;
163  ROS_ERROR_STREAM("step_angle_rad is negative.");
164  ROS_ERROR_STREAM("It will be set to zero.");
165  }
166  else
167  {
168  g_foot_stp_generator.rotate_step_angle_rad_ = msg->step_angle_rad;
169  }
170 
171  if(msg->step_time < MINIMUM_STEP_TIME_SEC)
172  {
173  g_foot_stp_generator.step_time_sec_ = MINIMUM_STEP_TIME_SEC;
174  ROS_ERROR_STREAM("step_time is less than minimum step time. ");
175  ROS_ERROR_STREAM("It will be set to minimum step time(0.4 sec).");
176  }
177  else
178  {
179  g_foot_stp_generator.step_time_sec_ = msg->step_time;
180  }
181 
182  g_foot_stp_generator.num_of_step_ = 2*(msg->step_num) + 2;
183 
184 
185  thormang3_walking_module_msgs::GetReferenceStepData get_ref_stp_data_srv;
186  thormang3_walking_module_msgs::StepData ref_step_data;
187  thormang3_walking_module_msgs::AddStepDataArray add_stp_data_srv;
188 
189 
190  //get reference step data
191  if(g_get_ref_step_data_client.call(get_ref_stp_data_srv) == false)
192  {
193  ROS_ERROR("Failed to get reference step data");
194  return;
195  }
196 
197  ref_step_data = get_ref_stp_data_srv.response.reference_step_data;
198 
199  //calc step data
200  if(msg->command == "forward")
201  {
202  if(g_is_running_check_needed == true)
203  if(isRunning() == true)
204  return;
205 
206  g_foot_stp_generator.getStepData( &add_stp_data_srv.request.step_data_array, ref_step_data, FORWARD_WALKING);
208  }
209  else if(msg->command == "backward")
210  {
211  if(g_is_running_check_needed == true)
212  if(isRunning() == true)
213  return;
214 
215  g_foot_stp_generator.getStepData( &add_stp_data_srv.request.step_data_array, ref_step_data, BACKWARD_WALKING);
217  }
218  else if(msg->command == "turn left")
219  {
220  if(g_is_running_check_needed == true)
221  if(isRunning() == true)
222  return;
223 
224  g_foot_stp_generator.getStepData( &add_stp_data_srv.request.step_data_array, ref_step_data, LEFT_ROTATING_WALKING);
226  }
227  else if(msg->command == "turn right")
228  {
229  if(g_is_running_check_needed == true)
230  if(isRunning() == true)
231  return;
232 
233  g_foot_stp_generator.getStepData( &add_stp_data_srv.request.step_data_array, ref_step_data, RIGHT_ROTATING_WALKING);
235  }
236  else if(msg->command == "right")
237  {
238  if(g_is_running_check_needed == true)
239  if(isRunning() == true)
240  return;
241 
242  g_foot_stp_generator.getStepData( &add_stp_data_srv.request.step_data_array, ref_step_data, RIGHTWARD_WALKING);
244  }
245  else if(msg->command == "left")
246  {
247  if(g_is_running_check_needed == true)
248  if(isRunning() == true)
249  return;
250 
251  g_foot_stp_generator.getStepData( &add_stp_data_srv.request.step_data_array, ref_step_data, LEFTWARD_WALKING);
253  }
254  else if(msg->command == "right kick")
255  {
256  if(isRunning() == true)
257  return;
258 
259  g_foot_stp_generator.calcRightKickStep( &add_stp_data_srv.request.step_data_array, ref_step_data);
261  }
262  else if(msg->command == "left kick")
263  {
264  if(isRunning() == true)
265  return;
266 
267  g_foot_stp_generator.calcLeftKickStep( &add_stp_data_srv.request.step_data_array, ref_step_data);
269  }
270  else if(msg->command == "stop")
271  {
272  if(g_is_running_check_needed == true)
273  if(isRunning() == true)
274  return;
275 
276  g_foot_stp_generator.getStepData( &add_stp_data_srv.request.step_data_array, ref_step_data, STOP_WALKING);
278  }
279  else
280  {
281  ROS_ERROR("[Demo] : Invalid Command");
282  return;
283  }
284 
285  //set add step data srv for auto start
286  add_stp_data_srv.request.auto_start = true;
287  add_stp_data_srv.request.remove_existing_step_data = true;
288 
289  //add step data
290  if(g_add_step_data_array_client.call(add_stp_data_srv) == true)
291  {
292  int add_stp_data_srv_result = add_stp_data_srv.response.result;
293  if(add_stp_data_srv_result== thormang3_walking_module_msgs::AddStepDataArray::Response::NO_ERROR)
294  {
295  ROS_INFO("[Demo] : Succeed to add step data array");
296  }
297  else
298  {
299  ROS_ERROR("[Demo] : Failed to add step data array");
300 
301  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::NOT_ENABLED_WALKING_MODULE)
302  ROS_ERROR("[Demo] : STEP_DATA_ERR::NOT_ENABLED_WALKING_MODULE");
303  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::PROBLEM_IN_POSITION_DATA)
304  ROS_ERROR("[Demo] : STEP_DATA_ERR::PROBLEM_IN_POSITION_DATA");
305  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::PROBLEM_IN_TIME_DATA)
306  ROS_ERROR("[Demo] : STEP_DATA_ERR::PROBLEM_IN_TIME_DATA");
307  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::TOO_MANY_STEP_DATA)
308  ROS_ERROR("[Demo] : STEP_DATA_ERR::TOO_MANY_STEP_DATA");
309  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::ROBOT_IS_WALKING_NOW)
310  ROS_ERROR("[Demo] : STEP_DATA_ERR::ROBOT_IS_WALKING_NOW");
311 
312  g_foot_stp_generator.initialize();
313 
314  return;
315  }
316  }
317  else
318  {
319  ROS_ERROR("[Demo] : Failed to add step data array ");
320  g_foot_stp_generator.initialize();
321  return;
322  }
323 
324 }
325 
326 
327 void step2DArrayCallback(const thormang3_foot_step_generator::Step2DArray::ConstPtr& msg)
328 {
329  thormang3_walking_module_msgs::GetReferenceStepData get_ref_stp_data_srv;
330  thormang3_walking_module_msgs::StepData ref_step_data;
331  thormang3_walking_module_msgs::AddStepDataArray add_stp_data_srv;
332  thormang3_walking_module_msgs::IsRunning is_running_srv;
333 
334  if(isRunning() == true)
335  return;
336 
337 
338  //get reference step data
339  if(g_get_ref_step_data_client.call(get_ref_stp_data_srv) == false)
340  {
341  ROS_ERROR("[Demo] : Failed to get reference step data");
342  return;
343  }
344 
345  ref_step_data = get_ref_stp_data_srv.response.reference_step_data;
346 
347  g_foot_stp_generator.getStepDataFromStepData2DArray(&add_stp_data_srv.request.step_data_array, ref_step_data, msg);
349 
350  //set add step data srv fot auto start and remove existing step data
351  add_stp_data_srv.request.auto_start = true;
352  add_stp_data_srv.request.remove_existing_step_data = true;
353 
354  //add step data
355  if(g_add_step_data_array_client.call(add_stp_data_srv) == true)
356  {
357  int add_stp_data_srv_result = add_stp_data_srv.response.result;
358  if(add_stp_data_srv_result== thormang3_walking_module_msgs::AddStepDataArray::Response::NO_ERROR)
359  ROS_INFO("[Demo] : Succeed to add step data array");
360  else {
361  ROS_ERROR("[Demo] : Failed to add step data array");
362 
363  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::NOT_ENABLED_WALKING_MODULE)
364  ROS_ERROR("[Demo] : STEP_DATA_ERR::NOT_ENABLED_WALKING_MODULE");
365  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::PROBLEM_IN_POSITION_DATA)
366  ROS_ERROR("[Demo] : STEP_DATA_ERR::PROBLEM_IN_POSITION_DATA");
367  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::PROBLEM_IN_TIME_DATA)
368  ROS_ERROR("[Demo] : STEP_DATA_ERR::PROBLEM_IN_TIME_DATA");
369  if(add_stp_data_srv_result & thormang3_walking_module_msgs::AddStepDataArray::Response::ROBOT_IS_WALKING_NOW)
370  ROS_ERROR("[Demo] : STEP_DATA_ERR::ROBOT_IS_WALKING_NOW");
371 
372  return;
373  }
374  }
375  else
376  {
377  ROS_ERROR("[Demo] : Failed to add step data array ");
378  return;
379  }
380 }
381 
382 bool isRunning(void)
383 {
384  thormang3_walking_module_msgs::IsRunning is_running_srv;
385  if(g_is_running_client.call(is_running_srv) == false)
386  {
387  ROS_ERROR("[Demo] : Failed to Walking Status");
388  return true;
389  }
390  else
391  {
392  if(is_running_srv.response.is_running == true)
393  {
394  ROS_ERROR("[Demo] : STEP_DATA_ERR::ROBOT_IS_WALKING_NOW");
395  return true;
396  }
397  }
398  return false;
399 }
400 
401 
#define LEFTWARD_WALKING
double g_last_command_time
ros::Subscriber g_footsteps_2d_sub
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ros::Subscriber g_walking_module_status_msg_sub
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::ServiceClient g_get_ref_step_data_client
#define MINIMUM_STEP_TIME_SEC
#define STOP_WALKING
ros::Subscriber g_walking_command_sub
ros::ServiceClient g_set_balance_param_client
bool isRunning(void)
bool call(MReq &req, MRes &res)
ros::Subscriber g_balance_command_sub
void step2DArrayCallback(const thormang3_foot_step_generator::Step2DArray::ConstPtr &msg)
#define BACKWARD_WALKING
thormang3_walking_module_msgs::AddStepDataArray add_step_data_array_srv
void walkingModuleStatusMSGCallback(const robotis_controller_msgs::StatusMsg::ConstPtr &msg)
thormang3_foot_step_generator::FootStepCommand last_command
#define ROS_INFO(...)
#define RIGHT_ROTATING_WALKING
void getStepData(thormang3_walking_module_msgs::AddStepDataArray::Request::_step_data_array_type *step_data_array, const thormang3_walking_module_msgs::StepData &ref_step_data, int desired_step_type)
void calcRightKickStep(thormang3_walking_module_msgs::AddStepDataArray::Request::_step_data_array_type *step_data_array, const thormang3_walking_module_msgs::StepData &ref_step_data)
#define RIGHTWARD_WALKING
void calcLeftKickStep(thormang3_walking_module_msgs::AddStepDataArray::Request::_step_data_array_type *step_data_array, const thormang3_walking_module_msgs::StepData &ref_step_data)
#define FORWARD_WALKING
#define ROS_WARN_STREAM(args)
#define LEFT_ROTATING_WALKING
thormang3::FootStepGenerator g_foot_stp_generator
ros::ServiceClient g_is_running_client
void initialize(void)
bool g_is_running_check_needed
void getStepDataFromStepData2DArray(thormang3_walking_module_msgs::AddStepDataArray::Request::_step_data_array_type *step_data_array, const thormang3_walking_module_msgs::StepData &ref_step_data, const thormang3_foot_step_generator::Step2DArray::ConstPtr &request_step_2d)
#define ROS_INFO_STREAM(args)
static Time now()
#define ROS_ERROR_STREAM(args)
#define ROS_ERROR(...)
void walkingCommandCallback(const thormang3_foot_step_generator::FootStepCommand::ConstPtr &msg)
ros::ServiceClient g_add_step_data_array_client


thormang3_foot_step_generator
Author(s): Kayman , Jay Song
autogenerated on Mon Jun 10 2019 15:38:28