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 std;
13 using namespace yarp::dev;
14 
15 namespace {
16 YARP_LOG_COMPONENT(FAKEANALOGSENSOR, "yarp.device.fakeAnalogSensor")
17 }
18 
19 FakeAnalogSensor::FakeAnalogSensor(double period) : PeriodicThread(period),
20  mutex(),
21  channelsNum(0),
22  status(IAnalogSensor::AS_OK)
23 {
24  yCTrace(FAKEANALOGSENSOR);
25  timeStamp = yarp::os::Time::now();
26 }
27 
29 {
30  yCTrace(FAKEANALOGSENSOR);
31 }
32 
33 
35 {
36  yCTrace(FAKEANALOGSENSOR);
37  bool correct=true;
38 
39  //debug
40  fprintf(stderr, "%s\n", config.toString().c_str());
41 
42  // Check parameters first
43 // if(!config.check("channels"))
44 // {
45 // correct = false;
46 // yCError(FAKEANALOGSENSOR) << "Parameter 'channels' missing";
47 // }
48 
49  if(!config.check("period"))
50  {
51  correct = false;
52  yCError(FAKEANALOGSENSOR) << "Parameter 'period' missing";
53  }
54 
55  if (!correct)
56  {
57  yCError(FAKEANALOGSENSOR) << "Insufficient parameters to FakeAnalogSensor\n";
58  return false;
59  }
60 
61  double period=config.find("period").asInt32() / 1000.0;
62  setPeriod(period);
63 
64  //create the data vector:
65  this->channelsNum = 1;
66  data.resize(channelsNum);
67  data.zero();
68 
69  return PeriodicThread::start();
70 }
71 
73 {
74  yCTrace(FAKEANALOGSENSOR);
75  //stop the thread
76  PeriodicThread::stop();
77 
78  return true;
79 }
80 
82 {
83  mutex.lock();
84  out[0] = yarp::os::Time::now();
85  mutex.unlock();
86 
87  return status;
88 }
89 
91 {
92  yCTrace(FAKEANALOGSENSOR);
93  // Always ok for now
94  return status;
95 }
96 
98 {
99  yCTrace(FAKEANALOGSENSOR);
100  return channelsNum;
101 }
102 
104 {
105  yCTrace(FAKEANALOGSENSOR);
106  //NOT YET IMPLEMENTED
107  return 0;
108 }
109 
111 {
112  yCTrace(FAKEANALOGSENSOR);
113  //NOT YET IMPLEMENTED
114  return 0;
115 }
116 
118 {
119  yCTrace(FAKEANALOGSENSOR);
120  //NOT YET IMPLEMENTED
121  return 0;
122 }
123 
125 {
126  yCTrace(FAKEANALOGSENSOR);
127  //NOT YET IMPLEMENTED
128  return 0;
129 }
130 
132 {
133  yCTrace(FAKEANALOGSENSOR);
134  return true;
135 }
136 
138 {
139  mutex.lock();
140 
141  // Do fake stuff
142  double timeNow = yarp::os::Time::now();
143 
144  //if 100ms have passed since the last received message
145  if (timeNow > timeStamp + 10) {
146  status = IAnalogSensor::AS_TIMEOUT;
147  } else {
148  status = IAnalogSensor::AS_OK;
149  }
150 
151  timeStamp = timeNow;
152  mutex.unlock();
153 }
154 
156 {
157  yCTrace(FAKEANALOGSENSOR);
158 }
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:28
bool setPeriod(double period)
Set the (new) period of the thread.
A base class for nested structures that can be searched.
Definition: Searchable.h:66
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:222
void zero()
Zero the elements of the vector.
Definition: Vector.h:345
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCTrace(component,...)
Definition: LogComponent.h:85
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121