Repository navigation
Wifi Signal Strength Mapping (User Data Usage) (ROS1)
Description: This tutorial shows how to add user data during mapping that will be saved directly in RTAB-Map's database for convenience.
Version >0.11.14 required.
This tutorial will show how to create a map including information taken with any other sensor (e.g., the user data). For convenience, we will map Wifi signal strength of the environment. The screenshot below shows how the Wifi signal strength is visualized. It is just a level bar used (sensor_msgs/Pointcloud2), but more fancy output like Markers can be used to customize the look.

To add user data to map, rtabmap node has topics called user_data and user_data_async. The first one is used when parameter subscribe_user_data:=true, and it will be synchronized with other sensor data. It means that the publishing rate of this topic should be high (at least as fast as the images received). The second topic user_data_async is asynchronous. It should be used only when user data is published slower than the detection rate of rtabmap (default 1 Hz). The received user data will be saved in the next node created in rtabmap (if user data is published faster than nodes are created, old user data will be overwritten).
For this example, we use user_data_async with a Wifi signal data published each 2 seconds. Here is the basic information needed to publish a rtabmap_msgs/UserData topic (the full code is here):
#!cplusplus
#include <rtabmap_conversions/MsgConversion.h>
#include <rtabmap_msgs/UserData.h>
int main(int argc, char** argv)
{
//... node initialization stuff
ros::Rate rate(0.5);
ros::Publisher wifiPub = nh.advertise<rtabmap_msgs::UserData>("wifi_signal", 1);
while(ros::ok())
{
ros::Time stamp = ros::Time::now();
// Get signal strength...
int dBm = ...
// Create user data [level, stamp].
// OpenCV matrix is used for convenience. Any format is accepted.
// However, if CV_8UC1 format is used, make sure rows > 1 as
// rtabmap will think it is already compressed.
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 the saved user data as we need
// to get precise position in the graph afterward.
data.at<double>(1) = stamp.toSec();
rtabmap_msgs::UserData dataMsg;
dataMsg.header.frame_id = "wifi_link";
dataMsg.header.stamp = stamp;
// Convert OpenCV matrix to UserData msg. Optionally,
// we can choose to compress the data now (here we keep uncompressed).
rtabmap_conversions::userDataToROS(data, dataMsg, false);
// Publish data!
wifiPub.publish<rtabmap_msgs::UserData>(dataMsg);
ros::spinOnce();
rate.sleep();
}
return 0;
}
#!python
#!/usr/bin/env python
import rospy
import struct
import os
from rtabmap_msgs.msg import UserData
def loop():
rospy.init_node('wifi_signal_pub', anonymous=True)
pub = rospy.Publisher('wifi_signal', UserData, queue_size=10)
rate = rospy.Rate(0.5) # 0.5hz
while not rospy.is_shutdown():
myCmd = os.popen('nmcli dev wifi | grep "^*"').read()
cmdList = myCmd.split()
if len(cmdList) > 6:
quality = float(cmdList[6])
msg = UserData()
# To make it compatible with c++ sub example, use dBm
dBm = quality/2-100
rospy.loginfo("Network \"%s\": Quality=%d, %f dBm", cmdList[1], quality, dBm)
# Create user data [level, stamp].
# Any format is accepted.
# However, if CV_8UC1 format is used, make sure rows > 1 as
# rtabmap will think it is already compressed.
msg.rows = 1
msg.cols = 2
msg.type = 6 # Use OpenCV type (here 6=CV_64FC1): http://ninghang.blogspot.com/2012/11/list-of-mat-type-in-opencv.html
# We should set stamp in data to be able to
# retrieve it from the saved user data as we need
# to get precise position in the graph afterward.
msg.data = struct.pack(b'dd', dBm, rospy.get_time())
pub.publish(msg)
else:
rospy.logerr("Cannot get info from wireless!")
rate.sleep()
if __name__ == '__main__':
try:
loop()
except rospy.ROSInterruptException:
pass
rtabmap publishes rtabmap_msgs/MapData topic after each update. In this topic, we can get the user data that we published to rtabmap earlier, which is now linked to graph. Here is an example (the full code is here):
#!cplusplus
#include <rtabmap_msgs/MapData.h>
#include <rtabmap_conversions/MsgConversion.h>
std::map<double, int> wifiLevels;
void mapDataCallback(const rtabmap_msgs::MapDataConstPtr & mapDataMsg)
{
// Convert MapData message in more user friendly objects
rtabmap::Transform mapToOdom;
std::map<int, rtabmap::Transform> poses;
std::multimap<int, rtabmap::Link> links;
std::map<int, rtabmap::Signature> signatures;
rtabmap_msgs::mapDataFromROS(*mapDataMsg, poses, links, signatures, mapToOdom);
// The user data of the latest node added to
// map is contained in signatures map. Iterate over
// to get the data. 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;
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.");
}
}
}
// "wifiLevels" contains all signal values sorted
// by timestamp. We can then find their exact position
// on the graph. See referred source code for details...
}
int main(int argc, char** argv)
{
//... node initialization stuff
ros::Subscriber mapDataSub = nh.subscribe("/rtabmap/mapData", 1, mapDataCallback);
ros::spin();
}
From the hand-held tutorial, start your camera and launch rtabmap.launch with argument "user_data_async_topic:=/wifi_signal", then start wifi publisher and subscriber:
#!bash
$ roslaunch rtabmap_launch rtabmap.launch [...] user_data_async_topic:=/wifi_signal
// C++:
$ rosrun rtabmap_demos wifi_signal_pub _interface:="wlan0"
// Python:
$ python wifi_signal_pub.py
$ rosrun rtabmap_demos wifi_signal_subIn RVIZ, add PointCloud2 topic called wifi_signals published from wifi_signal_sub node. Note that you may need to start wifi_signal_pub in sudo. To do so:
$ sudo su
$ source /opt/ros/kinetic/setup.bash
$ source /home/{user_name}/catkin_ws/devel/setup.bash (if rtabmap is built from source)
$ rosrun rtabmap_demos wifi_signal_pub interface:="wlan0"If you move the camera, you should see the signal strength bars like in the screenshots above.
You can show the map of the screenshots above using this database demo_wifi.db (taken with a stereo camera) with the lines below:
$ roslaunch rtabmap_launch rtabmap.launch database_path:=~/Downloads/demo_wifi.db rtabmap_viz:=false rviz:=true
$ rosrun rtabmap_demos wifi_signal_sub
// In RVIZ, add `PointCloud2` topic called `wifi_signals`
$ rosservice call /rtabmap/publish_map 1 1 0