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
AnalogSubDevice::resetTorque
void resetTorque()
Definition: VirtualAnalogWrapper.h:133
AnalogSubDevice::attach
bool attach(yarp::dev::PolyDriver *driver, const std::string &key)
Definition: VirtualAnalogWrapper.cpp:57
VirtualAnalogWrapper::VirtualAnalogWrapper
VirtualAnalogWrapper(const VirtualAnalogWrapper &)=delete
VirtualAnalogWrapper
virtualAnalogServer: An analog wrapper for virtual device A virtual device is a software emulated dev...
Definition: VirtualAnalogWrapper.h:66
Network.h
VirtualAnalogWrapper::mMutex
std::mutex mMutex
Definition: VirtualAnalogWrapper.h:97
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
Vector.h
contains the definition of a Vector type
VirtualAnalogWrapper::operator=
VirtualAnalogWrapper & operator=(const VirtualAnalogWrapper &)=delete
AnalogSubDevice::~AnalogSubDevice
~AnalogSubDevice()
Definition: VirtualAnalogWrapper.cpp:30
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
AnalogSubDevice::mpSensor
yarp::dev::IVirtualAnalogSensor * mpSensor
Definition: VirtualAnalogWrapper.h:156
AnalogSubDevice::mMap0
int mMap0
Definition: VirtualAnalogWrapper.h:148
yarp::dev::IVirtualAnalogSensor::updateVirtualAnalogSensorMeasure
virtual bool updateVirtualAnalogSensorMeasure(yarp::sig::Vector &measure)=0
Set a vector of torque values for virtual sensor.
VirtualAnalogWrapper::mSubdevices
std::vector< AnalogSubDevice > mSubdevices
Definition: VirtualAnalogWrapper.h:108
AnalogSubDevice::mpDevice
yarp::dev::PolyDriver * mpDevice
Definition: VirtualAnalogWrapper.h:155
VirtualAnalogWrapper::close
bool close() override
Close the DeviceDriver.
Definition: VirtualAnalogWrapper.cpp:207
VirtualAnalogWrapper::perform_first_check
bool perform_first_check(int elems)
Definition: VirtualAnalogWrapper.cpp:270
AnalogSubDevice::mKey
std::string mKey
Definition: VirtualAnalogWrapper.h:146
yarp::os::Thread
An abstraction for a thread of execution.
Definition: Thread.h:25
VirtualAnalogWrapper::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: VirtualAnalogWrapper.cpp:110
AnalogSubDevice::configure
bool configure(int map0, int map1, const std::string &key)
Definition: VirtualAnalogWrapper.cpp:35
yarp::dev::PolyDriverList
Definition: PolyDriverList.h:22
VirtualAnalogWrapper::mChan2Board
std::vector< int > mChan2Board
Definition: VirtualAnalogWrapper.h:103
PortablePair.h
AnalogSubDevice::mIsAttached
bool mIsAttached
Definition: VirtualAnalogWrapper.h:153
yarp::sig::VectorOf< double >
VirtualAnalogWrapper::~VirtualAnalogWrapper
~VirtualAnalogWrapper() override
Definition: VirtualAnalogWrapper.h:74
yarp::os::BufferedPort< yarp::os::Bottle >
AnalogSubDevice::setTorque
void setTorque(int joint, double torque)
Definition: VirtualAnalogWrapper.h:126
VirtualAnalogWrapper::VirtualAnalogWrapper
VirtualAnalogWrapper(VirtualAnalogWrapper &&)=delete
AnalogSubDevice::detach
void detach()
Definition: VirtualAnalogWrapper.cpp:99
AnalogSubDevice::flushTorques
void flushTorques()
Definition: VirtualAnalogWrapper.h:138
PolyDriver.h
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
AnalogSubDevice::mMap1
int mMap1
Definition: VirtualAnalogWrapper.h:148
AnalogSubDevice::isAttached
bool isAttached()
Definition: VirtualAnalogWrapper.h:124
VirtualAnalogWrapper::operator=
VirtualAnalogWrapper & operator=(VirtualAnalogWrapper &&)=delete
VirtualAnalogWrapper::detachAll
bool detachAll() override
Detach the object (you must have first called attach).
Definition: VirtualAnalogWrapper.cpp:254
yarp::sig::VectorOf::zero
void zero()
Zero the elements of the vector.
Definition: Vector.h:377
yarp::dev::IVirtualAnalogSensor
A generic interface to a virtual sensors.
Definition: IVirtualAnalogSensor.h:49
VirtualAnalogWrapper::attachAll
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
Definition: VirtualAnalogWrapper.cpp:215
VirtualAnalogWrapper::first_check
bool first_check
Definition: VirtualAnalogWrapper.h:106
AnalogSubDevice::mIsConfigured
bool mIsConfigured
Definition: VirtualAnalogWrapper.h:152
BufferedPort.h
AnalogSubDevice
Definition: VirtualAnalogWrapper.h:114
IMultipleWrapper.h
VirtualAnalogWrapper::lastRecv
double lastRecv
Definition: VirtualAnalogWrapper.h:105
PeriodicThread.h
VirtualAnalogWrapper::mNSubdevs
int mNSubdevs
Definition: VirtualAnalogWrapper.h:101
AnalogSubDevice::lastRecvMsg
double lastRecvMsg
Definition: VirtualAnalogWrapper.h:154
AnalogSubDevice::getKey
const std::string & getKey()
Definition: VirtualAnalogWrapper.h:143
VirtualAnalogWrapper::run
void run() override
Main body of the new thread.
Definition: VirtualAnalogWrapper.cpp:289
yarp::dev::IMultipleWrapper
Interface for an object that can wrap/attach to to another.
Definition: IMultipleWrapper.h:30
Vocab.h
VirtualAnalogWrapper::mChan2BAddr
std::vector< int > mChan2BAddr
Definition: VirtualAnalogWrapper.h:104
AnalogSubDevice::mTorques
yarp::sig::Vector mTorques
Definition: VirtualAnalogWrapper.h:150
Time.h
IVirtualAnalogSensor.h
virtual analog sensor interface
VirtualAnalogWrapper::VirtualAnalogWrapper
VirtualAnalogWrapper()=default
VirtualAnalogWrapper::mPortInputTorques
yarp::os::BufferedPort< yarp::os::Bottle > mPortInputTorques
Definition: VirtualAnalogWrapper.h:109
VirtualAnalogWrapper::mIsVerbose
bool mIsVerbose
Definition: VirtualAnalogWrapper.h:99
AnalogSubDevice::AnalogSubDevice
AnalogSubDevice()
Definition: VirtualAnalogWrapper.cpp:25