Skip to content

Commit b06ecb1

Browse files
authored
Merge pull request #47 from CoreSenseEU/cs_core_base_2
Afferent, Efferent and Core finished and tested
2 parents 3efb4e2 + eafbeb1 commit b06ecb1

16 files changed

Lines changed: 670 additions & 84 deletions

cs4home_core/include/cs4home_core/Afferent.hpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define CS4HOME_CORE__AFFERENT_HPP_
1818

1919
#include <memory>
20+
#include <utility>
21+
#include <queue>
2022
#include <vector>
2123
#include <string>
2224

@@ -32,16 +34,54 @@ class Afferent
3234
public:
3335
RCLCPP_SMART_PTR_DEFINITIONS(Afferent)
3436

37+
enum EfferentProcessMode {CALLBACK, ONDEMAND};
38+
3539
explicit Afferent(rclcpp_lifecycle::LifecycleNode::SharedPtr parent);
3640

37-
bool configure();
41+
virtual bool configure() = 0;
42+
43+
void set_mode(
44+
EfferentProcessMode mode,
45+
std::function<void(std::unique_ptr<rclcpp::SerializedMessage>)> cb = nullptr);
46+
47+
EfferentProcessMode get_mode() {return mode_;}
48+
void set_max_queue_size(size_t size) {max_queue_size_ = size;}
49+
size_t get_max_queue_size() {return max_queue_size_;}
50+
51+
template<class MessageT> std::unique_ptr<MessageT> get_msg(
52+
std::unique_ptr<rclcpp::SerializedMessage> msg)
53+
{
54+
auto typed_msg = std::make_unique<MessageT>();
55+
rclcpp::Serialization<MessageT> serializer;
56+
serializer.deserialize_message(msg.get(), typed_msg.get());
57+
58+
return std::move(typed_msg);
59+
}
60+
61+
template<class MessageT> std::unique_ptr<MessageT> get_msg()
62+
{
63+
if (msg_queue_.empty()) {
64+
return {};
65+
}
66+
67+
std::unique_ptr<rclcpp::SerializedMessage> msg = std::move(msg_queue_.front());
68+
msg_queue_.pop();
69+
70+
return get_msg<MessageT>(std::move(msg));
71+
}
3872

3973
protected:
4074
rclcpp_lifecycle::LifecycleNode::SharedPtr parent_;
41-
4275
std::vector<std::shared_ptr<rclcpp::GenericSubscription>> subs_;
4376

44-
private:
77+
EfferentProcessMode mode_ {ONDEMAND};
78+
79+
const size_t MAX_DEFAULT_QUEUE_SIZE = 100;
80+
size_t max_queue_size_ {MAX_DEFAULT_QUEUE_SIZE};
81+
std::queue<std::unique_ptr<rclcpp::SerializedMessage>> msg_queue_;
82+
83+
std::function<void(std::unique_ptr<rclcpp::SerializedMessage>)> callback_;
84+
4585
bool create_subscriber(const std::string & topic, const std::string & type);
4686
};
4787

cs4home_core/include/cs4home_core/Core.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
#include <memory>
2020

21+
22+
#include "cs4home_core/Afferent.hpp"
23+
#include "cs4home_core/Efferent.hpp"
24+
2125
#include "rclcpp_lifecycle/lifecycle_node.hpp"
2226
#include "rclcpp/macros.hpp"
2327

@@ -31,10 +35,17 @@ class Core
3135

3236
explicit Core(rclcpp_lifecycle::LifecycleNode::SharedPtr parent);
3337

34-
bool configure();
38+
virtual bool configure() = 0;
39+
virtual bool activate() = 0;
40+
virtual bool deactivate() = 0;
41+
42+
void set_afferent(cs4home_core::Afferent::SharedPtr afferent) {afferent_ = afferent;}
43+
void set_efferent(cs4home_core::Efferent::SharedPtr efferent) {efferent_ = efferent;}
3544

