Skip to content

Commit d0e88ad

Browse files
authored
Merge pull request #1734 from tier4/fix/multiple_states_on_phase
2 parents aa03f84 + ce8c4bc commit d0e88ad

7 files changed

Lines changed: 95 additions & 6 deletions

File tree

openscenario/openscenario_interpreter/include/openscenario_interpreter/simulator_core.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,20 @@ class SimulatorCore
749749
std::forward<decltype(xs)>(xs)...);
750750
}
751751

752+
template <typename... Ts>
753+
static auto clearConventionalTrafficLightsState(Ts &&... xs) -> decltype(auto)
754+
{
755+
return core->getConventionalTrafficLights()->clearTrafficLightsState(
756+
std::forward<decltype(xs)>(xs)...);
757+
}
758+
759+
template <typename... Ts>
760+
static auto addConventionalTrafficLightsState(Ts &&... xs) -> decltype(auto)
761+
{
762+
return core->getConventionalTrafficLights()->addTrafficLightsState(
763+
std::forward<decltype(xs)>(xs)...);
764+
}
765+
752766
template <typename... Ts>
753767
static auto setConventionalTrafficLightConfidence(Ts &&... xs) -> decltype(auto)
754768
{
@@ -782,6 +796,19 @@ class SimulatorCore
782796
return core->getV2ITrafficLights()->setTrafficLightsState(std::forward<decltype(xs)>(xs)...);
783797
}
784798

