Skip to content

Commit ba230f7

Browse files
Merge remote-tracking branch 'upstream/stamped_blackboard'
2 parents a048538 + 3a9784f commit ba230f7

16 files changed

+546
-116
lines changed

CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ list(APPEND BT_SOURCE
104104
src/decorators/repeat_node.cpp
105105
src/decorators/retry_node.cpp
106106
src/decorators/timeout_node.cpp
107+
src/decorators/skip_unless_updated.cpp
107108
src/decorators/subtree_node.cpp
109+
src/decorators/wait_update.cpp
108110

109111
src/controls/if_then_else_node.cpp
110112
src/controls/fallback_node.cpp
@@ -149,7 +151,8 @@ endif()
149151

150152
if (BTCPP_SHARED_LIBS)
151153
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
152-
add_library(${BTCPP_LIBRARY} SHARED ${BT_SOURCE})
154+
add_library(${BTCPP_LIBRARY} SHARED ${BT_SOURCE}
155+
src/decorators/wait_update.cpp)
153156
else()
154157
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
155158
add_library(${BTCPP_LIBRARY} STATIC ${BT_SOURCE})

include/behaviortree_cpp/basic_types.h

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ using StringView = std::string_view;
6161

6262
bool StartWith(StringView str, StringView prefix);
6363

64+
bool StartWith(StringView str, char prefix);
65+
6466
// vector of key/value pairs
6567
using KeyValueVector = std::vector<std::pair<std::string, std::string>>;
6668

@@ -331,6 +333,14 @@ using Optional = nonstd::expected<T, std::string>;
331333
* */
332334
using Result = Expected<std::monostate>;
333335

336+
struct Timestamp
337+
{
338+
uint64_t seq = 0;
339+
std::chrono::nanoseconds stamp = std::chrono::nanoseconds(0);
340+
};
341+
342+
using ResultStamped = Expected<Timestamp>;
343+
334344
[[nodiscard]] bool IsAllowedPortName(StringView str);
335345

336346
class TypeInfo

include/behaviortree_cpp/behavior_tree.h

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "behaviortree_cpp/decorators/run_once_node.h"
3535
#include "behaviortree_cpp/decorators/subtree_node.h"
3636
#include "behaviortree_cpp/decorators/loop_node.h"
37+
#include "behaviortree_cpp/decorators/skip_unless_updated.h"
38+
#include "behaviortree_cpp/decorators/wait_update.h"
3739

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

include/behaviortree_cpp/blackboard.h

+57-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <memory>
55
#include <unordered_map>
66
#include <mutex>
7+
#include <optional>
78

89
#include "behaviortree_cpp/basic_types.h"
910
#include "behaviortree_cpp/contrib/json.hpp"
@@ -40,8 +41,14 @@ class Blackboard
4041
StringConverter string_converter;
4142
mutable std::mutex entry_mutex;
4243

44+
uint64_t sequence_id = 0;
45+
// timestamp since epoch
46+
std::chrono::nanoseconds stamp = std::chrono::nanoseconds{ 0 };
47+
4348
Entry(const TypeInfo& _info) : info(_info)
4449
{}
50+
51+
Entry& operator=(const Entry& other);
4552
};
4653

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

85+
template <typename T>
86+
std::optional<Timestamp> getStamped(const std::string& key, T& value) const;
87+
7888
/**
7989
* Version of get() that throws if it fails.
8090
*/
8191
template <typename T>
8292
[[nodiscard]] T get(const std::string& key) const;
8393

94+
template <typename T>
95+
std::pair<T, Timestamp> getStamped(const std::string& key) const;
96+
8497
/// Update the entry with the given key
8598
template <typename T>
8699
void set(const std::string& key, const T& value);
@@ -155,10 +168,7 @@ inline T Blackboard::get(const std::string& key) const
155168
}
156169
return any_ref.get()->cast<T>();
157170
}
158-
else
159-
{
160-
throw RuntimeError("Blackboard::get() error. Missing key [", key, "]");
161-
}
171+
throw RuntimeError("Blackboard::get() error. Missing key [", key, "]");
162172
}
163173

164174
inline void Blackboard::unset(const std::string& key)
@@ -203,6 +213,8 @@ inline void Blackboard::set(const std::string& key, const T& value)
203213
lock.lock();
204214

