Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/publisher with customizable randomizer #1503

Merged
merged 29 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3e26cd7
Add new struct template `concealer::NormalDistribution`
yamacir-kit Jan 8, 2025
a0e5548
Add template specialization `NormalDistribution<nav_msgs::msg::Odomet…
yamacir-kit Jan 8, 2025
abbcdc4
Add launch argument `parameter_file_path` to `scenario_test_runner`
yamacir-kit Jan 8, 2025
996bc38
Add new struct template `Identity`
yamacir-kit Jan 9, 2025
0caa0ec
Update randomizer to receive `NodeParametersInterface`
yamacir-kit Jan 9, 2025
5ce7a0a
Update parameter file format
yamacir-kit Jan 9, 2025
719dda8
Merge remote-tracking branch 'origin/master' into feature/publisher-w…
yamacir-kit Jan 9, 2025
0ad745e
Merge remote-tracking branch 'origin/master' into feature/publisher-w…
yamacir-kit Jan 14, 2025
fc1930d
Update `NormalDistribution<Odometry>::device` type to `std::mt19937_64`
yamacir-kit Jan 16, 2025
35eadb7
Update `NormalDistribution<Odometry>` to use `std::normal_distribution`
yamacir-kit Jan 16, 2025
d3c0daf
Add support for multiplicative errors as well as additive errors
yamacir-kit Jan 17, 2025
e4f46c6
Cleanup
yamacir-kit Jan 17, 2025
86d6b2b
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Jan 21, 2025
24426bb
Update the position error to apply in the entity's local coordinate
yamacir-kit Jan 23, 2025
8b0948d
Rename the parameter `(x|y|z)` to `local_(x|y|z)`
yamacir-kit Jan 23, 2025
d42f314
Update `scenario_test_runner` to check the path given as a parameter
yamacir-kit Jan 24, 2025
9549881
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Jan 28, 2025
cb671dc
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Jan 31, 2025
73dabda
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Feb 6, 2025
dd6af6c
Merge remote-tracking branch 'origin/master' into feature/publisher-w…
yamacir-kit Feb 7, 2025
8273b71
Add new test file `concealer/test/normal_distribution.cpp`
yamacir-kit Feb 7, 2025
389b331
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Feb 7, 2025
f15bacd
Add new document `ConfiguringLocalizationTopics.md`
yamacir-kit Feb 12, 2025
04c76fb
Fix broken link
yamacir-kit Feb 12, 2025
54cd76b
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Feb 12, 2025
2e9918a
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Feb 13, 2025
ed78cb0
Update test `normal_distribution.cpp` to use `ASSERT_DOUBLE_EQ`
yamacir-kit Feb 14, 2025
1cbef82
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Feb 14, 2025
35c83a7
Merge branch 'master' into feature/publisher-with-customizable-random…
yamacir-kit Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions external/concealer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ament_auto_add_library(${PROJECT_NAME} SHARED
src/execute.cpp
src/field_operator_application.cpp
src/is_package_exists.cpp
src/publisher.cpp
src/task_queue.cpp)

target_link_libraries(${PROJECT_NAME}
Expand Down
16 changes: 8 additions & 8 deletions external/concealer/include/concealer/autoware_universe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ class AutowareUniverse : public rclcpp::Node,
Subscriber<TurnIndicatorsCommand> getTurnIndicatorsCommand;
Subscriber<PathWithLaneId> getPathWithLaneId;

Publisher<AccelWithCovarianceStamped> setAcceleration;
Publisher<Odometry> setOdometry;
Publisher<PoseWithCovarianceStamped> setPose;
Publisher<SteeringReport> setSteeringReport;
Publisher<GearReport> setGearReport;
Publisher<ControlModeReport> setControlModeReport;
Publisher<VelocityReport> setVelocityReport;
Publisher<TurnIndicatorsReport> setTurnIndicatorsReport;
Publisher<AccelWithCovarianceStamped> setAcceleration;
Publisher<Odometry, NormalDistribution> setOdometry;
Publisher<PoseWithCovarianceStamped> setPose;
Publisher<SteeringReport> setSteeringReport;
Publisher<GearReport> setGearReport;
Publisher<ControlModeReport> setControlModeReport;
Publisher<VelocityReport> setVelocityReport;
Publisher<TurnIndicatorsReport> setTurnIndicatorsReport;

std::atomic<geometry_msgs::msg::Accel> current_acceleration;
std::atomic<geometry_msgs::msg::Pose> current_pose;
Expand Down
95 changes: 90 additions & 5 deletions external/concealer/include/concealer/publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,112 @@
#ifndef CONCEALER__PUBLISHER_HPP_
#define CONCEALER__PUBLISHER_HPP_

#include <memory>
#include <nav_msgs/msg/odometry.hpp>
#include <random>
#include <rclcpp/rclcpp.hpp>

namespace concealer
{
template <typename Message>
template <typename T>
auto getParameter(
const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node,
const std::string & name, T value = {})
{
if (not node->has_parameter(name)) {
node->declare_parameter(name, rclcpp::ParameterValue(value));
}
return node->get_parameter(name).get_value<T>();
}

template <typename>
struct Identity
{
explicit constexpr Identity(
const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &, const std::string &)
{
}

template <typename T>
constexpr auto operator()(T && x) const -> decltype(auto)
{
return std::forward<decltype(x)>(x);
}
};

template <typename>
struct NormalDistribution;

template <>
struct NormalDistribution<nav_msgs::msg::Odometry>
{
std::random_device::result_type seed;

std::random_device device;

std::mt19937_64 engine;

struct Error
{
std::normal_distribution<double> additive, multiplicative;

explicit Error(
const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node,
const std::string & prefix)
: additive(
getParameter<double>(node, prefix + ".additive.mean"),
getParameter<double>(node, prefix + ".additive.standard_deviation")),
multiplicative(
getParameter<double>(node, prefix + ".multiplicative.mean"),
getParameter<double>(node, prefix + ".multiplicative.standard_deviation"))
{
}

auto apply(std::mt19937_64 & engine, double value) -> decltype(auto)
{
return value * (multiplicative(engine) + 1.0) + additive(engine);
}
};

// clang-format off
Error position_local_x_error,
position_local_y_error,
position_local_z_error,
orientation_r_error,
orientation_p_error,
orientation_y_error,
linear_x_error,
linear_y_error,
linear_z_error,
angular_x_error,
angular_y_error,
angular_z_error;
// clang-format on

explicit NormalDistribution(
const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &, const std::string &);

auto operator()(nav_msgs::msg::Odometry odometry) -> nav_msgs::msg::Odometry;
};

template <typename Message, template <typename> typename Randomizer = Identity>
class Publisher
{
typename rclcpp::Publisher<Message>::SharedPtr publisher;

Randomizer<Message> randomize;

public:
template <typename Node>
explicit Publisher(const std::string & topic, Node & node)
: publisher(node.template create_publisher<Message>(topic, rclcpp::QoS(1).reliable()))
: publisher(node.template create_publisher<Message>(topic, rclcpp::QoS(1).reliable())),
randomize(node.get_node_parameters_interface(), topic)
{
}

template <typename... Ts>
auto operator()(Ts &&... xs) const -> decltype(auto)
auto operator()(Ts &&... xs) -> decltype(auto)
{
return publisher->publish(std::forward<decltype(xs)>(xs)...);
return publisher->publish(randomize(std::forward<decltype(xs)>(xs)...));
}
};
} // namespace concealer
Expand Down
2 changes: 1 addition & 1 deletion external/concealer/src/autoware_universe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace concealer
{
AutowareUniverse::AutowareUniverse(bool simulate_localization)
: rclcpp::Node("concealer", "simulation", rclcpp::NodeOptions().use_global_arguments(false)),
: rclcpp::Node("AutowareUniverse", "simulation"),
getCommand("/control/command/control_cmd", rclcpp::QoS(1), *this),
getGearCommand("/control/command/gear_cmd", rclcpp::QoS(1), *this),
getTurnIndicatorsCommand("/control/command/turn_indicators_cmd", rclcpp::QoS(1), *this),
Expand Down
95 changes: 95 additions & 0 deletions external/concealer/src/publisher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2015 TIER IV, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <Eigen/Geometry>
#include <concealer/publisher.hpp>
#include <scenario_simulator_exception/exception.hpp>

namespace concealer
{
NormalDistribution<nav_msgs::msg::Odometry>::NormalDistribution(
const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node,
const std::string & topic)
: seed([&]() {
if (const auto value = getParameter<int>(node, topic + ".seed");
std::random_device::min() <= value and value <= std::random_device::max()) {
return value;
} else {
throw common::scenario_simulator_exception::Error(
"The value of parameter ", std::quoted(topic + ".seed"),
" must be greater than or equal to ", std::random_device::min(),
" and less than or equal to ", std::random_device::max());
}
}()),
engine(seed ? seed : device()),
position_local_x_error(node, topic + ".nav_msgs::msg::Odometry.pose.pose.position.local_x.error"),
position_local_y_error(node, topic + ".nav_msgs::msg::Odometry.pose.pose.position.local_y.error"),
position_local_z_error(node, topic + ".nav_msgs::msg::Odometry.pose.pose.position.local_z.error"),
orientation_r_error(node, topic + ".nav_msgs::msg::Odometry.pose.pose.orientation.r.error"),
orientation_p_error(node, topic + ".nav_msgs::msg::Odometry.pose.pose.orientation.p.error"),
orientation_y_error(node, topic + ".nav_msgs::msg::Odometry.pose.pose.orientation.y.error"),
linear_x_error(node, topic + ".nav_msgs::msg::Odometry.twist.twist.linear.x.error"),
linear_y_error(node, topic + ".nav_msgs::msg::Odometry.twist.twist.linear.y.error"),
linear_z_error(node, topic + ".nav_msgs::msg::Odometry.twist.twist.linear.z.error"),
angular_x_error(node, topic + ".nav_msgs::msg::Odometry.twist.twist.angular.x.error"),
angular_y_error(node, topic + ".nav_msgs::msg::Odometry.twist.twist.angular.y.error"),
angular_z_error(node, topic + ".nav_msgs::msg::Odometry.twist.twist.angular.z.error")
{
}

auto NormalDistribution<nav_msgs::msg::Odometry>::operator()(nav_msgs::msg::Odometry odometry)
-> nav_msgs::msg::Odometry
{
const Eigen::Quaterniond orientation = Eigen::Quaterniond(
odometry.pose.pose.orientation.w, odometry.pose.pose.orientation.x,
odometry.pose.pose.orientation.y, odometry.pose.pose.orientation.z);

Eigen::Vector3d local_position = Eigen::Vector3d(0.0, 0.0, 0.0);

local_position.x() = position_local_x_error.apply(engine, local_position.x());
local_position.y() = position_local_y_error.apply(engine, local_position.y());
local_position.z() = position_local_z_error.apply(engine, local_position.z());

const Eigen::Vector3d world_position = orientation.toRotationMatrix() * local_position;

odometry.pose.pose.position.x += world_position.x();
odometry.pose.pose.position.y += world_position.y();
odometry.pose.pose.position.z += world_position.z();

Eigen::Vector3d euler = orientation.matrix().eulerAngles(0, 1, 2);

euler.x() = orientation_r_error.apply(engine, euler.x());
euler.y() = orientation_p_error.apply(engine, euler.y());
euler.z() = orientation_y_error.apply(engine, euler.z());

const Eigen::Quaterniond q = Eigen::AngleAxisd(euler.x(), Eigen::Vector3d::UnitX()) *
Eigen::AngleAxisd(euler.y(), Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(euler.z(), Eigen::Vector3d::UnitZ());

odometry.pose.pose.orientation.x = q.x();
odometry.pose.pose.orientation.y = q.y();
odometry.pose.pose.orientation.z = q.z();
odometry.pose.pose.orientation.w = q.w();

odometry.twist.twist.linear.x = linear_x_error.apply(engine, odometry.twist.twist.linear.x);
odometry.twist.twist.linear.y = linear_y_error.apply(engine, odometry.twist.twist.linear.y);
odometry.twist.twist.linear.z = linear_z_error.apply(engine, odometry.twist.twist.linear.z);

odometry.twist.twist.angular.x = angular_x_error.apply(engine, odometry.twist.twist.angular.x);
odometry.twist.twist.angular.y = angular_y_error.apply(engine, odometry.twist.twist.angular.y);
odometry.twist.twist.angular.z = angular_z_error.apply(engine, odometry.twist.twist.angular.z);

return odometry;
}
} // namespace concealer
4 changes: 4 additions & 0 deletions mock/cpp_mock_scenarios/launch/mock_test.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def launch_setup(context, *args, **kwargs):
launch_rviz = LaunchConfiguration("launch_rviz", default=False)
launch_simple_sensor_simulator = LaunchConfiguration("launch_simple_sensor_simulator", default=True)
output_directory = LaunchConfiguration("output_directory", default=Path("/tmp"))
parameter_file_path = LaunchConfiguration("parameter_file_path", default=Path(get_package_share_directory("scenario_test_runner")) / "config/parameters.yaml")
port = LaunchConfiguration("port", default=5555)
publish_empty_context = LaunchConfiguration("publish_empty_context", default=False)
record = LaunchConfiguration("record", default=False)
Expand All @@ -140,6 +141,7 @@ def launch_setup(context, *args, **kwargs):
print(f"launch_autoware := {launch_autoware.perform(context)}")
print(f"launch_rviz := {launch_rviz.perform(context)}")
print(f"output_directory := {output_directory.perform(context)}")
print(f"parameter_file_path := {parameter_file_path.perform(context)}")
print(f"port := {port.perform(context)}")
print(f"publish_empty_context := {publish_empty_context.perform(context)}")
print(f"record := {record.perform(context)}")
Expand Down Expand Up @@ -174,6 +176,7 @@ def make_parameters():
{"junit_path": junit_path},
]
parameters += make_vehicle_parameters()
parameters += [parameter_file_path.perform(context)]
return parameters

def make_vehicle_parameters():
Expand Down Expand Up @@ -218,6 +221,7 @@ def description():
DeclareLaunchArgument("global_timeout", default_value=global_timeout ),
DeclareLaunchArgument("launch_autoware", default_value=launch_autoware ),
DeclareLaunchArgument("launch_rviz", default_value=launch_rviz ),
DeclareLaunchArgument("parameter_file_path", default_value=parameter_file_path ),
DeclareLaunchArgument("publish_empty_context", default_value=publish_empty_context ),
DeclareLaunchArgument("output_directory", default_value=output_directory ),
DeclareLaunchArgument("rviz_config", default_value=rviz_config ),
Expand Down
Loading
Loading