799+
template <typename... Ts>
800+
static auto clearV2ITrafficLightsState(Ts &&... xs) -> decltype(auto)
801+
{
802+
return core->getV2ITrafficLights()->clearTrafficLightsState(
803+
std::forward<decltype(xs)>(xs)...);
804+
}
805+
806+
template <typename... Ts>
807+
static auto addV2ITrafficLightsState(Ts &&... xs) -> decltype(auto)
808+
{
809+
return core->getV2ITrafficLights()->addTrafficLightsState(std::forward<decltype(xs)>(xs)...);
810+
}
811+
785812
template <typename... Ts>
786813
static auto resetV2ITrafficLightPublishRate(Ts &&... xs) -> decltype(auto)
787814
{

openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/phase.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__PHASE_HPP_
1616
#define OPENSCENARIO_INTERPRETER__SYNTAX__PHASE_HPP_
1717

18+
#include <map>
1819
#include <openscenario_interpreter/scope.hpp>
20+
#include <openscenario_interpreter/simulator_core.hpp>
1921
#include <openscenario_interpreter/syntax/double.hpp>
2022
#include <openscenario_interpreter/syntax/string.hpp>
2123
#include <openscenario_interpreter/syntax/traffic_signal_state.hpp>
2224
#include <pugixml.hpp>
25+
#include <vector>
2326

2427
namespace openscenario_interpreter
2528
{
@@ -36,7 +39,7 @@ inline namespace syntax
3639
* </xsd:complexType>
3740
*
3841
* -------------------------------------------------------------------------- */
39-
struct Phase
42+
struct Phase : private SimulatorCore::NonStandardOperation
4043
{
4144
// Name of the phase.
4245
const String name;
@@ -55,6 +58,12 @@ struct Phase
5558
* ------------------------------------------------------------------------ */
5659
const std::list<TrafficSignalState> traffic_signal_states;
5760

61+
// Grouped traffic signal states by their ID and type
62+
const std::map<
63+
std::pair<lanelet::Id, TrafficSignalState::TrafficSignalType>,
64+
std::vector<const TrafficSignalState *>>
65+
grouped_states;
66+
5867
explicit Phase(const pugi::xml_node &, Scope &);
5968

6069
auto evaluate() const -> Object;

openscenario/openscenario_interpreter/src/syntax/phase.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,36 @@ inline namespace syntax
2323
Phase::Phase(const pugi::xml_node & node, Scope & scope)
2424
: name(readAttribute<String>("name", node, scope)),
2525
duration(readAttribute<Double>("duration", node, scope, Double::infinity())),
26-
traffic_signal_states(readElements<TrafficSignalState, 0>("TrafficSignalState", node, scope))
26+
traffic_signal_states(readElements<TrafficSignalState, 0>("TrafficSignalState", node, scope)),
27+
grouped_states([this]() {
28+
std::map<
29+
std::pair<lanelet::Id, TrafficSignalState::TrafficSignalType>,
30+
std::vector<const TrafficSignalState *>>
31+
groups;
32+
for (const auto & traffic_signal_state : traffic_signal_states) {
33+
auto key =
34+
std::make_pair(traffic_signal_state.id(), traffic_signal_state.trafficSignalType());
35+
groups[key].push_back(&traffic_signal_state);
36+
}
37+
return groups;
38+
}())
2739
{
2840
}
2941

3042
auto Phase::evaluate() const -> Object
3143
{
32-
for (auto && traffic_signal_state : traffic_signal_states) {
33-
traffic_signal_state.evaluate();
44+
for (const auto & [key, states] : grouped_states) {
45+
const auto & [id, type] = key;
46+
// clear states before adding
47+
if (type == TrafficSignalState::TrafficSignalType::conventional) {
48+
clearConventionalTrafficLightsState(id);
49+
} else if (type == TrafficSignalState::TrafficSignalType::v2i) {
50+
clearV2ITrafficLightsState(id);
51+
}
52+
// then add states
53+
for (size_t i = 0; i < states.size(); ++i) {
54+
states[i]->evaluate();
55+
}
3456
}
3557

3658
return unspecified;

openscenario/openscenario_interpreter/src/syntax/traffic_signal_state.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ auto TrafficSignalState::evaluate() const -> Object
4747
{
4848
switch (trafficSignalType()) {
4949
case TrafficSignalType::conventional:
50-
setConventionalTrafficLightsState(id(), state);
50+
addConventionalTrafficLightsState(id(), state);
5151
break;
5252
case TrafficSignalType::v2i:
53-
setV2ITrafficLightsState(id(), state);
53+
addV2ITrafficLightsState(id(), state);
5454
break;
5555
default:
5656
throw Error("Unknown traffic signal type has set to TrafficSignalState");

simulation/traffic_simulator/include/traffic_simulator/traffic_lights/traffic_lights_base.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class TrafficLightsBase
6767

6868
auto setTrafficLightsState(const lanelet::Id lanelet_id, const std::string & state) -> void;
6969

70+
auto clearTrafficLightsState(const lanelet::Id lanelet_id) -> void;
71+
72+
auto addTrafficLightsState(const lanelet::Id lanelet_id, const std::string & state) -> void;
73+
7074
auto setTrafficLightsConfidence(const lanelet::Id lanelet_id, const double confidence) -> void;
7175

7276
auto getTrafficLightsComposedState(const lanelet::Id lanelet_id) -> std::string;

simulation/traffic_simulator/src/traffic_lights/traffic_lights_base.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,22 @@ auto TrafficLightsBase::setTrafficLightsColor(
6767

6868
auto TrafficLightsBase::setTrafficLightsState(
6969
const lanelet::Id lanelet_id, const std::string & state) -> void
70+
{
71+
clearTrafficLightsState(lanelet_id);
72+
addTrafficLightsState(lanelet_id, state);
73+
}
74+
75+
auto TrafficLightsBase::clearTrafficLightsState(const lanelet::Id lanelet_id) -> void
7076
{
7177
for (const auto & traffic_light : getTrafficLights(lanelet_id)) {
7278
traffic_light.get().clear();
79+
}
80+
}
81+
82+
auto TrafficLightsBase::addTrafficLightsState(
83+
const lanelet::Id lanelet_id, const std::string & state) -> void
84+
{
85+
for (const auto & traffic_light : getTrafficLights(lanelet_id)) {
7386
traffic_light.get().set(state);
7487
}
7588
}

simulation/traffic_simulator/test/src/traffic_lights/test_traffic_lights_internal_common.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,17 @@ TYPED_TEST(TrafficLightsInternalTest, generateAutowarePerceptionTrafficLightGrou
526526
EXPECT_NEAR(msg.traffic_light_groups[0].elements[1].confidence, expected_confidence, eps);
527527
}
528528
#endif // __has_include(<autoware_perception_msgs/msg/traffic_light_group_array.hpp>)
529+
530+
TYPED_TEST(TrafficLightsInternalTest, addAndClearTrafficLightsState)
531+
{
532+
this->lights->addTrafficLightsState(this->id, "green solidOn circle");
533+
this->lights->addTrafficLightsState(this->id, "red solidOn circle");
534+
535+
const auto state_before_clear = this->lights->getTrafficLightsComposedState(this->id);
536+
EXPECT_TRUE(state_before_clear.find("green") != std::string::npos);
537+
EXPECT_TRUE(state_before_clear.find("red") != std::string::npos);
538+
539+
this->lights->clearTrafficLightsState(this->id);
540+
const auto state_after_clear = this->lights->getTrafficLightsComposedState(this->id);
541+
EXPECT_TRUE(state_after_clear.empty());
542+
}

0 commit comments

Comments
 (0)