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 #include <sys/types.h>
00030 #include <unistd.h>
00031 #include <errno.h>
00032 #include <string>
00033 #include <stdio.h>
00034 #include <sys/socket.h>
00035 #include <netinet/in.h>
00036 #include <netdb.h>
00037 #include <errno.h>
00038 #include <iostream>
00039
00040 #include "trackerd_orientation.hh"
00041
00042 using namespace std;
00043
00044 TrackerdOrientation::TrackerdOrientation(const char* host, unsigned int port, unsigned int otid) throw (DeviceError)
00045 {
00046 id = otid;
00047 struct sockaddr_in address;
00048 if( (socket_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
00049 throw DeviceError(string("Error creating socket: ") + strerror(errno));
00050 }
00051 struct hostent* he;
00052 if(!(he = gethostbyname(host))) {
00053 throw DeviceError(string("Invalid address: ") + host);
00054 }
00055 memset(&address, 0, sizeof(address));
00056 address.sin_port=htons(port);
00057 address.sin_family=AF_INET;
00058 address.sin_addr=*((struct in_addr *)he->h_addr);
00059 if( connect(socket_fd, (struct sockaddr *)&address, sizeof(address)) == -1) {
00060 char port_s[16];
00061 snprintf(port_s, sizeof(port_s), "%d", port);
00062 throw DeviceError(string("Error connecting to server ") + host + ":" + port_s + ". " + strerror(errno));
00063 }
00064 }
00065
00066 TrackerdOrientation::~TrackerdOrientation()
00067 {
00068 shutdown(socket_fd, 2);
00069 }
00070
00071 void TrackerdOrientation::getOrientationMatrix(double mat[3][3]) throw(DeviceError)
00072 {
00073
00074 char msg[256];
00075 unsigned int id;
00076 if( write(socket_fd, "om:0", 4) != 4) {
00077 throw DeviceError( string("Error sending command to trackerd: ") + strerror(errno));
00078 }
00079 recv(socket_fd, msg, 256, 0);
00080
00081 if(sscanf(msg, "om:%u=%lf,%lf,%lf;%lf,%lf,%lf;%lf,%lf,%lf",
00082 &id,
00083 &mat[0][0], &mat[0][1], &mat[0][2],
00084 &mat[1][0], &mat[1][1], &mat[1][2],
00085 &mat[2][0], &mat[2][1], &mat[2][2]) < 9)
00086 {
00087 throw DeviceError("Misparse of data from trackerd");
00088 }
00089
00090 }
00091
00092 void TrackerdOrientation::getEulerAngles(double* yaw, double* pitch, double* roll) throw(DeviceError)
00093 {
00094 char msg[256];
00095 unsigned int id = 0;
00096 if( write(socket_fd, "oa:0", 4) != 4) {
00097 throw DeviceError( string("Error sending command to trackerd: ") + strerror(errno));
00098 }
00099 recv(socket_fd, msg, 256, 0);
00100
00101 if(sscanf(msg, "oa:%u=%lf,%lf,%lf", &id, yaw, pitch, roll) < 4) {
00102 throw DeviceError("Misparse of data from trackerd");
00103 }
00104 *yaw += eulerYawOffset;
00105 *pitch += eulerPitchOffset;
00106 *roll += eulerRollOffset;
00107
00108 }
00109