main_udpserver_navdata_islab.cpp
Go to the documentation of this file.
00001 #include <unistd.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <stdio.h>
00005 #include "udp.h"
00006 #include <pthread.h>
00007 // Include the ROS C++ APIs
00008 #include <ros/ros.h>
00009 //#include <ardrone_islab/Navdataislab.h>
00010 
00011 void diep(const char *s)
00012 {
00013   perror(s);
00014   exit(1);
00015 }
00016 //structure for ahrs
00017 struct att_struct {
00018   double tm; //timestamp in sec
00019   double tm_pre;
00020   float   frame;
00021 
00022   //pitch estimates in radians, positive is pitch down (fly forward)
00023   float pitch_w;  //=sum(gx * dt)
00024   float pitch_a;  //=pitch(az,ax)
00025   float pitch;    //kalman pitch estimate from gy and pitch_a
00026 
00027   //roll estimates in radians, positive is roll right (fly rightward)
00028   float roll_w;   //=sum(gy * dt)
00029   float roll_a;   //=roll(az,ay)
00030   float roll;     //kalman roll estimate from gx and roll_a
00031   
00032   //yaw estimate, positive is yaw left
00033   float yaw_w;      //=sum(gz * dt)
00034   float yaw_m;      //=sum(gz * dt)
00035   float yaw;      //=sum(gz * dt)
00036   
00037   double dt;  // time since last navdata sample in sec
00038   double dt2; // time consumed by the ahrs calculations
00039 
00040   double q_est[4];
00041   double b_est[3];
00042 
00043   //copy of physical navdata values
00044         double ts;  // navdata timestamp in sec
00045         float hraw;    // height above ground in [cm] 
00046         char h_meas;// 1=height was measured in this sample, 0=height is copy of prev value
00047         float ax;   // acceleration x-axis in [G] front facing up is positive         
00048         float ay;   // acceleration y-axis in [G] left facing up is positive                
00049         float az;   // acceleration z-axis in [G] top facing up is positive  
00050            
00051         float wx;   // gyro value x-axis in [rad/sec] right turn, i.e. roll right is positive           
00052         float wy;   // gyro value y-axis in [rad/sec] right turn, i.e. pirch down is positive                     
00053         float wz;   // gyro value z-axis in [rad/sec] right turn, i.e. yaw left is positive
00054 
00055         float magX;
00056         float magY; 
00057         float magZ;
00058 
00059         float pressure;
00060 
00061         float rotX;
00062         float rotY;
00063         float rotZ;
00064 
00065         float altd;
00066 
00067         float motor1;
00068         float motor2;
00069         float motor3;
00070         float motor4;
00071 };
00072 
00073 int main(int argc, char **argv)
00074 {
00075   udp_struct udp;
00076   ardrone_islab::Navdataislab navdata;
00077 
00078   int msglen;
00079   char buf[512];
00080   char * pch;
00081   ardrone_islab::Navdataislab navdataformat;
00082   ros::init(argc, argv, "ardrone_ground_node_server");
00083   ros::NodeHandle n;
00084   ros::Rate loop_rate(300);
00085   
00086   ros::Publisher navdata_pub = n.advertise<ardrone_islab::Navdataislab>("ardrone/navdata",1000);
00087   
00088   printf("check1");
00089   // Broadcast a simple log message
00090   ROS_INFO_STREAM("Start ROS node!");
00091   
00092   if(udpServer_Init(&udp,9930,0)) diep("udpServer_Init");
00093   printf("check2");
00094   while (ros::ok())
00095   {
00096 
00097         int cnt=0;
00098         do {
00099                 msglen = udpServer_Receive(&udp, buf, 512);
00100                 cnt++;
00101         } while(msglen<=0);
00102  
00103         pch = strtok (buf," ,");
00104         int countpart = 0;
00105         
00106         //printf("%d",len(buf));
00107         while (countpart<=42)
00108         {
00109                 if (countpart == 0) navdata.tm = atof(pch);
00110                 if (countpart == 1) navdata.tm_pre = atof(pch);
00111                 if (countpart == 2) navdata.frame = atof(pch);
00112                 if (countpart == 3) navdata.pitch_w = atof(pch);
00113                 if (countpart == 4) navdata.pitch_a = atof(pch);
00114                 if (countpart == 5) navdata.pitch = atof(pch);
00115                 if (countpart == 6) navdata.roll_w = atof(pch);
00116                 if (countpart == 7) navdata.roll_a = atof(pch);
00117                 if (countpart == 8) navdata.roll = atof(pch);
00118                 if (countpart == 9) navdata.yaw_w = atof(pch);
00119                 if (countpart == 10) navdata.yaw_m = atof(pch);
00120                 if (countpart == 11) navdata.yaw = atof(pch);
00121                 if (countpart == 12) navdata.dt = atof(pch);
00122                 if (countpart == 13) navdata.dt2 = atof(pch);
00123                 if (countpart == 14) navdata.q_est[0] = atof(pch);
00124                 if (countpart == 15) navdata.q_est[1] = atof(pch);
00125                 if (countpart == 16) navdata.q_est[2] = atof(pch);
00126                 if (countpart == 17) navdata.q_est[3] = atof(pch);
00127                 if (countpart == 18) navdata.b_est[0] = atof(pch);
00128                 if (countpart == 19) navdata.b_est[1] = atof(pch);
00129                 if (countpart == 20) navdata.b_est[2] = atof(pch);
00130 
00131                 if (countpart == 21) navdata.ts = atof(pch);
00132                 if (countpart == 22) navdata.hraw = atof(pch);
00133                 if (countpart == 23) navdata.h_meas = atof(pch);
00134                 if (countpart == 24) navdata.ax = atof(pch);         
00135                 if (countpart == 25) navdata.ay = atof(pch);               
00136                 if (countpart == 26) navdata.az = atof(pch);  
00137                 if (countpart == 27) navdata.wx = atof(pch);        
00138                 if (countpart == 28) navdata.wy = atof(pch);                  
00139                 if (countpart == 29) navdata.wz = atof(pch);
00140                 if (countpart == 30) navdata.magX = atof(pch);
00141                 if (countpart == 31) navdata.magY = atof(pch);
00142                 if (countpart == 32) navdata.magZ = atof(pch);
00143                 if (countpart == 33) navdata.pressure = atof(pch);
00144                 if (countpart == 34) navdata.rotX = atof(pch);
00145                 if (countpart == 35) navdata.rotY = atof(pch);
00146                 if (countpart == 36) navdata.rotZ = atof(pch);
00147                 if (countpart == 37) navdata.altd = atof(pch);
00148                 if (countpart == 38) navdata.motor1 = atof(pch);
00149                 if (countpart == 39) navdata.motor2 = atof(pch);
00150                 if (countpart == 40) navdata.motor3 = atof(pch);
00151                 if (countpart == 41) navdata.motor4 = atof(pch);
00152                 countpart ++;
00153                 pch = strtok (NULL, " ,");
00154         }
00155         
00156         //printf(" %.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%f,%f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f ", navdata.tm,navdata.pitch_w,navdata.pitch_a,navdata.pitch,navdata.roll_w,navdata.roll_a,navdata.roll,navdata.yaw_w,navdata.yaw_m,navdata.yaw,  navdata.dt,navdata.ts,navdata.hraw,navdata.h_meas,navdata.ax,navdata.ay,navdata.az,navdata.wx,navdata.wy,navdata.wz,navdata.magX,navdata.magY,navdata.magZ,navdata.pressure,navdata.rotX,navdata.rotY,navdata.rotZ,navdata.altd,navdata.motor1,navdata.motor2,navdata.motor3,navdata.motor4);
00157         navdata.header.stamp = ros::Time::now();        
00158         navdata_pub.publish(navdata);   
00159         //printf(" %.3f", navdata.tm);
00160         ros::spinOnce();
00161         loop_rate.sleep();        
00162   }
00163 
00164   udpClient_Close(&udp);
00165   return 0;
00166 }


ardrone2islab
Author(s): Trung Nguyen , Oscar De Silva
autogenerated on Thu Jun 6 2019 20:53:51