Skip to content

Commit 64b8613

Browse files
committed
Groot2 interface (#528)
* refactored groot2 interface * protocol updated
1 parent 2c92bf1 commit 64b8613

File tree

3 files changed

+176
-141
lines changed

3 files changed

+176
-141
lines changed

include/behaviortree_cpp/loggers/groot2_protocol.h

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ enum RequestType : uint8_t
3333
// retrieve the valus in a set of blackboards
3434
BLACKBOARD = 'B',
3535

36-
// Groot requests the insertion of a breakpoint
37-
BREAKPOINT_INSERT = 'I',
38-
// Groot requests to remove a breakpoint
39-
BREAKPOINT_REMOVE = 'R',
36+
// Groot requests the insertion of a hook
37+
HOOK_INSERT = 'I',
38+
// Groot requests to remove a hook
39+
HOOK_REMOVE = 'R',
4040
// Notify Groot that we reached a breakpoint
4141
BREAKPOINT_REACHED = 'N',
4242
// Groot will unlock a breakpoint
4343
BREAKPOINT_UNLOCK = 'U',
44-
// receive the existing breakpoints in JSON format
45-
BREAKPOINTS_DUMP = 'D',
44+
// receive the existing hooks in JSON format
45+
HOOKS_DUMP = 'D',
4646

47-
// Remove all breakpoints. To be done before disconnecting Groot
48-
REMOVE_ALL_BREAKPOINTS = 'A',
47+
// Remove all hooks. To be done before disconnecting Groot
48+
REMOVE_ALL_HOOKS = 'A',
4949

50-
DISABLE_ALL_BREAKPOINTS = 'X',
50+
DISABLE_ALL_HOOKS = 'X',
5151

5252
UNDEFINED = 0,
5353
};
@@ -60,13 +60,13 @@ inline const char* ToString(const RequestType& type)
6060
case RequestType::STATUS: return "status";
6161
case RequestType::BLACKBOARD: return "blackboard";
6262

63-
case RequestType::BREAKPOINT_INSERT: return "breakpoint_insert";
64-
case RequestType::BREAKPOINT_REMOVE: return "breakpoint_remove";
63+
case RequestType::HOOK_INSERT: return "hook_insert";
64+
case RequestType::HOOK_REMOVE: return "hook_remove";
6565
case RequestType::BREAKPOINT_REACHED: return "breakpoint_reached";
6666
case RequestType::BREAKPOINT_UNLOCK: return "breakpoint_unlock";
67-
case RequestType::REMOVE_ALL_BREAKPOINTS: return "breakpoint_remove_all";
68-
case RequestType::BREAKPOINTS_DUMP: return "breakpoints_dump";
69-
case RequestType::DISABLE_ALL_BREAKPOINTS: return "disable_breakpoints";
67+
case RequestType::REMOVE_ALL_HOOKS: return "hooks_remove_all";
68+
case RequestType::HOOKS_DUMP: return "hooks_dump";
69+
case RequestType::DISABLE_ALL_HOOKS: return "disable_hooks";
7070

7171
case RequestType::UNDEFINED: return "undefined";
7272
}
@@ -182,17 +182,29 @@ inline ReplyHeader DeserializeReplyHeader(const std::string& buffer)
182182
return header;
183183
}
184184

185-
struct Breakpoint
185+
struct Hook
186186
{
187-
using Ptr = std::shared_ptr<Breakpoint>;
187+
using Ptr = std::shared_ptr<Hook>;
188188

189189
// used to enable/disable the breakpoint
190190
bool enabled = true;
191191

192+
enum class Position {
193+
PRE = 0,
194+
POST = 1
195+
};
196+
197+
Position position = Position::PRE;
198+
192199
uint16_t node_uid = 0;
193200

194-
// interactive breakpoints are unblucked using unlockBreakpoint()
195-
bool is_interactive = true;
201+
enum class Mode {
202+
BREAKPOINT = 0,
203+
REPLACE = 1
204+
};
205+
206+
// interactive breakpoints are unblocked using unlockBreakpoint()
207+
Mode mode = Mode::BREAKPOINT;
196208

197209
// used by interactive breakpoints to wait for unlocking
198210
std::condition_variable wakeup;
@@ -210,21 +222,24 @@ struct Breakpoint
210222
};
211223

