YARP
Yet Another Robot Platform
VirtualAnalogWrapper.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_VIRTUALANALOGWRAPPER_VIRTUALANALOGWRAPPER_H
7 #define YARP_DEV_VIRTUALANALOGWRAPPER_VIRTUALANALOGWRAPPER_H
8 
9 
10 // VirtualAnalogWrapper
11 // A server that opens an input port getting externally measured analog values
12 // and it is able to attach to a one or more virtual analog sensor through
13 // IVirtualAnalogSensor interface.
14 
15 #include <yarp/os/PortablePair.h>
16 #include <yarp/os/BufferedPort.h>
17 #include <yarp/os/Time.h>
18 #include <yarp/os/Network.h>
19 #include <yarp/os/PeriodicThread.h>
20 #include <yarp/os/Vocab.h>
21 
22 #include <yarp/dev/PolyDriver.h>
23 #include <yarp/sig/Vector.h>
25 
27 
28 #include <mutex>
29 #include <string>
30 #include <vector>
31 #include <cstdarg>
32 
33 #ifdef MSVC
34  #pragma warning(disable:4355)
35 #endif
36 
37 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
38  TODO make more general
39  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
40 
41 
43 // TODO add IVirtualAnalogSensor interface to have Channels number and status??
44 
45 
46 class AnalogSubDevice;
47 
61  public yarp::os::Thread,
63 {
64 public:
65  VirtualAnalogWrapper() = default;
70 
72  {
73  close();
74  }
75 
76  // DeviceDriver //////////////////////////////////////////////////////////
77  bool open(yarp::os::Searchable& config) override;
78  bool close() override;
80 
81  // Thread ////////////////////////////////////////////////////////////////
82  void run() override;
84 
85  // IMultipleWrapper //////////////////////////////////////////////////////
86  bool attachAll(const yarp::dev::PolyDriverList &p) override;
87  bool detachAll() override;
89 
90  // Utility
91  bool perform_first_check(int elems);
92 
93 protected:
94  std::mutex mMutex;
95 
96  bool mIsVerbose{false};
97 
98  int mNSubdevs{0};
99 
100  std::vector<int> mChan2Board;
101  std::vector<int> mChan2BAddr;
102  double lastRecv{0.0};
103  bool first_check{false};
104 
105  std::vector<AnalogSubDevice> mSubdevices;
107 };
108 
109 
111 {
112 public:
113  AnalogSubDevice();
115 
116  bool attach(yarp::dev::PolyDriver *driver, const std::string &key);
117  void detach();
118 
119  bool configure(int map0, int map1, const std::string &key);
120 
121  bool isAttached(){ return mIsAttached; }
122 
123  void setTorque(int joint,double torque)
124  {
125  if (joint < mMap0 || mMap1 < joint) {
126  return;
127  }
128 
129  mTorques[joint-mMap0]=torque;
130  }
131 
132  void resetTorque()
133  {
134  mTorques.zero();
135  }
136 
138  {
139  if (mpSensor) {
141  }
142  }
143 
144  const std::string& getKey(){ return mKey; }
145 
146 protected:
147  std::string mKey;
148 
150 
152 
155  double lastRecvMsg{0.0};
158 };
159 
160 
161 #endif // YARP_DEV_VIRTUALANALOGWRAPPER_VIRTUALANALOGWRAPPER_H
virtual analog sensor interface
contains the definition of a Vector type
yarp::sig::Vector mTorques
yarp::dev::IVirtualAnalogSensor * mpSensor
yarp::dev::PolyDriver * mpDevice
bool configure(int map0, int map1, const std::string &key)
void setTorque(int joint, double torque)
bool attach(yarp::dev::PolyDriver *driver, const std::string &key)
const std::string & getKey()
virtualAnalogServer: An analog wrapper for virtual device A virtual device is a software emulated dev...
VirtualAnalogWrapper & operator=(VirtualAnalogWrapper &&)=delete
VirtualAnalogWrapper()=default
std::vector< AnalogSubDevice > mSubdevices
VirtualAnalogWrapper(VirtualAnalogWrapper &&)=delete
std::vector< int > mChan2Board
bool close() override
Close the DeviceDriver.
VirtualAnalogWrapper & operator=(const VirtualAnalogWrapper &)=delete
std::vector< int > mChan2BAddr
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
VirtualAnalogWrapper(const VirtualAnalogWrapper &)=delete
bool detachAll() override
Detach the object (you must have first called attach).
void run() override
Main body of the new thread.
yarp::os::BufferedPort< yarp::os::Bottle > mPortInputTorques
bool perform_first_check(int elems)
Interface implemented by all device drivers.
Definition: DeviceDriver.h:35
Interface for an object that can wrap/attach to to another.
A generic interface to a virtual sensors.
virtual bool updateVirtualAnalogSensorMeasure(yarp::sig::Vector &measure)=0
Set a vector of torque values for virtual sensor.
A container for a device driver.
Definition: PolyDriver.h:24
A base class for nested structures that can be searched.
Definition: Searchable.h:66
An abstraction for a thread of execution.
Definition: Thread.h:22
void zero()
Zero the elements of the vector.
Definition: Vector.h:345