YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeLaserWithMotor.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#define _USE_MATH_DEFINES
7
9
10#include <yarp/os/Time.h>
11#include <yarp/os/Log.h>
12#include <yarp/os/LogStream.h>
14#include <yarp/math/Vec2D.h>
15#include <iostream>
16#include <limits>
17#include <cstring>
18#include <cstdlib>
19#include <cmath>
20
21//#define LASER_DEBUG
22#ifndef DEG2RAD
23#define DEG2RAD M_PI/180.0
24#endif
25
26YARP_LOG_COMPONENT(FAKE_LASER, "yarp.devices.fakeLaserWithMotor")
27
30using namespace yarp::dev::Nav2D;
31
32bool FakeLaserWithMotor::open(yarp::os::Searchable& config)
33{
34 if (!this->parseParams(config)) {return false;}
35
36 m_info = "Fake Laser device for test/debugging";
37 m_device_status = DEVICE_OK_STANDBY;
38
39#ifdef LASER_DEBUG
40 yCDebug(FAKE_LASER) << "%s\n", config.toString().c_str());
41#endif
42
43 if (config.check("help"))
44 {
45 yCInfo(FAKE_LASER,"Some examples:");
46 yCInfo(FAKE_LASER,"yarpdev --device fakeLaserWithMotor --help");
47 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test no_obstacles");
48 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_pattern");
49 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_constant --const_distance 0.5");
50 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_constant --const_distance 0.5 --SENSOR::resolution 0.5 --SKIP::min 0 50 --SKIP::max 10 60");
51 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map");
52 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_port /fakeLaser/location:i");
53 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_server /localizationServer");
54 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_client /fakeLaser/localizationClient --localization_server /localizationServer");
55 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_mapfile --map_context context --map_file mymap.map");
56 yCInfo(FAKE_LASER,"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaserWithMotor --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_client /fakeLaser/localizationClient --localization_server /localization2D_nws_yarp --localization_device localization2D_nwc_yarp");
57 return false;
58 }
59
60 std::string string_test_mode = m_test;
61 if (string_test_mode == "no_obstacles") { m_test_mode = NO_OBSTACLES; }
62 else if (string_test_mode == "use_pattern") { m_test_mode = USE_PATTERN; }
63 else if (string_test_mode == "use_mapfile") { m_test_mode = USE_MAPFILE; }
64 else if (string_test_mode == "use_constant") { m_test_mode = USE_CONSTANT_VALUE; }
65 else if (string_test_mode == "use_square_trap") { m_test_mode = USE_SQUARE_TRAP; }
66 else { yCError(FAKE_LASER) << "invalid/unknown value for param 'test'"; return false; }
67
68 //parse all the parameters related to the linear/angular range of the sensor
69 if (this->parseConfiguration(config) == false)
70 {
71 yCError(FAKE_LASER) << ": error parsing parameters";
72 return false;
73 }
74
75 if (m_test_mode == USE_SQUARE_TRAP)
76 {
77 m_robot_loc_x = 0;
78 m_robot_loc_y = 0;
79 m_robot_loc_t = 0;
80 m_map.m_map_name = "test_map_10x10m";
81 m_map.m_resolution = 0.01; //m/pixel
82 m_map.m_origin.setOrigin(-5,-5,0); //m
83 m_map.setSize_in_meters(10,10);
84 for (size_t y = 0; y < m_map.m_height; y++)
85 {
86 for (size_t x = 0; x < m_map.m_width; x++)
87 {
88 m_map.setOccupancyData(yarp::dev::Nav2D::XYCell(x, y),0);
89 m_map.setMapFlag(yarp::dev::Nav2D::XYCell(x, y), MapGrid2D::map_flags::MAP_CELL_FREE);
90 }
91 }
92 m_originally_loaded_map = m_map;
93 trap_the_robot(3); //3m
95 m_map.saveToFile("mio1");
96 }
97 else if (m_test_mode == USE_MAPFILE)
98 {
99 std::string map_file;
100 if (!m_MAP_MODE_map_context.empty() && !m_MAP_MODE_map_file.empty())
101 {
103 std::string tmp_filename = m_MAP_MODE_map_file;
104 std::string tmp_contextname = m_MAP_MODE_map_context;
107 bool b = rf.configure(0, nullptr);
108 if (b)
109 {
111 if (map_file == "")
112 {
113 yCWarning(FAKE_LASER, "Unable to find file: %s from context: %s\n", tmp_filename.c_str(), tmp_contextname.c_str());
114 }
115 }
116 else
117 {
118 yCWarning(FAKE_LASER, "Unable to find file: %s from context: %s\n", tmp_filename.c_str(), tmp_contextname.c_str());
119 }
120 }
121 else if (!m_MAP_MODE_map_file.empty())
122 {
123 map_file = m_MAP_MODE_map_file;
124 }
125 else
126 {
127 yCError(FAKE_LASER) << "Missing `map_file` or `map_context`+`map_file` parameters";
128 return false;
129 }
130
131 if (map_file=="")
132 {
133 yCError(FAKE_LASER) << "File not found";
134 return false;
135 }
136 bool ret = m_originally_loaded_map.loadFromFile(map_file);
137 if (ret == false)
138 {
139 yCError(FAKE_LASER) << "A problem occurred while opening:" << map_file;
140 return false;
141 }
142 m_map = m_originally_loaded_map;
143 m_robot_loc_x=0;
144 m_robot_loc_y=0;
145 m_robot_loc_t=0;
146 }
147
148 yCInfo(FAKE_LASER) << "Starting debug mode";
149 yCInfo(FAKE_LASER) << "test mode:"<< m_test_mode << " i.e. " << string_test_mode;
150
151 // INIT ALL INTERFACES
152 std::vector<double> tmpZeros; tmpZeros.resize(m_njoints, 0.0);
153 std::vector<double> tmpOnes; tmpOnes.resize(m_njoints, 1.0);
154 std::vector<int> axisMap; axisMap.resize(m_njoints, 1.0);
155 axisMap[0] = 0;
156 axisMap[1] = 1;
157 axisMap[2] = 2;
158 ImplementEncodersTimed::initialize(m_njoints, axisMap.data(), tmpOnes.data(), nullptr);
159 ImplementPositionControl::initialize(m_njoints, axisMap.data(), tmpOnes.data(), nullptr);
160 ImplementControlMode::initialize(m_njoints, axisMap.data());
161 ImplementVelocityControl::initialize(m_njoints, axisMap.data(), tmpOnes.data(), nullptr);
162 ImplementInteractionMode::initialize(m_njoints, axisMap.data(), tmpOnes.data(), nullptr);
163 ImplementAxisInfo::initialize(m_njoints, axisMap.data());
164 if (!alloc(m_njoints))
165 {
166 yCError(FAKE_LASER) << "Malloc failed";
167 return false;
168 }
169 _jointType[0] = VOCAB_JOINTTYPE_PRISMATIC;
170 _jointType[1] = VOCAB_JOINTTYPE_PRISMATIC;
171 _jointType[2] = VOCAB_JOINTTYPE_REVOLUTE;
172 _axisName[0] = "joint_x";
173 _axisName[1] = "joint_y";
174 _axisName[2] = "joint_t";
175 _controlModes[0] = VOCAB_CM_IDLE;
176 _controlModes[1] = VOCAB_CM_IDLE;
177 _controlModes[2] = VOCAB_CM_IDLE;
178
179 if (!m_rpcPort.open("/fakeLaser/rpc:i"))
180 {
181 yCError(FAKE_LASER, "Failed to open port %s", "/fakeLaser/rpc:i");
182 return false;
183 }
184 m_rpcPort.setReader(*this);
185
186 return PeriodicThread::start();
187}
188
190{
192 dealloc();
195
196 return true;
197}
198
200{
201#ifdef LASER_DEBUG
202 yCDebug(FAKE_LASER)<<"thread initialising...\n");
203 yCDebug(FAKE_LASER)<<"... done!\n");
204#endif
205
206 return true;
207}
208
210{
211 for (size_t i=0; i<m_njoints; i++)
212 {
214 {
216 }
217 else if (_controlModes[i] == VOCAB_CM_POSITION)
218 {
219 /* if (a>b)
220 {
221 _posCtrl_references[i] = _posCtrl_references[i] + _command_speeds[i] / m_period * 1000;
222 if (b > a) _posCtrl_references[i] =a;
223 }
224 else if (b<a)
225 {
226 _posCtrl_references[i] = _posCtrl_references[i] + _command_speeds[i] / m_period * 1000;
227 if (b > a) _posCtrl_references[i] = a;
228 }
229*/
230 }
231 }
232 m_mutex.lock();
234 m_mutex.unlock();
235 return;
236}
237
239{
240 yarp::os::Bottle command;
241 yarp::os::Bottle reply;
242 bool ok = command.read(connection);
243 if (!ok) {
244 return false;
245 }
246 reply.clear();
247
248 if (command.get(0).asString() == "trap")
249 {
250 if (command.size() == 1)
251 {
252 trap_the_robot();
253 reply.addVocab32(VOCAB_OK);
254 }
255 else if (command.size() == 2)
256 {
257 trap_the_robot(command.get(1).asFloat64());
258 reply.addVocab32(VOCAB_OK);
259 }
260 else
261 {
262 reply.addVocab32(VOCAB_ERR);
263 }
264 }
265 else if (command.get(0).asString() == "wall")
266 {
267 if (command.size() == 1)
268 {
269 wall_the_robot(1.0, 1.0);
270 wall_the_robot(1.0, 1.05);
271 reply.addVocab32(VOCAB_OK);
272 }
273 else if (command.size() == 2)
274 {
275 wall_the_robot(command.get(1).asFloat64(), 1.0);
276 wall_the_robot(command.get(1).asFloat64(), 1.05);
277 reply.addVocab32(VOCAB_OK);
278 }
279 else if (command.size() == 3)
280 {
281 wall_the_robot(command.get(1).asFloat64(), command.get(2).asFloat64());
282 wall_the_robot(command.get(1).asFloat64(), command.get(2).asFloat64()+0.05);
283 reply.addVocab32(VOCAB_OK);
284 }
285 else
286 {
287 reply.addVocab32(VOCAB_ERR);
288 }
289 }
290 else if (command.get(0).asString() == "free")
291 {
292 free_the_robot();
293 reply.addVocab32(VOCAB_OK);
294 }
295 else if (command.get(0).asString() == "help")
296 {
297 reply.addVocab32("many");
298 reply.addString("wall <size> <distance>: creates a wall in front of the robot");
299 reply.addString("trap <size>: traps the robot in a box obstacle");
300 reply.addString("free: removes all generated obstacles");
301 }
302 else
303 {
304 yCError(FAKE_LASER) << "Invalid command";
305 reply.addVocab32(VOCAB_ERR);
306 }
307
309 if (returnToSender != nullptr)
310 {
311 reply.write(*returnToSender);
312 }
313 return true;
314}
315
317{
318#ifdef LASER_DEBUG
319 yCDebug(FAKE_LASER) << "FakeLaser Thread releasing...");
320 yCDebug(FAKE_LASER) << "... done.");
321#endif
322}
const yarp::os::LogComponent & FAKE_LASER()
const yarp::os::LogComponent & FAKE_LASER()
Definition FakeLaser.cpp:26
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_ERR
constexpr yarp::conf::vocab32_t VOCAB_CM_IDLE
constexpr yarp::conf::vocab32_t VOCAB_CM_POSITION
constexpr yarp::conf::vocab32_t VOCAB_CM_VELOCITY
bool ret
fakeLaserWithMotor : fake sensor device driver for testing purposes and reference for IRangefinder2D ...
void run() override
Loop function.
bool threadInit() override
Initialization method.
bool close() override
Close the DeviceDriver.
void threadRelease() override
Release method.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
yarp::os::Port m_rpcPort
bool initialize(int size, const int *amap)
Initialize the internal data and alloc memory.
bool initialize(int k, const int *amap)
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
bool initialize(int size, const int *amap)
Initialize the internal data and alloc memory, smaller version.
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
virtual bool updateLidarData()
This utility method calls in sequence: grabDataFromHW(), updateTimestamp and applyLimitsOnLaserData()...
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition Bottle.cpp:164
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:251
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:240
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition Bottle.cpp:230
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
An interface for writing to a network connection.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Port.cpp:383
void close() override
Stop port activity.
Definition Port.cpp:363
Helper class for finding config files and other external resources.
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
std::string findFile(const std::string &name)
Find the full path to a file.
bool setDefaultConfigFile(const std::string &fname)
Provide a default value for the configuration file (can be overridden from command line with the –fro...
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition Value.cpp:222
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
@ VOCAB_JOINTTYPE_REVOLUTE
Definition IAxisInfo.h:24
@ VOCAB_JOINTTYPE_PRISMATIC
Definition IAxisInfo.h:25
The main, catch-all namespace for YARP.
Definition dirs.h:16