Skip to content

Commit e066971

Browse files
add Stamped blackboard (#805)
* improve blackboard exporting * add timestamp and sequence to blackboard entries * new decorators using the entry sequence, added * updated interface with Expected * Apply suggestions from code review
1 parent 3cc7b4e commit e066971

File tree

13 files changed

+489
-60
lines changed

13 files changed

+489
-60
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ list(APPEND BT_SOURCE
103103
src/decorators/repeat_node.cpp
104104
src/decorators/retry_node.cpp
105105
src/decorators/timeout_node.cpp
106+
src/decorators/skip_unless_updated.cpp
106107
src/decorators/subtree_node.cpp
108+
src/decorators/wait_update.cpp
107109

108110
src/controls/if_then_else_node.cpp
109111
src/controls/fallback_node.cpp

include/behaviortree_cpp/basic_types.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ using Optional = nonstd::expected<T, std::string>;
332332
* */
333333
using Result = Expected<std::monostate>;
334334

335+
struct Timestamp
336+
{
337+
// Number being incremented every time a new value is written
338+
uint64_t seq = 0;
339+
// Last update time. Nanoseconds since epoch
340+
std::chrono::nanoseconds time = std::chrono::nanoseconds(0);
341+
};
342+
335343
[[nodiscard]] bool IsAllowedPortName(StringView str);
336344

337345
class TypeInfo

include/behaviortree_cpp/behavior_tree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "behaviortree_cpp/decorators/run_once_node.h"
3434
#include "behaviortree_cpp/decorators/subtree_node.h"
3535
#include "behaviortree_cpp/decorators/loop_node.h"
36+
#include "behaviortree_cpp/decorators/skip_unless_updated.h"
37+
#include "behaviortree_cpp/decorators/wait_update.h"
3638

3739
#include "behaviortree_cpp/actions/always_success_node.h"
3840
#include "behaviortree_cpp/actions/always_failure_node.h"

include/behaviortree_cpp/blackboard.h

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ namespace BT
1818
/// with a locked mutex as long as the object is in scope
1919
using AnyPtrLocked = LockedPtr<Any>;
2020

21+
template <typename T>
22+
struct StampedValue
23+
{
24+
T value;
25+
Timestamp stamp;
26+
};
27+
2128
/**
2229
* @brief The Blackboard is the mechanism used by BehaviorTrees to exchange
2330
* typed data.
@@ -40,8 +47,14 @@ class Blackboard
4047
StringConverter string_converter;
4148
mutable std::mutex entry_mutex;
4249

50+
uint64_t sequence_id = 0;
51+
// timestamp since epoch
52+
std::chrono::nanoseconds stamp = std::chrono::nanoseconds{ 0 };
53+
4354
Entry(const TypeInfo& _info) : info(_info)
4455
{}
56+
57+
Entry& operator=(const Entry& other);
4558
};
4659

4760
/** Use this static method to create an instance of the BlackBoard
@@ -75,12 +88,18 @@ class Blackboard
7588
template <typename T>
7689
[[nodiscard]] bool get(const std::string& key, T& value) const;
7790

91+
template <typename T>
92+
[[nodiscard]] Expected<Timestamp> getStamped(const std::string& key, T& value) const;
93+
7894
/**
7995
* Version of get() that throws if it fails.
8096
*/
8197
template <typename T>
8298
[[nodiscard]] T get(const std::string& key) const;
8399

100+
template <typename T>
101+
[[nodiscard]] Expected<StampedValue<T>> getStamped(const std::string& key) const;
102+
84103
/// Update the entry with the given key
85104
template <typename T>
86105
void set(const std::string& key, const T& value);
@@ -155,10 +174,7 @@ inline T Blackboard::get(const std::string& key) const
155174
}
156175
return any_ref.get()->cast<T>();
157176
}
158-
else
159-
{
160-
throw RuntimeError("Blackboard::get() error. Missing key [", key, "]");
161-
}
177+
throw RuntimeError("Blackboard::get() error. Missing key [", key, "]");
162178
}
163179

164180
inline void Blackboard::unset(const std::string& key)
@@ -203,6 +219,8 @@ inline void Blackboard::set(const std::string& key, const T& value)
203219
lock.lock();
204220

205221
entry->value = new_value;
222+
entry->sequence_id++;
223+
entry->stamp = std::chrono::steady_clock::now().time_since_epoch();
206224
}
207225
else
208226
{
@@ -212,14 +230,15 @@ inline void Blackboard::set(const std::string& key, const T& value)
212230
std::scoped_lock scoped_lock(entry.entry_mutex);
213231

214232
Any& previous_any = entry.value;
215-
216233
Any new_value(value);
217234

218235
// special case: entry exists but it is not strongly typed... yet
219236
if(!entry.info.isStronglyTyped())
220237
{
221238
// Use the new type to create a new entry that is strongly typed.
222239
entry.info = TypeInfo::Create<T>();
240+
entry.sequence_id++;
241+
entry.stamp = std::chrono::steady_clock::now().time_since_epoch();
223242
previous_any = std::move(new_value);
224243
return;
225244
}
@@ -273,6 +292,8 @@ inline void Blackboard::set(const std::string& key, const T& value)
273292
// copy only if the type is compatible
274293
new_value.copyInto(previous_any);
275294
}
295+
entry.sequence_id++;
296+
entry.stamp = std::chrono::steady_clock::now().time_since_epoch();
276297
}
277298
}
278299