212224

213-
void to_json(nlohmann::json& js, const Breakpoint& bp) {
225+
void to_json(nlohmann::json& js, const Hook& bp) {
214226
js = nlohmann::json {
215227
{"enabled", bp.enabled},
216228
{"uid", bp.node_uid},
217-
{"interactive", bp.is_interactive},
229+
{"mode", int(bp.mode)},
218230
{"once", bp.remove_when_done},
219-
{"desired_status", toStr(bp.desired_status)}
231+
{"desired_status", toStr(bp.desired_status)},
232+
{"position", int(bp.position)}
220233
};
221234
}
222235

223-
void from_json(const nlohmann::json& js, Breakpoint& bp) {
236+
void from_json(const nlohmann::json& js, Hook& bp) {
224237
js.at("enabled").get_to(bp.enabled);
225238
js.at("uid").get_to(bp.node_uid);
226-
js.at("interactive").get_to(bp.is_interactive);
227239
js.at("once").get_to(bp.remove_when_done);
240+
bp. mode = static_cast<Hook::Mode>(js.at("mode").get<int>());
241+
bp.position = static_cast<Hook::Position>(js.at("position").get<int>());
242+
228243
const std::string desired_value = js.at("desired_status").get<std::string>();
229244
bp.desired_status = convertFromString<NodeStatus>(desired_value);
230245
}

include/behaviortree_cpp/loggers/groot2_publisher.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Groot2Publisher : public StatusChangeLogger
1313
static std::mutex used_ports_mutex;
1414
static std::set<unsigned> used_ports;
1515

16+
using Position = Monitor::Hook::Position;
17+
1618
public:
1719
Groot2Publisher(const BT::Tree& tree, unsigned server_port = 1667);
1820

@@ -35,15 +37,15 @@ class Groot2Publisher : public StatusChangeLogger
3537

3638
std::vector<uint8_t> generateBlackboardsDump(const std::string& bb_list);
3739

38-
bool insertBreakpoint(Monitor::Breakpoint::Ptr breakpoint);
40+
bool insertHook(Monitor::Hook::Ptr breakpoint);
3941

40-
bool unlockBreakpoint(uint16_t node_uid, NodeStatus result, bool remove);
42+
bool unlockBreakpoint(Position pos, uint16_t node_uid, NodeStatus result, bool remove);
4143

42-
bool removeBreakpoint(uint16_t node_uid);
44+
bool removeHook(Position pos, uint16_t node_uid);
4345

44-
void removeAllBreakpoints();
46+
void removeAllHooks();
4547

46-
Monitor::Breakpoint::Ptr getBreakpoint(uint16_t node_uid);
48+
Monitor::Hook::Ptr getHook(Position pos, uint16_t node_uid);
4749

4850
unsigned server_port_ = 0;
4951
std::string server_address_;
@@ -63,17 +65,17 @@ class Groot2Publisher : public StatusChangeLogger
6365
std::unordered_map<std::string, std::weak_ptr<BT::Tree::Subtree>> subtrees_;
6466
std::unordered_map<uint16_t, std::weak_ptr<BT::TreeNode>> nodes_by_uid_;
6567

66-
std::mutex breakpoints_map_mutex_;
67-
std::unordered_map<uint16_t, Monitor::Breakpoint::Ptr> pre_breakpoints_;
68-
68+
std::mutex hooks_map_mutex_;
69+
std::unordered_map<uint16_t, Monitor::Hook::Ptr> pre_hooks_;
70+
std::unordered_map<uint16_t, Monitor::Hook::Ptr> post_hooks_;
6971

7072
std::chrono::system_clock::time_point last_heartbeat_;
7173

7274
std::thread heartbeat_thread_;
7375

7476
struct Pimpl;
7577
Pimpl* zmq_;
76-
void enableAllBreakpoints(bool enable);
78+
void enableAllHooks(bool enable);
7779
};
7880
} // namespace BT
7981

0 commit comments

Comments
 (0)