Robot loaded in different positions in Gazebo and Rviz

Hi
How can I change the origin of a world in Gazebo? is that possible ?

I loaded an image for Willow Garage in Rviz, and a world for Willow Garage in Gazebo. My rosbot2 is loaded in the default position which is the origin. However, my rosbot2 was loaded in different positions in map and world. Here is rosbot2 in Rviz(inside the red circle): rosbot2 is at (0,0) based on map description in .yaml file
rviz2
Here is rosbot2 in Gazebo(inside the red circle): it does not match the position in Rviz

Here are my files:

willow_garage_map.yaml
image: willow_garage_map.pgm
# a 584 X 526 map @ 0.100 m/cell
resolution: 0.1
origin: [-18, -15, 0.0]
occupied_thresh: 0.65
free_thresh: 0.196
negate: 0
move_my_rosbot.launch







    <node pkg="move_base" type="move_base" name="move_base" output="screen">
        <param name="base_global_planner" value="navfn/NavfnROS"/>
        <param name="base_local_planner" value="base_local_planner/TrajectoryPlannerROS"/>
        <param name="controller_frequency" value="10.0"/>
        <rosparam file="$(find rosbot_navigation)/config/costmap_common_params.yaml" command="load" ns="global_costmap" />
        <rosparam file="$(find rosbot_navigation)/config/costmap_common_params.yaml" command="load" ns="local_costmap" />
        <rosparam file="$(find rosbot_navigation)/config/local_costmap_params.yaml" command="load" />
        <rosparam file="$(find rosbot_navigation)/config/global_costmap_params.yaml" command="load" />
        <rosparam file="$(find rosbot_navigation)/config/trajectory_planner.yaml" command="load" />
    </node>
</launch>

costmap_common_params.yaml
obstacle_range: 6.0
raytrace_range: 8.5
footprint: [[0.12, 0.14], [0.12, -0.14], [-0.12, -0.14], [-0.12, 0.14]]
map_topic: /map
subscribe_to_updates: true
observation_sources: laser_scan_sensor
laser_scan_sensor: {sensor_frame: laser, data_type: LaserScan, topic: scan, marking: true, clearing: true}
global_frame: map
robot_base_frame: base_link
always_send_full_costmap: true
global_costmap_params.yaml
global_costmap:
global_frame: map
update_frequency: 0.5
publish_frequency: 1.5
transform_tolerance: 1.0
width: 16
height: 16
origin_x: -8
origin_y: -8
static_map: true
rolling_window: true
inflation_radius: 0.0
cost_scaling_factor: 10.0
resolution: 0.01
local_costmap_params.yaml
local_costmap:
global_frame: map
update_frequency: 2.5
publish_frequency: 2.5
transform_tolerance: 1.0
static_map: false
rolling_window: true
width: 3
height: 3
origin_x: -1.5
origin_y: -1.5
resolution: 0.01
inflation_radius: 0.0
cost_scaling_factor: 10.0
trajectory_planner.yaml
TrajectoryPlannerROS:
max_vel_x: 0.5
min_vel_x: 0.1
max_vel_theta: 2.5
min_vel_theta: -2.5
min_in_place_vel_theta: 0.5
acc_lim_theta: 1.0
acc_lim_x: 1.5
acc_lim_Y: 1.5
holonomic_robot: false
meter_scoring: true
xy_goal_tolerance: 0.5
yaw_goal_tolerance: 0.5

 vx_samples: 6 
 vtheta_samples: 20
 controller_frequency: 0.5 


 occdist_scale:  0.01
 pdist_scale: 0.4
 gdist_scale: 0.2
 dwa: true

These are map and world files:
https://www.mediafire.com/folder/q5xtxi7tfc4r2/rosbot_move_files

Hi Eman,

I would suggest manually spawning ROSBot in different position in Gazebo. You can do it by modifying rosbot_gazebo.launch in the following way:

<?xml version="1.0" encoding="UTF-8"?>
<launch>

  <!-- Robot pose -->
  <arg name="x" default="0"/>
  <arg name="y" default="0"/>
  <arg name="z" default="0"/>
  <arg name="roll" default="0"/>
  <arg name="pitch" default="0"/>
  <arg name="yaw" default="0"/>

  <rosparam command="load" file="$(find joint_state_controller)/joint_state_controller.yaml" />
  <node name="joint_state_controller_spawner" pkg="controller_manager" type="spawner" output="screen" args="joint_state_controller" />

  <param name="robot_description" command="$(find xacro)/xacro.py '$(find rosbot_description)/urdf/rosbot.xacro'"/>

  <node name="rosbot_spawn" pkg="gazebo_ros" type="spawn_model"
            output="screen" args="-urdf -param robot_description -model rosbot
                                  -x $(arg x) -y $(arg y) -z $(arg z)
                                  -R $(arg roll) -P $(arg pitch) -Y $(arg yaw)"/>

  <node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher"/>