@@ -281,10 +302,47 @@ inline bool Blackboard::get(const std::string& key, T& value) const
281302
{
282303
if(auto any_ref = getAnyLocked(key))
283304
{
305+
if(any_ref.get()->empty())
306+
{
307+
return false;
308+
}
284309
value = any_ref.get()->cast<T>();
285310
return true;
286311
}
287312
return false;
288313
}
289314

315+
template <typename T>
316+
inline Expected<Timestamp> Blackboard::getStamped(const std::string& key, T& value) const
317+
{
318+
if(auto entry = getEntry(key))
319+
{
320+
std::unique_lock lk(entry->entry_mutex);
321+
if(entry->value.empty())
322+
{
323+
return nonstd::make_unexpected(StrCat("Blackboard::getStamped() error. Entry [",
324+
key, "] hasn't been initialized, yet"));
325+
}
326+
value = entry->value.cast<T>();
327+
return Timestamp{ entry->sequence_id, entry->stamp };
328+
}
329+
return nonstd::make_unexpected(
330+
StrCat("Blackboard::getStamped() error. Missing key [", key, "]"));
331+
}
332+
333+
template <typename T>
334+
inline Expected<StampedValue<T>> Blackboard::getStamped(const std::string& key) const
335+
{
336+
StampedValue<T> out;
337+
if(auto res = getStamped<T>(key, out.value))
338+
{
339+
out.stamp = *res;
340+
return out;
341+
}
342+
else
343+
{
344+
return nonstd::make_unexpected(res.error());
345+
}
346+
}
347+
290348
} // namespace BT

include/behaviortree_cpp/bt_factory.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
#ifndef BT_FACTORY_H
1515
#define BT_FACTORY_H
1616

17-
#include <algorithm>
18-
#include <cstring>
1917
#include <filesystem>
2018
#include <functional>
2119
#include <memory>
2220
#include <unordered_map>
2321
#include <set>
24-
#include <utility>
2522
#include <vector>
2623

2724
#include "behaviortree_cpp/contrib/magic_enum.hpp"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright (C) 2024 Davide Faconti - All Rights Reserved
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4+
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
#pragma once
14+
15+
#include "behaviortree_cpp/decorator_node.h"
16+
17+
namespace BT
18+
{
19+
20+
/**
21+
* @brief The SkipUnlessUpdated checks the Timestamp in an entry
22+
* to determine if the value was updated since the last time (true,
23+
* the first time).
24+
*
25+
* If it is, the child will be executed, otherwise SKIPPED is returned.
26+
*/
27+
class SkipUnlessUpdated : public DecoratorNode
28+
{
29+
public:
30+
SkipUnlessUpdated(const std::string& name, const NodeConfig& config);
31+
32+
~SkipUnlessUpdated() override = default;
33+
34+
static PortsList providedPorts()
35+
{
36+
return { InputPort<BT::Any>("entry", "Skip this branch unless the blackboard value "
37+
"was updated") };
38+
}
39+
40+
private:
41+
int64_t sequence_id_ = -1;
42+
std::string entry_key_;
43+
bool still_executing_child_ = false;
44+
45+
NodeStatus tick() override;
46+
47+
void halt() override;
48+
};
49+
50+
} // namespace BT
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Copyright (C) 2024 Davide Faconti - All Rights Reserved
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4+
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
#pragma once
14+
15+
#include "behaviortree_cpp/decorator_node.h"
16+
17+
namespace BT
18+
{
19+
/**
20+
* @brief The WaitValueUpdate checks the Timestamp in an entry
21+
* to determine if the value was updated since the last time (true,
22+
* the first time).
23+
*
24+
* If it is, the child will be executed, otherwise RUNNING is returned.
25+
*/
26+
class WaitValueUpdate : public DecoratorNode
27+
{
28+
public:
29+
WaitValueUpdate(const std::string& name, const NodeConfig& config);
30+
31+
~WaitValueUpdate() override = default;
32+
33+
static PortsList providedPorts()
34+
{
35+
return { InputPort<BT::Any>("entry", "Sleep until the entry in the blackboard is "
36+
"updated") };
37+
}
38+
39+
private:
40+
int64_t sequence_id_ = -1;
41+
std::string entry_key_;
42+
bool still_executing_child_ = false;
43+
44+
NodeStatus tick() override;
45+
46+
void halt() override;
47+
};
48+
49+
} // namespace BT

0 commit comments

Comments
 (0)