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

RJD-1057 (4/5): Remove non-API member functions: EntityManager’s member functions forwarded to EntityBase (2/2) #1334

Open
wants to merge 52 commits into
base: master
Choose a base branch
from

Conversation

TauTheLepton
Copy link
Contributor

@TauTheLepton TauTheLepton commented Jul 31, 2024

Description

Abstract

This pull request mainly consists of refactoring and introduces changes in the API and EntityManager.

Background

This pull request is an extension of the #1473.
Due to the fact that these pull requests combined would have been too large for a review, they have been split into two.

As a result of the previous PR, changes have been made to the API and EntityManager classes.
These changes created a great opportunity to declutter / tidy up the code of these classes.
This pull request consists of the code cleanup and refactoring. Many of the changes introduced are just changes to the order of functions inside the implementation file to keep them grouped by functionality and consistent with the header file.

Tip

When reviewing the changes, it may be easier to see the actual modifications by opening both versions of each file side by side rather than looking at the diff generated by GitHub.
This is due to the fact that GitHub shows lots of changes where mostly copying and pasting the code has been done (for reordering purposes).

Some other minor changes have been introduced as well, like the addition of the ostream_helpers.cpp.

What is more, the functions EntityManager::spawnEntity and (most importantly) API::spawn have been modified to return the EntityBase reference:

Before:

template <typename Pose>
auto spawn(
const std::string & name, const Pose & pose,
const traffic_simulator_msgs::msg::VehicleParameters & parameters,
const std::string & behavior = VehicleBehavior::defaultBehavior(),
const std::string & model3d = "")

After:

template <
typename PoseType, typename ParamsType,
typename = std::enable_if_t<std::disjunction_v<
std::is_same<std::decay_t<ParamsType>, traffic_simulator_msgs::msg::VehicleParameters>,
std::is_same<std::decay_t<ParamsType>, traffic_simulator_msgs::msg::PedestrianParameters>,
std::is_same<std::decay_t<ParamsType>, traffic_simulator_msgs::msg::MiscObjectParameters>>>>
auto spawn(
const std::string & name, const PoseType & pose, const ParamsType & parameters,
const std::string & behavior = "", const std::string & model3d = "") -> entity::EntityBase &

Cpp mock scenarios

The change mentioned above has been applied in the whole codebase, which includes most of the cpp mock scenarios.

Cpp mock scenarios have been all changed like the following:

Before:

api_.spawn(
"npc", traffic_simulator::helper::constructCanonicalizedLaneletPose(34741, 10.0, 0.0),
getVehicleParameters());
auto & npc_entity = api_.getEntity("npc");
npc_entity.setLinearVelocity(10);
npc_entity.requestSpeedChange(10, true);

After:

auto & npc_entity = api_.spawn(
"npc", traffic_simulator::helper::constructCanonicalizedLaneletPose(34741, 10.0, 0.0),
getVehicleParameters());
npc_entity.setLinearVelocity(10);
npc_entity.requestSpeedChange(10, true);

Details

Below are all changes made in this PR

