Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/tracking/trackerd_orientation.cc

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001, 2002 Peter Amstutz, Reed Hedges
00006 
00007     This software was written at the University of Massachusetts with
00008     support from NSF grant #EIA 9703217
00009 
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2 of the License, or (at your option) any later version.
00014 
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00023 
00024     Peter Amstutz <tetron@interreality.org>
00025     Reed Hedges <reed@interreality.org>
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     // TODO use write and read instead of send and recv?
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     //cerr << "Debug: got om from trackerd: " << msg <<endl;
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     // XXX TODO matrix offset
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     //cerr << "Debug: got oa from trackerd: \"" << msg << "\"\n";
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 

Generated on Tue Aug 12 03:55:38 2003 for Interreality Project - VOS by doxygen 1.3.2