wit_imu_node.cpp
Go to the documentation of this file.
1 #include "ros/ros.h"
2 #include <serial/serial.h> //ROS已经内置了的串口包
3 #include <std_msgs/String.h>
4 #include <sensor_msgs/Imu.h>
5 #include <sensor_msgs/MagneticField.h>
6 // #include <std_msgs/Empty.h>
7 
8 #include "JY901.h"
9 
10 serial::Serial ser; //声明串口对象
11 //回调函数
12 // void write_callback(const std_msgs::String::ConstPtr &msg)
13 // {
14 // ROS_INFO_STREAM("Writing to serial port" << msg->data);
15 // ser.write(msg->data); //发送串口数据
16 // }
17 int main(int argc, char **argv)
18 {
19  std::string port;
20  int baudrate;
21  int pub_rate;
22  bool pub_mag;
23  std::string imu_topic;
24  std::string mag_topic;
25  std::string imu_frame;
26 
27  //初始化节点
28  ros::init(argc, argv, "imu");
29  ROS_INFO("Init IMU Node.");
30  //声明节点句柄
31  ros::NodeHandle nh;
32  ros::NodeHandle nh_private("~");
33 
34  nh_private.param<std::string>("port", port, "ttyS0");
35  port = "/dev/" + port;
36  nh_private.param<int>("baudrate", baudrate, 9600);
37  nh_private.param<int>("publish_rate", pub_rate, 20);
38  nh_private.param<bool>("publish_mag", pub_mag, true);
39  nh_private.param<std::string>("imu_topic", imu_topic, "imu_data");
40  nh_private.param<std::string>("mag_topic", mag_topic, "mag_data");
41  nh_private.param<std::string>("imu_frame", imu_frame, "imu_link");
42 
43  ROS_INFO_STREAM("port : " << port);
44  ROS_INFO_STREAM("baudrate : " << baudrate);
45  ROS_INFO_STREAM("publish_rate : " << pub_rate);
46  ROS_INFO_STREAM("publish_mag : " << (pub_mag ? "true" : "false"));
47  ROS_INFO_STREAM("imu_topic : " << imu_topic);
48  ROS_INFO_STREAM("mag_topic : " << mag_topic);
49  ROS_INFO_STREAM("imu_frame : " << imu_frame);
50 
51  //发布主题
52  ros::Publisher imu_pub = nh.advertise<sensor_msgs::Imu>(imu_topic, 1000);
53  ros::Publisher mag_pub = nh.advertise<sensor_msgs::MagneticField>(mag_topic, 1000);
54 
55  CJY901 imu = CJY901();
56  try
57  {
58  //设置串口属性,并打开串口
59  ser.setPort(port);
60  ser.setBaudrate(baudrate);
62  ser.setTimeout(to);
63  ser.open();
64  }
65  catch (serial::IOException &e)
66  {
67  ROS_ERROR_STREAM("Unable to open port ");
68  return -1;
69  }
70  //检测串口是否已经打开,并给出提示信息
71  if (ser.isOpen())
72  {
73  ROS_INFO_STREAM("Serial Port initialized");
74  }
75  else
76  {
77  return -1;
78  }
79  // ser.flush();
80  int size;
81  //指定循环的频率
82  ros::Rate loop_rate(pub_rate);
83  while (ros::ok())
84  {
85  int count = ser.available();
86  if (count != 0)
87  {
88  ROS_INFO_ONCE("Data received from serial port.");
89  // std::string result;
90  // result = ser.read(ser.available());
91  // // result.data = ser.readline(500, "\n");
92  // ROS_INFO("\nRead %d byte:", result.size());
93  // for (int i = 0; i < result.size(); i++)
94  // printf("0x%.2X ", result.data()[i]);
95  // continue;
96  // imu.CopeSerialData(const_cast<char *>(result.data()), result.size());
97 
98  int num;
99  unsigned char read_buf[count];
100  num = ser.read(read_buf, count);
101  // result.data = ser.readline(500, "\n");
102  // ROS_INFO("\nRead %d byte:", num);
103  // for (int i = 0; i < num; i++)
104  // printf("0x%.2X ", read_buf[i]);
105  // continue;
106  //imu.CopeSerialData((char *)read_buf, num);
107  imu.FetchData((char *)read_buf, num);
108  // ROS_INFO("IMU Data : Quaternion{ x: %f y: %f z: %f w: %f} ", imu.quat.x, imu.quat.y, imu.quat.z, imu.quat.w);
109  sensor_msgs::Imu imu_data;
110 
111  imu_data.header.stamp = ros::Time::now();
112  imu_data.header.frame_id = imu_frame;
113 
114  imu_data.linear_acceleration.x = imu.acc.x;
115  imu_data.linear_acceleration.y = imu.acc.y;
116  imu_data.linear_acceleration.z = imu.acc.z;
117  imu_data.linear_acceleration_covariance = {1e-6, 0, 0, 0, 1e-6, 0, 0, 0, 1e-6};
118 
119  imu_data.angular_velocity.x = imu.gyro.x;
120  imu_data.angular_velocity.y = imu.gyro.y;
121  imu_data.angular_velocity.z = imu.gyro.z;
122  imu_data.linear_acceleration_covariance = {1e-6, 0, 0, 0, 1e-6, 0, 0, 0, 1e-6};
123 
124  imu_data.orientation.x = imu.quat.x;
125  imu_data.orientation.y = imu.quat.y;
126  imu_data.orientation.z = imu.quat.z;
127  imu_data.orientation.w = imu.quat.w;
128  imu_data.orientation_covariance = {1e-6, 0, 0, 0, 1e-6, 0, 0, 0, 1e-6};
129 
130  imu_pub.publish(imu_data);
131 
132  if (pub_mag)
133  {
134  sensor_msgs::MagneticField mag_data;
135 
136  mag_data.header.stamp = imu_data.header.stamp;
137  mag_data.header.frame_id = imu_data.header.frame_id;
138 
139  mag_data.magnetic_field.x = imu.mag.x;
140  mag_data.magnetic_field.y = imu.mag.y;
141  mag_data.magnetic_field.z = imu.mag.z;
142  mag_data.magnetic_field_covariance = {1e-6, 0, 0, 0, 1e-6, 0, 0, 0, 1e-6};
143 
144  mag_pub.publish(mag_data);
145  }
146  }
147 
148  //处理ROS的信息,比如订阅消息,并调用回调函数
149  //ros::spinOnce();
150  loop_rate.sleep();
151  }
152 }
size_t available()
void publish(const boost::shared_ptr< M > &message) const
#define ROS_INFO_ONCE(...)
void setTimeout(Timeout &timeout)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void setBaudrate(uint32_t baudrate)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
int main(int argc, char **argv)
static Timeout simpleTimeout(uint32_t timeout)
serial::Serial ser
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool isOpen() const
bool sleep()
void setPort(const std::string &port)
#define ROS_INFO_STREAM(args)
size_t read(uint8_t *buffer, size_t size)
static Time now()
#define ROS_ERROR_STREAM(args)
Definition: JY901.h:187


wit-imu-driver
Author(s): Inspur Group, Penn Zhang
autogenerated on Thu Sep 10 2020 03:49:48