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/synchronized action #1177

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
45db78d
Add draft functions for synchronized action
curious-jp Jan 16, 2024
aa28c60
changed the function and made it simple
curious-jp Jan 16, 2024
bdb284d
Implemented a draft of getDistanceToTargetLaneletPose and requestSync…
curious-jp Jan 18, 2024
435e986
fixed revert and errors
curious-jp Jan 18, 2024
cbde06e
error is not fixed but pushing today's implements
curious-jp Jan 18, 2024
bced89e
Fix distance calculation in EntityBase::getDistanceToTargetLaneletPose()
curious-jp Jan 18, 2024
4cf36b8
Fix synchronization issue in EntityBase class
curious-jp Jan 24, 2024
36d4697
implemented atleast functions with no error
curious-jp Jan 24, 2024
4bb9f92
tried to format
curious-jp Jan 24, 2024
59ab9c7
Add requestSynchronize method to API and EntityManager and few bug fix
curious-jp Jan 24, 2024
a42a801
Made draft mock scenarios
curious-jp Jan 24, 2024
ab572de
Disable building of C++ mock scenarios and update requestSynchronize …
curious-jp Jan 29, 2024
0c5fb8b
Refactor synchronization logic and add new API method
curious-jp Feb 1, 2024
c4a673d
途中経過
curious-jp Feb 15, 2024
f18fc68
Update target lanelet poses and velocities
curious-jp Feb 20, 2024
f8f6f3e
Merge branch 'feature/synchronized_action' of https://github.com/tier…
curious-jp Feb 20, 2024
6e4af62
Update entity synchronization logic to consider acceleration limit
curious-jp Feb 22, 2024
01abacc
Update synchronized action behavior
curious-jp Mar 28, 2024
c5436e2
Update entity_base.hpp and synchronized_action.cpp
curious-jp Mar 29, 2024
0f089d5
Merge commit '253fa785573217ad3a6bde882724a9e35a0c99ed' into feature/…
curious-jp Mar 29, 2024
be53d79
Merge commit 'ada77d59ffd6545105e40e88e4ad50050062a3d6' into feature/…
curious-jp Apr 10, 2024
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 mock/cpp_mock_scenarios/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ if(BUILD_CPP_MOCK_SCENARIOS)
add_subdirectory(src/pedestrian)
add_subdirectory(src/random_scenario)
add_subdirectory(src/speed_planning)
add_subdirectory(src/synchronized_action)
endif()