3645
protected:
3746
rclcpp_lifecycle::LifecycleNode::SharedPtr parent_;
47+
cs4home_core::Afferent::SharedPtr afferent_;
48+
cs4home_core::Efferent::SharedPtr efferent_;
3849
};
3950

4051
} // namespace cs4home_core

cs4home_core/include/cs4home_core/Efferent.hpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#ifndef CS4HOME_CORE__EFFERENT_HPP_
1717
#define CS4HOME_CORE__EFFERENT_HPP_
1818

19+
#include <memory>
20+
#include <string>
21+
#include <vector>
22+
1923
#include "rclcpp_lifecycle/lifecycle_node.hpp"
2024
#include "rclcpp/macros.hpp"
2125

@@ -29,10 +33,26 @@ class Efferent
2933

3034
explicit Efferent(rclcpp_lifecycle::LifecycleNode::SharedPtr parent);
3135

32-
bool configure();
36+
virtual bool configure() = 0;
37+
38+
template<class MessageT>
39+
void publish(std::unique_ptr<MessageT> msg)
40+
{
41+
rclcpp::Serialization<MessageT> serializer;
42+
auto untyped_msg = rclcpp::SerializedMessage();
43+
44+
serializer.serialize_message(msg.get(), &untyped_msg);
45+
46+
for (auto & pub : pubs_) {
47+
pub->publish(untyped_msg);
48+
}
49+
}
3350

3451
protected:
3552
rclcpp_lifecycle::LifecycleNode::SharedPtr parent_;
53+
std::vector<std::shared_ptr<rclcpp::GenericPublisher>> pubs_;
54+
55+
bool create_publisher(const std::string & topic, const std::string & type);
3656
};
3757

3858
} // namespace cs4home_core

cs4home_core/src/cs4home_core/Afferent.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
1516
#include "cs4home_core/Afferent.hpp"
1617

17-
#include "rclcpp/node.hpp"
18-
#include "rclcpp/serialization.hpp"
18+
#include "rclcpp/rclcpp.hpp"
1919

