Skip to content

Commit 4660b04

Browse files
authored
Merge pull request #689 from aquintan4/odometry-publisher-gzharmonic
Odometry publisher gzharmonic
2 parents 031233f + ea0632f commit 4660b04

7 files changed

Lines changed: 431 additions & 23 deletions

File tree

CustomRobots/turtlebot3/models/turtlebot3_noisy_high/model.sdf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,18 @@
556556

557557
</plugin>
558558

559-
<plugin filename="gz-sim-odometry-publisher-system" name="gz::sim::systems::OdometryPublisher">
560-
<odom_topic>turtlebot3/odom_noisy</odom_topic>
561-
<robot_base_frame>base_footprint</robot_base_frame>
562-
<odom_frame>odom_noisy</odom_frame>
563-
<odom_publish_frequency>50</odom_publish_frequency>
564-
<dimensions>3</dimensions>
565-
<gaussian_noise>6</gaussian_noise>
559+
<plugin filename="libNoisyOdometryPlugin.so" name="custom_plugins::NoisyOdometryPlugin">
560+
<ros_topic>/turtlebot3/odom_noisy</ros_topic>
561+
<gz_cmd_vel_topic>/turtlebot3/cmd_vel</gz_cmd_vel_topic>
562+
<frame_id>odom</frame_id>
563+
<child_frame_id>base_footprint</child_frame_id>
564+
565+
<alpha1>0.005</alpha1>
566+
<alpha2>0.005</alpha2>
567+
<alpha3>0.003</alpha3>
568+
<alpha4>0.003</alpha4>
566569
</plugin>
570+
567571
</model>
568572

569573

CustomRobots/turtlebot3/models/turtlebot3_noisy_low/model.sdf

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,17 @@
556556

557557
</plugin>
558558

559-
<plugin filename="gz-sim-odometry-publisher-system" name="gz::sim::systems::OdometryPublisher">
560-
<odom_topic>turtlebot3/odom_noisy</odom_topic>
561-
<robot_base_frame>base_footprint</robot_base_frame>
562-
<odom_frame>odom_noisy</odom_frame>
563-
<odom_publish_frequency>50</odom_publish_frequency>
564-
<dimensions>3</dimensions>
565-
<gaussian_noise>0.01</gaussian_noise>
566-
</plugin>
567-
</model>
559+
<plugin filename="libNoisyOdometryPlugin.so" name="custom_plugins::NoisyOdometryPlugin">
560+
<ros_topic>/turtlebot3/odom_noisy</ros_topic>
561+
<gz_cmd_vel_topic>/turtlebot3/cmd_vel</gz_cmd_vel_topic>
562+
<frame_id>odom</frame_id>
563+
<child_frame_id>base_footprint</child_frame_id>
568564

565+
<alpha1>0.0005</alpha1>
566+
<alpha2>0.0005</alpha2>
567+
<alpha3>0.0002</alpha3>
568+
<alpha4>0.0002</alpha4>
569+
</plugin>
569570

571+
</model>
570572
</sdf>

CustomRobots/turtlebot3/models/turtlebot3_noisy_med/model.sdf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,18 @@
556556

557557
</plugin>
558558

559-
<plugin filename="gz-sim-odometry-publisher-system" name="gz::sim::systems::OdometryPublisher">
560-
<odom_topic>turtlebot3/odom_noisy</odom_topic>
561-
<robot_base_frame>base_footprint</robot_base_frame>
562-
<odom_frame>odom_noisy</odom_frame>
563-
<odom_publish_frequency>50</odom_publish_frequency>
564-
<dimensions>3</dimensions>
565-
<gaussian_noise>0.03</gaussian_noise>
559+
<plugin filename="libNoisyOdometryPlugin.so" name="custom_plugins::NoisyOdometryPlugin">
560+
<ros_topic>/turtlebot3/odom_noisy</ros_topic>
561+
<gz_cmd_vel_topic>/turtlebot3/cmd_vel</gz_cmd_vel_topic>
562+
<frame_id>odom</frame_id>
563+
<child_frame_id>base_footprint</child_frame_id>
564+
565+
<alpha1>0.002</alpha1>
566+
<alpha2>0.002</alpha2>
567+
<alpha3>0.001</alpha3>
568+
<alpha4>0.001</alpha4>
566569
</plugin>
570+
567571
</model>
568572

