YARP
Yet Another Robot Platform
AnalogSensorClient.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_ANALOGSENSORCLIENT_ANALOGSENSORCLIENT_H
10 #define YARP_DEV_ANALOGSENSORCLIENT_ANALOGSENSORCLIENT_H
11 
12 
13 #include <yarp/os/Network.h>
14 #include <yarp/os/BufferedPort.h>
16 #include <yarp/dev/IAnalogSensor.h>
19 #include <yarp/sig/Vector.h>
20 #include <yarp/os/Time.h>
21 #include <yarp/dev/PolyDriver.h>
22 
23 #include <mutex>
24 
25 
26 const int ANALOG_TIMEOUT=100; //ms
27 
28 
29 class InputPortProcessor : public yarp::os::BufferedPort<yarp::sig::Vector>
30 {
31  yarp::sig::Vector lastVector;
32  std::mutex mutex;
33  yarp::os::Stamp lastStamp;
34  double deltaT;
35  double deltaTMax;
36  double deltaTMin;
37  double prev;
38  double now;
39 
40  int state;
41  int count;
42 
43 public:
44 
45  inline void resetStat();
46 
48 
50  void onRead(yarp::sig::Vector &v) override;
51 
52  inline int getLast(yarp::sig::Vector &data, yarp::os::Stamp &stmp);
53 
54  inline int getIterations();
55 
56  // time is in ms
57  void getEstFrequency(int &ite, double &av, double &min, double &max);
58 
59  int getState();
60 
61  int getChannels();
62 };
63 
64 /*
65  * The client side of any IAnalogSensor capable device.
66  * Still single thread! concurrent access is unsafe.
67  */
68 
93 {
94 protected:
97  std::string local;
98  std::string remote;
99  yarp::os::Stamp lastTs; //used by IPreciselyTimed
100  std::string robotName;
101  std::string deviceId;
102  std::string sensorType;
103  std::string portPrefix;
104 
105  void removeLeadingTrailingSlashesOnly(std::string &name);
106 
107 public:
108  AnalogSensorClient() = default;
113  ~AnalogSensorClient() override = default;
114 
115  /* DeviceDriver methods */
116  bool open(yarp::os::Searchable& config) override;
117  bool close() override;
118 
119  /* IAnalogSensor methods*/
120  int read(yarp::sig::Vector &out) override;
121 
122  /* Check the state value of a given channel.
123  * @param ch: channel number.
124  * @return status.
125  */
126  int getState(int ch) override;
127 
128  /* Get the number of channels of the sensor.
129  * @return number of channels (0 in case of errors).
130  */
131  int getChannels() override;
132 
133  /* Calibrates the whole sensor.
134  * @return status.
135  */
136  int calibrateSensor() override;
137 
138  /* Calibrates the whole sensor, using a vector of calibration values.
139  * @param value: a vector of calibration values.
140  * @return status.
141  */
142  int calibrateSensor(const yarp::sig::Vector& value) override;
143 
144  /* Calibrates one single channel.
145  * @param ch: channel number.
146  * @return status.
147  */
148  int calibrateChannel(int ch) override;
149 
150  /* Calibrates one single channel.
151  * @param ch: channel number.
152  * @param value: calibration value.
153  * @return status.
154  */
155  int calibrateChannel(int ch, double value) override;
156 
157  /* IPreciselyTimed methods */
163 };
164 
165 #endif // YARP_DEV_ANALOGSENSORCLIENT_ANALOGSENSORCLIENT_H
Network.h
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
AnalogSensorClient::operator=
AnalogSensorClient & operator=(const AnalogSensorClient &)=delete
Vector.h
contains the definition of a Vector type
InputPortProcessor::getChannels
int getChannels()
Definition: AnalogSensorClient.cpp:150
InputPortProcessor
Definition: AnalogSensorClient.h:30
IPreciselyTimed.h
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
ControlBoardHelpers.h
AnalogSensorClient::getState
int getState(int ch) override
Check the state value of a given channel.
Definition: AnalogSensorClient.cpp:257
AnalogSensorClient::AnalogSensorClient
AnalogSensorClient()=default
InputPortProcessor::onRead
void onRead(yarp::sig::Vector &v) override
Definition: AnalogSensorClient.cpp:52
ControlBoardInterfaces.h
define control board standard interfaces
AnalogSensorClient::deviceId
std::string deviceId
Definition: AnalogSensorClient.h:101
InputPortProcessor::InputPortProcessor
InputPortProcessor()
Definition: AnalogSensorClient.cpp:46
AnalogSensorClient::rpcPort
yarp::os::Port rpcPort
Definition: AnalogSensorClient.h:96
yarp::sig::VectorOf< double >
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
AnalogSensorClient::getChannels
int getChannels() override
Get the number of channels of the sensor.
Definition: AnalogSensorClient.cpp:263
ANALOG_TIMEOUT
const int ANALOG_TIMEOUT
Definition: AnalogSensorClient.h:26
AnalogSensorClient::local
std::string local
Definition: AnalogSensorClient.h:97
AnalogSensorClient::sensorType
std::string sensorType
Definition: AnalogSensorClient.h:102
InputPortProcessor::getLast
int getLast(yarp::sig::Vector &data, yarp::os::Stamp &stmp)
Definition: AnalogSensorClient.cpp:104
AnalogSensorClient
analogsensorclient: Device that reads an AnalogSensor (using the IAnalogSensor interface) from the YA...
Definition: AnalogSensorClient.h:93
PolyDriver.h
InputPortProcessor::getEstFrequency
void getEstFrequency(int &ite, double &av, double &min, double &max)
Definition: AnalogSensorClient.cpp:127
AnalogSensorClient::close
bool close() override
Close the DeviceDriver.
Definition: AnalogSensorClient.cpp:245
InputPortProcessor::getIterations
int getIterations()
Definition: AnalogSensorClient.cpp:118
AnalogSensorClient::getLastInputStamp
yarp::os::Stamp getLastInputStamp() override
Get the time stamp for the last read data.
Definition: AnalogSensorClient.cpp:310
AnalogSensorClient::calibrateSensor
int calibrateSensor() override
Calibrates the whole sensor.
Definition: AnalogSensorClient.cpp:268
yarp::dev::IAnalogSensor
A generic interface to sensors (gyro, a/d converters).
Definition: IAnalogSensor.h:31
InputPortProcessor::getState
int getState()
Definition: AnalogSensorClient.cpp:145
BufferedPort.h
AnalogSensorClient::calibrateChannel
int calibrateChannel(int ch) override
Calibrates one single channel.
Definition: AnalogSensorClient.cpp:289
AnalogSensorClient::remote
std::string remote
Definition: AnalogSensorClient.h:98
yarp::os::Stamp
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
AnalogSensorClient::AnalogSensorClient
AnalogSensorClient(const AnalogSensorClient &)=delete
AnalogSensorClient::AnalogSensorClient
AnalogSensorClient(AnalogSensorClient &&)=delete
AnalogSensorClient::removeLeadingTrailingSlashesOnly
void removeLeadingTrailingSlashesOnly(std::string &name)
Definition: AnalogSensorClient.cpp:162
AnalogSensorClient::lastTs
yarp::os::Stamp lastTs
Definition: AnalogSensorClient.h:99
AnalogSensorClient::operator=
AnalogSensorClient & operator=(AnalogSensorClient &&)=delete
AnalogSensorClient::~AnalogSensorClient
~AnalogSensorClient() override=default
InputPortProcessor::resetStat
void resetStat()
Definition: AnalogSensorClient.cpp:34
Time.h
AnalogSensorClient::read
int read(yarp::sig::Vector &out) override
Read a vector from the sensor.
Definition: AnalogSensorClient.cpp:252
IAnalogSensor.h
analog sensor interface
AnalogSensorClient::portPrefix
std::string portPrefix
Definition: AnalogSensorClient.h:103
AnalogSensorClient::robotName
std::string robotName
Definition: AnalogSensorClient.h:100
yarp::dev::IPreciselyTimed
Definition: IPreciselyTimed.h:21
AnalogSensorClient::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: AnalogSensorClient.cpp:189
AnalogSensorClient::inputPort
InputPortProcessor inputPort
Definition: AnalogSensorClient.h:95