2 #include <ros/publisher.h>
3 #include <sensor_msgs/Imu.h>
7 #include <tf/transform_datatypes.h>
10 #define GRAVITY_MSS 9.80665
11 #define DEG_TO_RAD (3.14159265/180.0)
15 int main(
int argc,
char** argv)
17 ros::init(argc, argv,
"ardupilot");
21 ros::Publisher pub = nh.advertise<sensor_msgs::Imu>(
"/imu", 1000);
25 while(ros::ok() && port.isOpen())
29 auto line = port.readln();
31 if(line[0] !=
'A')
continue;
33 auto tokens =
split(line,
' ');
35 if(tokens.size() == 10)
39 msg.header.frame_id =
"/imu";
40 msg.header.stamp = ros::Time::now();
51 msg.orientation = tf::createQuaternionMsgFromRollPitchYaw(pitch, roll, -yaw);
53 msg.linear_acceleration.x = stof(tokens[4]);
54 msg.linear_acceleration.y = -stof(tokens[5]);
55 msg.linear_acceleration.z = stof(tokens[6]);
57 msg.angular_velocity.x = stof(tokens[7]);
58 msg.angular_velocity.y = stof(tokens[8]);
59 msg.angular_velocity.z = stof(tokens[9]);
61 }
catch(
const invalid_argument &e) {
62 ROS_ERROR_STREAM(
"Exception in parsing IMU message.");
63 ROS_ERROR_STREAM(e.what());
69 ROS_WARN_STREAM(
"IMU received " << tokens.size() <<
" tokens. Expected 7.");
70 ROS_WARN_STREAM(line);
int main(int argc, char **argv)
std::vector< std::string > split(const std::string &s, const char &delim)
Helper class to simplify interfacing with serial port hardware.