mirror of
https://github.com/introlab/rtabmap_ros.git
synced 2026-10-07 18:27:46 +08:00
First version rtabmap_launch working
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
/*
|
||||
Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Universite de Sherbrooke nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ros/ros.h>
|
||||
#include <tf/transform_listener.h>
|
||||
#include <find_object_2d/ObjectsStamped.h>
|
||||
#include <rtabmap/utilite/UConversion.h>
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <rtabmap_msgs/UserData.h>
|
||||
#include <rtabmap_conversions/MsgConversion.h>
|
||||
#include <rtabmap/core/Compression.h>
|
||||
#include <visualization_msgs/MarkerArray.h>
|
||||
|
||||
class SaveObjectsExample
|
||||
{
|
||||
public:
|
||||
SaveObjectsExample() :
|
||||
objFramePrefix_("object"),
|
||||
frameId_("base_link")
|
||||
{
|
||||
ros::NodeHandle pnh("~");
|
||||
pnh.param("object_prefix", objFramePrefix_, objFramePrefix_);
|
||||
pnh.param("frame_id", frameId_, frameId_);
|
||||
|
||||
ros::NodeHandle nh;
|
||||
subObjects_ = nh.subscribe("objectsStamped", 1, &SaveObjectsExample::objectsDetectedCallback, this);
|
||||
subsMapData_ = nh.subscribe("mapData", 1, &SaveObjectsExample::mapDataCallback, this);
|
||||
pub_ = nh.advertise<rtabmap_msgs::UserData>("objectsData", 1);
|
||||
pubMarkers_ = nh.advertise<visualization_msgs::MarkerArray>("objectsMarkers", 1);
|
||||
}
|
||||
|
||||
// from find_object_2d to rtabmap
|
||||
void objectsDetectedCallback(const find_object_2d::ObjectsStampedConstPtr & msg)
|
||||
{
|
||||
if(msg->objects.data.size())
|
||||
{
|
||||
cv::Mat data(msg->objects.data.size()/12, 9, CV_64FC1);
|
||||
for(unsigned int i=0; i<msg->objects.data.size(); i+=12)
|
||||
{
|
||||
// get data
|
||||
int id = (int)msg->objects.data[i];
|
||||
std::string objectFrameId = uFormat("%s_%d", objFramePrefix_.c_str(),id); // "object_1", "object_2"
|
||||
|
||||
// get pose of the object in base frame
|
||||
tf::StampedTransform pose;
|
||||
try
|
||||
{
|
||||
tfListener_.lookupTransform(frameId_, objectFrameId, msg->header.stamp, pose);
|
||||
}
|
||||
catch(tf::TransformException & ex)
|
||||
{
|
||||
ROS_WARN("%s",ex.what());
|
||||
return;
|
||||
}
|
||||
|
||||
data.at<double>(i, 0) = double(id);
|
||||
data.at<double>(i, 1) = ros::Time(msg->header.stamp).toSec();
|
||||
data.at<double>(i, 2) = pose.getOrigin().x();
|
||||
data.at<double>(i, 3) = pose.getOrigin().y();
|
||||
data.at<double>(i, 4) = pose.getOrigin().z();
|
||||
data.at<double>(i, 5) = pose.getRotation().x();
|
||||
data.at<double>(i, 6) = pose.getRotation().y();
|
||||
data.at<double>(i, 7) = pose.getRotation().z();
|
||||
data.at<double>(i, 8) = pose.getRotation().w();
|
||||
}
|
||||
|
||||
rtabmap_msgs::UserData dataMsg;
|
||||
dataMsg.header.frame_id = msg->header.frame_id;
|
||||
dataMsg.header.stamp = msg->header.stamp;
|
||||
rtabmap_conversions::userDataToROS(data, dataMsg, false);
|
||||
pub_.publish(dataMsg);
|
||||
}
|
||||
}
|
||||
|
||||
// from rtabmap to rviz visualization
|
||||
void mapDataCallback(const rtabmap_msgs::MapDataConstPtr & msg)
|
||||
{
|
||||
std::map<int, rtabmap::Signature> signatures;
|
||||
std::map<int, rtabmap::Transform> poses;
|
||||
std::multimap<int, rtabmap::Link> links;
|
||||
rtabmap::Transform mapToOdom;
|
||||
rtabmap_conversions::mapDataFromROS(*msg, poses, links, signatures, mapToOdom);
|
||||
|
||||
// handle the case where we can receive only latest data, or if all data are published
|
||||
for(std::map<int, rtabmap::Signature>::iterator iter=signatures.begin(); iter!=signatures.end(); ++iter)
|
||||
{
|
||||
int id = iter->first;
|
||||
rtabmap::Signature & node = iter->second;
|
||||
|
||||
nodeStamps_.insert(std::make_pair(node.getStamp(), node.id()));
|
||||
|
||||
if(!node.sensorData().userDataCompressed().empty() && nodeToObjects_.find(id)==nodeToObjects_.end())
|
||||
{
|
||||
cv::Mat data = rtabmap::uncompressData(node.sensorData().userDataCompressed());
|
||||
ROS_ASSERT(data.cols == 9 && data.type() == CV_64FC1);
|
||||
ROS_INFO("Node %d has %d object(s)", id, data.rows);
|
||||
nodeToObjects_.insert(std::make_pair(id, data));
|
||||
}
|
||||
}
|
||||
|
||||
// for the logic below, we should keep only stamps for
|
||||
// nodes still in the graph (in case nodes are ignored when not moving)
|
||||
std::map<double, int> nodeStamps;
|
||||
for(std::map<double, int>::iterator iter=nodeStamps_.begin(); iter!=nodeStamps_.end(); ++iter)
|
||||
{
|
||||
std::map<int, rtabmap::Transform>::const_iterator jter = poses.find(iter->second);
|
||||
if(jter != poses.end())
|
||||
{
|
||||
nodeStamps.insert(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
// Publish markers accordingly to current optimized graph
|
||||
std::map<int, float> objectsAdded;
|
||||
visualization_msgs::MarkerArray markers;
|
||||
|
||||
if(nodeToObjects_.size())
|
||||
{
|
||||
for(std::map<int, rtabmap::Transform>::iterator iter=poses.lower_bound(1); iter!=poses.end(); ++iter)
|
||||
{
|
||||
if(nodeToObjects_.find(iter->first) != nodeToObjects_.end())
|
||||
{
|
||||
cv::Mat data = nodeToObjects_.at(iter->first);
|
||||
for(int i=0; i<data.rows; ++i)
|
||||
{
|
||||
int objId = int(data.at<double>(i,0));
|
||||
double stamp = data.at<double>(i,1);
|
||||
|
||||
// The object detection may have been taken between two nodes, interpolate its position.
|
||||
std::map<double, int>::iterator previousNode = nodeStamps.lower_bound(stamp); // lower bound of the stamp
|
||||
if(previousNode!=nodeStamps.end() && previousNode->first > stamp && previousNode != nodeStamps.begin())
|
||||
{
|
||||
--previousNode;
|
||||
}
|
||||
std::map<double, int>::iterator nextNode = nodeStamps.upper_bound(stamp); // upper bound of the stamp
|
||||
|
||||
if(previousNode != nodeStamps.end() &&
|
||||
nextNode != nodeStamps.end() &&
|
||||
previousNode->second != nextNode->second &&
|
||||
poses.find(previousNode->second)!=poses.end() && poses.find(nextNode->second)!=poses.end())
|
||||
{
|
||||
rtabmap::Transform poseA = poses.at(previousNode->second);
|
||||
rtabmap::Transform poseB = poses.at(nextNode->second);
|
||||
double stampA = previousNode->first;
|
||||
double stampB = nextNode->first;
|
||||
UASSERT(stamp>=stampA && stamp <=stampB);
|
||||
|
||||
double ratio = (stamp-stampA)/(stampB-stampA);
|
||||
rtabmap::Transform robotPose = poseA.interpolate(ratio, poseB);
|
||||
|
||||
// transform object pose in map frame
|
||||
rtabmap::Transform objPose(
|
||||
data.at<double>(i,2), data.at<double>(i,3), data.at<double>(i,4),
|
||||
data.at<double>(i,5), data.at<double>(i,6), data.at<double>(i,7), data.at<double>(i,8));
|
||||
float distanceFromNode = objPose.getNorm();
|
||||
objPose = robotPose * objPose;
|
||||
|
||||
if(objectsAdded.find(objId) == objectsAdded.end())
|
||||
{
|
||||
visualization_msgs::Marker marker;
|
||||
marker.header.frame_id = msg->header.frame_id;
|
||||
marker.header.stamp = msg->header.stamp;
|
||||
marker.ns = "objects";
|
||||
marker.id = objId;
|
||||
marker.action = visualization_msgs::Marker::ADD;
|
||||
marker.pose.position.x = objPose.x();
|
||||
marker.pose.position.y = objPose.y();
|
||||
marker.pose.position.z = objPose.z();
|
||||
Eigen::Quaterniond q = objPose.getQuaterniond();
|
||||
marker.pose.orientation.x = q.x();
|
||||
marker.pose.orientation.y = q.y();
|
||||
marker.pose.orientation.z = q.z();
|
||||
marker.pose.orientation.w = q.w();
|
||||
marker.scale.x = 0.3;
|
||||
marker.scale.y = 0.3;
|
||||
marker.scale.z = 0.3;
|
||||
marker.color.a = 0.8;
|
||||
marker.color.r = 0.0;
|
||||
marker.color.g = 1.0;
|
||||
marker.color.b = 0.0;
|
||||
|
||||
// cube
|
||||
marker.type = visualization_msgs::Marker::CUBE;
|
||||
markers.markers.push_back(marker);
|
||||
|
||||
// text
|
||||
marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
|
||||
marker.text = uFormat("%s_%d", objFramePrefix_.c_str(), objId);
|
||||
marker.id = -objId;
|
||||
marker.color.a = 1.0;
|
||||
marker.pose.position.z += 0.3; // text over the cube
|
||||
markers.markers.push_back(marker);
|
||||
|
||||
objectsAdded.insert(std::make_pair(objId, distanceFromNode));
|
||||
}
|
||||
else
|
||||
{
|
||||
// update pose of the marker only if current observation is closer to robot
|
||||
if(distanceFromNode < objectsAdded.at(objId))
|
||||
{
|
||||
for(unsigned int j=0; j<markers.markers.size(); ++j)
|
||||
{
|
||||
if(markers.markers[j].id == objId || markers.markers[j].id == -objId)
|
||||
{
|
||||
markers.markers[j].pose.position.x = objPose.x();
|
||||
markers.markers[j].pose.position.y = objPose.y();
|
||||
markers.markers[j].pose.position.z = objPose.z() + (markers.markers[j].id<0?0.3:0);
|
||||
Eigen::Quaterniond q = objPose.getQuaterniond();
|
||||
markers.markers[j].pose.orientation.x = q.x();
|
||||
markers.markers[j].pose.orientation.y = q.y();
|
||||
markers.markers[j].pose.orientation.z = q.z();
|
||||
markers.markers[j].pose.orientation.w = q.w();
|
||||
}
|
||||
}
|
||||
objectsAdded.at(objId) = distanceFromNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!markers.markers.empty())
|
||||
{
|
||||
pubMarkers_.publish(markers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string objFramePrefix_;
|
||||
ros::Subscriber subObjects_;
|
||||
ros::Subscriber subsMapData_;
|
||||
tf::TransformListener tfListener_;
|
||||
ros::Publisher pub_;
|
||||
ros::Publisher pubMarkers_;
|
||||
std::map<int, cv::Mat> nodeToObjects_;
|
||||
std::map<double, int> nodeStamps_; // <stamp, id>
|
||||
std::string frameId_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
ros::init(argc, argv, "save_objects_example");
|
||||
|
||||
SaveObjectsExample sync;
|
||||
ros::spin();
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Universite de Sherbrooke nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ros/ros.h>
|
||||
#include <ros/publisher.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <linux/wireless.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <rtabmap_conversions/MsgConversion.h>
|
||||
#include <rtabmap_msgs/UserData.h>
|
||||
|
||||
// Demo:
|
||||
// $ roslaunch freenect_launch freenect.launch depth_registration:=true
|
||||
// $ roslaunch rtabmap_launch rtabmap.launch rtabmap_args:="--delete_db_on_start" user_data_async_topic:=/wifi_signal rtabmapviz:=false rviz:=true
|
||||
// $ rosrun rtabmap_demos wifi_signal_pub interface:="wlan0"
|
||||
// $ rosrun rtabmap_demos wifi_signal_sub
|
||||
// In RVIZ add PointCloud2 "wifi_signals"
|
||||
|
||||
// A percentage value that represents the signal quality
|
||||
// of the network. WLAN_SIGNAL_QUALITY is of type ULONG.
|
||||
// This member contains a value between 0 and 100. A value
|
||||
// of 0 implies an actual RSSI signal strength of -100 dbm.
|
||||
// A value of 100 implies an actual RSSI signal strength of -50 dbm.
|
||||
// You can calculate the RSSI signal strength value for wlanSignalQuality
|
||||
// values between 1 and 99 using linear interpolation.
|
||||
inline int quality2dBm(int quality)
|
||||
{
|
||||
// Quality to dBm:
|
||||
if(quality <= 0)
|
||||
return -100;
|
||||
else if(quality >= 100)
|
||||
return -50;
|
||||
else
|
||||
return (quality / 2) - 100;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
ros::init(argc, argv, "wifi_signal_pub");
|
||||
|
||||
ros::NodeHandle nh;
|
||||
ros::NodeHandle pnh("~");
|
||||
|
||||
std::string interface = "wlan0";
|
||||
double rateHz = 0.5; // Hz
|
||||
std::string frameId = "base_link";
|
||||
|
||||
pnh.param("interface", interface, interface);
|
||||
pnh.param("rate", rateHz, rateHz);
|
||||
pnh.param("frame_id", frameId, frameId);
|
||||
|
||||
ros::Rate rate(rateHz);
|
||||
|
||||
ros::Publisher wifiPub = nh.advertise<rtabmap_msgs::UserData>("wifi_signal", 1);
|
||||
|
||||
while(ros::ok())
|
||||
{
|
||||
int dBm = 0;
|
||||
|
||||
// Code inspired from http://blog.ajhodges.com/2011/10/using-ioctl-to-gather-wifi-information.html
|
||||
|
||||
//have to use a socket for ioctl
|
||||
int sockfd;
|
||||
/* Any old socket will do, and a datagram socket is pretty cheap */
|
||||
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
|
||||
ROS_ERROR("Could not create simple datagram socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct iwreq req;
|
||||
struct iw_statistics stats;
|
||||
|
||||
strncpy(req.ifr_name, interface.c_str(), IFNAMSIZ);
|
||||
|
||||
//make room for the iw_statistics object
|
||||
req.u.data.pointer = (caddr_t) &stats;
|
||||
req.u.data.length = sizeof(stats);
|
||||
// clear updated flag
|
||||
req.u.data.flags = 1;
|
||||
|
||||
//this will gather the signal strength
|
||||
if(ioctl(sockfd, SIOCGIWSTATS, &req) == -1)
|
||||
{
|
||||
//die with error, invalid interface
|
||||
ROS_ERROR("Invalid interface (\"%s\"). Tip: Try with sudo!", interface.c_str());
|
||||
}
|
||||
else if(((iw_statistics *)req.u.data.pointer)->qual.updated & IW_QUAL_DBM)
|
||||
{
|
||||
//signal is measured in dBm and is valid for us to use
|
||||
dBm = ((iw_statistics *)req.u.data.pointer)->qual.level - 256;
|
||||
}
|
||||
else
|
||||
{
|
||||
ROS_ERROR("Could not get signal level.");
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
|
||||
if(dBm != 0)
|
||||
{
|
||||
ros::Time stamp = ros::Time::now();
|
||||
|
||||
// Create user data [level] with the value
|
||||
cv::Mat data(1, 2, CV_64FC1);
|
||||
data.at<double>(0) = double(dBm);
|
||||
|
||||
// we should set stamp in data to be able to
|
||||
// retrieve it from rtabmap map data to get precise
|
||||
// position in the graph afterward
|
||||
data.at<double>(1) = stamp.toSec();
|
||||
|
||||
rtabmap_msgs::UserData dataMsg;
|
||||
dataMsg.header.frame_id = frameId;
|
||||
dataMsg.header.stamp = stamp;
|
||||
rtabmap_conversions::userDataToROS(data, dataMsg, false);
|
||||
wifiPub.publish<rtabmap_msgs::UserData>(dataMsg);
|
||||
}
|
||||
ros::spinOnce();
|
||||
rate.sleep();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
/*
|
||||
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Universite de Sherbrooke nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ros/ros.h>
|
||||
#include <ros/publisher.h>
|
||||
#include <ros/subscriber.h>
|
||||
|
||||
#include <rtabmap/utilite/UStl.h>
|
||||
#include <rtabmap/utilite/ULogger.h>
|
||||
#include <rtabmap/core/util3d_transforms.h>
|
||||
|
||||
#include <rtabmap_msgs/MapData.h>
|
||||
#include <rtabmap_conversions/MsgConversion.h>
|
||||
|
||||
#include <sensor_msgs/PointCloud2.h>
|
||||
|
||||
#include <pcl/point_cloud.h>
|
||||
#include <pcl/point_types.h>
|
||||
#include <pcl_conversions/pcl_conversions.h>
|
||||
|
||||
bool hueSymbol = false;
|
||||
int min_dbm = -100;
|
||||
int max_dbm = -50;
|
||||
bool autoScale = false;
|
||||
|
||||
// A percentage value that represents the signal quality
|
||||
// of the network. WLAN_SIGNAL_QUALITY is of type ULONG.
|
||||
// This member contains a value between 0 and 100. A value
|
||||
// of 0 implies an actual RSSI signal strength of -100 dbm.
|
||||
// A value of 100 implies an actual RSSI signal strength of -50 dbm.
|
||||
// You can calculate the RSSI signal strength value for wlanSignalQuality
|
||||
// values between 1 and 99 using linear interpolation.
|
||||
inline int dBm2Quality(int dBm)
|
||||
{
|
||||
// dBm to Quality:
|
||||
if(dBm <= min_dbm)
|
||||
return 0;
|
||||
else if(dBm >= max_dbm)
|
||||
return 100;
|
||||
else
|
||||
{
|
||||
return -(dBm-min_dbm)*100/(min_dbm-max_dbm);
|
||||
}
|
||||
}
|
||||
|
||||
void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v )
|
||||
{
|
||||
int i;
|
||||
float f, p, q, t;
|
||||
if( s == 0 ) {
|
||||
// achromatic (grey)
|
||||
*r = *g = *b = v;
|
||||
return;
|
||||
}
|
||||
h /= 60; // sector 0 to 5
|
||||
i = floor( h );
|
||||
f = h - i; // factorial part of h
|
||||
p = v * ( 1 - s );
|
||||
q = v * ( 1 - s * f );
|
||||
t = v * ( 1 - s * ( 1 - f ) );
|
||||
switch( i ) {
|
||||
case 0:
|
||||
*r = v;
|
||||
*g = t;
|
||||
*b = p;
|
||||
break;
|
||||
case 1:
|
||||
*r = q;
|
||||
*g = v;
|
||||
*b = p;
|
||||
break;
|
||||
case 2:
|
||||
*r = p;
|
||||
*g = v;
|
||||
*b = t;
|
||||
break;
|
||||
case 3:
|
||||
*r = p;
|
||||
*g = q;
|
||||
*b = v;
|
||||
break;
|
||||
case 4:
|
||||
*r = t;
|
||||
*g = p;
|
||||
*b = v;
|
||||
break;
|
||||
default: // case 5:
|
||||
*r = v;
|
||||
*g = p;
|
||||
*b = q;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ros::Publisher wifiSignalCloudPub;
|
||||
std::map<double, int> wifiLevels;
|
||||
std::map<double, int> nodeStamps_;
|
||||
|
||||
void mapDataCallback(const rtabmap_msgs::MapDataConstPtr & mapDataMsg)
|
||||
{
|
||||
ROS_INFO("Received map data!");
|
||||
|
||||
rtabmap::Transform mapToOdom;
|
||||
std::map<int, rtabmap::Transform> poses;
|
||||
std::multimap<int, rtabmap::Link> links;
|
||||
std::map<int, rtabmap::Signature> signatures;
|
||||
rtabmap_conversions::mapDataFromROS(*mapDataMsg, poses, links, signatures, mapToOdom);
|
||||
|
||||
// handle the case where we can receive only latest data, or if all data are published
|
||||
for(std::map<int, rtabmap::Signature>::iterator iter=signatures.begin(); iter!=signatures.end(); ++iter)
|
||||
{
|
||||
int id = iter->first;
|
||||
rtabmap::Signature & node = iter->second;
|
||||
|
||||
nodeStamps_.insert(std::make_pair(node.getStamp(), node.id()));
|
||||
|
||||
if(node.sensorData().envSensors().find(rtabmap::EnvSensor::kWifiSignalStrength) != node.sensorData().envSensors().end())
|
||||
{
|
||||
rtabmap::EnvSensor sensor = node.sensorData().envSensors().at(rtabmap::EnvSensor::kWifiSignalStrength);
|
||||
wifiLevels.insert(std::make_pair(sensor.stamp()>0.0?sensor.stamp():iter->second.getStamp(), sensor.value()));
|
||||
}
|
||||
else if(!node.sensorData().userDataCompressed().empty())
|
||||
{
|
||||
cv::Mat data;
|
||||
node.sensorData().uncompressDataConst(0 ,0, 0, &data);
|
||||
|
||||
if(data.type() == CV_64FC1 && data.rows == 1 && data.cols == 2)
|
||||
{
|
||||
// format [int level, double stamp], see wifi_signal_pub_node.cpp
|
||||
int level = data.at<double>(0);
|
||||
double stamp = data.at<double>(1);
|
||||
wifiLevels.insert(std::make_pair(stamp, level));
|
||||
}
|
||||
else if(!data.empty())
|
||||
{
|
||||
ROS_ERROR("Wrong user data format for wifi signal.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// for the logic below, we should keep only stamps for
|
||||
// nodes still in the graph (in case nodes are ignored when not moving)
|
||||
std::map<double, int> nodeStamps;
|
||||
for(std::map<double, int>::iterator iter=nodeStamps_.begin(); iter!=nodeStamps_.end(); ++iter)
|
||||
{
|
||||
std::map<int, rtabmap::Transform>::const_iterator jter = poses.find(iter->second);
|
||||
if(jter != poses.end())
|
||||
{
|
||||
nodeStamps.insert(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
if(wifiLevels.size() == 0)
|
||||
{
|
||||
ROS_WARN("No wifi signal detected yet in user data of map data");
|
||||
}
|
||||
|
||||
//============================
|
||||
// Add WIFI symbols
|
||||
//============================
|
||||
|
||||
pcl::PointCloud<pcl::PointXYZRGB>::Ptr assembledWifiSignals(new pcl::PointCloud<pcl::PointXYZRGB>);
|
||||
int id = 0;
|
||||
float min=0,max=0;
|
||||
for(std::map<double, int>::iterator iter=wifiLevels.begin(); iter!=wifiLevels.end(); ++iter, ++id)
|
||||
{
|
||||
if(min == 0.0f || min > iter->second)
|
||||
{
|
||||
min = iter->second;
|
||||
}
|
||||
if(max == 0.0f || max < iter->second)
|
||||
{
|
||||
max = iter->second;
|
||||
}
|
||||
}
|
||||
ROS_INFO("Min/Max dBm = %f %f", min, max);
|
||||
if(autoScale && min<0 && min < max)
|
||||
{
|
||||
min_dbm = min;
|
||||
max_dbm = max;
|
||||
}
|
||||
for(std::map<double, int>::iterator iter=wifiLevels.begin(); iter!=wifiLevels.end(); ++iter, ++id)
|
||||
{
|
||||
// The Wifi value may be taken between two nodes, interpolate its position.
|
||||
double stampWifi = iter->first;
|
||||
std::map<double, int>::iterator previousNode = nodeStamps.lower_bound(stampWifi); // lower bound of the stamp
|
||||
if(previousNode!=nodeStamps.end() && previousNode->first > stampWifi && previousNode != nodeStamps.begin())
|
||||
{
|
||||
--previousNode;
|
||||
}
|
||||
std::map<double, int>::iterator nextNode = nodeStamps.upper_bound(stampWifi); // upper bound of the stamp
|
||||
|
||||
if(previousNode != nodeStamps.end() &&
|
||||
nextNode != nodeStamps.end() &&
|
||||
previousNode->second != nextNode->second &&
|
||||
uContains(poses, previousNode->second) && uContains(poses, nextNode->second))
|
||||
{
|
||||
rtabmap::Transform poseA = poses.at(previousNode->second);
|
||||
rtabmap::Transform poseB = poses.at(nextNode->second);
|
||||
double stampA = previousNode->first;
|
||||
double stampB = nextNode->first;
|
||||
UASSERT(stampWifi>=stampA && stampWifi <=stampB);
|
||||
|
||||
rtabmap::Transform v = poseA.inverse() * poseB;
|
||||
double ratio = (stampWifi-stampA)/(stampB-stampA);
|
||||
|
||||
v.x()*=ratio;
|
||||
v.y()*=ratio;
|
||||
v.z()*=ratio;
|
||||
|
||||
rtabmap::Transform wifiPose = (poseA*v).translation(); // rip off the rotation
|
||||
|
||||
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
|
||||
if(hueSymbol)
|
||||
{
|
||||
// scale between red -> yellow -> green
|
||||
int quality = dBm2Quality(iter->second)*120/100;
|
||||
float r,g,b;
|
||||
HSVtoRGB(&r,&g,&b,quality,1,1);
|
||||
pcl::PointXYZRGB anchor;
|
||||
anchor.r = r*255;
|
||||
anchor.g = g*255;
|
||||
anchor.b = b*255;
|
||||
cloud->push_back(anchor);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Make a line with points
|
||||
int quality = dBm2Quality(iter->second)/10;
|
||||
for(int i=0; i<10; ++i)
|
||||
{
|
||||
// 2 cm between each points
|
||||
// the number of points depends on the dBm (which varies from -30 (near) to -80 (far))
|
||||
pcl::PointXYZRGB pt;
|
||||
pt.z = float(i+1)*0.02f;
|
||||
if(i<quality)
|
||||
{
|
||||
// green
|
||||
pt.g = 255;
|
||||
if(i<7)
|
||||
{
|
||||
// yellow
|
||||
pt.r = 255;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// gray
|
||||
pt.r = pt.g = pt.b = 100;
|
||||
}
|
||||
cloud->push_back(pt);
|
||||
}
|
||||
pcl::PointXYZRGB anchor;
|
||||
anchor.r = 255;
|
||||
cloud->push_back(anchor);
|
||||
}
|
||||
|
||||
cloud = rtabmap::util3d::transformPointCloud(cloud, wifiPose);
|
||||
|
||||
if(assembledWifiSignals->size() == 0)
|
||||
{
|
||||
*assembledWifiSignals = *cloud;
|
||||
}
|
||||
else
|
||||
{
|
||||
*assembledWifiSignals += *cloud;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(assembledWifiSignals->size())
|
||||
{
|
||||
sensor_msgs::PointCloud2 cloudMsg;
|
||||
pcl::toROSMsg(*assembledWifiSignals, cloudMsg);
|
||||
cloudMsg.header = mapDataMsg->header;
|
||||
wifiSignalCloudPub.publish(cloudMsg);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
ros::init(argc, argv, "wifi_signal_sub");
|
||||
|
||||
ros::NodeHandle nh;
|
||||
ros::NodeHandle pnh("~");
|
||||
|
||||
pnh.param("hue_symbol", hueSymbol, hueSymbol);
|
||||
pnh.param("min", min_dbm, min_dbm);
|
||||
pnh.param("max", max_dbm, max_dbm);
|
||||
pnh.param("auto", autoScale, autoScale);
|
||||
|
||||
wifiSignalCloudPub = nh.advertise<sensor_msgs::PointCloud2>("wifi_signals", 1);
|
||||
ros::Subscriber mapDataSub = nh.subscribe("/rtabmap/mapData", 1, mapDataCallback);
|
||||
|
||||
ros::spin();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user