API

  • Renamed member configuration to configuration_

  • Most of the constructor logic has been moved to API::init() member function

    auto API::init() -> bool
    {
    if (not configuration_.standalone_mode) {
    simulation_api_schema::InitializeRequest request;
    request.set_initialize_time(clock_.getCurrentSimulationTime());
    request.set_lanelet2_map_path(configuration_.lanelet2_map_path().string());
    request.set_realtime_factor(clock_.realtime_factor);
    request.set_step_time(clock_.getStepTime());
    simulation_interface::toProto(
    clock_.getCurrentRosTime(), *request.mutable_initialize_ros_time());
    return zeromq_client_.call(request).result().success();
    } else {
    return true;
    }
    }

  • Added member function setSiulationStepTime

    auto API::setSimulationStepTime(const double step_time) -> bool
    {
    /**
    * @note Pausing the simulation by setting the realtime_factor_ value to 0 is not supported and causes the simulation crash.
    * For that reason, before performing the action, it needs to be ensured that the incoming request data is a positive number.
    */
    if (step_time >= 0.001) {
    clock_.realtime_factor = step_time;
    simulation_api_schema::UpdateStepTimeRequest request;
    request.set_simulation_step_time(clock_.getStepTime());
    return zeromq_client_.call(request).result().success();
    } else {
    return false;
    }
    }

  • Removed member function getZMQSocketPort in favor of using getROS2Parameter

  • 3 spawn functions

    1. template <typename Pose>
      auto spawn(
      const std::string & name, const Pose & pose,
      const traffic_simulator_msgs::msg::VehicleParameters & parameters,
      const std::string & behavior = VehicleBehavior::defaultBehavior(),
      const std::string & model3d = "")
    2. template <typename Pose>
      auto spawn(
      const std::string & name, const Pose & pose,
      const traffic_simulator_msgs::msg::PedestrianParameters & parameters,
      const std::string & behavior = PedestrianBehavior::defaultBehavior(),
      const std::string & model3d = "")
    3. template <typename Pose>
      auto spawn(
      const std::string & name, const Pose & pose,
      const traffic_simulator_msgs::msg::MiscObjectParameters & parameters,
      const std::string & model3d = "")

    have been combined into 1 spawn function (additional template parameter ParamsType was introduced for this reason)

    template <
    typename PoseType, typename ParamsType,
    typename = std::enable_if_t<std::disjunction_v<
    std::is_same<std::decay_t<ParamsType>, traffic_simulator_msgs::msg::VehicleParameters>,
    std::is_same<std::decay_t<ParamsType>, traffic_simulator_msgs::msg::PedestrianParameters>,
    std::is_same<std::decay_t<ParamsType>, traffic_simulator_msgs::msg::MiscObjectParameters>>>>
    auto spawn(
    const std::string & name, const PoseType & pose, const ParamsType & parameters,
    const std::string & behavior = "", const std::string & model3d = "") -> entity::EntityBase &

  • The remaining member functions that were defined using the FORWARD_TO_ENTITY_MANAGER macro have been converted to standard member functions:

    • getEgoEntity
    • getEntityNames
    • getEntityPointer
    • getFirstEgoName
    • getHdmapUtils
    • isEntityExist
    • isNpcLogicStarted
    • resetBehaviorPlugin

Important

All other changes are minor refactoring or just reordering functions in the file, which GitHub shows as big changes.

EntityManager

  • Renamed member configuration to configuration_
  • Moved member function getOrigin to free function
    auto getOrigin(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters)
    -> geographic_msgs::msg::GeoPoint;
  • Removed these member functions in favor of using Entity member function directly:
    • getObstacle
    • getWaypoints
    • getGoalPoses
  • Removed these member functions in favor of using Entity field directly:
    • getPedestrianParameters
    • getVehicleParameters
  • As mentioned in Background, the spawnEntity function now returns a reference to the created entity
    template <typename EntityType, typename PoseType, typename ParametersType, typename... Ts>
    auto spawnEntity(
    const std::string & name, const PoseType & pose, const ParametersType & parameters,
    const double current_time, Ts &&... xs) -> entity::EntityBase &
  • Removed member function createPublisher
  • Removed member function createSubscription

Important

All other changes are minor refactoring or just reordering functions in the file, which GitHub shows as big changes.

EntityBase

MiscObjectEntity

  • Change getGoalPoses return type
    • auto getGoalPoses() -> std::vector<CanonicalizedLaneletPose>

      auto getGoalPoses() -> std::vector<geometry_msgs::msg::Pose>

PedestrianEntity

  • Change getGoalPoses return type
    • auto getGoalPoses() -> std::vector<CanonicalizedLaneletPose>

      auto getGoalPoses() -> std::vector<geometry_msgs::msg::Pose>
  • Added member function auto getParameters() const -> const traffic_simulator_msgs::msg::PedestrianParameters &;

VehicleEntity

  • Change getGoalPoses return type
    • auto getGoalPoses() -> std::vector<CanonicalizedLaneletPose>

      auto getGoalPoses() -> std::vector<geometry_msgs::msg::Pose>
  • Added member function auto getParameters() const -> const traffic_simulator_msgs::msg::VehicleParameters &;

Ostream helpers

All existing operators std::ostream & operator<< have been moved from file helper.[ch]pp to a dedicated file ostream_helper.[ch]pp.
In addition many more operators have been added for the types

  • traffic_simulator_msgs::msg::LaneletPose
  • traffic_simulator_msgs::msg::EntitySubtype
  • traffic_simulator_msgs::msg::Axle
  • traffic_simulator_msgs::msg::Axles
  • traffic_simulator_msgs::msg::BoundingBox
  • traffic_simulator_msgs::msg::Performance
  • traffic_simulator_msgs::msg::VehicleParameters

References

INTERNAL LINK 1
INTERNAL LINK 2

Destructive Changes

--

Known Limitations

--

dmoszynski and others added 13 commits July 2, 2024 13:09
…o RJD-1057-remove-functions-forwarded-to-entity-base

