Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <stdint.h>
00033 #include <math.h>
00034
00035 #ifdef _WIN32
00036 #include <Windows.h>
00037 #else
00038 #include <unistd.h>
00039 #endif
00040
00041 #include "rs232/rs232.h"
00042 #include "an_packet_protocol.h"
00043 #include "orientus_packets.h"
00044
00045 #define RADIANS_TO_DEGREES (180.0/M_PI)
00046
00047 int com_port_number;
00048
00049 int an_packet_transmit(an_packet_t *an_packet)
00050 {
00051 an_packet_encode(an_packet);
00052 return SendBuf(com_port_number, an_packet_pointer(an_packet), an_packet_size(an_packet));
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 void set_sensor_ranges()
00065 {
00066 an_packet_t *an_packet;
00067 sensor_ranges_packet_t sensor_ranges_packet;
00068
00069 sensor_ranges_packet.permanent = TRUE;
00070 sensor_ranges_packet.accelerometers_range = accelerometer_range_4g;
00071 sensor_ranges_packet.gyroscopes_range = gyroscope_range_500dps;
00072 sensor_ranges_packet.magnetometers_range = magnetometer_range_2g;
00073
00074 an_packet = encode_sensor_ranges_packet(&sensor_ranges_packet);
00075
00076 an_packet_transmit(an_packet);
00077
00078 an_packet_free(&an_packet);
00079 }
00080
00081 int main(int argc, char *argv[])
00082 {
00083 an_decoder_t an_decoder;
00084 an_packet_t *an_packet;
00085
00086 status_packet_t status_packet;
00087 euler_orientation_packet_t euler_orientation_packet;
00088 raw_sensors_packet_t raw_sensors_packet;
00089
00090 int bytes_received;
00091
00092 if (argc != 3)
00093 {
00094 printf("Usage - program com_port_number baud_rate\nExample - packet_example.exe 0 115200\n");
00095 exit(EXIT_FAILURE);
00096 }
00097
00098 com_port_number = atoi(argv[1]);
00099
00100
00101 if (OpenComport(com_port_number, atoi(argv[2])))
00102 {
00103 exit(EXIT_FAILURE);
00104 }
00105
00106 an_decoder_initialise(&an_decoder);
00107
00108 while (1)
00109 {
00110 if ((bytes_received = PollComport(com_port_number, an_decoder_pointer(&an_decoder), an_decoder_size(&an_decoder))) > 0)
00111 {
00112
00113 an_decoder_increment(&an_decoder, bytes_received);
00114
00115
00116 while ((an_packet = an_packet_decode(&an_decoder)) != NULL)
00117 {
00118 if(an_packet->id == packet_id_status)
00119 {
00120 if(decode_status_packet(&status_packet, an_packet) == 0)
00121 {
00122 printf("Status Packet:\n");
00123 printf("\tFilter Ready = %d\n", status_packet.filter_status.b.orientation_filter_initialised);
00124 }
00125 }
00126 else if (an_packet->id == packet_id_euler_orientation)
00127 {
00128
00129
00130 if(decode_euler_orientation_packet(&euler_orientation_packet, an_packet) == 0)
00131 {
00132 printf("Euler Orientation Packet:\n");
00133 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);
00134 }
00135 }
00136 else if (an_packet->id == packet_id_raw_sensors)
00137 {
00138
00139
00140 if(decode_raw_sensors_packet(&raw_sensors_packet, an_packet) == 0)
00141 {
00142 printf("Raw Sensors Packet:\n");
00143 printf("\tAccelerometers X: %f Y: %f Z: %f\n", raw_sensors_packet.accelerometers[0], raw_sensors_packet.accelerometers[1], raw_sensors_packet.accelerometers[2]);
00144 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);
00145 }
00146 }
00147 else
00148 {
00149 printf("Packet ID %u of Length %u\n", an_packet->id, an_packet->length);
00150 }
00151
00152
00153 an_packet_free(&an_packet);
00154 }
00155 }
00156 #ifdef _WIN32
00157 Sleep(10);
00158 #else
00159 usleep(10000);
00160 #endif
00161 }
00162 }