moveto.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
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 /*
19  * LOCAL includes
20  */
21 #include "moveto.hpp"
22 
23 /*
24  * ROS includes
25  */
26 //#include <tf/transform_datatypes.h>
28 
29 #include "../helpers/transform_helpers.hpp"
30 
31 namespace naoqi
32 {
33 namespace subscriber
34 {
35 
36 MovetoSubscriber::MovetoSubscriber( const std::string& name, const std::string& topic, const qi::SessionPtr& session,
37  const boost::shared_ptr<tf2_ros::Buffer>& tf2_buffer):
38  BaseSubscriber( name, topic, session ),
39  p_motion_( session->service("ALMotion") ),
40  tf2_buffer_( tf2_buffer )
41 {}
42 
44 {
46  is_initialized_ = true;
47 }
48 
49 void MovetoSubscriber::callback( const geometry_msgs::PoseStampedConstPtr& pose_msg )
50 {
51  if (pose_msg->header.frame_id == "odom") {
52  geometry_msgs::PoseStamped pose_msg_bf;
53 
54  bool canTransform = tf2_buffer_->canTransform(
55  "base_footprint",
56  "odom",
57  ros::Time(0),
58  ros::Duration(2));
59 
60  if (!canTransform) {
61  std::cout << "Cannot transform from "
62  << "odom"
63  << " to base_footprint"
64  << std::endl;
65  return;
66  }
67 
68  try {
69  tf2_buffer_->transform(
70  *pose_msg,
71  pose_msg_bf,
72  "base_footprint",
73  ros::Time(0),
74  "odom");
75 
76  double yaw = helpers::transform::getYaw(pose_msg_bf.pose);
77 
78  std::cout << "odom to move x: " << pose_msg_bf.pose.position.x
79  << " y: " << pose_msg_bf.pose.position.y
80  << " yaw: " << yaw << std::endl;
81 
82  if (std::isnan(yaw)) {
83  yaw = 0.0;
84  std::cout << "Yaw is nan, changed to 0.0" << std::endl;
85  }
86 
87  p_motion_.async<void>(
88  "moveTo",
89  pose_msg_bf.pose.position.x,
90  pose_msg_bf.pose.position.y,
91  yaw);
92 
93  } catch( const tf2::LookupException& e) {
94  std::cout << e.what() << std::endl;
95  std::cout << "moveto position in frame_id "
96  << pose_msg->header.frame_id
97  << "is not supported in any other base frame than "
98  "basefootprint"
99  << std::endl;
100  } catch( const tf2::ExtrapolationException& e) {
101  std::cout << "received an error on the time lookup" << std::endl;
102  }
103  }
104  else if (pose_msg->header.frame_id == "base_footprint"){
105  double yaw = helpers::transform::getYaw(pose_msg->pose);
106  std::cout << "going to move x: "
107  << pose_msg->pose.position.x
108  << " y: " << pose_msg->pose.position.y
109  << " yaw: " << yaw << std::endl;
110 
111  if (std::isnan(yaw)) {
112  yaw = 0.0;
113  std::cout << "Yaw is nan, changed to 0.0" << std::endl;
114  }
115 
116  p_motion_.async<void>(
117  "moveTo",
118  pose_msg->pose.position.x,
119  pose_msg->pose.position.y,
120  yaw);
121  }
122 
123  else
124  std::cout << "Cannot reach position expressed in the "
125  << pose_msg->header.frame_id
126  << " frame, enter a valid frame id in the pose's header"
127  " (base_footprint or odom)"
128  << std::endl;
129 }
130 
131 } //publisher
132 } // naoqi
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void callback(const geometry_msgs::PoseStampedConstPtr &pose_msg)
Definition: moveto.cpp:49
void reset(ros::NodeHandle &nh)
Definition: moveto.cpp:43
double getYaw(const geometry_msgs::Pose &pose)
MovetoSubscriber(const std::string &name, const std::string &topic, const qi::SessionPtr &session, const boost::shared_ptr< tf2_ros::Buffer > &tf2_buffer)
Definition: moveto.cpp:36
boost::shared_ptr< tf2_ros::Buffer > tf2_buffer_
Definition: moveto.hpp:51


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 15 2020 03:24:26