569573

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(gz_noisy_odometry)
3+
4+
# Default to C99
5+
if(NOT CMAKE_C_STANDARD)
6+
set(CMAKE_C_STANDARD 99)
7+
endif()
8+
9+
# Default to C++14
10+
if(NOT CMAKE_CXX_STANDARD)
11+
set(CMAKE_CXX_STANDARD 14)
12+
endif()
13+
14+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
15+
add_compile_options(-Wall -Wextra -Wpedantic)
16+
endif()
17+
18+
# ===========================================================================
19+
# find dependencies
20+
21+
find_package(ament_cmake REQUIRED)
22+
find_package(rclcpp REQUIRED)
23+
find_package(nav_msgs REQUIRED)
24+
find_package(tf2 REQUIRED)
25+
26+
find_package(gz-plugin2 REQUIRED COMPONENTS register)
27+
find_package(gz-sim8 REQUIRED)
28+
find_package(gz-msgs10 REQUIRED)
29+
find_package(gz-math7 REQUIRED)
30+
find_package(gz-common5 REQUIRED)
31+
find_package(gz-transport13 REQUIRED)
32+
33+
# ============================================================================
34+
# Noisy Odometry plugin
35+
36+
add_library(NoisyOdometryPlugin SHARED
37+
src/gz_noisy_odometry/NoisyOdometryPlugin.cpp
38+
)
39+
40+
target_include_directories(NoisyOdometryPlugin PUBLIC
41+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
42+
$<INSTALL_INTERFACE:include>
43+
)
44+
45+
# Dependencias de ROS 2
46+
ament_target_dependencies(NoisyOdometryPlugin
47+
rclcpp
48+
nav_msgs
49+
tf2
50+
)
51+
52+
# Enlace de librerías de Gazebo calcando la sintaxis del proyecto que funciona
53+
target_link_libraries(NoisyOdometryPlugin
54+
gz-plugin2::gz-plugin2
55+
gz-plugin2::register
56+
gz-sim8::gz-sim8
57+
gz-msgs10::gz-msgs10
58+
gz-math7::gz-math7
59+
gz-common5::gz-common5
60+
gz-transport13::gz-transport13
61+
)
62+
63+
install(
64+
TARGETS NoisyOdometryPlugin
65+
ARCHIVE DESTINATION lib
66+
LIBRARY DESTINATION lib
67+
RUNTIME DESTINATION bin
68+
)
69+
70+
install(
71+
DIRECTORY include/
72+
DESTINATION include
73+
)
74+
75+
ament_export_include_directories(include)
76+
ament_export_libraries(NoisyOdometryPlugin)
77+
78+
if(BUILD_TESTING)
79+
find_package(ament_lint_auto REQUIRED)
80+
find_package(ament_lint_common REQUIRED)
81+
endif()
82+
83+
ament_package()
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifndef NOISY_ODOMETRY_PLUGIN_HPP_
2+
#define NOISY_ODOMETRY_PLUGIN_HPP_
3+
4+
#include <gz/sim/System.hh>
5+
#include <gz/sim/Model.hh>
6+
#include <gz/transport/Node.hh>
7+
#include <gz/msgs/twist.pb.h>
8+
#include <gz/math/Pose3.hh>
9+
#include <gz/math/Rand.hh>
10+
11+
#include <rclcpp/rclcpp.hpp>
12+
#include <nav_msgs/msg/odometry.hpp>
13+
14+
#include <mutex>
15+
#include <string>
16+
#include <memory>
17+
18+
namespace custom_plugins
19+
{
20+
class NoisyOdometryPlugin :
21+
public gz::sim::System,
22+
public gz::sim::ISystemConfigure,
23+
public gz::sim::ISystemPostUpdate
24+
{
25+
public:
26+
NoisyOdometryPlugin();
27+
~NoisyOdometryPlugin() override = default;
28+
29+
void Configure(const gz::sim::Entity &_entity,
30+
const std::shared_ptr<const sdf::Element> &_sdf,
31+
gz::sim::EntityComponentManager &_ecm,
32+
gz::sim::EventManager &_eventMgr) override;
33+
34+
void PostUpdate(const gz::sim::UpdateInfo &_info,
35+
const gz::sim::EntityComponentManager &_ecm) override;
36+
37+
private:
38+
void OnCmdVel(const gz::msgs::Twist &_msg);
39+
40+
// Keeps angles bounded in [-pi, pi] so yaw differences remain well behaved.
41+
double NormalizeAngle(double _angle) const;
42+
43+
gz::sim::Model model_;
44+
gz::transport::Node gz_node_;
45+
46+
std::shared_ptr<rclcpp::Node> ros_node_;
47+
rclcpp::Publisher<nav_msgs::msg::Odometry>::SharedPtr ros_pub_;
48+
49+
std::string ros_topic_;
50+
std::string gz_cmd_vel_topic_;
51+
std::string frame_id_;
52+
std::string child_frame_id_;
53+
54+
bool initialized_{false};
55+
56+
// Internal odometric estimate that is propagated only with noisy increments.
57+
// This is the state published as odom_noisy and it is allowed to drift.
58+
gz::math::Pose3d noisy_pose_internal_;
59+
60+
// True simulator pose used only to extract the real motion increment
61+
// between consecutive simulation steps.
62+
gz::math::Pose3d last_true_pose_;
63+
64+
std::chrono::steady_clock::duration last_update_time_{0};
65+
66+
// Latest commanded velocities are still stored because they are useful
67+
// as auxiliary information and to keep compatibility with the original plugin.
68+
double current_v_{0.0};
69+
double current_w_{0.0};
70+
71+
// Last noisy body-frame velocities published in the odometry message.
72+
double last_noisy_linear_velocity_{0.0};
73+
double last_noisy_angular_velocity_{0.0};
74+
75+
std::mutex mutex_;
76+
77+
// Legacy generic noise parameter kept for compatibility with the original SDF.
78+
double gaussian_noise_coeff_{0.0};
79+
80+
// Standard odometry motion-model coefficients.
81+
// They control how rotation and translation uncertainties affect each other.
82+
double alpha1_{0.0};
83+
double alpha2_{0.0};
84+
double alpha3_{0.0};
85+
double alpha4_{0.0};
86+
};
87+
}
88+
89+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>gz_noisy_odometry</name>
5+
<version>0.0.1</version>
6+
<description>Gazebo Harmonic plugin for noisy odometry published directly to ROS 2</description>
7+
<maintainer email="user@todo.todo">User</maintainer>
8+
<license>Apache-2.0</license>
9+
10+
<buildtool_depend>ament_cmake</buildtool_depend>
11+
12+
<depend>rclcpp</depend>
13+
<depend>nav_msgs</depend>
14+
<depend>tf2</depend>
15+
16+
<export>
17+
<build_type>ament_cmake</build_type>
18+
</export>
19+
</package>

0 commit comments

Comments
 (0)