install(
Expand Down
14 changes: 14 additions & 0 deletions mock/cpp_mock_scenarios/src/synchronized_action/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ament_auto_add_executable(synchronized_action
synchronized_action.cpp
)
target_link_libraries(synchronized_action cpp_scenario_node)

install(TARGETS
synchronized_action
DESTINATION lib/cpp_mock_scenarios
)

if(BUILD_TESTING)
include(../../cmake/add_cpp_mock_scenario_test.cmake)
add_cpp_mock_scenario_test(${PROJECT_NAME} "synchronized_action" "15.0")
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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 <quaternion_operation/quaternion_operation.h>

#include <ament_index_cpp/get_package_share_directory.hpp>
#include <cpp_mock_scenarios/catalogs.hpp>
#include <cpp_mock_scenarios/cpp_scenario_node.hpp>
#include <rclcpp/rclcpp.hpp>
#include <traffic_simulator/api/api.hpp>
#include <traffic_simulator_msgs/msg/behavior_parameter.hpp>

// headers in STL
#include <memory>
#include <string>
#include <vector>

namespace cpp_mock_scenarios
{
class SynchronizedAction : public cpp_mock_scenarios::CppScenarioNode
{
public:
explicit SynchronizedAction(const rclcpp::NodeOptions & option)
: cpp_mock_scenarios::CppScenarioNode(
"synchronized_action",
ament_index_cpp::get_package_share_directory("kashiwanoha_map") + "/map", "lanelet2_map.osm",
__FILE__, false, option)
{
start();
}

private:
bool requested = false;
const traffic_simulator::CanonicalizedLaneletPose ego_target =
getSampleLaneletPose(traffic_simulator::helper::constructLaneletPose(34510, 0, 0, 0, 0, 0));
const traffic_simulator::CanonicalizedLaneletPose npc_target =
getSampleLaneletPose(traffic_simulator::helper::constructLaneletPose(34495, 15, 0, 0, 0, 0));

void onUpdate() override
{
// SUCCESS
// check if npc is in the target lanelet when ego is in the target lanelet and npc is stopped
if (
api_.requestSynchronize("npc", ego_target, npc_target, 0.2) &&
// api_.getIfArrivedToTargetLaneletPose("ego", ego_target, 0.2) &&
// api_.getCurrentTwist("npc").linear.x == 0.0) {
true) {
stop(cpp_mock_scenarios::Result::SUCCESS);
}

// FAILURES
if (api_.getCurrentTime() >= 20.0) {
stop(cpp_mock_scenarios::Result::FAILURE);
}
if (api_.checkCollision("ego", "npc")) {
stop(cpp_mock_scenarios::Result::FAILURE);
}
}
void onInitialize() override
{
api_.spawn(
"ego",
api_.canonicalize(traffic_simulator::helper::constructLaneletPose(34513, 15, 0, 0, 0, 0)),
getVehicleParameters());
api_.setLinearVelocity("ego", 5);
// api_.requestSpeedChange("ego", 5, true);
// api_.requestLaneChange("ego", 34513);

api_.spawn(
"npc",
api_.canonicalize(traffic_simulator::helper::constructLaneletPose(34564, 5, 0, 0, 0, 0)),
getVehicleParameters());
api_.setLinearVelocity("npc", 5);
}

auto getSampleLaneletPose(const traffic_simulator::LaneletPose & lanelet_pose)
-> traffic_simulator::CanonicalizedLaneletPose
{
return api_.canonicalize(lanelet_pose);
}
};
} // namespace cpp_mock_scenarios

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::NodeOptions options;
auto component = std::make_shared<cpp_mock_scenarios::SynchronizedAction>(options);
rclcpp::spin(component);
rclcpp::shutdown();
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class API
FORWARD_TO_ENTITY_MANAGER(getTraveledDistance);
FORWARD_TO_ENTITY_MANAGER(getV2ITrafficLight);
FORWARD_TO_ENTITY_MANAGER(getV2ITrafficLights);
FORWARD_TO_ENTITY_MANAGER(getIfArrivedToTargetLaneletPose);
FORWARD_TO_ENTITY_MANAGER(isEgoSpawned);
FORWARD_TO_ENTITY_MANAGER(isInLanelet);
FORWARD_TO_ENTITY_MANAGER(isNpcLogicStarted);
Expand All @@ -330,6 +331,7 @@ class API
FORWARD_TO_ENTITY_MANAGER(requestAcquirePosition);
FORWARD_TO_ENTITY_MANAGER(requestAssignRoute);
FORWARD_TO_ENTITY_MANAGER(requestSpeedChange);
FORWARD_TO_ENTITY_MANAGER(requestSynchronize);
FORWARD_TO_ENTITY_MANAGER(requestWalkStraight);
FORWARD_TO_ENTITY_MANAGER(resetConventionalTrafficLightPublishRate);
FORWARD_TO_ENTITY_MANAGER(resetV2ITrafficLightPublishRate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ class EntityBase

/* */ auto updateTraveledDistance(const double step_time) -> double;

/* */ auto getDistanceToTargetLaneletPose(const CanonicalizedLaneletPose & target_lanelet_pose)
-> std::optional<double>;

/* */ auto getIfArrivedToTargetLaneletPose(
const CanonicalizedLaneletPose & target_lanelet_pose, const double threshold) -> bool;

/* */ auto requestSynchronize(
const CanonicalizedLaneletPose & ego_target, const CanonicalizedLaneletPose & entity_target,
const double threshold) -> bool;

virtual auto fillLaneletPose(CanonicalizedEntityStatus & status, bool include_crosswalk)
-> void final;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class EntityManager
FORWARD_TO_ENTITY(getDistanceToRightLaneBound, const);
FORWARD_TO_ENTITY(getEntityStatusBeforeUpdate, const);
FORWARD_TO_ENTITY(getEntityType, const);
FORWARD_TO_ENTITY(getIfArrivedToTargetLaneletPose,);
FORWARD_TO_ENTITY(fillLaneletPose, const);
FORWARD_TO_ENTITY(getLaneletPose, const);
FORWARD_TO_ENTITY(getLinearJerk, const);
Expand All @@ -311,6 +312,7 @@ class EntityManager
FORWARD_TO_ENTITY(requestAssignRoute, );
FORWARD_TO_ENTITY(requestFollowTrajectory, );
FORWARD_TO_ENTITY(requestLaneChange, );
FORWARD_TO_ENTITY(requestSynchronize, );
FORWARD_TO_ENTITY(requestWalkStraight, );
FORWARD_TO_ENTITY(activateOutOfRangeJob, );
FORWARD_TO_ENTITY(setAccelerationLimit, );
Expand Down
88 changes: 88 additions & 0 deletions simulation/traffic_simulator/src/entity/entity_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,5 +818,93 @@ auto EntityBase::updateTraveledDistance(const double step_time) -> double
return traveled_distance_;
}

auto EntityBase::getDistanceToTargetLaneletPose(
const CanonicalizedLaneletPose & target_lanelet_pose) -> std::optional<double>
{
constexpr double matching_distance = 100.0; // may be better to use a parameter
const auto entity_lanelet_pose_ = getLaneletPose(matching_distance);
const auto target_lanelet_pose_ = target_lanelet_pose;

// check if the entity is on the lanelet
if (entity_lanelet_pose_) {
const auto entity_distance_to_target = hdmap_utils_ptr_->getLongitudinalDistance(
static_cast<LaneletPose>(entity_lanelet_pose_.value()),
static_cast<LaneletPose>(target_lanelet_pose_));

return entity_distance_to_target;
}

// may be give error here too?
return std::nullopt;
}

auto EntityBase::getIfArrivedToTargetLaneletPose(
const CanonicalizedLaneletPose & target_lanelet_pose, const double threshold) -> bool
{
if (!getDistanceToTargetLaneletPose(target_lanelet_pose).has_value()) {
// error here
}
const auto entity_lanelet_pose_ = getLaneletPose();
const auto target_lanelet_pose_ = target_lanelet_pose;

const auto entity_distance_to_target = hdmap_utils_ptr_->getLongitudinalDistance(
static_cast<LaneletPose>(entity_lanelet_pose_.value()),
static_cast<LaneletPose>(target_lanelet_pose_));

if (entity_distance_to_target <= threshold) {
return true;
}
return false;
}

/** WARNING***************
* This should not be called from ego entity
*/
hakuturu583 marked this conversation as resolved.
Show resolved Hide resolved
auto EntityBase::requestSynchronize(
const CanonicalizedLaneletPose & ego_target, const CanonicalizedLaneletPose & entity_target,
const double threshold) -> bool
{
if (getIfArrivedToTargetLaneletPose(entity_target, threshold)) {
return true;
}

job_list_.append(
[this, ego_target, entity_target](double) -> bool {
// WARNING****************
// not checking job_duration_ at all
const auto entity_distance = getDistanceToTargetLaneletPose(entity_target);
const auto ego_distance = this->hdmap_utils_ptr_->getLongitudinalDistance(
static_cast<LaneletPose>(other_status_.find("ego")->second.getLaneletPose()),
static_cast<LaneletPose>(ego_target));

if (!entity_distance.has_value() ){
THROW_SEMANTIC_ERROR(
"Failed to get entity's distance to target lanelet pose. Please check entity distance exists.");
}
if (!ego_distance.has_value() ){
THROW_SEMANTIC_ERROR(
"Failed to get ego's distance to target lanelet pose. Please check ego distance exists.");
}

const auto ego_velocity = other_status_.find("ego")->second.getTwist().linear.x;
// be better to use acceleration,jerk to estimate the arrival time

RCLCPP_ERROR(rclcpp::get_logger("simulation"), "#####################");
// estimate ego's arrival time to the target point
// can estimate it more precisely by using accel,jerk
const auto ego_arrival_time = (ego_velocity != 0) ? ego_distance.value() / ego_velocity : 0;

// calculate the speed of entity to synchronize with ego
// really just a simple example, kind of like a joke
const auto entity_velocity_to_synchronize = entity_distance.value() / ego_arrival_time;

this->requestSpeedChange(entity_velocity_to_synchronize, true);
return true;
},
// after this im not sure it is correct, just an draft
[this]() {}, job::Type::LINEAR_ACCELERATION, true, job::Event::POST_UPDATE);
return false;
}

} // namespace entity
} // namespace traffic_simulator
Loading