@@ -67,6 +67,17 @@ bool RGLServerPluginInstance::LoadConfiguration(const std::shared_ptr<const sdf:
67
67
if (!LidarPatternLoader::Load (sdf, lidarPattern)) {
68
68
return false ;
69
69
}
70
+
71
+ // Check for 2d pattern and get LaserScan parameters
72
+ if (sdf->HasElement (" pattern_lidar2d" )) {
73
+ ignmsg << " Lidar is 2D, switching to publish LaserScan messages" ;
74
+ publishLaserScan = true ;
75
+ resultDistances.resize (lidarPattern.size ());
76
+ scanHMin = sdf->FindElement (" pattern_lidar2d" )->FindElement (" horizontal" )->Get <float >(" min_angle" );
77
+ scanHMax = sdf->FindElement (" pattern_lidar2d" )->FindElement (" horizontal" )->Get <float >(" max_angle" );
78
+ scanHSamples = lidarPattern.size ();
79
+ }
80
+
70
81
// Resize container for result point cloud
71
82
resultPointCloud.resize (lidarPattern.size ());
72
83
@@ -84,10 +95,19 @@ void RGLServerPluginInstance::CreateLidar(ignition::gazebo::Entity entity,
84
95
0 , 0 , 1 , 0
85
96
};
86
97
98
+ // set desired fields for API call
99
+ std::vector<rgl_field_t > yieldFields;
100
+ yieldFields.push_back (RGL_FIELD_XYZ_F32);
101
+
102
+ if (publishLaserScan) {
103
+ yieldFields.push_back (RGL_FIELD_DISTANCE_F32);
104
+ }
105
+
87
106
if (!CheckRGL (rgl_node_rays_from_mat3x4f (&rglNodeUseRays, lidarPattern.data (), lidarPattern.size ())) ||
88
107
!CheckRGL (rgl_node_rays_transform (&rglNodeLidarPose, &identity)) ||
89
108
!CheckRGL (rgl_node_raytrace (&rglNodeRaytrace, nullptr , lidarRange)) ||
90
109
!CheckRGL (rgl_node_points_compact (&rglNodeCompact)) ||
110
+ !CheckRGL (rgl_node_points_yield (&rglNodeYield, yieldFields.data (), yieldFields.size ())) ||
91
111
!CheckRGL (rgl_node_points_transform (&rglNodeToLidarFrame, &identity))) {
92
112
93
113
ignerr << " Failed to create RGL nodes when initializing lidar. Disabling plugin.\n " ;
@@ -103,7 +123,23 @@ void RGLServerPluginInstance::CreateLidar(ignition::gazebo::Entity entity,
103
123
return ;
104
124
}
105
125
106
- pointCloudPublisher = gazeboNode.Advertise <ignition::msgs::PointCloudPacked>(topicName);
126
+ if (!publishLaserScan) {
127
+
128
+ if (!CheckRGL (rgl_graph_node_add_child (rglNodeToLidarFrame, rglNodeYield))) {
129
+ ignerr << " Failed to connect RGL nodes when initializing lidar. Disabling plugin.\n " ;
130
+ }
131
+ ignmsg << " Start publishing PointCloudPacked messages on topic '" << topicName << " '\n " ;
132
+ pointCloudPublisher = gazeboNode.Advertise <ignition::msgs::PointCloudPacked>(topicName);
133
+
134
+ } else {
135
+
136
+ if (!CheckRGL (rgl_graph_node_add_child (rglNodeRaytrace, rglNodeYield))) {
137
+ ignerr << " Failed to connect RGL nodes when initializing lidar. Disabling plugin.\n " ;
138
+ }
139
+ ignmsg << " Start publishing LaserScan messages on topic '" << topicName << " '\n " ;
140
+ laserScanPublisher = gazeboNode.Advertise <ignition::msgs::LaserScan>(topicName);
141
+
142
+ }
107
143
pointCloudWorldPublisher = gazeboNode.Advertise <ignition::msgs::PointCloudPacked>(topicName + worldTopicPostfix);
108
144
109
145
isLidarInitialized = true ;
@@ -155,15 +191,30 @@ void RGLServerPluginInstance::RayTrace(std::chrono::steady_clock::duration simTi
155
191
}
156
192
157
193
int32_t hitpointCount = 0 ;
158
- if (!CheckRGL (rgl_graph_get_result_size (rglNodeToLidarFrame, RGL_FIELD_XYZ_F32, &hitpointCount, nullptr )) ||
159
- !CheckRGL (rgl_graph_get_result_data (rglNodeToLidarFrame, RGL_FIELD_XYZ_F32, resultPointCloud.data ()))) {
160
194
161
- ignerr << " Failed to get result data from RGL lidar.\n " ;
195
+ if (!publishLaserScan) {
196
+ if (!CheckRGL (rgl_graph_get_result_size (rglNodeYield, RGL_FIELD_XYZ_F32, &hitpointCount, nullptr )) ||
197
+ !CheckRGL (rgl_graph_get_result_data (rglNodeYield, RGL_FIELD_XYZ_F32, resultPointCloud.data ()))) {
198
+
199
+ ignerr << " Failed to get result data from RGL lidar.\n " ;
200
+ return ;
201
+ }
202
+ } else {
203
+ if (!CheckRGL (rgl_graph_get_result_size (rglNodeYield, RGL_FIELD_DISTANCE_F32, &hitpointCount, nullptr )) ||
204
+ !CheckRGL (rgl_graph_get_result_data (rglNodeYield, RGL_FIELD_DISTANCE_F32, resultDistances.data ()))) {
205
+
206
+ ignerr << " Failed to get result distances from RGL lidar.\n " ;
162
207
return ;
208
+ }
163
209
}
164
210
165
- auto msg = CreatePointCloudMsg (simTime, frameId, hitpointCount);
166
- pointCloudPublisher.Publish (msg);
211
+ if (!publishLaserScan) {
212
+ auto msg = CreatePointCloudMsg (simTime, frameId, hitpointCount);
213
+ pointCloudPublisher.Publish (msg);
214
+ } else {
215
+ auto msg = CreateLaserScanMsg (simTime, frameId, hitpointCount);
216
+ laserScanPublisher.Publish (msg);
217
+ }
167
218
168
219
if (pointCloudWorldPublisher.HasConnections ()) {
169
220
if (!CheckRGL (rgl_graph_get_result_size (rglNodeToLidarFrame, RGL_FIELD_XYZ_F32, &hitpointCount, nullptr )) ||
@@ -177,6 +228,36 @@ void RGLServerPluginInstance::RayTrace(std::chrono::steady_clock::duration simTi
177
228
}
178
229
}
179
230
231
+ ignition::msgs::LaserScan RGLServerPluginInstance::CreateLaserScanMsg (std::chrono::steady_clock::duration simTime, std::string frame, int hitpointCount)
232
+ {
233
+ ignition::msgs::LaserScan outMsg;
234
+ *outMsg.mutable_header ()->mutable_stamp () = ignition::msgs::Convert (simTime);
235
+ auto _frame = outMsg.mutable_header ()->add_data ();
236
+ _frame->set_key (" frame_id" );
237
+ _frame->add_value (frame);
238
+
239
+ outMsg.set_frame (frame);
240
+ outMsg.set_count (hitpointCount);
241
+
242
+ outMsg.set_range_min (0.0 );
243
+ outMsg.set_range_max (lidarRange);
244
+
245
+ ignition::math::Angle hStep ((scanHMax-scanHMin)/scanHSamples);
246
+
247
+ outMsg.set_angle_min (scanHMin.Radian ());
248
+ outMsg.set_angle_max (scanHMax.Radian ());
249
+ outMsg.set_angle_step (hStep.Radian ());
250
+
251
+ if (outMsg.ranges_size () != hitpointCount) {
252
+ for (int i=0 ; i < hitpointCount; ++i) {
253
+ outMsg.add_ranges (resultDistances[i]);
254
+ outMsg.add_intensities (100.0 );
255
+ }
256
+ }
257
+
258
+ return outMsg;
259
+ }
260
+
180
261
ignition::msgs::PointCloudPacked RGLServerPluginInstance::CreatePointCloudMsg (std::chrono::steady_clock::duration simTime, std::string frame, int hitpointCount)
181
262
{
182
263
ignition::msgs::PointCloudPacked outMsg;
@@ -202,7 +283,11 @@ void RGLServerPluginInstance::DestroyLidar()
202
283
ignerr << " Failed to destroy RGL lidar.\n " ;
203
284
}
204
285
// Reset publishers
205
- pointCloudPublisher = ignition::transport::Node::Publisher ();
286
+ if (!publishLaserScan) {
287
+ pointCloudPublisher = ignition::transport::Node::Publisher ();
288
+ } else {
289
+ laserScanPublisher = ignition::transport::Node::Publisher ();
290
+ }
206
291
pointCloudWorldPublisher = ignition::transport::Node::Publisher ();
207
292
isLidarInitialized = false ;
208
293
}
0 commit comments