packet_example.cpp
Go to the documentation of this file.
00001 /****************************************************************/
00002 /*                                                              */
00003 /*          Advanced Navigation Packet Protocol Library         */
00004 /*          C Language Dynamic Spatial SDK, Version 2.3         */
00005 /*   Copyright 2013, Xavier Orr, Advanced Navigation Pty Ltd    */
00006 /*                                                              */
00007 /****************************************************************/
00008 /*
00009  * Copyright (C) 2013 Advanced Navigation Pty Ltd
00010  *
00011  * Permission is hereby granted, free of charge, to any person obtaining
00012  * a copy of this software and associated documentation files (the "Software"),
00013  * to deal in the Software without restriction, including without limitation
00014  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00015  * and/or sell copies of the Software, and to permit persons to whom the
00016  * Software is furnished to do so, subject to the following conditions:
00017  *
00018  * The above copyright notice and this permission notice shall be included
00019  * in all copies or substantial portions of the Software.
00020  *
00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00022  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
00024  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00026  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00027  * DEALINGS IN THE SOFTWARE.
00028  */
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <stdint.h>
00033 #include <math.h>
00034 #include "ros/ros.h"
00035 #include <tf/transform_broadcaster.h>
00036 #ifdef _WIN32
00037 #include <Windows.h>
00038 #else
00039 #include <unistd.h>
00040 #endif
00041 
00042 #include "rs232/rs232.h"
00043 #include "an_packet_protocol.h"
00044 #include "spatial_packets.h"
00045 
00046 #define RADIANS_TO_DEGREES (180.0/M_PI)
00047 #define pai 3.141592653
00048 
00049 int com_port_number;
00050 
00051 int an_packet_transmit(an_packet_t *an_packet)
00052 {
00053         an_packet_encode(an_packet);
00054         return SendBuf(com_port_number, an_packet_pointer(an_packet), an_packet_size(an_packet));
00055 }
00056 
00057 /*
00058  * This is an example of sending a configuration packet to Spatial.
00059  *
00060  * 1. First declare the structure for the packet, in this case sensor_ranges_packet_t.
00061  * 2. Set all the fields of the packet structure
00062  * 3. Encode the packet structure into an an_packet_t using the appropriate helper function
00063  * 4. Send the packet
00064  * 5. Free the packet
00065  */
00066 void set_sensor_ranges()
00067 {
00068         an_packet_t *an_packet;
00069         sensor_ranges_packet_t sensor_ranges_packet;
00070 
00071         sensor_ranges_packet.permanent = TRUE;
00072         sensor_ranges_packet.accelerometers_range = accelerometer_range_4g;
00073         sensor_ranges_packet.gyroscopes_range = gyroscope_range_500dps;
00074         sensor_ranges_packet.magnetometers_range = magnetometer_range_2g;
00075 
00076         an_packet = encode_sensor_ranges_packet(&sensor_ranges_packet);
00077 
00078         an_packet_transmit(an_packet);
00079 
00080         an_packet_free(&an_packet);
00081 }
00082 
00083 void get_data(uint8_t requested_packet_id)
00084 {
00085         /*an_packet_t *an_packet;
00086         sensor_ranges_packet_t sensor_ranges_packet;
00087 
00088         sensor_ranges_packet.permanent = TRUE;
00089         sensor_ranges_packet.accelerometers_range = accelerometer_range_4g;
00090         sensor_ranges_packet.gyroscopes_range = gyroscope_range_500dps;
00091         sensor_ranges_packet.magnetometers_range = magnetometer_range_2g;
00092 
00093         an_packet = encode_sensor_ranges_packet(&sensor_ranges_packet);
00094 
00095         an_packet_transmit(an_packet);
00096 
00097         an_packet_free(&an_packet);*/
00098 
00099         an_packet_t *an_packet;
00100         an_packet = encode_request_packet(requested_packet_id);
00101         an_packet_transmit(an_packet);
00102 
00103         an_packet_free(&an_packet);
00104 }
00105 int main(int argc, char *argv[])
00106 {
00107         ros::init(argc, argv, "navigation");
00108         ros::NodeHandle n;
00109         tf::TransformBroadcaster odom_broadcaster;
00110         ros::Time current_time,last_time;
00111         current_time=ros::Time::now();
00112         last_time=ros::Time::now();
00113         ros::Rate r(100);
00114         an_decoder_t an_decoder;
00115         an_packet_t *an_packet;
00116         
00117         system_state_packet_t system_state_packet;
00118         raw_sensors_packet_t raw_sensors_packet;
00119         euler_orientation_packet_t euler_orientation_packet;
00120         body_velocity_packet_t body_velocity_packet;
00121         ecef_position_packet_t ecef_position_packet;
00122         quaternion_orientation_packet_t quaternion_orientation_packet;
00123         external_position_velocity_packet_t external_position_velocity_packet;
00124         running_time_packet_t running_time_packet;
00125         
00126         int bytes_received;
00127 
00128         if (argc != 3)
00129         {
00130                 printf("Usage - program com_port_number baud_rate\nExample - packet_example.exe 0 115200\n");
00131                 exit(EXIT_FAILURE);
00132         }
00133 
00134         com_port_number = atoi(argv[1]);
00135         
00136         /* open the com port */
00137         if (OpenComport(com_port_number, atoi(argv[2])))
00138         {
00139                 exit(EXIT_FAILURE);
00140         }
00141         
00142         an_decoder_initialise(&an_decoder);
00143         /*set_sensor_ranges();*/
00144         
00145         get_data(packet_id_system_state);
00146         while (n.ok())
00147         {
00148                 /*get_data(packet_id_system_state);*/
00149                 /*get_data(packet_id_ecef_position);*/
00150                 get_data(packet_id_body_velocity);
00151                 if ((bytes_received = PollComport(com_port_number, an_decoder_pointer(&an_decoder), an_decoder_size(&an_decoder))) > 0)
00152                 {
00153                         /* increment the decode buffer length by the number of bytes received */
00154                         an_decoder_increment(&an_decoder, bytes_received);
00155                         
00156                         /* decode all the packets in the buffer */
00157                         while ((an_packet = an_packet_decode(&an_decoder)) != NULL)
00158                         {
00159                                 if (an_packet->id == packet_id_system_state) /* system state packet */
00160                                 {
00161                                         /* copy all the binary data into the typedef struct for the packet */
00162                                         /* this allows easy access to all the different values             */
00163                                         if(decode_system_state_packet(&system_state_packet, an_packet) == 0)
00164                                         {
00165                                                 printf("System State Packet:\n");
00166                                                 printf("\tLatitude = %f, Longitude = %f, Height = %f\n", system_state_packet.latitude * RADIANS_TO_DEGREES, system_state_packet.longitude * RADIANS_TO_DEGREES, system_state_packet.height);
00167                                                 printf("\tRoll = %f, Pitch = %f, Yaw = %f\n", system_state_packet.orientation[0] * RADIANS_TO_DEGREES, system_state_packet.orientation[1] * RADIANS_TO_DEGREES, system_state_packet.orientation[2] * RADIANS_TO_DEGREES);
00168                                                 double roll=(system_state_packet.orientation[0] * RADIANS_TO_DEGREES*pai)/360;
00169 double pitch=(system_state_packet.orientation[1] * RADIANS_TO_DEGREES*pai)/360;
00170 double yaw=(system_state_packet.orientation[2] * RADIANS_TO_DEGREES*pai)/360;
00171 
00172 double w=cos(roll)*cos(pitch)*cos(yaw)+sin(roll)*sin(pitch)*sin(yaw);
00173 double x=sin(roll)*cos(pitch)*cos(yaw)-cos(roll)*sin(pitch)*sin(yaw);
00174 double y=cos(roll)*sin(pitch)*cos(yaw)+sin(roll)*cos(pitch)*sin(yaw);
00175 double z=cos(roll)*cos(pitch)*sin(yaw)-sin(roll)*sin(pitch)*cos(yaw);
00176 
00177 current_time=ros::Time::now();
00178                                         geometry_msgs::Quaternion odom_quat;
00179                                         geometry_msgs::TransformStamped odom_trans;
00180                                         odom_trans.header.stamp=current_time;
00181                                         odom_trans.header.frame_id="odom";
00182                                         odom_trans.child_frame_id="base_link";
00183                                         odom_trans.transform.translation.x=0;
00184                                         odom_trans.transform.translation.y=0;
00185                                         odom_trans.transform.translation.z=0;
00186                                         odom_trans.transform.rotation.x=x;
00187                                         odom_trans.transform.rotation.y=y;
00188                                         odom_trans.transform.rotation.z=z;
00189                                         odom_trans.transform.rotation.w=w;
00190                                         odom_broadcaster.sendTransform(odom_trans);
00191 
00192 printf("\t11111  Q0 = %f, Q1 = %f, Q2 = %f, Q3 = %f\n",x,y,z,w);
00193 
00194 
00195 
00196                                         }
00197                                 }
00198                                 else if (an_packet->id == packet_id_raw_sensors) /* raw sensors packet */
00199                                 {
00200                                         /* copy all the binary data into the typedef struct for the packet */
00201                                         /* this allows easy access to all the different values             */
00202                                         if(decode_raw_sensors_packet(&raw_sensors_packet, an_packet) == 0)
00203                                         {
00204                                                 printf("Raw Sensors Packet:\n");
00205                                                 printf("\tAccelerometers X: %f Y: %f Z: %f\n", raw_sensors_packet.accelerometers[0], raw_sensors_packet.accelerometers[1], raw_sensors_packet.accelerometers[2]);
00206                                                 printf("\tGyroscopes X: %f Y: %f Z: %f\n", raw_sensors_packet.gyroscopes[0] * RADIANS_TO_DEGREES, raw_sensors_packet.gyroscopes[1] * RADIANS_TO_DEGREES, raw_sensors_packet.gyroscopes[2] * RADIANS_TO_DEGREES);
00207                                         }
00208                                 }/*no use*/
00209                                 /*else
00210                                 {
00211                                         printf("Packet ID %u of Length %u\n", an_packet->id, an_packet->length);
00212                                 }*/
00213                                 else if (an_packet->id == packet_id_euler_orientation) /* euler_orientation packet */
00214                                 {
00215                                         if(decode_euler_orientation_packet(&euler_orientation_packet, an_packet) == 0)                                  
00216                                         printf("euler_orientation_packet:\n");
00217                                         printf("\tRoll = %f, Pitch = %f, Yaw = %f\n", euler_orientation_packet.orientation[0] * RADIANS_TO_DEGREES, euler_orientation_packet.orientation[1] * RADIANS_TO_DEGREES, euler_orientation_packet.orientation[2] * RADIANS_TO_DEGREES);
00218 
00219                                         /*printf("Packet ID %u of Length %u\n", an_packet->id, an_packet->length);*/
00220                                 }
00221                                 else if (an_packet->id == packet_id_body_velocity)
00222                                 {
00223                                         if(decode_body_velocity_packet(&body_velocity_packet,an_packet) == 0)                                   
00224                                         printf("body_velocity_packet_t:\n");
00225                                         printf("\tVX = %f, VY = %f, VZ = %f\n", body_velocity_packet.velocity[0], body_velocity_packet.velocity[1], body_velocity_packet.velocity[2]);
00226 
00227                                         /*printf("Packet ID %u of Length %u\n", an_packet->id, an_packet->length);*/
00228                                 }
00229                                 else if (an_packet->id == packet_id_ecef_position)
00230                                 {
00231                                         if(decode_ecef_position_packet(&ecef_position_packet,an_packet) == 0)                                   
00232                                         printf("ecef_position_packet:\n");
00233                                         printf("\tX = %f, Y = %f, Z = %f\n", ecef_position_packet.position[0], ecef_position_packet.position[1], ecef_position_packet.position[2]);
00234 
00235                                 }
00236                                 else if (an_packet->id == packet_id_quaternion_orientation)
00237                                 {
00238                                         if(decode_quaternion_orientation_packet(&quaternion_orientation_packet,an_packet) == 0)                                 
00239                                         printf("quaternion_orientation_packet:\n");
00240                                         printf("\tQ0 = %f, Q1 = %f, Q2 = %f, Q3 = %f\n", quaternion_orientation_packet.orientation[0], quaternion_orientation_packet.orientation[1], quaternion_orientation_packet.orientation[2],quaternion_orientation_packet.orientation[3]);
00241                                         
00242                                 }
00243                                 else if (an_packet->id == packet_id_external_position_velocity)
00244                                 {
00245                                         if(decode_external_position_velocity_packet(&external_position_velocity_packet,an_packet) == 0)                                 
00246                                         printf("external_position_velocity_packet:\n");
00247                                         printf("\tVEL_NORTH = %f, VEL_EAST = %f, VEL_DOWN = %f\n", external_position_velocity_packet.velocity[0], external_position_velocity_packet.velocity[1], external_position_velocity_packet.velocity[2]);
00248                                 }
00249                                 else if (an_packet->id == packet_id_running_time)
00250                                 {
00251                                         if(decode_running_time_packet(&running_time_packet,an_packet) == 0)                                     
00252                                         printf("running_time_packet:\n");
00253                                         printf("\tseconds = %d, microseconds = %d", running_time_packet.seconds, running_time_packet.microseconds);
00254                                 }
00255                                 /* Ensure that you free the an_packet when your done with it or you will leak memory */
00256                                 an_packet_free(&an_packet);
00257                         }
00258                 }
00259 #ifdef _WIN32
00260     Sleep(10);
00261 #else
00262     usleep(10000);
00263 #endif
00264         }
00265 }


dynamic
Author(s): wnl
autogenerated on Tue Feb 25 2014 12:52:52