laser_joint_processor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
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 Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this 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 
36 
38 #include <ros/console.h>
39 
40 using namespace laser_joint_processor;
41 using namespace std;
42 
44 {
45 
46 }
47 
48 void LaserJointProcessor::setCacheSize(unsigned int max_size)
49 {
50  joint_state_cache_.setMaxSize(max_size);
51 }
52 
53 void LaserJointProcessor::setJointNames(const std::vector<std::string>& joint_names)
54 {
55  joint_names_ = joint_names;
56  deflater_.setDeflationJointNames(joint_names);
57 }
58 
59 bool LaserJointProcessor::addJointState(const sensor_msgs::JointStateConstPtr& joint_state)
60 {
62  deflater_.deflate(joint_state, deflated);
63  //printf("Deflated: %f\n", deflated.channels_[0]);
64  joint_state_cache_.add(deflated);
65 
66  return true;
67 }
68 
69 bool LaserJointProcessor::isSnapshotEarly(const calibration_msgs::DenseLaserSnapshot& snapshot) const
70 {
71  ROS_DEBUG("Checking if snapshot is early:");
72  if (joint_state_cache_.size() == 0)
73  {
74  ROS_DEBUG(" cache is empty, so snapshot is definitely early");
75  return true;
76  }
77  if (snapshot.scan_start.size() == 0)
78  ROS_FATAL("Received malformed DenseLaserSnapshot. (scan_start.size() == 0)");
79  ros::Time snapshot_end = snapshot.scan_start.back() + ros::Duration(snapshot.time_increment*(snapshot.num_scans-1));
80 
81  ros::Time cache_front = joint_state_cache_.front().header.stamp;
82  ros::Time cache_back = joint_state_cache_.back().header.stamp;
83  ROS_DEBUG(" Newest elem in cache (back): %u.%u", cache_back.sec, cache_back.nsec);
84  ROS_DEBUG(" Oldest elem in cache (front): %u.%u", cache_front.sec, cache_front.nsec);
85  ROS_DEBUG(" End of snapshot: %u.%u", snapshot_end.sec, snapshot_end.nsec);
86  ros::Duration diff = snapshot_end - cache_back;
87  ROS_DEBUG(" cache_back - snapshot_end: %.2f", diff.toSec());
88 
89  return diff >= ros::Duration(0,0);
90 }
91 
92 bool LaserJointProcessor::isSnapshotLate(const calibration_msgs::DenseLaserSnapshot& snapshot) const
93 {
94  ROS_DEBUG("Checking if snapshot is late:");
95  if (joint_state_cache_.size() == 0)
96  {
97  ROS_DEBUG(" cache is empty, so snapshot isn't late yet");
98  return false;
99  }
100 
101  if (snapshot.scan_start.size() == 0)
102  ROS_FATAL("Received malformed DenseLaserSnapshot. (scan_start.size() == 0)");
103  ros::Time snapshot_start = snapshot.scan_start.front();
104  ros::Time cache_front = joint_state_cache_.front().header.stamp;
105  ros::Time cache_back = joint_state_cache_.back().header.stamp;
106 
107  ros::Duration diff = snapshot_start - cache_front;
108 
109  ROS_DEBUG(" Newest elem in cache (back): %u.%u", cache_back.sec, cache_back.nsec);
110  ROS_DEBUG(" Oldest elem in cache (front): %u.%u", cache_front.sec, cache_front.nsec);
111  ROS_DEBUG(" Start of snapshot: %u.%u", snapshot_start.sec, snapshot_start.nsec);
112  ROS_DEBUG(" cache_front - snapshot_start: %.2f", diff.toSec());
113 
114  return diff <= ros::Duration(0,0);
115 }
116 
117 bool LaserJointProcessor::processLaserData( const calibration_msgs::DenseLaserSnapshot& snapshot,
118  const calibration_msgs::CalibrationPattern& features,
119  calibration_msgs::JointStateCalibrationPattern& result,
120  const ros::Duration& max_interp)
121 {
122  // Store # of joints
123  const unsigned int num_joints = joint_names_.size();
124  // store # of feature points
125  const unsigned int P = features.image_points.size();
126 
127  result.object_points = features.object_points;
128  result.joint_points.clear();
129  result.joint_points.resize(P);
130 
131  bool success;
132  success = imager_.update(snapshot, joint_state_cache_, max_interp);
133 
134  //imager_.displayImage(0);
135 /* imager_.writeImage(0, "/wg/stor2/vpradeep/ros/pkgs/wg-ros-pkg/calibration_experimental/pr2_calibration_launch/data/image.txt");
136 
137 
138  FILE* file;
139  file = fopen("/wg/stor2/vpradeep/ros/pkgs/wg-ros-pkg/calibration_experimental/pr2_calibration_launch/data/scan_start.txt", "w");
140  for (unsigned int i=0; i<snapshot.scan_start.size(); i++)
141  {
142  fprintf(file, "%u.%u\n", snapshot.scan_start[i].sec, snapshot.scan_start[i].nsec);
143  }
144  fclose(file);
145 
146  file = fopen("/wg/stor2/vpradeep/ros/pkgs/wg-ros-pkg/calibration_experimental/pr2_calibration_launch/data/joint_cache.txt", "w");
147  for (unsigned int i=0; i<joint_state_cache_.size(); i++)
148  {
149  fprintf(file, "%u.%u ", joint_state_cache_[i].header.stamp.sec, joint_state_cache_[i].header.stamp.nsec);
150  for (unsigned int j=0; j<joint_state_cache_[i].channels_.size(); j++)
151  {
152  fprintf(file, "%4.3f ", joint_state_cache_[i].channels_[j]);
153  }
154  fprintf(file, "\n");
155  }
156  fclose(file);
157 
158  file = fopen("/wg/stor2/vpradeep/ros/pkgs/wg-ros-pkg/calibration_experimental/pr2_calibration_launch/data/features.txt", "w");
159  for (unsigned int i=0; i<features.image_points.size(); i++)
160  {
161  fprintf(file, "%4.3f %4.3f\n", features.image_points[i].x, features.image_points[i].y);
162  }
163  fclose(file);
164 */
165  //printf("\n");
166  //imager_.displayImage(1);
167 
168  if (!success)
169  {
170  ROS_ERROR("imager_.update failed");
171  return false;
172  }
173 
174  for (unsigned int j=0; j<num_joints; j++)
175  {
176 
177  vector<float> positions;
178  vector<float> velocities;
179  interp_.interp(features.image_points, imager_.getJointImage(j),
180  positions, velocities);
181 
182  for (unsigned int i=0; i<P; i++)
183  {
184  result.joint_points[i].name.push_back(joint_names_[j]);
185  result.joint_points[i].position.push_back(positions[i]);
186  result.joint_points[i].velocity.push_back(velocities[i]);
187  result.joint_points[i].effort.push_back(1234);
188  }
189  }
190 
191  vector<float> laser_angles;
192  vector<float> laser_ranges;
193 
194  success = interpSnapshot(features.image_points, snapshot, laser_angles, laser_ranges);
195  if (!success)
196  {
197  ROS_ERROR("interpSnapshot failed");
198  return false;
199  }
200 
201  for (unsigned int i=0; i<P; i++)
202  {
203  result.joint_points[i].name.push_back("laser_angle_joint");
204  result.joint_points[i].position.push_back(laser_angles[i]);
205  result.joint_points[i].velocity.push_back(0);
206  result.joint_points[i].effort.push_back(1234);
207 
208  result.joint_points[i].name.push_back("laser_range_joint");
209  result.joint_points[i].position.push_back(laser_ranges[i]);
210  result.joint_points[i].velocity.push_back(0);
211  result.joint_points[i].effort.push_back(1234);
212  }
213 
214  result.header.stamp = snapshot.header.stamp;
215 
216  return true;
217 }
218 
219 
220 
221 
bool addJointState(const sensor_msgs::JointStateConstPtr &joint_state)
#define ROS_FATAL(...)
bool interpSnapshot(const std::vector< geometry_msgs::Point > &points, const calibration_msgs::DenseLaserSnapshot &snapshot, std::vector< float > &pointing_angles, std::vector< float > &ranges)
bool processLaserData(const calibration_msgs::DenseLaserSnapshot &snapshot, const calibration_msgs::CalibrationPattern &features, calibration_msgs::JointStateCalibrationPattern &result, const ros::Duration &max_interp=ros::Duration(.25))
bool isSnapshotLate(const calibration_msgs::DenseLaserSnapshot &snapshot) const
bool isSnapshotEarly(const calibration_msgs::DenseLaserSnapshot &snapshot) const
void setJointNames(const std::vector< std::string > &joint_names)
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


laser_joint_processor
Author(s): Vijay Pradeep
autogenerated on Tue Jun 1 2021 02:50:55