cht10_node.cpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11  ** Includes
12  *****************************************************************************/
13 #include <string>
14 #include <ros/ros.h>
15 #include <std_msgs/String.h>
16 #include <nodelet/nodelet.h>
17 #include <ecl/threads/thread.hpp>
18 #include <sensor_msgs/LaserScan.h>
19 #include <sensor_msgs/Range.h>
22 
23 
24 namespace cht10_seiral_func{
25 
26 class Cht10Func : public nodelet::Nodelet{
27 #define BUFSIZE 17
28 #define SCALE 1000
29 
30 public:
31  Cht10Func() : shutdown_requested_(false),serialNumber_("/dev/USB0"),frame_id("laser"){}
32 
34  NODELET_DEBUG_STREAM("Waiting for update thread to finish.");
35  shutdown_requested_ = true;
36  update_thread_.join();
37  }
38 
39  virtual void onInit(){
40 
42  std::string name = nh.getUnresolvedNamespace();
43  nh.getParam("serialNumber", serialNumber_);
44  nh.getParam("baudRate", baudRate_);
45  nh.getParam("frame_id", frame_id);
46  rcv_cnt = 0;
47  success_flag = 0;
48 
49  fd = open(serialNumber_.c_str(), O_RDWR | O_NOCTTY | O_NDELAY );
50  if(fd < 0){
51  ROS_ERROR_STREAM("Open Serial: "<<serialNumber_.c_str()<<" Error!");
52  exit(0);
53  }
54 
55  int countSeq=0;
56  scan_pub = nh.advertise<sensor_msgs::Range>("range",100);
57  memset(buf, 0, sizeof(buf));
58  memset(temp_buf, 0, sizeof(temp_buf));
59  memset(result_buf, 0, sizeof(result_buf));
61  ROS_INFO_STREAM("Open serial: ["<< serialNumber_.c_str() <<" ] successful, with idex: "<<fd<<".");
62  NODELET_INFO_STREAM("Cht10Func initialised. Spinning up update thread ... [" << name << "]");
63  update_thread_.start(&Cht10Func::update, *this);
64  }
65 
66  double data_to_meters(int &data, int scale){
67  return (double)data/scale;
68  }
69 
71  double nodes, ros::Time start,
72  std::string frame_id){
73 
74  float final_range;
75  sensor_msgs::Range range_msg;
76  range_msg.field_of_view = 0.05235988;
77  range_msg.max_range = 10.0;
78  range_msg.min_range = 0.05;
79  range_msg.header.frame_id = frame_id;
80  range_msg.radiation_type = sensor_msgs::Range::INFRARED;
81  if(nodes > range_msg.max_range){
82  final_range = std::numeric_limits<float>::infinity();
83  }else if(nodes < range_msg.min_range){
84  final_range = -std::numeric_limits<float>::infinity();
85  }else{
86  final_range = nodes;
87  }
88  range_msg.header.stamp = start;
89  range_msg.header.seq = countSeq;
90  range_msg.range = final_range;
91  scan_pub.publish(range_msg);
92 
93  }
94 
95  bool get_scan_data(){
97  if(len>0){
98  for(int i = 0; i < len; i++){
99  if(rcv_cnt<=BUFSIZE){
100  result_buf[rcv_cnt++] = buf[i];
101  if(rcv_cnt == BUFSIZE){
102  success_flag = true;
103  }
104  }//end if
105  else{
106  /****
107  * checkout received data
108  */
109  success_flag = false;
110  for(int count = 0; count < 4; count++){
111  data_buf[count] = result_buf[9+count];
112  }
113  sscanf(data_buf, "%x", &laser_data);
114 
115  //std::cout<<"sensor data:"<<laser_data<<std::endl;
116 
117  /****
118  * data writing end
119  */
120  if('~' == buf[i]){
121  rcv_cnt = 0;
122  result_buf[rcv_cnt++] = buf[i];
123  }
124  }//end else
125  }//end for
126  }
127  }
128 private:
129  void update(){
130  rcv_cnt = 0;
131  success_flag = 0;
132  laser_data = 0;
133  ros::Rate spin_rate(50);
134  memset(buf, 0, sizeof(buf));
135  memset(temp_buf, 0, sizeof(temp_buf));
136  memset(result_buf, 0, sizeof(result_buf));
137  ROS_INFO_STREAM("Begin receive data from "<<serialNumber_.c_str()<<", with idex:"<<fd<<".");
138  fd = open(serialNumber_.c_str(), O_RDWR | O_NOCTTY | O_NDELAY );
139  Cht10driver_.UART0_Init(fd,baudRate_,0,8,1,'N');
140  while (! shutdown_requested_ && ros::ok())
141  {
144 
145  //Send data
148  spin_rate.sleep();
149 
150  countSeq++;
151  }
152 
153  ROS_INFO_STREAM("Shotdown and close serial: "<<serialNumber_.c_str()<<".");
155  }
156 private:
157  int fd, len, rcv_cnt;
160 
162  ecl::Thread update_thread_;
166  char data_buf[4];
167  // ROS Parameters
168  std::string serialNumber_;
170  int countSeq;
171 
172  std::string frame_id;
173 
175 };
176 
177 } //namespace Cht10_serial_func
#define NODELET_INFO_STREAM(...)
const std::string & getUnresolvedNamespace() const
PLUGINLIB_EXPORT_CLASS(cht10_seiral_func::Cht10Func, nodelet::Nodelet)
void publish(const boost::shared_ptr< M > &message) const
int UART0_Recv(int fd, char *rcv_buf, int data_len)
ros::NodeHandle & getPrivateNodeHandle() const
void publish_scan(ros::Publisher *pub, double nodes, ros::Time start, std::string frame_id)
Definition: cht10_node.cpp:70
#define SCALE
Definition: cht10_node.cpp:28
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define NODELET_DEBUG_STREAM(...)
double data_to_meters(int &data, int scale)
Definition: cht10_node.cpp:66
bool sleep()
int UART0_Init(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity)
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
static Time now()
#define ROS_ERROR_STREAM(args)
#define BUFSIZE
Definition: cht10_node.cpp:27


cht10_node
Author(s): Carl <1271087623@qq.comd>
autogenerated on Mon Jun 10 2019 12:48:57