ls01b_node.cpp
Go to the documentation of this file.
1 /*******************************************************
2 @company: Copyright (C) 2018, Leishen Intelligent System
3 @product: LS01B
4 @filename: ls01b_node.cpp
5 @brief:
6 @version: date: author: comments:
7 @v1.0 18-8-28 fu new
8 *******************************************************/
9 #include "ls01b_v2/ls01b_node.h"
10 #include "ls01b_v2/ls01b.h"
11 #include <signal.h>
12 
13 namespace ls {
14 
16 {
17  initParam();
18  pub_ = n_.advertise<sensor_msgs::LaserScan>(scan_topic_, 1000);
21 }
22 
24 {
25  printf("start ls01b_node\n");
26  ls01b_->stopScan();
27  ls01b_ = NULL;
28  delete ls01b_;
29  printf("end ls01b_node\n");
30 }
31 
33 {
34  std::string scan_topic = "/scan";
35  std::string frame_id = "laser_link";
36  std::string port = "/dev/ttyUSB0";
37  ros::NodeHandle nh("~");
38  nh.param("scan_topic", scan_topic_, scan_topic);
39  nh.param("frame_id", frame_id_, frame_id);
40  nh.param("serial_port", serial_port_, port);
41  nh.param("baud_rate", baud_rate_, 460800);
42  nh.param("angle_resolution", angle_resolution_, 1.0);
43  nh.param("angle_disable_min_0", angle_disable_min_0, -1.0);
44  nh.param("angle_disable_max_0", angle_disable_max_0, -1.0);
45  nh.param("angle_disable_min_1", angle_disable_min_1, -1.0);
46  nh.param("angle_disable_max_1", angle_disable_max_1, -1.0);
47  nh.param("angle_disable_min_2", angle_disable_min_2, -1.0);
48  nh.param("angle_disable_max_2", angle_disable_max_2, -1.0);
49  nh.param("angle_disable_min_3", angle_disable_min_3, -1.0);
50  nh.param("angle_disable_max_3", angle_disable_max_3, -1.0);
51  nh.param("angle_disable_min_4", angle_disable_min_4, -1.0);
52  nh.param("angle_disable_max_4", angle_disable_max_4, -1.0);
53  nh.param("robot_radius", robot_radius_, 0.2);
54  nh.param("center_x", center_x_, 0.0);
55  nh.param("center_y", center_y_, 0.0);
56 }
57 
59 {
60  // ls01b_->stopScan();
61  // sleep(1);
63  ls01b_->setMotorSpeed(600);
64 
65  // sleep(1);
66  ls01b_->switchData(false);
67 
68  // sleep(1);
69  ls01b_->startScan();
70 
71  // sleep(1);
72  ls01b_->setScanMode(true);
73 
74 
75 }
76 
78 {
79  bool is_health = ls01b_->isHealth();
80  if (!is_health)
81  {
82  // ls01b_->stopScan();
85  run();
86  return;
87  }
88 
89  std::vector<ScanPoint> points;
90  ros::Time start_time;
91  float scan_time;
92  ls01b_->getScan(points, start_time, scan_time);
93  int count = points.size();
94  if (count <= 0)
95  return;
96 
97  sensor_msgs::LaserScan msg;
98  msg.header.frame_id = frame_id_;
99  msg.header.stamp = start_time;
100  msg.angle_min = 0.0;
101  msg.angle_max = 2*M_PI;
102  msg.angle_increment = (msg.angle_max - msg.angle_min) / count;
103  msg.range_min = 0.01;
104  msg.range_max = 25;
105  msg.ranges.resize(count);
106  msg.intensities.resize(count);
107  msg.scan_time = scan_time;
108  msg.time_increment = scan_time / (double)(count - 1);
109 
110  for (int i = count - 1; i >= 0; i--)
111  {
112  if (
113  ((i >= (angle_disable_min_0 * count/360)) && (i < (angle_disable_max_0 * count/360)))
114  || ((i >= (angle_disable_min_1 * count/360)) && (i < (angle_disable_max_1 * count/360)))
115  || ((i >= (angle_disable_min_2 * count/360)) && (i < (angle_disable_max_2 * count/360)))
116  || ((i >= (angle_disable_min_3 * count/360)) && (i < (angle_disable_max_3 * count/360)))
117  || ((i >= (angle_disable_min_4 * count/360)) && (i < (angle_disable_max_4 * count/360)))
118  )
119  {
120  msg.ranges[i] = std::numeric_limits<float>::infinity();
121  msg.intensities[i] = 0;
122  }
123  else if (points[count - i - 1].range == 0.0)
124  {
125  msg.ranges[i] = std::numeric_limits<float>::infinity();
126  msg.intensities[i] = 0;
127  }
128  else
129  {
130  msg.ranges[i] = points[count - i - 1].range;
131  msg.intensities[i] = points[count-i-1].intensity;
132  }
133 
134  double point_dist = msg.ranges[i];
135  if(point_dist < 1.0 && point_dist > 0.06)
136  {
137  double x = point_dist*cos(i*angle_resolution_*M_PI/180);
138  double y = point_dist*sin(i*angle_resolution_*M_PI/180);
139 
140  double dist2center = sqrt((y-center_y_)*(y-center_y_) + (x-center_x_)*(x-center_x_));
141  if (dist2center < robot_radius_)
142  msg.ranges[i] = std::numeric_limits<float>::infinity();
143  }
144 
145  }
146  pub_.publish(msg);
147 
148 }
149 }
150 
151 void handleSig(int signo)
152 {
153  printf("handleSig\n");
154  ros::shutdown();
155  exit(0);
156 }
157 
158 int main(int argv, char **argc)
159 {
160  signal(SIGINT, handleSig);
161  signal(SIGTERM, handleSig);
162  ros::init(argv, argc, "ls01b");
163 
164  ls::LS01B_Node ls01b_node;
165  ls01b_node.run();
166  ros::spin();
167  return 0;
168 }
Definition: ls01b.h:17
void publishScan(const ros::TimerEvent &)
Definition: ls01b_node.cpp:77
double angle_disable_max_3
Definition: ls01b_node.h:55
std::string frame_id_
Definition: ls01b_node.h:42
double angle_disable_min_0
Definition: ls01b_node.h:48
ros::Timer timer_
Definition: ls01b_node.h:35
void publish(const boost::shared_ptr< M > &message) const
bool isHealth()
Definition: ls01b.cpp:52
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
double center_y_
Definition: ls01b_node.h:47
static LS01B * instance(std::string port, int baud_rate, double resolution)
Definition: ls01b.cpp:13
std::string scan_topic_
Definition: ls01b_node.h:41
double angle_disable_min_4
Definition: ls01b_node.h:56
int setMotorSpeed(int rpm)
Definition: ls01b.cpp:194
double angle_disable_min_2
Definition: ls01b_node.h:52
int stopScan()
Definition: ls01b.cpp:102
std::string serial_port_
Definition: ls01b_node.h:38
void initParam()
Definition: ls01b_node.cpp:32
double robot_radius_
Definition: ls01b_node.h:45
int switchData(bool use_angle)
Definition: ls01b.cpp:165
bool resetHealth()
Definition: ls01b.cpp:57
bool param(const std::string &param_name, T &param_val, const T &default_val) const
double angle_disable_max_2
Definition: ls01b_node.h:53
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
double center_x_
Definition: ls01b_node.h:46
int getScan(std::vector< ScanPoint > &points, ros::Time &scan_time, float &scan_duration)
Definition: ls01b.cpp:62
double angle_resolution_
Definition: ls01b_node.h:40
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
int setScanMode(bool is_continuous)
Definition: ls01b.cpp:120
double angle_disable_min_3
Definition: ls01b_node.h:54
double angle_disable_max_1
Definition: ls01b_node.h:51
double angle_disable_max_0
Definition: ls01b_node.h:49
int setResolution(double resolution)
Definition: ls01b.cpp:216
int main(int argv, char **argc)
Definition: ls01b_node.cpp:158
void handleSig(int signo)
Definition: ls01b_node.cpp:151
ros::NodeHandle n_
Definition: ls01b_node.h:33
ros::Publisher pub_
Definition: ls01b_node.h:34
double angle_disable_max_4
Definition: ls01b_node.h:57
ROSCPP_DECL void shutdown()
LS01B * ls01b_
Definition: ls01b_node.h:37
int startScan()
Definition: ls01b.cpp:83
double angle_disable_min_1
Definition: ls01b_node.h:50


ls01b_v2
Author(s): fu
autogenerated on Sat Sep 28 2019 03:51:19