Signed-off-by: Mateusz Palczuk <[email protected]>
…ed-to-entity-base' into RJD-1057-remove-functions-forwarded-to-entity-base

Signed-off-by: Mateusz Palczuk <[email protected]>
…o RJD-1057-remove-functions-forwarded-to-entity-base

Signed-off-by: Mateusz Palczuk <[email protected]>
Signed-off-by: Mateusz Palczuk <[email protected]>
…le' into RJD-1057-remove-functions-forwarded-to-entity-base-with-middle

Signed-off-by: Mateusz Palczuk <[email protected]>
@dmoszynski dmoszynski self-assigned this Jul 31, 2024
…le' into RJD-1057-remove-functions-forwarded-to-entity-base-with-middle

Signed-off-by: Mateusz Palczuk <[email protected]>
@TauTheLepton TauTheLepton force-pushed the RJD-1057-remove-functions-forwarded-to-entity-base-refactor branch from c9057f9 to ff759fe Compare July 31, 2024 14:16
@yamacir-kit yamacir-kit deleted the branch master September 18, 2024 01:42
@yamacir-kit yamacir-kit deleted the RJD-1057-remove-functions-forwarded-to-entity-base-refactor branch September 18, 2024 01:43
@TauTheLepton TauTheLepton restored the RJD-1057-remove-functions-forwarded-to-entity-base-refactor branch October 2, 2024 07:32
@TauTheLepton TauTheLepton reopened this Oct 2, 2024
TauTheLepton and others added 3 commits October 3, 2024 16:43
…le' into RJD-1057-remove-functions-forwarded-to-entity-base-refactor

Signed-off-by: Mateusz Palczuk <[email protected]>
…ded-to-entity-base-middle' into RJD-1057-remove-functions-forwarded-to-entity-base-refactor
…le' into RJD-1057-remove-functions-forwarded-to-entity-base-refactor
@TauTheLepton TauTheLepton self-assigned this Oct 15, 2024
@dmoszynski dmoszynski changed the title [tmp] Remove non-API member functions: EntityManager’s member functions forwarded to EntityBase (2/2) [tmp] RJD-1057 (4/5): Remove non-API member functions: EntityManager’s member functions forwarded to EntityBase (2/2) Oct 15, 2024
…le' into RJD-1057-remove-functions-forwarded-to-entity-base-refactor
…le' into RJD-1057-remove-functions-forwarded-to-entity-base-refactor
…le' into RJD-1057-remove-functions-forwarded-to-entity-base-refactor
@HansRobo HansRobo reopened this Jan 30, 2025
@HansRobo HansRobo changed the base branch from RJD-1057-remove-functions-forwarded-to-entity-base-middle to master January 30, 2025 07:22
@HansRobo
Copy link
Member

This pull-request was closed automatically, because the target branch was deleted due to merging its pull-request.
So, I reopened this pull-request and replace the target branch with master.

@HansRobo
Copy link
Member

@TauTheLepton Could you fix typos pointed out by spell-check CI?

Signed-off-by: Mateusz Palczuk <[email protected]>
@TauTheLepton
Copy link
Contributor Author

@HansRobo

@TauTheLepton Could you fix typos pointed out by spell-check CI?

Done

Please note, that there is an error in "Documentation" check in CI. I believe it is not caused by my changes.

@TauTheLepton TauTheLepton added the bump minor If this pull request merged, bump minor version of the scenario_simulator_v2 label Feb 11, 2025
@TauTheLepton TauTheLepton changed the title [tmp] RJD-1057 (4/5): Remove non-API member functions: EntityManager’s member functions forwarded to EntityBase (2/2) RJD-1057 (4/5): Remove non-API member functions: EntityManager’s member functions forwarded to EntityBase (2/2) Feb 11, 2025
TauTheLepton and others added 5 commits February 11, 2025 16:21
Signed-off-by: Mateusz Palczuk <[email protected]>
…ed-to-entity-base-refactor' into RJD-1057-remove-functions-forwarded-to-entity-base-refactor

Signed-off-by: Mateusz Palczuk <[email protected]>
@TauTheLepton TauTheLepton added bump major If this pull request merged, bump major version of the scenario_simulator_v2 and removed bump minor If this pull request merged, bump minor version of the scenario_simulator_v2 labels Feb 12, 2025
@TauTheLepton TauTheLepton marked this pull request as ready for review February 12, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump major If this pull request merged, bump major version of the scenario_simulator_v2 refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants