The MAUVE Toolchain
Subscriber.hpp
1 /*
2  * Copyright 2017 ONERA
3  *
4  * This file is part of the MAUVE ROS project.
5  *
6  * MAUVE ROS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License version 3 as
8  * published by the Free Software Foundation.
9  *
10  * MAUVE ROS is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with MAUVE. If not, see <https://www.gnu.org/licenses/lgpl-3.0>.
17  */
18 #ifndef MAUVE_ROS_SUBSCRIBER_HPP
19 #define MAUVE_ROS_SUBSCRIBER_HPP
20 
21 #include <string>
22 #include <functional>
23 
24 #include <ros/ros.h>
25 
26 #include <mauve/runtime.hpp>
27 #include <mauve/base/PeriodicStateMachine.hpp>
28 
29 #include "Connector.hpp"
30 #include "Shell.hpp"
31 
32 namespace mauve
33 {
34  namespace ros
35  {
36 
42  template <typename ROS_T, typename T>
43  struct SubscriberCore : public runtime::Core< RosShell<ROS_T, T> >
44  {
45  bool configure_hook() final override;
46  void cleanup_hook() final override;
51  inline T read_value() {
52  if (status == runtime::DataStatus::NEW_DATA)
53  status = runtime::DataStatus::OLD_DATA;
54  return value;
55  };
61  auto sv = runtime::StatusValue<T> {status, value};
62  if (status == runtime::DataStatus::NEW_DATA)
63  status = runtime::DataStatus::OLD_DATA;
64  return sv;
65  };
70  inline runtime::DataStatus read_status() { return status; };
72  void update();
73 
74  private:
76  void callback(const ROS_T& msg);
78  ::ros::Subscriber subscriber;
80  T value;
82  runtime::DataStatus status;
84  runtime::RtMutex mutex;
85  };
86 
92  template <typename ROS_T, typename T>
94  public runtime::Interface< RosShell<ROS_T, T>, SubscriberCore<ROS_T, T> >
95  {
97  runtime::ReadService<runtime::StatusValue<T>> & read = this->template mk_read_service<runtime::StatusValue<T>>("read", &SubscriberCore<ROS_T, T>::read);
98  runtime::ReadService<T> & read_value = this->template mk_read_service<T>("read_value", &SubscriberCore<ROS_T, T>::read_value);
99  runtime::ReadService<runtime::DataStatus> & read_status = this->template mk_read_service<runtime::DataStatus>("read_status", &SubscriberCore<ROS_T, T>::read_status);
100  };
101 
102  template <typename ROS_T, typename T>
105 
106  template <typename ROS_T, typename T>
108  SubscriberCore<ROS_T, T>,
110  SubscriberCore<ROS_T, T>> >;
111 
112 }} // namespaces
113 
114 #include "ipp/Subscriber.ipp"
115 
116 #endif
Definition: Interface.hpp:34
void update()
Update function: write received message to output.
Definition: Subscriber.ipp:58
Interface of the ROS Subscriber.
Definition: Subscriber.hpp:93
Definition: DataStatus.hpp:29
Definition: ReadPort.hpp:27
runtime::DataStatus read_status()
Read the status of the value received on the topic.
Definition: Subscriber.hpp:70
T read_value()
Read the value received on the topic.
Definition: Subscriber.hpp:51
Core of the ROS Subscriber.
Definition: Subscriber.hpp:43
Definition: RtMutex.hpp:26
Component class.
Definition: Architecture.hpp:34
runtime::StatusValue< T > read()
Read the value received on the topic with its status.
Definition: Subscriber.hpp:60
The MAUVE namespace.
Definition: tracing.hpp:24
Definition: Architecture.hpp:38
void cleanup_hook() finaloverride
Hook function called when cleaning the shell.
Definition: Subscriber.ipp:43
bool configure_hook() finaloverride
Hook function called when configuring the shell.
Definition: Subscriber.ipp:30
Defines a Periodic State Machine.
Definition: PeriodicStateMachine.hpp:32
The Core defines the methods of the component.
Definition: Core.hpp:38