bin_pose_emulator.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016, Photoneo s.r.o.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Photoneo nor the names of its contributors
18  * may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 #ifndef BIN_POSE_EMULATOR_CPP
35 #define BIN_POSE_EMULATOR_CPP
36 
38 
39 Emulator::Emulator(ros::NodeHandle* nh, std::string filepath)
40 {
41  parseConfig(filepath); // parse yaml config file
42  srandom(time(NULL)); // initialize random generator
43 
44  marker_pub =
45  nh->advertise<visualization_msgs::Marker>("bin_pose_visualization", 1);
46 
47  ROS_INFO("Bin Pose Emulator Ready!");
48 }
49 
51 
52 bool Emulator::callback(bin_pose_msgs::bin_pose::Request& req,
53  bin_pose_msgs::bin_pose::Response& res)
54 {
55 
56  //-----------------------------------------------------------------------------------------
57  // Generate random Grasp pose
58  geometry_msgs::Pose grasp_pose;
59  grasp_pose.position.x = randGen(config.bin_center_x - config.bin_size_x / 2,
61  grasp_pose.position.y = randGen(config.bin_center_y - config.bin_size_y / 2,
63  grasp_pose.position.z = randGen(config.bin_center_z - config.bin_size_z / 2,
65 
66  double grasp_roll = randGen(config.roll_default - config.roll_range / 2,
68  double grasp_pitch = randGen(config.pitch_default - config.pitch_range / 2,
70  double grasp_yaw = randGen(config.yaw_default - config.yaw_range / 2,
72 
73  tf::Quaternion grasp_orientation;
74  grasp_orientation.setRPY(grasp_roll, grasp_pitch, grasp_yaw);
75  grasp_pose.orientation.x = grasp_orientation.getX();
76  grasp_pose.orientation.y = grasp_orientation.getY();
77  grasp_pose.orientation.z = grasp_orientation.getZ();
78  grasp_pose.orientation.w = grasp_orientation.getW();
79 
80  res.grasp_pose = grasp_pose;
81 
82  //------------------------------------------------------------------------------------------
83  // Calculate Approach pose according to existing grasp pose
84  geometry_msgs::Pose approach_pose;
85 
86  tf::Vector3 vector(0, 0, 1);
87  tf::Vector3 rotated_vector = tf::quatRotate(grasp_orientation, vector);
88 
89  approach_pose.position.x =
90  grasp_pose.position.x - config.approach_distance * rotated_vector.getX();
91  approach_pose.position.y =
92  grasp_pose.position.y - config.approach_distance * rotated_vector.getY();
93  approach_pose.position.z =
94  grasp_pose.position.z - config.approach_distance * rotated_vector.getZ();
95 
96  approach_pose.orientation = grasp_pose.orientation;
97  res.approach_pose = approach_pose;
98 
99  //-------------------------------------------------------------------------------------------
100  // Calculate Deapproach point according to exitsing grasp pose
101  geometry_msgs::Pose deapproach_pose;
102 
103  deapproach_pose = grasp_pose;
104  deapproach_pose.position.z =
105  deapproach_pose.position.z + config.deapproach_height;
106 
107  visualize_bin();
108  visualize_pose(grasp_pose, approach_pose);
109  broadcast_pose_tf(grasp_pose);
110 
111  res.deapproach_pose = deapproach_pose;
112 
113  return true;
114 }
115 
116 double Emulator::randGen(double fMin, double fMax)
117 {
118  double f = (double)rand() / RAND_MAX;
119  return fMin + f * (fMax - fMin);
120 }
121 
122 bool Emulator::parseConfig(std::string filepath)
123 {
124  try
125  {
126  YAML::Node config_file = YAML::LoadFile(filepath);
127  config.bin_center_x = config_file["bin_center_x"].as<float>();
128  config.bin_center_y = config_file["bin_center_y"].as<float>();
129  config.bin_center_z = config_file["bin_center_z"].as<float>();
130 
131  config.bin_size_x = config_file["bin_size_x"].as<float>();
132  config.bin_size_y = config_file["bin_size_y"].as<float>();
133  config.bin_size_z = config_file["bin_size_z"].as<float>();
134 
135  config.roll_default = config_file["roll_default"].as<float>();
136  config.pitch_default = config_file["pitch_default"].as<float>();
137  config.yaw_default = config_file["yaw_default"].as<float>();
138 
139  config.roll_range = config_file["roll_range"].as<float>();
140  config.pitch_range = config_file["pitch_range"].as<float>();
141  config.yaw_range = config_file["yaw_range"].as<float>();
142 
143  config.approach_distance = config_file["approach_distance"].as<float>();
144  config.deapproach_height = config_file["deapproach_height"].as<float>();
145  }
146  catch (YAML::ParserException& e)
147  {
148  ROS_ERROR("Error reading yaml config file");
149  }
150 }
151 
153 {
154  uint32_t shape = visualization_msgs::Marker::CUBE;
155  visualization_msgs::Marker marker;
156 
157  marker.header.frame_id = "/base_link";
158  marker.header.stamp = ros::Time::now();
159 
160  marker.ns = "bin";
161  marker.id = 0;
162  marker.type = shape;
163  marker.action = visualization_msgs::Marker::ADD;
164 
165  marker.pose.position.x = config.bin_center_x;
166  marker.pose.position.y = config.bin_center_y;
167  marker.pose.position.z = config.bin_center_z;
168 
169  marker.scale.x = config.bin_size_x;
170  marker.scale.y = config.bin_size_y;
171  marker.scale.z = config.bin_size_z;
172 
173  marker.color.r = 0.8f;
174  marker.color.g = 0.0f;
175  marker.color.b = 0.8f;
176  marker.color.a = 0.5;
177 
178  marker.lifetime = ros::Duration();
179  marker_pub.publish(marker);
180 }
181 
182 void Emulator::visualize_pose(geometry_msgs::Pose grasp_pose,
183  geometry_msgs::Pose approach_pose)
184 {
185  uint32_t shape = visualization_msgs::Marker::ARROW;
186  visualization_msgs::Marker marker;
187 
188  marker.header.frame_id = "/base_link";
189  marker.header.stamp = ros::Time::now();
190 
191  marker.ns = "bin";
192  marker.id = 1;
193  marker.type = shape;
194  marker.action = visualization_msgs::Marker::ADD;
195 
196  geometry_msgs::Point approach_point;
197  approach_point.x = approach_pose.position.x;
198  approach_point.y = approach_pose.position.y;
199  approach_point.z = approach_pose.position.z;
200 
201  geometry_msgs::Point grasp_point;
202  grasp_point.x = grasp_pose.position.x;
203  grasp_point.y = grasp_pose.position.y;
204  grasp_point.z = grasp_pose.position.z;
205 
206  marker.points.push_back(approach_point);
207  marker.points.push_back(grasp_point);
208 
209  marker.scale.x = 0.01;
210  marker.scale.y = 0.02;
211  marker.scale.z = 0.05;
212 
213  marker.color.r = 0.9f;
214  marker.color.g = 0.9f;
215  marker.color.b = 0.0f;
216  marker.color.a = 1.0;
217 
218  marker.lifetime = ros::Duration();
219  marker_pub.publish(marker);
220 }
221 
222 void Emulator::broadcast_pose_tf(geometry_msgs::Pose grasp_pose)
223 {
224  static tf::TransformBroadcaster br;
225  tf::Transform transform;
226 
227  transform.setOrigin(tf::Vector3(grasp_pose.position.x, grasp_pose.position.y,
228  grasp_pose.position.z));
229  transform.setRotation(
230  tf::Quaternion(grasp_pose.orientation.x, grasp_pose.orientation.y,
231  grasp_pose.orientation.z, grasp_pose.orientation.w));
233  "base_link", "current_goal"));
234 }
235 
236 #endif // BIN_POSE_EMULATOR_CPP
TFSIMD_FORCE_INLINE void setRotation(const Quaternion &q)
void broadcast_pose_tf(geometry_msgs::Pose grasp_pose)
ros::Publisher marker_pub
void publish(const boost::shared_ptr< M > &message) const
f
double roll_default
double deapproach_height
TFSIMD_FORCE_INLINE const tfScalar & getY() const
Emulator(ros::NodeHandle *nh, std::string filepath)
TFSIMD_FORCE_INLINE const tfScalar & getW() const
bool callback(bin_pose_msgs::bin_pose::Request &req, bin_pose_msgs::bin_pose::Response &res)
TFSIMD_FORCE_INLINE const tfScalar & getZ() const
double pitch_range
double bin_center_z
bool parseConfig(std::string filepath)
double randGen(double fMin, double fMax)
void setRPY(const tfScalar &roll, const tfScalar &pitch, const tfScalar &yaw)
#define ROS_INFO(...)
void sendTransform(const StampedTransform &transform)
TFSIMD_FORCE_INLINE Vector3 quatRotate(const Quaternion &rotation, const Vector3 &v)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
double bin_center_y
ConfigData config
double yaw_default
void visualize_bin(void)
double approach_distance
TFSIMD_FORCE_INLINE const tfScalar & getX() const
static Time now()
double pitch_default
double bin_center_x
#define ROS_ERROR(...)
TFSIMD_FORCE_INLINE void setOrigin(const Vector3 &origin)
void visualize_pose(geometry_msgs::Pose grasp_pose, geometry_msgs::Pose approach_pose)


bin_pose_emulator
Author(s): Frantisek Durovsky
autogenerated on Sat Apr 24 2021 02:14:46