forked from clearpathrobotics/LMS1xx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLMS1xx_node.cpp
155 lines (119 loc) · 4.16 KB
/
LMS1xx_node.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <csignal>
#include <cstdio>
#include <LMS1xx/LMS1xx.h>
#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#define DEG2RAD M_PI/180.0
int main(int argc, char **argv)
{
// laser data
LMS1xx laser;
scanCfg cfg;
scanOutputRange outputRange;
scanDataCfg dataCfg;
scanData data;
// published data
sensor_msgs::LaserScan scan_msg;
// parameters
std::string host;
std::string frame_id;
ros::init(argc, argv, "lms1xx");
ros::NodeHandle nh;
ros::NodeHandle n("~");
ros::Publisher scan_pub = nh.advertise<sensor_msgs::LaserScan>("scan", 1);
n.param<std::string>("host", host, "192.168.1.2");
n.param<std::string>("frame_id", frame_id, "laser");
while(ros::ok())
{
ROS_INFO("Connecting to laser at : %s", host.c_str());
// initialize hardware
do {
laser.connect(host);
if (laser.isConnected())
{
laser.login();
cfg = laser.getScanCfg();
outputRange = laser.getScanOutputRange();
}
//check if laser is fully initialized, else reconnect
//assuming fully initialized => scaningFrequency=5000
if (cfg.scaningFrequency != 5000) {
laser.disconnect();
ROS_INFO("Waiting for laser to initialize...");
}
} while (!laser.isConnected() || cfg.scaningFrequency != 5000);
if (laser.isConnected()) {
ROS_INFO("Connected to laser.");
ROS_DEBUG("Laser configuration: scaningFrequency %d, angleResolution %d, startAngle %d, stopAngle %d",
cfg.scaningFrequency, cfg.angleResolution, cfg.startAngle, cfg.stopAngle);
ROS_DEBUG("Laser output range:angleResolution %d, startAngle %d, stopAngle %d",
outputRange.angleResolution, outputRange.startAngle, outputRange.stopAngle);
scan_msg.header.frame_id = frame_id;
scan_msg.range_min = 0.01;
scan_msg.range_max = 20.0;
scan_msg.scan_time = 100.0/cfg.scaningFrequency;
scan_msg.angle_increment = (double)outputRange.angleResolution/10000.0 * DEG2RAD;
scan_msg.angle_min = (double)outputRange.startAngle/10000.0 * DEG2RAD - M_PI/2;
scan_msg.angle_max = (double)outputRange.stopAngle/10000.0 * DEG2RAD - M_PI/2;
ROS_DEBUG_STREAM("resolution : " << (double)outputRange.angleResolution/10000.0 << " deg");
ROS_DEBUG_STREAM("frequency : " << (double)cfg.scaningFrequency/100.0 << " Hz");
int angle_range = outputRange.stopAngle - outputRange.startAngle;
int num_values = angle_range / outputRange.angleResolution ;
if (angle_range % outputRange.angleResolution == 0) {
// Include endpoint
++num_values;
}
scan_msg.time_increment =
(outputRange.angleResolution / 10000.0)
/ 360.0
/ (cfg.scaningFrequency / 100.0);
ROS_DEBUG_STREAM("time increment : " << (double)scan_msg.time_increment << " seconds");
scan_msg.ranges.resize(num_values);
scan_msg.intensities.resize(num_values);
dataCfg.outputChannel = 1;
dataCfg.remission = true;
dataCfg.resolution = 1;
dataCfg.encoder = 0;
dataCfg.position = false;
dataCfg.deviceName = false;
dataCfg.outputInterval = 1;
laser.setScanDataCfg(dataCfg);
laser.startMeas();
status_t stat;
do // wait for ready status
{
stat = laser.queryStatus();
ros::Duration(1.0).sleep();
}
while (stat != ready_for_measurement);
laser.startDevice(); // Log out to properly re-enable system after config
laser.scanContinous(1);
while (ros::ok())
{
ros::Time start = ros::Time::now();
scan_msg.header.stamp = start;
++scan_msg.header.seq;
laser.getData(data);
for (int i = 0; i < data.dist_len1; i++)
{
scan_msg.ranges[i] = data.dist1[i] * 0.001;
}
for (int i = 0; i < data.rssi_len1; i++)
{
scan_msg.intensities[i] = data.rssi1[i];
}
scan_pub.publish(scan_msg);
ros::spinOnce();
}
laser.scanContinous(0);
laser.stopMeas();
laser.disconnect();
}
else
{
ROS_ERROR("Connection to LMS1xx device failed, retrying in 1 second.");
ros::Duration(1.0).sleep();
}
}
return 0;
}