Ask Your Question

How to create a 2d map with rplidar_ros

asked 2020-10-16 13:06:06 -0500

Robot_fox gravatar image

Hello I'm very much new to ros. I'm currently using noetic distribution to create a navigation system using a lidar A1M8 by slamtec. I downloaded the Rplidar_ros package as well as the slam_gmapping. I placed both packages in the src folder of my catkin. I rosrun both packages and launch rviz but I am unable to do the map making from the lidar data. Do i need to set up an odometry or TF? Or does anybody have a set of tutorials that is similar to what I am trying to do. How to i properly set up TF or odometry as well?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-10-20 09:09:01 -0500

jcbordon gravatar image

Hello! Yes, you need set up an odometry and tf to do mapping with gmapping. Your tf tree have to look like odom->base_link->laser. You have to make launch files, for the rplidar node specifying the frame and the topic, for the gmapping node also you have to specify the topic and the frames of the laser and the odom. In this link you have a guide to obtain the best results from a rplidar on gmapping For setting up the tf you have to make a tf broadcaster that make the tf link from base_link to laser, an example code in c++:

#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
int main(int argc, char** argv){
ros::init(argc, argv, "tina_tf_publisher");
ros::NodeHandle n;

ros::Rate r(100);

tf::TransformBroadcaster broadcaster;

    tf::Transform(tf::Quaternion(0, 0, 0, 1), tf::Vector3(0.25, 0.0, 0.2)),
    ros::Time::now(),"base_link", "laser"));

Also you have a program to compute odom, in our robot we have a topic from an arduino that publish the velocity and from that we have the odom, the code in python is:

#!/usr/bin/env python

import math
from math import sin, cos, pi
import rospy
import tf
from nav_msgs.msg  import Odometry
from std_msgs.msg  import Float64
from geometry_msgs.msg import Twist, Point, Quaternion, Vector3, Pose
v_x = 0
v_y = 0
vth = 0

def callback_x(msg):
   global v_x
   v_x =
def callback_y(msg):
   global v_y
   v_y =

rospy.Subscriber('velo_x', Float64, callback_x)
rospy.Subscriber('velo_y', Float64, callback_y)
odom_pub = rospy.Publisher("odom", Odometry, queue_size=50)
odom_broadcaster = tf.TransformBroadcaster()  
x = 0.0
y = 0.0
th = 0.0
vx = (v_x+v_y)/2
vy = 0.0
vth = (v_x-v_y)/0.5
current_time =
last_time =
r = rospy.Rate(10.0)
while not rospy.is_shutdown():
   vx = (v_x+v_y)/2
   vy = 0.0
   vth = (v_x-v_y)/0.5

   current_time =
   dt = (current_time - last_time).to_sec()
   delta_x = (vx * cos(th))*dt
   delta_y = (vx * sin(th))*dt
   delta_th = vth * dt

    x += delta_x
    y += delta_y
    th += delta_th

   odom_quat = tf.transformations.quaternion_from_euler(0,0,th)


   odom = Odometry()
   odom.header.stamp = current_time
   odom.header.frame_id = "odom"

   odom.pose.pose = Pose(Point(x,y,0.),Quaternion(*odom_quat))

   odom.child_frame_id = "base_link"
   odom.twist.twist = Twist(Vector3(vx,vy,0), Vector3(0,0,vth))


   last_time = current_time

I hope that it helps!

edit flag offensive delete link more


yes it helped very much so for these files do i add them to src folder?

Robot_fox gravatar image Robot_fox  ( 2020-11-02 09:21:21 -0500 )edit
jcbordon gravatar image jcbordon  ( 2020-11-02 14:41:26 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-10-16 13:06:06 -0500

Seen: 141 times

Last updated: Oct 16 '20