NRP Core  1.4.1
output_node.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef OUTPUT_ROS_NODE_H
23 #define OUTPUT_ROS_NODE_H
24 
25 #include <boost/python.hpp>
26 #include <iostream>
27 #include <mutex>
28 
31 
33 
39 template<class MSG_TYPE>
40 class OutputROSNode : public OutputNode<MSG_TYPE> {
41 public:
42 
46  OutputROSNode(const std::string &id,
47  bool publishFromCache = false,
48  unsigned int computePeriod = 1) :
50  { }
51 
52  std::string typeStr() const override
53  { return "RosPublisher"; }
54 
55 protected:
56 
57  void sendSingleMsg(const std::string& /*id*/, const MSG_TYPE* data) override
58  {
59  // TODO: check that 'id' is equal to the topic address this node publishes to?
60 
61  NRPROSProxy* rosProxy = &(NRPROSProxy::getInstance());
62  if(rosProxy)
63  rosProxy->publish(this->id(), *data);
64  else
65  NRPLogger::warn("From OutputROSNode \"" + this->id() +
66  "\". NRPCoreSim is not connected to ROS and this node can't publish. Add \"ROSNode\" parameter to your experiment configuration");
67  }
68 
69  // TODO: implement this node in a way that supports ROS msg types that contains a field with an array of another type.
70  // Its input port will accept the type of the array field and in this function the array msg will be composed and
71  // published. Maybe a new class OutputROSArrayNode
72  void sendBatchMsg(const std::string& /*id*/, const std::vector<const MSG_TYPE*>& /*data*/) override
73  {
74  throw NRPException::logCreate("BATCH update policy is not supported by OutputROSNode");
75  }
76 
77 };
78 
79 template<class MSG_TYPE>
80 class OutputROSEdge : public SimpleOutputEdge<MSG_TYPE, OutputROSNode<MSG_TYPE>> {
81 
82 public:
83 
84  OutputROSEdge(const std::string &keyword, const std::string &address,
85  bool publishFromCache = false,
86  unsigned int computePeriod = 1) :
87  SimpleOutputEdge<MSG_TYPE, OutputROSNode<MSG_TYPE>>(keyword, address, address,
88  publishFromCache, computePeriod)
89  {}
90 
91 protected:
92 
94  { return new OutputROSNode<MSG_TYPE>(this->_id, this->_publishFromCache, this->_computePeriod); }
95 };
96 
97 
98 #endif //OUTPUT_ROS_NODE_H
NRPLogger::warn
static void warn(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for warning level.
Definition: nrp_logger.h:149
OutputEdge::_computePeriod
unsigned int _computePeriod
Definition: output_edge.h:173
NRPROSProxy
Definition: nrp_ros_proxy.h:30
OutputEdge::_publishFromCache
bool _publishFromCache
Definition: output_edge.h:172
nrp_ros_proxy.h
OutputROSEdge::OutputROSEdge
OutputROSEdge(const std::string &keyword, const std::string &address, bool publishFromCache=false, unsigned int computePeriod=1)
Definition: output_node.h:84
OutputEdge::_id
std::string _id
Definition: output_edge.h:170
OutputNodePolicies
Definition: computational_node_policies.h:47
OutputROSEdge
Definition: output_node.h:80
OutputNode< MSG_TYPE >::publishFromCache
bool publishFromCache()
Definition: output_node.h:109
output_node.h
ComputationalNode::id
const std::string & id() const
Returns the node 'id'.
Definition: computational_node.h:57
OutputEdge
Helper class used to implement Python output edge decorators.
Definition: output_edge.h:36
NRPROSProxy::getInstance
static NRPROSProxy & getInstance()
Get singleton instance of NRPROSProxy.
Definition: nrp_ros_proxy.cpp:26
OutputNode
Implementation of an output node in the computation graph.
Definition: output_node.h:38
OutputROSNode::OutputROSNode
OutputROSNode(const std::string &id, bool publishFromCache=false, unsigned int computePeriod=1)
Constructor.
Definition: output_node.h:46
output_edge.h
OutputROSNode::typeStr
std::string typeStr() const override
Returns the node 'type' as a string.
Definition: output_node.h:52
OutputNodePolicies::SERIES
@ SERIES
Definition: computational_node_policies.h:51
NRPException::logCreate
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73
OutputROSNode::sendBatchMsg
void sendBatchMsg(const std::string &, const std::vector< const MSG_TYPE * > &) override
Sends out a vector of msg as a single batch, to be implemented by derived classes.
Definition: output_node.h:72
OutputROSNode
Output node used to connect a ROS publisher to the computational graph.
Definition: output_node.h:40
OutputROSNode::sendSingleMsg
void sendSingleMsg(const std::string &, const MSG_TYPE *data) override
Sends out a single msg, to be implemented by derived classes.
Definition: output_node.h:57
OutputROSEdge::makeNewNode
OutputROSNode< MSG_TYPE > * makeNewNode() override
Definition: output_node.h:93
OutputNodePolicies::PublishFormatPolicy
PublishFormatPolicy
Defines how output nodes send stored msgs.
Definition: computational_node_policies.h:50
NRPROSProxy::publish
void publish(const std::string &address, const MSG_TYPE &msg, size_t queueSize=10)
Publishes 'msg' to ROS topic 'address'.
Definition: nrp_ros_proxy.h:62