40 #define M_PI 3.14159265358979323846
43 #define RAD2DEG 180/M_PI
44 #define DEG2RAD M_PI/180
52 m_enable_publish_map =
false;
53 m_enable_subscribe_map =
false;
63 bool ok = command.
read(connection);
73 reply.
addString(
"No commands currently available:");
77 yCError(MAP2D_NWS_ROS) <<
"Invalid command. Try `help`";
82 if (returnToSender !=
nullptr)
84 reply.
write(*returnToSender);
95 if (!config.
check(
"name"))
97 m_rpcPortName =
"/map2D_nws_ros/rpc";
105 if (config.
check(
"subdevice"))
111 if (!m_drv.open(p) || !m_drv.isValid())
113 yCError(MAP2D_NWS_ROS) <<
"Failed to open subdevice.. check params";
119 yCError(MAP2D_NWS_ROS) <<
"Failed to open subdevice.. check params";
125 yCInfo(MAP2D_NWS_ROS) <<
"Waiting for device to attach";
129 if (!m_rpcPort.open(m_rpcPortName))
131 yCError(MAP2D_NWS_ROS,
"Failed to open port %s", m_rpcPortName.c_str());
134 m_rpcPort.setReader(*
this);
137 if (config.
check(
"ROS"))
139 yCInfo(MAP2D_NWS_ROS,
"Configuring ROS params");
142 if (ROS_config.
find(
"enable_publisher").
asInt32() == 1 || ROS_config.
find(
"enable_publisher").
asString() ==
"true")
144 m_enable_publish_map =
true;
145 yCInfo(MAP2D_NWS_ROS) <<
"Enabled ROS publisher";
148 if (ROS_config.
find(
"enable_subscriber").
asInt32() == 1 || ROS_config.
find(
"enable_subscriber").
asString() ==
"true")
150 m_enable_subscribe_map =
true;
151 yCInfo(MAP2D_NWS_ROS) <<
"Enabled ROS subscriber";
154 if (m_enable_publish_map)
156 if (m_node ==
nullptr)
162 yCError(MAP2D_NWS_ROS) <<
"Unable to publish to" <<
ROSTOPICNAME_MAP <<
"topic, check your YARP-ROS network configuration";
174 if (m_enable_subscribe_map)
178 yCError(MAP2D_NWS_ROS) <<
"Unable to subscribe to " <<
ROSTOPICNAME_MAP <<
" topic, check your YARP-ROS network configuration";
186 m_subscriberPort_map.setStrict();
187 m_subscriberPort_metamap.setStrict();
196 yCWarning(MAP2D_NWS_ROS) <<
"ROS Group not configured";
202 bool Map2D_nws_ros::publishMapToRos(std::string map_name)
205 if (!m_iMap2D->get_map(map_name, current_map))
207 yCError(MAP2D_NWS_ROS) <<
"publishMapToRos() " << map_name <<
" does not exists";
227 v[0] = 0; v[1] = 0; v[2] = 1; v[3] =
t *
DEG2RAD;
236 for (cell.
y = current_map.
height(); cell.
y-- > 0;) {
237 for (cell.
x = 0; cell.
x < current_map.
width(); cell.
x++)
240 ogrid.
data[index++] = (int)tmp;
244 m_publisherPort_map.write();
252 bool Map2D_nws_ros::subscribeMapFromRos(std::string map_name)
259 map_ros = m_subscriberPort_map.
read(
true);
260 metamap_ros = m_subscriberPort_metamap.
read(
true);
261 if (map_ros !=
nullptr && metamap_ros !=
nullptr)
263 yCInfo(MAP2D_NWS_ROS) <<
"Received map for ROS";
264 std::string map_name =
"ros_map";
275 double orig_angle = vec[2];
277 for (
size_t y = 0; y < map_ros->
info.
height; y++)
279 for (
size_t x = 0; x < map_ros->
info.
width; x++)
285 if (occ >= 0 && occ <= 70) {
286 map.
setMapFlag(cell, MapGrid2D::MAP_CELL_FREE);
287 }
else if (occ >= 71 && occ <= 100) {
288 map.
setMapFlag(cell, MapGrid2D::MAP_CELL_WALL);
290 map.
setMapFlag(cell, MapGrid2D::MAP_CELL_UNKNOWN);
294 if (m_iMap2D->store_map(map))
300 yCInfo(MAP2D_NWS_ROS) <<
"Unable to add map " << map.
getMapName() <<
" to storage";
309 yCTrace(MAP2D_NWS_ROS,
"Close");
310 if (m_enable_publish_map)
312 m_publisherPort_map.interrupt();
313 m_publisherPort_metamap.interrupt();
314 m_publisherPort_map.close();
315 m_publisherPort_metamap.close();
317 if (m_enable_subscribe_map)
319 m_subscriberPort_map.interrupt();
320 m_subscriberPort_metamap.interrupt();
321 m_subscriberPort_map.close();
322 m_subscriberPort_metamap.close();
327 bool Map2D_nws_ros::updateVizMarkers(std::string map_name)
329 if (m_publisherPort_markers.asPort().isOpen()==
false)
331 m_publisherPort_markers.topic(
"/locationServerMarkers");
339 time = (uint64_t)(yarpTimeStamp * 1000000000UL);
340 nsec_part = (time % 1000000000UL);
341 sec_part = (time / 1000000000UL);
342 if (sec_part > std::numeric_limits<unsigned int>::max())
344 yCWarning(MAP2D_NWS_ROS) <<
"Timestamp exceeded the 64 bit representation, resetting it to 0";
356 std::vector<std::string> locations;
358 m_iMap2D->getLocationsList(locations);
359 for (
auto it : locations)
362 m_iMap2D->getLocation(it, loc);
365 rpy[2] = loc.
theta / 180 * 3.14159265359;
373 marker.
ns =
"my_namespace";
393 markers.
markers.push_back(marker);
397 m_publisherPort_markers.write();
412 driver->
view(m_iMap2D);
415 if (
nullptr == m_iMap2D)
417 yCError(MAP2D_NWS_ROS,
"Subdevice passed to attach method is invalid");
constexpr yarp::conf::vocab32_t VOCAB_ERR
#define ROSTOPICNAME_MAPMETADATA
bool close() override
Close the DeviceDriver.
bool attach(yarp::dev::PolyDriver *driver) override
Attach to another object.
bool detach() override
Detach the object (you must have first called attach).
bool open(yarp::os::Searchable ¶ms) override
Open the DeviceDriver.
Map2D_nws_ros()
Map2D_nws_ros.
bool view(T *&x)
Get an interface to the device driver.
size_t height() const
Retrieves the map height, expressed in cells.
bool setResolution(double resolution)
Sets the resolution of the map, i.e.
bool setOrigin(double x, double y, double theta)
Sets the origin of the map reference frame (according to ROS convention)
std::string getMapName() const
Retrieves the map name.
bool setSize_in_cells(size_t x, size_t y)
Sets the size of the map in cells.
void getOrigin(double &x, double &y, double &theta) const
Retrieves the origin of the map reference frame (according to ROS convention)
bool setMapName(std::string map_name)
Sets the map name.
bool setMapFlag(XYCell cell, map_flags flag)
Set the flag of a specific cell of the map.
size_t width() const
Retrieves the map width, expressed in cells.
bool setOccupancyData(XYCell cell, double occupancy)
Set the occupancy data of a specific cell of the map.
bool getOccupancyData(XYCell cell, double &occupancy) const
Retrieves the occupancy data of a specific cell of the map.
void getResolution(double &resolution) const
Retrieves the resolution of the map, i.e.
A container for a device driver.
bool isValid() const
Check if device is valid.
void fromRotationMatrix(const yarp::sig::Matrix &R)
Converts a rotation matrix to a quaternion.
void fromAxisAngle(const yarp::sig::Vector &v)
Computes the quaternion from an axis-angle representation.
A simple collection of objects that can be described and transmitted in a portable way.
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void clear()
Empties the bottle of any objects it contains.
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
A class for storing options and configuration information.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
A base class for nested structures that can be searched.
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 Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
virtual bool isString() const
Checks if value is a string.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual std::string asString() const
Get string value.
yarp::rosmsg::geometry_msgs::Point position
yarp::rosmsg::geometry_msgs::Quaternion orientation
std::vector< std::int8_t > data
yarp::rosmsg::nav_msgs::MapMetaData info
yarp::rosmsg::std_msgs::Header header
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
std::vector< yarp::rosmsg::visualization_msgs::Marker > markers
yarp::rosmsg::std_msgs::ColorRGBA color
static const std::uint8_t ARROW
yarp::rosmsg::TickDuration lifetime
yarp::rosmsg::geometry_msgs::Vector3 scale
static const std::uint8_t ADD
yarp::rosmsg::geometry_msgs::Pose pose
yarp::rosmsg::std_msgs::Header header
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
yarp::sig::Matrix rpy2dcm(const yarp::sig::Vector &rpy)
Converts roll-pitch-yaw angles in the corresponding dcm (direction cosine matrix) rotation matrix (de...
yarp::sig::Vector dcm2rpy(const yarp::sig::Matrix &R)
Converts a dcm (direction cosine matrix) rotation matrix to roll-pitch-yaw angles (defined in Math....
double now()
Return the current time in seconds, relative to an arbitrary starting point.
An interface to the operating system, including Port based communication.
std::uint32_t NetUint32
Definition of the NetUint32 type.