ardupilot.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <ros/publisher.h>
3 #include <sensor_msgs/Imu.h>
4 #include <igvc/SerialPort.h>
5 #include <vector>
6 #include <sstream>
7 #include <tf/transform_datatypes.h>
8 #include <igvc/StringUtils.hpp>
9 
10 #define GRAVITY_MSS 9.80665
11 #define DEG_TO_RAD (3.14159265/180.0)
12 
13 using namespace std;
14 
15 int main(int argc, char** argv)
16 {
17  ros::init(argc, argv, "ardupilot");
18 
19  ros::NodeHandle nh;
20 
21  ros::Publisher pub = nh.advertise<sensor_msgs::Imu>("/imu", 1000);
22 
23  SerialPort port("/dev/igvc_imu", 115200);
24 
25  while(ros::ok() && port.isOpen())
26  {
27  ros::spinOnce();
28 
29  auto line = port.readln();
30 
31  if(line[0] != 'A') continue;
32 
33  auto tokens = split(line, ' ');
34 
35  if(tokens.size() == 10)
36  {
37  sensor_msgs::Imu msg;
38 
39  msg.header.frame_id = "/imu";
40  msg.header.stamp = ros::Time::now();
41 
42  try
43  {
44  auto roll = stof(tokens[1]) * DEG_TO_RAD;
45  auto pitch = stof(tokens[2]) * DEG_TO_RAD;
46  auto yaw = stof(tokens[3]) * DEG_TO_RAD;
47 
48  /* These arguments are intentionally out of order, because TF issues further upstream
49  * have not been fixed. See GitHub issue #35.
50  */
51  msg.orientation = tf::createQuaternionMsgFromRollPitchYaw(pitch, roll, -yaw);
52 
53  msg.linear_acceleration.x = stof(tokens[4]);
54  msg.linear_acceleration.y = -stof(tokens[5]);
55  msg.linear_acceleration.z = stof(tokens[6]);
56 
57  msg.angular_velocity.x = stof(tokens[7]);
58  msg.angular_velocity.y = stof(tokens[8]);
59  msg.angular_velocity.z = stof(tokens[9]);
60 
61  } catch(const invalid_argument &e) {
62  ROS_ERROR_STREAM("Exception in parsing IMU message.");
63  ROS_ERROR_STREAM(e.what());
64  }
65 
66  pub.publish(msg);
67 
68  } else {
69  ROS_WARN_STREAM("IMU received " << tokens.size() << " tokens. Expected 7.");
70  ROS_WARN_STREAM(line);
71  }
72  }
73 
74 
75  return 0;
76 }
#define DEG_TO_RAD
Definition: ardupilot.cpp:11
int main(int argc, char **argv)
Definition: ardupilot.cpp:15
std::vector< std::string > split(const std::string &s, const char &delim)
Definition: StringUtils.hpp:5
Helper class to simplify interfacing with serial port hardware.
Definition: SerialPort.h:14


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45