</launch>

Here is post you can refer to. You have to manually try to find the exact spawn point and rotation of the robot. Fortunately, it doesn’t have to be perfect. After some movement robot will estimate its true position.

Best regards,
Krzysztof Wojciechowski.

Many thanks Krzysztof for your help.
I have tried using different pgm file, and it is better than the pgm above. Using the new pgm, the difference in rosbot2 position at Gazebo and Rviz (world and .pgm map) is not a big difference. However, it still not the same exact position.

I made your suggestions, and I modified move_my_rosbot.launch(add tf and AMCL), but still there is difference:

<launch>
    <arg name="use_rosbot" default="false"/>
    <arg name="use_gazebo" default="true"/>
    <param if="$(arg use_gazebo)" name="use_sim_time" value="true"/>
<!-- Bring the ROSbot model and show it in Gazebo and in Rviz  -->
    <include file="$(find rosbot_description)/launch/rosbot_rviz.launch"/>
<!-- Map server -->
    <arg name="map_file" default="$(find rosbot_navigation)/maps/willowgarage-refined.yaml"/>
    <node name="map_server" pkg="map_server" type="map_server" args="$(arg map_file)" />
<!--- tf -->
    <node unless="$(arg use_rosbot)" pkg="tf" type="static_transform_publisher" name="map_odom_tf" args="0 0 0 0 0 0 map odom 100" /> 
    <node if="$(arg use_rosbot)" pkg="tf" type="static_transform_publisher" name="base_link_to_laser" args="0 0 0 3.14 0 0 base_link laser 100" />
   
    <node pkg="joint_state_publisher" type="joint_state_publisher" name="joint_state_publisher">
    <param name="publish_frequency" type="double" value="30.0" />
    </node>
<!--- Localization: Run AMCL -->
  <node pkg="amcl" type="amcl" name="amcl" output="screen">
        <remap from="scan" to="/scan"/>
        <param name="odom_frame_id" value="odom"/>
        <!-- <param name="odom_model_type" value="diff-corrected"/> --> 
        <param name="odom_model_type" value="diff"/>
        <param name="base_frame_id" value="base_link"/>
        <param name="update_min_d" value="0.05"/>
        <param name="update_min_a" value="0.1"/>
        <param name="min_particles" value="500"/>
        <param name="max_particles" value="10000"/>
        <param name="initial_pose_x" value="0"/>
        <param name="initial_pose_y" value="0"/>
    </node>
<!-- Move base -->
    <node pkg="move_base" type="move_base" name="move_base" output="screen">
        <param name="base_global_planner" value="navfn/NavfnROS"/>
        <param name="base_local_planner" value="base_local_planner/TrajectoryPlannerROS"/>
        <param name="controller_frequency" value="10.0"/>
        <rosparam file="$(find rosbot_navigation)/config/costmap_common_params.yaml" command="load" ns="global_costmap" />
        <rosparam file="$(find rosbot_navigation)/config/costmap_common_params.yaml" command="load" ns="local_costmap" />
        <rosparam file="$(find rosbot_navigation)/config/local_costmap_params.yaml" command="load" />
        <rosparam file="$(find rosbot_navigation)/config/global_costmap_params.yaml" command="load" />
        <rosparam file="$(find rosbot_navigation)/config/trajectory_planner.yaml" command="load" />
    </node>
</launch> 

Gazebo wolrd:

Rviz map:
Rviz

NOTE: I tried sending new pose, but still difference
rostopic pub -1 /gazebo/set_model_state gazebo_msgs/ModelState ‘{model_name: rosbot, pose: { position: { x: 2, y: 2, z: 0 }, orientation: {x: 0, y: 0, z: 0, w: 1 } }, twist: { linear: { x: 0, y: 0, z: 0 }, angular: { x: 0, y: 0, z: 0} }, reference_frame: world }’

Hi Eman,

you can make the map fit your robot’s location better in another way. Try modifying map’s origin in willow_garage_map.yaml. As I said before you have to make guess about your robot’s location on the map. Simple trial and error should be sufficinet. AMCL should estimate its true position as the robot will be moving.

Best regards,
Krzysztof Wojciechowski.