YARP
Yet Another Robot Platform
AnalogWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_DEV_ANALOGWRAPPER_ANALOGWRAPPER_H
10 #define YARP_DEV_ANALOGWRAPPER_ANALOGWRAPPER_H
11 
12  //#include <list>
13 #include <vector>
14 #include <iostream>
15 #include <string>
16 #include <sstream>
17 
18 #include <yarp/os/Network.h>
19 #include <yarp/os/Port.h>
20 #include <yarp/os/BufferedPort.h>
21 #include <yarp/os/Bottle.h>
22 #include <yarp/os/Time.h>
23 #include <yarp/os/Property.h>
24 
25 #include <yarp/os/PeriodicThread.h>
26 #include <yarp/os/BufferedPort.h>
27 #include <yarp/os/Stamp.h>
28 
29 #include <yarp/sig/Vector.h>
30 
31 #include <yarp/dev/IAnalogSensor.h>
32 #include <yarp/dev/PolyDriver.h>
33 #include <yarp/dev/DeviceDriver.h>
35 #include <yarp/dev/api.h>
36 
37 
38 // ROS state publisher
39 #include <yarp/os/Node.h>
40 #include <yarp/os/Publisher.h>
44 
45 
46 #define DEFAULT_THREAD_PERIOD 20 //ms
47 
186 class AnalogServerHandler;
187 class AnalogPortEntry;
188 
189 
194 {
195 public:
196  AnalogWrapper();
197 
198  AnalogWrapper(const AnalogWrapper&) = delete;
202 
203  ~AnalogWrapper() override;
204 
205  bool open(yarp::os::Searchable &params) override;
206  bool close() override;
208 
209  void setId(const std::string &id);
210  std::string getId();
211 
215  bool attachAll(const yarp::dev::PolyDriverList &p) override;
216  bool detachAll() override;
217 
219  void detach();
220 
221  bool threadInit() override;
222  void threadRelease() override;
223  void run() override;
224 
225 private:
226  std::string streamingPortName;
227  std::string rpcPortName;
228  yarp::dev::IAnalogSensor *analogSensor_p{nullptr}; // the analog sensor to read from
229  std::vector<AnalogPortEntry> analogPorts; // the list of output ports
230  std::vector<AnalogServerHandler*> handlers; // the list of rpc port handlers
231  yarp::os::Stamp lastStateStamp; // the last reading time stamp
232  yarp::sig::Vector lastDataRead; // the last vector of data read from the attached IAnalogSensor
233  int _rate{DEFAULT_THREAD_PERIOD};
234  std::string sensorId;
235 
236  // ROS state publisher
237  ROSTopicUsageType useROS{ROS_disabled}; // decide if open ROS topic or not
238  std::vector<std::string> frame_idVec; // name of the reference frame the measures are referred to
239  std::vector<std::string> ros_joint_names;
240  std::string rosMsgType; // ros message type
241  std::string rosNodeName{""}; // name of the rosNode
242  std::vector<std::string> rosTopicNamesVec; // names of the rosTopics
243  yarp::os::Node* rosNode{nullptr}; // add a ROS node
244  std::vector<yarp::os::NetUint32> rosMsgCounterVec; // incremental counter in the ROS message
245  int rosOffset{0}; // offset to be ignored from the analog sensor data
246  int rosPadding{0}; // padding to be ignored from the analog sensor data
247 
248  // TODO: in the future, in order to support multiple ROS msgs this should be a pointer allocated dynamically depending on the msg maybe (??)
249  // yarp::os::PortWriterBuffer<yarp::rosmsg::geometry_msgs::WrenchStamped> rosOutputWrench_buffer; // Buffer associated to the ROS topic
250  std::vector<yarp::os::Publisher<yarp::rosmsg::geometry_msgs::WrenchStamped>> rosPublisherWrenchPortVec; // Dedicated ROS topic publisher
251 
252  //yarp::os::PortWriterBuffer<yarp::rosmsg::sensor_msgs::JointState> rosOutputJoint_buffer; // Buffer associated to the ROS topic
253  yarp::os::Publisher<yarp::rosmsg::sensor_msgs::JointState> rosPublisherJointPort; // Dedicated ROS topic publisher
254 
255 
256  bool ownDevices{false};
257  // Open the wrapper only, the attach method needs to be called before using it
258  bool openDeferredAttach(yarp::os::Searchable &prop);
259 
260  // For the simulator, if a subdevice parameter is given to the wrapper, it will
261  // open it and attach to it immediately.
262  yarp::dev::PolyDriver *subDeviceOwned{nullptr};
263  bool openAndAttachSubDevice(yarp::os::Searchable &prop);
264 
265  bool checkROSParams(yarp::os::Searchable &config);
266  bool initialize_ROS();
267  bool initialize_YARP(yarp::os::Searchable &config);
268 
269  void setHandlers();
270  void removeHandlers();
271 
272  // Function used when there is only one output port
273  bool createPort(const char* name, int rate=20);
274  // Function used when one or more output ports are specified
275  bool createPorts(const std::vector<AnalogPortEntry>& _analogPorts, int rate=20);
276  bool checkForDeprecatedParams(yarp::os::Searchable &params);
277 };
278 
279 #endif // YARP_DEV_ANALOGWRAPPER_ANALOGWRAPPER_H
#define DEFAULT_THREAD_PERIOD
Definition: AnalogWrapper.h:46
analog sensor interface
contains the definition of a Vector type
A yarp port that output data read from an analog sensor.
Handler of the rpc port related to an analog sensor.
AnalogWrapper & operator=(const AnalogWrapper &)=delete
void threadRelease() override
Release method.
void setId(const std::string &id)
void attach(yarp::dev::IAnalogSensor *s)
bool threadInit() override
Initialization method.
~AnalogWrapper() override
AnalogWrapper(AnalogWrapper &&)=delete
void run() override
Loop function.
AnalogWrapper & operator=(AnalogWrapper &&)=delete
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
AnalogWrapper(const AnalogWrapper &)=delete
std::string getId()
bool close() override
Close the DeviceDriver.
bool attachAll(const yarp::dev::PolyDriverList &p) override
Specify which analog sensor this thread has to read from.
bool detachAll() override
Detach the object (you must have first called attach).
yarp::os::Bottle getOptions()
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
A generic interface to sensors (gyro, a/d converters).
Definition: IAnalogSensor.h:31
Interface for an object that can wrap/attach to to another.
A container for a device driver.
Definition: PolyDriver.h:27
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
The Node class.
Definition: Node.h:27
An abstraction for a periodic thread.
A base class for nested structures that can be searched.
Definition: Searchable.h:69
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
ROSTopicUsageType
Definition: yarpRosHelper.h:19
@ ROS_disabled
Definition: yarpRosHelper.h:21