-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into doc/longitudinal-control
- Loading branch information
Showing
16 changed files
with
309 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...ple_sensor_simulator/include/simple_sensor_simulator/sensor_simulation/imu/imu_sensor.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2024 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. | ||
|
||
#ifndef SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__IMU_SENSOR_HPP_ | ||
#define SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__IMU_SENSOR_HPP_ | ||
|
||
#include <simulation_api_schema.pb.h> | ||
|
||
#include <array> | ||
#include <geometry_msgs/msg/accel.hpp> | ||
#include <geometry_msgs/msg/twist.hpp> | ||
#include <random> | ||
#include <rclcpp/rclcpp.hpp> | ||
#include <sensor_msgs/msg/imu.hpp> | ||
#include <simulation_interface/conversions.hpp> | ||
#include <traffic_simulator_msgs/msg/entity_status.hpp> | ||
|
||
namespace simple_sensor_simulator | ||
{ | ||
class ImuSensorBase | ||
{ | ||
public: | ||
explicit ImuSensorBase(const simulation_api_schema::ImuSensorConfiguration & configuration) | ||
: add_gravity_(configuration.add_gravity()), | ||
noise_standard_deviation_orientation_(configuration.noise_standard_deviation_orientation()), | ||
noise_standard_deviation_twist_(configuration.noise_standard_deviation_twist()), | ||
noise_standard_deviation_acceleration_(configuration.noise_standard_deviation_acceleration()), | ||
random_generator_(configuration.use_seed() ? configuration.seed() : std::random_device{}()), | ||
noise_distribution_orientation_(0.0, noise_standard_deviation_orientation_), | ||
noise_distribution_twist_(0.0, noise_standard_deviation_twist_), | ||
noise_distribution_acceleration_(0.0, noise_standard_deviation_acceleration_), | ||
orientation_covariance_(calculateCovariance(noise_standard_deviation_orientation_)), | ||
angular_velocity_covariance_(calculateCovariance(noise_standard_deviation_twist_)), | ||
linear_acceleration_covariance_(calculateCovariance(noise_standard_deviation_acceleration_)) | ||
{ | ||
} | ||
|
||
virtual ~ImuSensorBase() = default; | ||
|
||
virtual auto update( | ||
const rclcpp::Time & current_ros_time, | ||
const std::vector<traffic_simulator_msgs::EntityStatus> & statuses) const -> bool = 0; | ||
|
||
protected: | ||
const bool add_gravity_; | ||
const double noise_standard_deviation_orientation_; | ||
const double noise_standard_deviation_twist_; | ||
const double noise_standard_deviation_acceleration_; | ||
mutable std::mt19937 random_generator_; | ||
mutable std::normal_distribution<> noise_distribution_orientation_; | ||
mutable std::normal_distribution<> noise_distribution_twist_; | ||
mutable std::normal_distribution<> noise_distribution_acceleration_; | ||
const std::array<double, 9> orientation_covariance_; | ||
const std::array<double, 9> angular_velocity_covariance_; | ||
const std::array<double, 9> linear_acceleration_covariance_; | ||
|
||
auto calculateCovariance(const double stddev) const -> std::array<double, 9> | ||
{ | ||
return {std::pow(stddev, 2), 0, 0, 0, std::pow(stddev, 2), 0, 0, 0, std::pow(stddev, 2)}; | ||
}; | ||
}; | ||
|
||
template <typename MessageType> | ||
class ImuSensor : public ImuSensorBase | ||
{ | ||
public: | ||
explicit ImuSensor( | ||
const simulation_api_schema::ImuSensorConfiguration & configuration, | ||
const typename rclcpp::Publisher<MessageType>::SharedPtr & publisher) | ||
: ImuSensorBase(configuration), | ||
entity_name_(configuration.entity()), | ||
frame_id_(configuration.frame_id()), | ||
publisher_(publisher) | ||
{ | ||
} | ||
|
||
auto update( | ||
const rclcpp::Time & current_ros_time, | ||
const std::vector<traffic_simulator_msgs::EntityStatus> & statuses) const -> bool override | ||
{ | ||
for (const auto & status : statuses) { | ||
if (status.name() == entity_name_) { | ||
traffic_simulator_msgs::msg::EntityStatus status_msg; | ||
simulation_interface::toMsg(status, status_msg); | ||
publisher_->publish(generateMessage(current_ros_time, status_msg)); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private: | ||
auto generateMessage( | ||
const rclcpp::Time & current_ros_time, | ||
const traffic_simulator_msgs::msg::EntityStatus & status) const -> const MessageType; | ||
|
||
const std::string entity_name_; | ||
const std::string frame_id_; | ||
const typename rclcpp::Publisher<MessageType>::SharedPtr publisher_; | ||
}; | ||
} // namespace simple_sensor_simulator | ||
#endif // SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__IMU_SENSOR_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
simulation/simple_sensor_simulator/src/sensor_simulation/imu/imu_sensor.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2024 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 <tf2/LinearMath/Matrix3x3.h> | ||
#include <tf2/LinearMath/Quaternion.h> | ||
#include <tf2/LinearMath/Transform.h> | ||
|
||
#include <algorithm> | ||
#include <geometry/quaternion/euler_to_quaternion.hpp> | ||
#include <geometry/quaternion/quaternion_to_euler.hpp> | ||
#include <simple_sensor_simulator/sensor_simulation/imu/imu_sensor.hpp> | ||
|
||
namespace simple_sensor_simulator | ||
{ | ||
template <> | ||
auto ImuSensor<sensor_msgs::msg::Imu>::generateMessage( | ||
const rclcpp::Time & current_ros_time, | ||
const traffic_simulator_msgs::msg::EntityStatus & status) const -> const sensor_msgs::msg::Imu | ||
{ | ||
const auto applyNoise = | ||
[&](geometry_msgs::msg::Vector3 & v, std::normal_distribution<> & distribution) { | ||
v.x += distribution(random_generator_); | ||
v.y += distribution(random_generator_); | ||
v.z += distribution(random_generator_); | ||
}; | ||
|
||
auto imu_msg = sensor_msgs::msg::Imu(); | ||
imu_msg.header.stamp = current_ros_time; | ||
imu_msg.header.frame_id = frame_id_; | ||
|
||
auto orientation_rpy = math::geometry::convertQuaternionToEulerAngle(status.pose.orientation); | ||
auto twist = status.action_status.twist; | ||
auto accel = status.action_status.accel; | ||
|
||
// Apply noise | ||
if (noise_standard_deviation_orientation_ > 0.0) { | ||
applyNoise(orientation_rpy, noise_distribution_orientation_); | ||
} | ||
if (noise_standard_deviation_twist_ > 0.0) { | ||
applyNoise(twist.angular, noise_distribution_twist_); | ||
} | ||
if (noise_standard_deviation_acceleration_ > 0.0) { | ||
applyNoise(accel.linear, noise_distribution_acceleration_); | ||
} | ||
|
||
// Apply gravity | ||
if (add_gravity_) { | ||
tf2::Quaternion orientation_quaternion; | ||
orientation_quaternion.setRPY(orientation_rpy.x, orientation_rpy.y, orientation_rpy.z); | ||
tf2::Matrix3x3 rotation_matrix(orientation_quaternion); | ||
tf2::Vector3 gravity(0.0, 0.0, -9.81); | ||
tf2::Vector3 transformed_gravity = rotation_matrix * gravity; | ||
accel.linear.x += transformed_gravity.x(); | ||
accel.linear.y += transformed_gravity.y(); | ||
accel.linear.z += transformed_gravity.z(); | ||
} | ||
|
||
// Set data | ||
imu_msg.orientation = math::geometry::convertEulerAngleToQuaternion(orientation_rpy); | ||
imu_msg.angular_velocity.x = twist.angular.x; | ||
imu_msg.angular_velocity.y = twist.angular.y; | ||
imu_msg.angular_velocity.z = twist.angular.z; | ||
imu_msg.linear_acceleration.x = accel.linear.x; | ||
imu_msg.linear_acceleration.y = accel.linear.y; | ||
imu_msg.linear_acceleration.z = accel.linear.z; | ||
|
||
// Set covariance | ||
std::copy( | ||
orientation_covariance_.begin(), orientation_covariance_.end(), | ||
imu_msg.orientation_covariance.data()); | ||
std::copy( | ||
angular_velocity_covariance_.begin(), angular_velocity_covariance_.end(), | ||
imu_msg.angular_velocity_covariance.data()); | ||
std::copy( | ||
linear_acceleration_covariance_.begin(), linear_acceleration_covariance_.end(), | ||
imu_msg.linear_acceleration_covariance.data()); | ||
return imu_msg; | ||
} | ||
} // namespace simple_sensor_simulator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.