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