YARP
Yet Another Robot Platform
VirtualAnalogWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2019 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_VIRTUALANALOGWRAPPER_VIRTUALANALOGWRAPPER_H
10 #define YARP_DEV_VIRTUALANALOGWRAPPER_VIRTUALANALOGWRAPPER_H
11 
12 
13 // VirtualAnalogWrapper
14 // A server that opens an input port getting externally measured analog values
15 // and it is able to attach to a one or more virtual analog sensor through
16 // IVirtualAnalogSensor interface.
17 
18 #include <yarp/os/PortablePair.h>
19 #include <yarp/os/BufferedPort.h>
20 #include <yarp/os/Time.h>
21 #include <yarp/os/Network.h>
22 #include <yarp/os/PeriodicThread.h>
23 #include <yarp/os/Vocab.h>
24 
25 #include <yarp/dev/PolyDriver.h>
26 #include <yarp/sig/Vector.h>
27 #include <yarp/dev/Wrapper.h>
28 
30 
31 #include <mutex>
32 #include <string>
33 #include <vector>
34 #include <cstdarg>
35 
36 #ifdef MSVC
37  #pragma warning(disable:4355)
38 #endif
39 
40 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
41  TODO make more general
42  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
43 
44 
45 /* Using yarp::dev::impl namespace for all helper class inside yarp::dev to reduce
46  * name conflicts
47  */
48 
49 namespace yarp{
50  namespace dev{
51  class VirtualAnalogWrapper;
52  namespace impl {
53  class AnalogSubDevice;
54  }
55  }
56 }
57 
59 // TODO add IVirtualAnalogSensor interface to have Channels number and status??
60 
73 {
74 public:
75  VirtualAnalogWrapper() : mMutex()
76  {
77  lastRecv = 0;
78  mIsVerbose=false;
79  mNSubdevs=0;
80  first_check = false;
81  }
82 
84  {
85  close();
86  }
87 
88  // DeviceDriver //////////////////////////////////////////////////////////
89  bool open(yarp::os::Searchable& config) override;
90  bool close() override;
92 
93  // Thread ////////////////////////////////////////////////////////////////
94  void run() override;
96 
97  // IMultipleWrapper //////////////////////////////////////////////////////
98  bool attachAll(const yarp::dev::PolyDriverList &p) override;
99  bool detachAll() override;
101 
102  // Utility
103  bool perform_first_check(int elems);
104 
105 #ifndef DOXYGEN_SHOULD_SKIP_THIS
106 protected:
107  std::mutex mMutex;
108 
109  bool mIsVerbose;
110 
111  int mNSubdevs;
112 
113  std::vector<int> mChan2Board;
114  std::vector<int> mChan2BAddr;
115  double lastRecv;
116  bool first_check;
117 
118  std::vector<yarp::dev::impl::AnalogSubDevice> mSubdevices;
120 #endif // DOXYGEN_SHOULD_SKIP_THIS
121 };
122 
123 #ifndef DOXYGEN_SHOULD_SKIP_THIS
124 
125 class yarp::dev::impl::AnalogSubDevice
126 {
127 public:
128  AnalogSubDevice();
129  ~AnalogSubDevice();
130 
131  bool attach(yarp::dev::PolyDriver *driver, const std::string &key);
132  void detach();
133 
134  bool configure(int map0, int map1, const std::string &key);
135 
136  bool isAttached(){ return mIsAttached; }
137 
138  void setTorque(int joint,double torque)
139  {
140  if (joint<mMap0 || mMap1<joint) return;
141 
142  mTorques[joint-mMap0]=torque;
143  }
144 
145  void resetTorque()
146  {
147  mTorques.zero();
148  }
149 
150  void flushTorques()
151  {
152  if (mpSensor) mpSensor->updateVirtualAnalogSensorMeasure(mTorques);
153  }
154 
155  const std::string& getKey(){ return mKey; }
156 
157 protected:
158  std::string mKey;
159 
160  int mMap0,mMap1;
161 
162  yarp::sig::Vector mTorques;
163 
164  bool mIsConfigured;
165  bool mIsAttached;
166  double lastRecvMsg;
167  yarp::dev::PolyDriver *mpDevice;
169 };
170 
171 #endif // DOXYGEN_SHOULD_SKIP_THIS
172 
173 #endif // YARP_DEV_VIRTUALANALOGWRAPPER_VIRTUALANALOGWRAPPER_H
Interface implemented by all device drivers.
Definition: DeviceDriver.h:37
define the interface for an attachable object
An analog wrapper for virtual device A virtual device is a software emulated device, for example force-torque computed from a real sensor and then relocated to another part of the robot or some kind of estimated measurement needed by the robot.
A base class for nested structures that can be searched.
Definition: Searchable.h:68
Provides:
Definition: Vector.h:32
Interface for an object that can wrap/attach to to another.
Definition: Wrapper.h:67
The main, catch-all namespace for YARP.
Definition: numeric.h:47
virtual analog sensor interface
contains the definition of a Vector type
An abstraction for a thread of execution.
Definition: Thread.h:24
A container for a device driver.
Definition: PolyDriver.h:25
A generic interface to a virtual sensors.