-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray_sensor_plugin.cc
102 lines (75 loc) · 2.83 KB
/
ray_sensor_plugin.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef MEMSLIDAR_SENSOR_PLUGIN
#define MEMSLIDAR_SENSOR_PLUGIN
// Include Gazebo headers.
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/plugins/RayPlugin.hh>
#include <gazebo/sensors/Sensor.hh>
#include <gazebo/sensors/SensorTypes.hh>
#include <gazebo/sensors/RaySensor.hh>
// Include ROS headers so we can communicate with our robot
#include <ros/ros.h>
// Include std::string's because they're pretty darn useful.
#include <string>
// So we don't have to do gazebo::everything
namespace gazebo
{
// Defining our plugin class
class MemslidarSensorPlugin : public RayPlugin
{
private:
sensors::RaySensorPtr _sensor;
private:
physics::LinkPtr _l;
public:
MemslidarSensorPlugin() {}
// Runs when the model is loaded
virtual void Load(sensors::SensorPtr _s, sdf::ElementPtr _sdf)
{
if(!ros::isInitialized)
{
ROS_FATAL_STREAM("ROS node for Gazebo not established. Plugin failed.");
return;
}
RayPlugin::Load(_s, _sdf);
_sensor = std::dynamic_pointer_cast<sensors::RaySensor>(_s);
std::string parentName = _sensor->ParentName();
std::string modelName = "";
std::string linkName = "";
modelName = parentName.substr(0, parentName.find(':'));
linkName = parentName.substr(parentName.find(':') + 2, parentName.size());
ROS_INFO("parentName:%s",parentName.c_str());
ROS_INFO("modelName:%s",modelName.c_str());
ROS_INFO("linkName:%s",linkName.c_str());
physics::WorldPtr _w = physics::get_world(_sensor->WorldName());
physics::ModelPtr _m = _w->ModelByName(modelName.c_str());
_l = _m->GetLink(linkName);
ROS_INFO("Laser Sensor Plugin Loaded");
//std::cout<<"Laser Sensor Plugin Loaded"<<std::endl;
}
virtual void OnNewLaserScans()
{
std::string out = "";
std::vector<double> ranges;
_sensor->Ranges(ranges);
double yaw = _sensor->Pose().Rot().Yaw() + _l->RelativePose().Rot().Yaw();
ROS_INFO("[sensor yaw %f]", _sensor->Pose().Rot().Yaw());
ROS_INFO("[link relative yaw %f]", _l->RelativePose().Rot().Yaw()*180.0/3.14159265);
ROS_INFO("[link relative pitch %f]", _l->RelativePose().Rot().Roll()*180.0/3.14159265);
ROS_INFO("[link world yaw %f]", _l->WorldPose().Rot().Yaw()*180.0/3.14159265);
ROS_INFO("[link world pitch %f]", _l->WorldPose().Rot().Roll()*180.0/3.14159265);
for(int i = 0; i < ranges.size(); i++)
{
out = out + "[";
out = out + std::to_string(i);
out = out + "] ";
out = out + std::to_string(ranges[i]);
out = out + ", ";
}
ROS_INFO("[%f] %s", yaw,out.c_str());
};
};
// Gazebo macro to set up the rest of the plugin functionality
GZ_REGISTER_SENSOR_PLUGIN(MemslidarSensorPlugin)
}
#endif