205215
entry->value = new_value;
216+
entry->sequence_id++;
217+
entry->stamp = std::chrono::steady_clock::now().time_since_epoch();
206218
}
207219
else
208220
{
@@ -212,14 +224,15 @@ inline void Blackboard::set(const std::string& key, const T& value)
212224
std::scoped_lock scoped_lock(entry.entry_mutex);
213225

214226
Any& previous_any = entry.value;
215-
216227
Any new_value(value);
217228

218229
// special case: entry exists but it is not strongly typed... yet
219230
if(!entry.info.isStronglyTyped())
220231
{
221232
// Use the new type to create a new entry that is strongly typed.
222233
entry.info = TypeInfo::Create<T>();
234+
entry.sequence_id++;
235+
entry.stamp = std::chrono::steady_clock::now().time_since_epoch();
223236
previous_any = std::move(new_value);
224237
return;
225238
}
@@ -273,6 +286,8 @@ inline void Blackboard::set(const std::string& key, const T& value)
273286
// copy only if the type is compatible
274287
new_value.copyInto(previous_any);
275288
}
289+
entry.sequence_id++;
290+
entry.stamp = std::chrono::steady_clock::now().time_since_epoch();
276291
}
277292
}
278293

@@ -281,10 +296,47 @@ inline bool Blackboard::get(const std::string& key, T& value) const
281296
{
282297
if(auto any_ref = getAnyLocked(key))
283298
{
299+
if(any_ref.get()->empty())
300+
{
301+
return false;
302+
}
284303
value = any_ref.get()->cast<T>();
285304
return true;
286305
}
287306
return false;
288307
}
289308

309+
template <typename T>
310+
inline std::optional<Timestamp> Blackboard::getStamped(const std::string& key,
311+
T& value) const
312+
{
313+
if(auto entry = getEntry(key))
314+
{
315+
std::unique_lock lk(entry->entry_mutex);
316+
if(entry->value.empty())
317+
{
318+
return std::nullopt;
319+
}
320+
value = entry->value.cast<T>();
321+
return Timestamp{ entry->sequence_id, entry->stamp };
322+
}
323+
return std::nullopt;
324+
}
325+
326+
template <typename T>
327+
inline std::pair<T, Timestamp> Blackboard::getStamped(const std::string& key) const
328+
{
329+
if(auto entry = getEntry(key))
330+
{
331+
std::unique_lock lk(entry->entry_mutex);
332+
if(entry->value.empty())
333+
{
334+
throw RuntimeError("Blackboard::get() error. Entry [", key,
335+
"] hasn't been initialized, yet");
336+
}
337+
return { entry->value.cast<T>(), Timestamp{ entry->sequence_id, entry->stamp } };
338+
}
339+
throw RuntimeError("Blackboard::get() error. Missing key [", key, "]");
340+
}
341+
290342
} // namespace BT

include/behaviortree_cpp/bt_factory.h

-3
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"
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
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

include/behaviortree_cpp/scripting/any_types.hpp

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2022 Davide Faconti - All Rights Reserved
1+
/* Copyright (C) 2022-24 Davide Faconti - All Rights Reserved
22
*
33
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
44
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
@@ -12,14 +12,6 @@
1212

1313
#pragma once
1414

15-
#include <cmath>
16-
#include <cstdio>
17-
#include <map>
18-
#include <string>
19-
#include <vector>
20-
#include <utility>
21-
#include <charconv>
22-
2315
#include "lexy/action/parse.hpp"
2416
#include "lexy/callback.hpp"
2517
#include "lexy/dsl.hpp"
@@ -31,6 +23,46 @@ namespace BT::Grammar
3123
{
3224
namespace dsl = lexy::dsl;
3325

26+
struct _xid_start_character : lexyd::char_class_base<_xid_start_character>
27+
{
28+
static LEXY_CONSTEVAL auto char_class_name()
29+
{
30+
return "code-point.BT-start-character";
31+
}
32+
33+
static LEXY_CONSTEVAL auto char_class_ascii()
34+
{
35+
lexy::_detail::ascii_set result;
36+
result.insert('a', 'z');
37+
result.insert('A', 'Z');
38+
result.insert('_');
39+
result.insert('@');
40+
return result;
41+
}
42+
43+
static LEXY_UNICODE_CONSTEXPR bool char_class_match_cp(char32_t cp)
44+
{
45+
// underscore handled as part of ASCII.
46+
return lexy::_detail::code_point_has_properties<LEXY_UNICODE_PROPERTY(xid_start)>(cp);
47+
}
48+
49+
template <typename Encoding>
50+
static constexpr auto char_class_match_swar(lexy::_detail::swar_int c)
51+
{
52+
return lexyd::ascii::_alphau::template char_class_match_swar<Encoding>(c);
53+
}
54+
};
55+
inline constexpr auto xid_start_character = _xid_start_character{};
56+
57+
// A Unicode-aware identifier.
58+
struct Name
59+
{
60+
static constexpr auto rule =
61+
dsl::identifier(xid_start_character, dsl::unicode::xid_continue);
62+
63+
static constexpr auto value = lexy::as_string<std::string>;
64+
};
65+
3466
//----------
3567
struct Integer : lexy::token_production
3668
{

0 commit comments

Comments
 (0)