YARP
Yet Another Robot Platform
ControlBoardWrapper.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_DEV_CONTROLBOARDWRAPPER_CONTROLBOARDWRAPPER_H
7 #define YARP_DEV_CONTROLBOARDWRAPPER_CONTROLBOARDWRAPPER_H
8 
9 
10 // ControlBoardWrapper
11 // A modified version of the remote control board class
12 // which remaps joints, it can also merge networks into a single part.
13 
14 
15 #include <yarp/dev/DeviceDriver.h>
17 
18 #include <yarp/os/BufferedPort.h>
19 #include <yarp/os/PeriodicThread.h>
20 #include <yarp/os/Stamp.h>
21 #include <yarp/sig/Vector.h>
23 
24 #include <yarp/dev/impl/jointData.h> // struct for YARP extended port
25 
26 #include <mutex>
27 #include <string>
28 #include <vector>
29 
30 // ROS state publisher
31 #include <yarp/os/Node.h>
32 #include <yarp/os/Publisher.h>
35 
36 
57 
58 #include "SubDevice.h"
60 #include "RPCMessagesParser.h"
61 
62 
63 #ifdef MSVC
64  #pragma warning(disable:4355)
65 #endif
66 
67 /*
68  * To optimize memory allocation, for group of joints we can have one mem reserver for rpc port
69  * and on e for streaming. The size could be numOfSubDevices*maxNumOfjointForSubdevice.
70  * (we could also use the actual joint number for each subdevice using a for loop). TODO
71  */
72 
73 class CommandsHelper;
74 class SubDevice;
75 
76 
180  virtual public ControlBoardWrapperCommon,
201 {
202 private:
203  std::string rootName;
204 
205  bool checkPortName(yarp::os::Searchable &params);
206 
208 
209  yarp::os::BufferedPort<yarp::sig::Vector> outputPositionStatePort; // Port /state:o streaming out the encoder positions
210  yarp::os::BufferedPort<CommandMessage> inputStreamingPort; // Input streaming port for high frequency commands
211  yarp::os::Port inputRPCPort; // Input RPC port for set/get remote calls
212  yarp::sig::Vector times; // time for each joint
213 
214  // Buffer associated to the extendedOutputStatePort port; in this case we will use the type generated
215  // from the YARP .thrift file
217  yarp::os::Port extendedOutputStatePort; // Port /stateExt:o streaming out the struct with the robot data
218 
219  // ROS state publisher
220  ROSTopicUsageType useROS {ROS_disabled}; // decide if open ROS topic or not
221  std::vector<std::string> jointNames; // name of the joints
222  std::string rosNodeName; // name of the rosNode
223  std::string rosTopicName; // name of the rosTopic
224  yarp::os::Node *rosNode {nullptr}; // add a ROS node
225  yarp::os::NetUint32 rosMsgCounter {0}; // incremental counter in the ROS message
226  yarp::os::PortWriterBuffer<yarp::rosmsg::sensor_msgs::JointState> rosOutputState_buffer; // Buffer associated to the ROS topic
227  yarp::os::Publisher<yarp::rosmsg::sensor_msgs::JointState> rosPublisherPort; // Dedicated ROS topic publisher
228 
229  yarp::os::PortReaderBuffer<yarp::os::Bottle> inputRPC_buffer; // Buffer associated to the inputRPCPort port
230  RPCMessagesParser RPC_parser; // Message parser associated to the inputRPCPort port
231  StreamingMessagesParser streaming_parser; // Message parser associated to the inputStreamingPort port
232 
233  static constexpr double default_period = 0.02; // s
234  double period {default_period};
235 
236  yarp::os::Bottle getOptions();
237  bool updateAxisName();
238  bool checkROSParams(yarp::os::Searchable &config);
239  bool initialize_ROS();
240  bool initialize_YARP(yarp::os::Searchable &prop);
241  void cleanup_yarpPorts();
242 
243  // Default usage
244  // Open the wrapper only, the attach method needs to be called before using it
245  bool openDeferredAttach(yarp::os::Property& prop);
246 
247  // For the simulator, if a subdevice parameter is given to the wrapper, it will
248  // open it and attach to it immediately.
249  yarp::dev::PolyDriver *subDeviceOwned {nullptr};
250  bool openAndAttachSubDevice(yarp::os::Property& prop);
251 
252  bool ownDevices {true};
253 
254  void calculateMaxNumOfJointsInDevices();
255 
256 public:
263 
264 
265  /* Return id of this device */
266  std::string getId()
267  {
268  return partName;
269  }
270 
275  bool close() override;
276 
277 
287  bool open(yarp::os::Searchable& prop) override;
288 
289  bool detachAll() override;
290 
291  bool attachAll(const yarp::dev::PolyDriverList &l) override;
292 
296  void run() override;
297 };
298 
299 
300 #endif // YARP_DEV_CONTROLBOARDWRAPPER_CONTROLBOARDWRAPPER_H
contains the definition of a Vector type
controlboardwrapper2 deprecated: An updated version of the controlBoard network wrapper.
bool open(yarp::os::Searchable &prop) override
Open the device driver.
bool detachAll() override
Detach the object (you must have first called attach).
ControlBoardWrapper & operator=(const ControlBoardWrapper &)=delete
ControlBoardWrapper & operator=(ControlBoardWrapper &&)=delete
ControlBoardWrapper(const ControlBoardWrapper &)=delete
~ControlBoardWrapper() override
bool close() override
Close the device driver by deallocating all resources and closing ports.
ControlBoardWrapper(ControlBoardWrapper &&)=delete
void run() override
The thread main loop deals with writing on ports here.
bool attachAll(const yarp::dev::PolyDriverList &l) override
Attach to a list of objects.
Helper object for parsing RPC port messages.
Callback implementation after buffered input.
Interface implemented by all device drivers.
Definition: DeviceDriver.h:35
Interface for an object that can wrap/attach to to another.
A container for a device driver.
Definition: PolyDriver.h:24
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
The Node class.
Definition: Node.h:24
An abstraction for a periodic thread.
A mini-server for network communication.
Definition: Port.h:47
A class for storing options and configuration information.
Definition: Property.h:34
A base class for nested structures that can be searched.
Definition: Searchable.h:66
std::uint32_t NetUint32
Definition of the NetUint32 type.
Definition: NetUint32.h:30
ROSTopicUsageType
Definition: yarpRosHelper.h:16
@ ROS_disabled
Definition: yarpRosHelper.h:18