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

fix(cpp_mock_scenarios, ego_entity_simulation): fix ego issue in random001, fix getCurrentPose() #1361

Merged
merged 9 commits into from
Oct 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace geometry
{
template <
typename T, std::enable_if_t<std::conjunction_v<IsLikeQuaternion<T>>, std::nullptr_t> = nullptr>
auto getRotationMatrix(T quat)
auto getRotationMatrix(T quat) -> Eigen::Matrix3d
{
auto x = quat.x;
auto y = quat.y;
Expand Down
16 changes: 12 additions & 4 deletions mock/cpp_mock_scenarios/src/random_scenario/random001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ class RandomScenario : public cpp_mock_scenarios::CppScenarioNode
{
const auto trigger_position = traffic_simulator::helper::constructCanonicalizedLaneletPose(
34621, 10, 0.0, api_.getHdmapUtils());
const auto ego_goal_position = traffic_simulator::helper::constructCanonicalizedLaneletPose(
34606, 0.0, 0.0, api_.getHdmapUtils());
const auto entity_name = "spawn_nearby_ego";

if (const auto ego = api_.getEntity("ego")) {
if (api_.reachPosition("ego", trigger_position, 20.0) && !api_.entityExists(entity_name)) {
api_.spawn(
Expand All @@ -198,12 +201,16 @@ class RandomScenario : public cpp_mock_scenarios::CppScenarioNode
.orientation(geometry_msgs::msg::Quaternion())),
getVehicleParameters(),
traffic_simulator::entity::VehicleEntity::BuiltinBehavior::doNothing());
} else {
stop(cpp_mock_scenarios::Result::FAILURE);
}

if (!api_.reachPosition("ego", trigger_position, 20.0) && api_.entityExists(entity_name)) {
api_.despawn(entity_name);
}
}
if (!api_.reachPosition("ego", trigger_position, 20.0) && api_.entityExists(entity_name)) {
api_.despawn(entity_name);

if (api_.reachPosition("ego", ego_goal_position, 1.0)) {
api_.despawn("ego");
stop(cpp_mock_scenarios::Result::SUCCESS);
}
}
}
Expand Down Expand Up @@ -232,6 +239,7 @@ class RandomScenario : public cpp_mock_scenarios::CppScenarioNode
getVehicleParameters(),
traffic_simulator::entity::VehicleEntity::BuiltinBehavior::doNothing());
} else {
api_.despawn("ego");
stop(cpp_mock_scenarios::Result::FAILURE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class EgoEntitySimulation

traffic_simulator::CanonicalizedEntityStatus status_;

geometry_msgs::msg::Pose initial_pose_;
const geometry_msgs::msg::Pose initial_pose_;

const Eigen::Matrix3d initial_rotation_matrix_;

static auto getVehicleModelType() -> VehicleModelType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ EgoEntitySimulation::EgoEntitySimulation(
vehicle_model_type_(getVehicleModelType()),
vehicle_model_ptr_(makeSimulationModel(vehicle_model_type_, step_time, parameters)),
status_(initial_status, std::nullopt),
initial_pose_(status_.getMapPose()),
initial_rotation_matrix_(math::geometry::getRotationMatrix(initial_pose_.orientation)),
consider_acceleration_by_road_slope_(consider_acceleration_by_road_slope),
hdmap_utils_ptr_(hdmap_utils),
vehicle_parameters(parameters)
{
setStatus(initial_status);
initial_pose_ = status_.getMapPose();
autoware->set_parameter(use_sim_time);
}

Expand Down Expand Up @@ -232,17 +233,16 @@ auto EgoEntitySimulation::overwrite(
considered unchangeable and stored in an additional variable
world_relative_position_ that is used in calculations.
*/
world_relative_position_ = getRotationMatrix(initial_pose_.orientation).transpose() *
Eigen::Vector3d(
status.pose.position.x - initial_pose_.position.x,
status.pose.position.y - initial_pose_.position.y,
status.pose.position.z - initial_pose_.position.z);
world_relative_position_ =
initial_rotation_matrix_.transpose() * Eigen::Vector3d(
status.pose.position.x - initial_pose_.position.x,
status.pose.position.y - initial_pose_.position.y,
status.pose.position.z - initial_pose_.position.z);

if (is_npc_logic_started) {
const auto yaw = [&]() {
const auto q = Eigen::Quaterniond(
getRotationMatrix(initial_pose_.orientation).transpose() *
getRotationMatrix(status.pose.orientation));
initial_rotation_matrix_.transpose() * getRotationMatrix(status.pose.orientation));
geometry_msgs::msg::Quaternion relative_orientation;
relative_orientation.x = q.x();
relative_orientation.y = q.y();
Expand Down Expand Up @@ -296,7 +296,7 @@ void EgoEntitySimulation::update(
considered unchangeable and stored in an additional variable
world_relative_position_ that is used in calculations.
*/
world_relative_position_ = getRotationMatrix(initial_pose_.orientation).transpose() *
world_relative_position_ = initial_rotation_matrix_.transpose() *
Eigen::Vector3d(
status_.getMapPose().position.x - initial_pose_.position.x,
status_.getMapPose().position.y - initial_pose_.position.y,
Expand Down Expand Up @@ -413,8 +413,8 @@ auto EgoEntitySimulation::getCurrentPose(const double pitch_angle = 0.) const
-> geometry_msgs::msg::Pose
{
using math::geometry::operator*;
const Eigen::Vector3d relative_position =
math::geometry::getRotationMatrix(initial_pose_.orientation) * world_relative_position_;
const auto relative_position =
Eigen::Vector3d(initial_rotation_matrix_ * world_relative_position_);
const auto relative_orientation = math::geometry::convertEulerAngleToQuaternion(
geometry_msgs::build<geometry_msgs::msg::Vector3>()
.x(0)
Expand Down
Loading