00001 #include <ros/ros.h>
00002 #include <string.h>
00003 #include <sensor_msgs/Imu.h>
00004 #include <std_msgs/String.h>
00005 #include <geometry_msgs/Vector3Stamped.h>
00006 #include <stdio.h>
00007 #include <math.h>
00008 #include <stdlib.h>
00009 #include <Ivy/ivy.h>
00010 #include <Ivy/ivyloop.h>
00011 #include <tf/tf.h>
00012 #include <Ivy/timer.h>
00013
00014 #define DEG2RAD 3.14159265359/180
00015
00016 ros::Publisher imu_message, mag_message, acc_message, att_message;
00017
00018 sensor_msgs::Imu imu_data, att_data;
00019 geometry_msgs::Vector3Stamped mag_data, acc_data;
00020 tf::Quaternion q;
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 void ATTCallback (IvyClientPtr app, void* data , int argc, char **argv)
00031 {
00032 double att_unit_coef= 0.0139882;
00033 double phi, theta, yaw;
00034
00035 sscanf(argv[0],"%lf %lf %lf %*d %*d %*d",&phi,&theta,&yaw);
00036 phi*=att_unit_coef*DEG2RAD;
00037 theta*=-att_unit_coef*DEG2RAD;
00038 yaw*=-att_unit_coef*DEG2RAD;
00039
00040 q.setRPY(phi,theta,yaw);
00041
00042
00043
00044
00045 imu_data.header.stamp = ros::Time::now();
00046 imu_data.orientation.x=q.x();
00047 imu_data.orientation.y=q.y();
00048 imu_data.orientation.z=q.z();
00049 imu_data.orientation.w=q.w();
00050
00051 imu_message.publish(imu_data);
00052
00053
00054 att_data.header.stamp = ros::Time::now();
00055 att_data.orientation.x=q.x();
00056 att_data.orientation.y=q.y();
00057 att_data.orientation.z=q.z();
00058 att_data.orientation.w=q.w();
00059
00060 att_message.publish(att_data);
00061 }
00062
00063 void GYROCallback (IvyClientPtr app, void* data , int argc, char **argv)
00064 {
00065 double gyro_unit_coef= 0.0139882;
00066
00067 sscanf(argv[0],"%lf %lf %lf",&imu_data.angular_velocity.x,&imu_data.angular_velocity.y,&imu_data.angular_velocity.z);
00068 imu_data.angular_velocity.x*=-gyro_unit_coef*DEG2RAD;
00069 imu_data.angular_velocity.y*=gyro_unit_coef*DEG2RAD;
00070 imu_data.angular_velocity.z*=gyro_unit_coef*DEG2RAD;
00071
00072 imu_data.header.stamp = ros::Time::now();
00073 imu_message.publish(imu_data);
00074 }
00075
00076 void ACCELCallback (IvyClientPtr app, void* data , int argc, char **argv)
00077 {
00078 double acc_unit_coef= 0.0009766;
00079
00080 sscanf(argv[0],"%lf %lf %lf",&imu_data.linear_acceleration.x,&imu_data.linear_acceleration.y,&imu_data.linear_acceleration.z);
00081 imu_data.linear_acceleration.x*=-acc_unit_coef;
00082 imu_data.linear_acceleration.y*=acc_unit_coef;
00083 imu_data.linear_acceleration.z*=acc_unit_coef;
00084
00085 imu_data.header.stamp = ros::Time::now();
00086 imu_message.publish(imu_data);
00087
00088
00089 acc_data.vector=imu_data.linear_acceleration;
00090 acc_data.header=imu_data.header;
00091 acc_message.publish(acc_data);
00092 }
00093 void MAGCallback (IvyClientPtr app, void* data , int argc, char **argv)
00094 {
00095 double mag_unit_coef= 0.0004883;
00096
00097 sscanf(argv[0],"%lf %lf %lf",&mag_data.vector.x,&mag_data.vector.y,&mag_data.vector.z);
00098 mag_data.vector.x*=mag_unit_coef;
00099 mag_data.vector.y*=mag_unit_coef;
00100 mag_data.vector.z*=mag_unit_coef;
00101
00102 mag_data.header.stamp = ros::Time::now();
00103 mag_message.publish(mag_data);
00104 }
00105
00106 void ROSCallback (TimerId id, void *user_data, unsigned long delta)
00107 {
00108 if (!ros::ok()){
00109 IvyStop();
00110 exit(0);
00111 }
00112 }
00113
00114 int main(int argc, char **argv)
00115 {
00116 double angular_velocity_covariance, angular_velocity_stdev;
00117 double linear_acceleration_covariance, linear_acceleration_stdev;
00118 double orientation_covariance, orientation_stdev;
00119 std::string frame_id;
00120
00121
00122 ros::init(argc, argv, "imu");
00123 ros::NodeHandle nh("~");
00124
00125 imu_message = nh.advertise<sensor_msgs::Imu>("data", 10);
00126 att_message = nh.advertise<sensor_msgs::Imu>("att", 10);
00127 mag_message = nh.advertise<geometry_msgs::Vector3Stamped>("mag", 10);
00128 acc_message = nh.advertise<geometry_msgs::Vector3Stamped>("acc", 10);
00129
00130
00131 nh.param<std::string>("frame_id", frame_id, "imu");
00132 imu_data.header.frame_id = frame_id;
00133 mag_data.header.frame_id = frame_id;
00134
00135 nh.param("linear_acceleration_stdev", linear_acceleration_stdev, 0.0);
00136 nh.param("orientation_stdev", orientation_stdev, 0.0);
00137 nh.param("angular_velocity_stdev", angular_velocity_stdev, 0.0);
00138
00139 angular_velocity_covariance = pow(angular_velocity_stdev,2);
00140 orientation_covariance = pow(orientation_stdev,2);
00141 linear_acceleration_covariance = pow(linear_acceleration_stdev,2);
00142
00143
00144 imu_data.linear_acceleration_covariance[0] = linear_acceleration_covariance;
00145 imu_data.linear_acceleration_covariance[4] = linear_acceleration_covariance;
00146 imu_data.linear_acceleration_covariance[8] = linear_acceleration_covariance;
00147
00148 imu_data.angular_velocity_covariance[0] = angular_velocity_covariance;
00149 imu_data.angular_velocity_covariance[4] = angular_velocity_covariance;
00150 imu_data.angular_velocity_covariance[8] = angular_velocity_covariance;
00151
00152 imu_data.orientation_covariance[0] = orientation_covariance;
00153 imu_data.orientation_covariance[4] = orientation_covariance;
00154 imu_data.orientation_covariance[8] = orientation_covariance;
00155
00156
00157 IvyInit ("imu_parser", "'Imu Parser' is READY!", 0, 0, 0, 0);
00158 IvyStart("127.255.255.255");
00159 TimerRepeatAfter (TIMER_LOOP, 500, ROSCallback, 0);
00160
00161
00162 IvyBindMsg(ATTCallback, 0, "BOOZ2_AHRS_EULER(.*)");
00163 IvyBindMsg(GYROCallback, 0, "IMU_GYRO_SCALED(.*)");
00164 IvyBindMsg(ACCELCallback, 0, "IMU_ACCEL_SCALED(.*)");
00165 IvyBindMsg(MAGCallback, 0, "IMU_MAG_SCALED(.*)");
00166
00167 ROS_INFO("IMU: Starting Aquisition Loop");
00168
00169 IvyMainLoop();
00170
00171 return 0;
00172 }