YARP
Yet Another Robot Platform
fakeAnalogSensor.cpp
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 #include "fakeAnalogSensor.h"
7 
8 #include <yarp/os/Time.h>
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
11 
12 using namespace yarp::dev;
13 
14 namespace {
15 YARP_LOG_COMPONENT(FAKEANALOGSENSOR, "yarp.device.fakeAnalogSensor")
16 }
17 
18 FakeAnalogSensor::FakeAnalogSensor(double period) : PeriodicThread(period),
19  mutex(),
20  channelsNum(0),
21  status(IAnalogSensor::AS_OK)
22 {
23  yCTrace(FAKEANALOGSENSOR);
24  timeStamp = yarp::os::Time::now();
25 }
26 
28 {
29  yCTrace(FAKEANALOGSENSOR);
30 }
31 
32 
34 {
35  yCTrace(FAKEANALOGSENSOR);
36  bool correct=true;
37 
38  //debug
39  fprintf(stderr, "%s\n", config.toString().c_str());
40 
41  // Check parameters first
42 // if(!config.check("channels"))
43 // {
44 // correct = false;
45 // yCError(FAKEANALOGSENSOR) << "Parameter 'channels' missing";
46 // }
47 
48  if(!config.check("period"))
49  {
50  correct = false;
51  yCError(FAKEANALOGSENSOR) << "Parameter 'period' missing";
52  }
53 
54  if (!correct)
55  {
56  yCError(FAKEANALOGSENSOR) << "Insufficient parameters to FakeAnalogSensor\n";
57  return false;
58  }
59 
60  double period=config.find("period").asInt32() / 1000.0;
61  setPeriod(period);
62 
63  //create the data vector:
64  this->channelsNum = 1;
65  data.resize(channelsNum);
66  data.zero();
67 
68  return PeriodicThread::start();
69 }
70 
72 {
73  yCTrace(FAKEANALOGSENSOR);
74  //stop the thread
75  PeriodicThread::stop();
76 
77  return true;
78 }
79 
81 {
82  mutex.lock();
83  out[0] = yarp::os::Time::now();
84  mutex.unlock();
85 
86  return status;
87 }
88 
90 {
91  yCTrace(FAKEANALOGSENSOR);
92  // Always ok for now
93  return status;
94 }
95 
97 {
98  yCTrace(FAKEANALOGSENSOR);
99  return channelsNum;
100 }
101 
103 {
104  yCTrace(FAKEANALOGSENSOR);
105  //NOT YET IMPLEMENTED
106  return 0;
107 }
108 
110 {
111  yCTrace(FAKEANALOGSENSOR);
112  //NOT YET IMPLEMENTED
113  return 0;
114 }
115 
117 {
118  yCTrace(FAKEANALOGSENSOR);
119  //NOT YET IMPLEMENTED
120  return 0;
121 }
122 
124 {
125  yCTrace(FAKEANALOGSENSOR);
126  //NOT YET IMPLEMENTED
127  return 0;
128 }
129 
131 {
132  yCTrace(FAKEANALOGSENSOR);
133  return true;
134 }
135 
137 {
138  mutex.lock();
139 
140  // Do fake stuff
141  double timeNow = yarp::os::Time::now();
142 
143  //if 100ms have passed since the last received message
144  if (timeNow > timeStamp + 10) {
145  status = IAnalogSensor::AS_TIMEOUT;
146  } else {
147  status = IAnalogSensor::AS_OK;
148  }
149 
150  timeStamp = timeNow;
151  mutex.unlock();
152 }
153 
155 {
156  yCTrace(FAKEANALOGSENSOR);
157 }
bool threadInit() override
Initialization method.
void run() override
Loop function.
int calibrateChannel(int ch) override
Calibrates one single channel.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
int getChannels() override
Get the number of channels of the sensor.
int calibrateSensor() override
Calibrates the whole sensor.
int read(yarp::sig::Vector &out) override
Read a vector from the sensor.
void threadRelease() override
Release method.
int getState(int ch) override
Check the state value of a given channel.
FakeAnalogSensor(double period=0.02)
A generic interface to sensors (gyro, a/d converters).
Definition: IAnalogSensor.h:27
bool setPeriod(double period)
Set the (new) period of the thread.
A base class for nested structures that can be searched.
Definition: Searchable.h:63
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:220
void zero()
Zero the elements of the vector.
Definition: Vector.h:343
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCTrace(component,...)
Definition: LogComponent.h:84
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
For streams capable of holding different kinds of content, check what they actually have.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121