YARP
Yet Another Robot Platform
VirtualAnalogWrapper.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_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>
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 
46 // TODO add IVirtualAnalogSensor interface to have Channels number and status??
47 
48 
49 class AnalogSubDevice;
50 
64  public yarp::os::Thread,
66 {
67 public:
68  VirtualAnalogWrapper() = default;
73 
75  {
76  close();
77  }
78 
79  // DeviceDriver //////////////////////////////////////////////////////////
80  bool open(yarp::os::Searchable& config) override;
81  bool close() override;
83 
84  // Thread ////////////////////////////////////////////////////////////////
85  void run() override;
87 
88  // IMultipleWrapper //////////////////////////////////////////////////////
89  bool attachAll(const yarp::dev::PolyDriverList &p) override;
90  bool detachAll() override;
92 
93  // Utility
94  bool perform_first_check(int elems);
95 
96 protected:
97  std::mutex mMutex;
98 
99  bool mIsVerbose{false};
100 
101  int mNSubdevs{0};
102 
103  std::vector<int> mChan2Board;
104  std::vector<int> mChan2BAddr;
105  double lastRecv{0.0};
106  bool first_check{false};
107 
108  std::vector<AnalogSubDevice> mSubdevices;
110 };
111 
112 
114 {
115 public:
116  AnalogSubDevice();
118 
119  bool attach(yarp::dev::PolyDriver *driver, const std::string &key);
120  void detach();
121 
122  bool configure(int map0, int map1, const std::string &key);
123 
124  bool isAttached(){ return mIsAttached; }
125 
126  void setTorque(int joint,double torque)
127  {
128  if (joint<mMap0 || mMap1<joint) return;
129 
130  mTorques[joint-mMap0]=torque;
131  }
132 
133  void resetTorque()
134  {
135  mTorques.zero();
136  }
137 
139  {
141  }
142 
143  const std::string& getKey(){ return mKey; }
144 
145 protected:
146  std::string mKey;
147 
149 
151 
154  double lastRecvMsg{0.0};
157 };
158 
159 
160 #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()
An analog wrapper for virtual device A virtual device is a software emulated device,...
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:38
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:27
A base class for nested structures that can be searched.
Definition: Searchable.h:69
An abstraction for a thread of execution.
Definition: Thread.h:25
void zero()
Zero the elements of the vector.
Definition: Vector.h:377