Skip to content

Commit 9fe4406

Browse files
committed
StateAndMode improvements
Signed-off-by: Arne Nordmann <arne.nordmann@de.bosch.com>
1 parent 11a85d2 commit 9fe4406

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

system_modes/include/system_modes/mode_impl.hpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
#include <rclcpp/node_interfaces/node_parameters.hpp>
1818
#include <rclcpp/parameter_map.hpp>
1919

20+
#include <lifecycle_msgs/msg/state.hpp>
21+
2022
#include <map>
2123
#include <mutex>
2224
#include <vector>
2325
#include <string>
2426
#include <memory>
2527
#include <utility>
2628

29+
using lifecycle_msgs::msg::State;
30+
2731
namespace system_modes
2832
{
2933

@@ -49,17 +53,22 @@ struct StateAndMode
4953
mode = newmode;
5054
}
5155

52-
bool operator!=(const StateAndMode & cmp) const
56+
bool operator==(const StateAndMode & cmp) const
5357
{
54-
return cmp.state == state && // same state
55-
(cmp.mode.compare(mode) == 0 || // same mode
58+
if (cmp.state != state) {
59+
return false;
60+
} else if (cmp.state != State::PRIMARY_STATE_ACTIVE) {
61+
return true;
62+
}
63+
64+
return (cmp.mode.compare(mode) == 0 || // same mode
5665
(cmp.mode.compare(DEFAULT_MODE) == 0 && mode.empty()) || // we consider empty and
5766
(mode.compare(DEFAULT_MODE) == 0 && cmp.mode.empty())); // DEFAULT_MODE the same
5867
}
5968

60-
bool operator==(const StateAndMode & cmp) const
69+
bool operator!=(const StateAndMode & cmp) const
6170
{
62-
return !(*this != cmp);
71+
return !(*this == cmp);
6372
}
6473

6574
void from_string(const std::string & sam) {
@@ -73,9 +82,8 @@ struct StateAndMode
7382
}
7483
}
7584

76-
std::string as_string() const
77-
{
78-
if (mode.empty()) {
85+
std::string as_string() const {
86+
if (state != State::PRIMARY_STATE_ACTIVE || mode.empty()) {
7987
return state_label_(state);
8088
}
8189
return state_label_(state) + "." + mode;

system_modes/src/system_modes/mode_manager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ using system_modes::msg::ModeEvent;
5050
using system_modes::srv::ChangeMode;
5151
using system_modes::srv::GetAvailableModes;
5252

53+
using namespace std::chrono_literals;
54+
5355
namespace system_modes
5456
{
5557

@@ -327,7 +329,6 @@ ModeManager::on_get_available_modes(
327329
}
328330
}
329331

330-
331332
bool
332333
ModeManager::change_state(
333334
const std::string & node_name,

system_modes/test/test_state_and_mode_struct.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,20 @@ TEST_F(TestStateAndMode, comparison) {
6060
EXPECT_NE(active, active_foo);
6161
EXPECT_NE(active_default, active_foo);
6262
EXPECT_NE(active_default, inactive);
63+
64+
active_foo.state = State::PRIMARY_STATE_INACTIVE;
65+
EXPECT_EQ(inactive, active_foo);
6366
}
6467
}
6568

6669
TEST_F(TestStateAndMode, string_getter) {
6770
{
71+
EXPECT_EQ("inactive"s, inactive.as_string());
6872
EXPECT_EQ("active"s, active.as_string());
6973
EXPECT_EQ("active.__DEFAULT__"s, active_default.as_string());
7074
EXPECT_EQ("active.FOO"s, active_foo.as_string());
71-
EXPECT_EQ("inactive"s, inactive.as_string());
75+
active_foo.state = State::PRIMARY_STATE_INACTIVE;
76+
EXPECT_EQ("inactive"s, active_foo.as_string());
7277
}
7378
}
7479

0 commit comments

Comments
 (0)