2020
namespace cs4home_core
2121
{
@@ -25,38 +25,48 @@ Afferent::Afferent(rclcpp_lifecycle::LifecycleNode::SharedPtr parent)
2525
{
2626
}
2727

28-
bool
29-
Afferent::configure()
28+
void
29+
Afferent::set_mode(
30+
EfferentProcessMode mode,
31+
std::function<void(std::unique_ptr<rclcpp::SerializedMessage>)> cb)
3032
{
31-
/*std::vector<std::string> afferents;
32-
parent_->declare_parameter("afferent", afferents);
33-
parent_->get_parameter("afferent", afferents);
34-
35-
for (const auto & afferent: afferents) {
36-
std::string topic, type;
37-
parent_->declare_parameter("afferent." + afferent + ".topic", topic);
38-
parent_->get_parameter("afferent." + afferent + ".type", type);
39-
40-
if (topic == "" || type == "") {
41-
return false;
42-
}
43-
44-
if (!create_subscriber(topic, type)) {
45-
RCLCPP_ERROR(
46-
parent_->get_logger(), "Error creating afferent [%s, %s]", topic.c_str(), type.c_str());
47-
return false;
33+
if (mode == CALLBACK) {
34+
if (cb) {
35+
callback_ = cb;
36+
} else {
37+
RCLCPP_WARN(
38+
parent_->get_logger(), "[Afferent] Error setting callback: not function specified");
39+
return;
4840
}
4941
}
50-
*/
51-
return true;
42+
mode_ = mode;
5243
}
5344

5445
bool
5546
Afferent::create_subscriber(const std::string & topic, const std::string & type)
5647
{
57-
auto sub = parent_->create_generic_subscription(topic, type, 100,
58-
[&](std::shared_ptr<rclcpp::SerializedMessage> msg) {
59-
RCLCPP_INFO(parent_->get_logger(), "Llega el mensaje");
48+
RCLCPP_DEBUG(
49+
parent_->get_logger(),
50+
"[Afferent] Creating subscription [%s, %s]",
51+
topic.c_str(), type.c_str());
52+
53+
auto sub = rclcpp::create_generic_subscription(
54+
parent_->get_node_topics_interface(), topic, type, 100,
55+
[&](std::unique_ptr<rclcpp::SerializedMessage> msg)
56+
{
57+
if (mode_ == CALLBACK) {
58+
if (callback_) {
59+
callback_(std::move(msg));
60+
} else {
61+
RCLCPP_WARN(
62+
parent_->get_logger(), "[Afferent] Error calling callback: not function specified");
63+
}
64+
} else {
65+
msg_queue_.push(std::move(msg));
66+
if (msg_queue_.size() > max_queue_size_) {
67+
msg_queue_.pop();
68+
}
69+
}
6070
});
6171

6272
subs_.push_back(sub);

cs4home_core/src/cs4home_core/CognitiveModule.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ CallbackReturnT CognitiveModule::on_configure(const rclcpp_lifecycle::State & st
3434
(void)state;
3535

3636
get_parameter("core", core_name_);
37-
auto [core_, error_core] = load_component<Core>(core_name_, shared_from_this());
37+
std::string error_core;
38+
std::tie(core_, error_core) = load_component<Core>(core_name_, shared_from_this());
3839
if (core_ == nullptr || !core_->configure()) {
3940
RCLCPP_ERROR(
4041
get_logger(), "Error configuring core at %s with name %s: %s",
@@ -43,7 +44,9 @@ CallbackReturnT CognitiveModule::on_configure(const rclcpp_lifecycle::State & st
4344
}
4445

4546
get_parameter("efferent", efferent_name_);
46-
auto [efferent_, error_efferent] = load_component<Efferent>(efferent_name_, shared_from_this());
47+
std::string error_efferent;
48+
std::tie(efferent_, error_efferent) = load_component<Efferent>(efferent_name_,
49+
shared_from_this());
4750
if (efferent_ == nullptr || !efferent_->configure()) {
4851
RCLCPP_ERROR(
4952
get_logger(), "Error configuring efferent at %s with name %s: %s",
@@ -52,16 +55,22 @@ CallbackReturnT CognitiveModule::on_configure(const rclcpp_lifecycle::State & st
5255
}
5356

5457
get_parameter("afferent", afferent_name_);
55-
auto [afferent_, error_afferent] = load_component<Afferent>(afferent_name_, shared_from_this());
58+
std::string error_afferent;
59+
std::tie(afferent_, error_afferent) = load_component<Afferent>(afferent_name_,
60+
shared_from_this());
5661
if (afferent_ == nullptr || !afferent_->configure()) {
5762
RCLCPP_ERROR(
5863
get_logger(), "Error configuring afferent at %s with name %s: %s",
5964
get_name(), afferent_name_.c_str(), error_afferent.c_str());
6065
return CallbackReturnT::FAILURE;
6166
}
6267

68+
core_->set_afferent(afferent_);
69+
core_->set_efferent(efferent_);
70+
6371
get_parameter("meta", meta_name_);
64-
auto [meta_, error_meta] = load_component<Meta>(meta_name_, shared_from_this());
72+
std::string error_meta;
73+
std::tie(meta_, error_meta) = load_component<Meta>(meta_name_, shared_from_this());
6574
if (meta_ == nullptr || !meta_->configure()) {
6675
RCLCPP_ERROR(
6776
get_logger(), "Error configuring efferent at %s with name %s: %s",
@@ -70,7 +79,9 @@ CallbackReturnT CognitiveModule::on_configure(const rclcpp_lifecycle::State & st
7079
}
7180

7281
get_parameter("coupling", coupling_name_);
73-
auto [coupling_, error_coupling] = load_component<Coupling>(coupling_name_, shared_from_this());
82+
std::string error_coupling;
83+
std::tie(coupling_, error_coupling) = load_component<Coupling>(coupling_name_,
84+
shared_from_this());
7485
if (coupling_ == nullptr || !coupling_->configure()) {
7586
RCLCPP_ERROR(
7687
get_logger(), "Error configuring efferent at %s with name %s: %s",
@@ -85,13 +96,23 @@ CallbackReturnT CognitiveModule::on_activate(const rclcpp_lifecycle::State & sta
8596
{
8697
(void)state;
8798

99+
if (!core_->activate()) {
100+
RCLCPP_ERROR(get_logger(), "Unable to activate Core");
101+
return CallbackReturnT::FAILURE;
102+
}
103+
88104
return CallbackReturnT::SUCCESS;
89105
}
90106

91107
CallbackReturnT CognitiveModule::on_deactivate(const rclcpp_lifecycle::State & state)
92108
{
93109
(void)state;
94110

111+
if (!core_->deactivate()) {
112+
RCLCPP_ERROR(get_logger(), "Unable to activate Core");
113+
return CallbackReturnT::FAILURE;
114+
}
115+
95116
return CallbackReturnT::SUCCESS;
96117
}
97118

cs4home_core/src/cs4home_core/Efferent.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ Efferent::Efferent(rclcpp_lifecycle::LifecycleNode::SharedPtr parent)
2323
}
2424

2525
bool
26-
Efferent::configure()
26+
Efferent::create_publisher(const std::string & topic, const std::string & type)
2727
{
28+
auto pub = rclcpp::create_generic_publisher(
29+
parent_->get_node_topics_interface(), topic, type, 100);
30+
31+
pubs_.push_back(pub);
32+
2833
return true;
2934
}
3035

cs4home_core/test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ add_library(image_filter SHARED ImageFilter.cpp)
1212
ament_target_dependencies(image_filter ${dependencies} ${test_dependencies})
1313
target_link_libraries(image_filter ${PROJECT_NAME})
1414

15+
add_library(image_filter_cb SHARED ImageFilterCB.cpp)
16+
ament_target_dependencies(image_filter_cb ${dependencies} ${test_dependencies})
17+
target_link_libraries(image_filter_cb ${PROJECT_NAME})
18+
1519
add_library(simple_image_input SHARED SimpleImageInput.cpp)
1620
ament_target_dependencies(simple_image_input ${dependencies} ${test_dependencies})
1721
target_link_libraries(simple_image_input ${PROJECT_NAME})
@@ -31,6 +35,7 @@ target_link_libraries(default_coupling ${PROJECT_NAME})
3135

3236
install(TARGETS
3337
image_filter
38+
image_filter_cb
3439
simple_image_input
3540
simple_image_output
3641
default_meta

cs4home_core/test/DefaultCoupling.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class DefaultCoupling : public cs4home_core::Coupling
2727
explicit DefaultCoupling(rclcpp_lifecycle::LifecycleNode::SharedPtr parent)
2828
: Coupling(parent)
2929
{
30-
RCLCPP_INFO(parent_->get_logger(), "Coupling created: [DefaultCoupling]");
30+
RCLCPP_DEBUG(parent_->get_logger(), "Coupling created: [DefaultCoupling]");
3131
}
3232

3333

3434
bool configure()
3535
{
36-
RCLCPP_INFO(parent_->get_logger(), "Coupling configured");
36+
RCLCPP_DEBUG(parent_->get_logger(), "Coupling configured");
3737
return true;
3838
}
3939
};

cs4home_core/test/DefaultMeta.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class DefaultMeta : public cs4home_core::Meta
2727
explicit DefaultMeta(rclcpp_lifecycle::LifecycleNode::SharedPtr parent)
2828
: Meta(parent)
2929
{
30-
RCLCPP_INFO(parent_->get_logger(), "Meta created: [DefaultMeta]");
30+
RCLCPP_DEBUG(parent_->get_logger(), "Meta created: [DefaultMeta]");
3131
}
3232

3333

3434
bool configure()
3535
{
36-
RCLCPP_INFO(parent_->get_logger(), "Meta configured");
36+
RCLCPP_DEBUG(parent_->get_logger(), "Meta configured");
3737
return true;
3838
}
3939
};

0 commit comments

Comments
 (0)