/Users/craigcornelius/Projects/SPRING Mac Release 0.2/haptic_v2.cpp

Go to the documentation of this file.
00001 // $Id: haptic_v2.cpp,v 1.9 2001/10/17 16:49:35 kevin Exp $
00002 //
00003 //  Haptic_v2.cpp
00004 //
00005 
00006 #include "haptic_v2.h"
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include "space.h"
00010 #include "triangles.h"
00011 #include "void.h"
00012 
00013 #ifdef _WIN32
00014 #else
00015 #include <string.h>
00016 #endif
00017 
00018 // port number to connect to
00019 #define HSOCK 9999
00020 
00021 // buffer size- well under the 1500 bytes ethernet limit, 
00022 // so will fit in 1 packet
00023 #define BUFSIZE 300
00024 
00025 const char* Haptic_v2::rcsid = "@(#) $Id: haptic_v2.cpp,v 1.9 2001/10/17 16:49:35 kevin Exp $ $Copyright: (c)2001 National Biocomputation Center, Stanford University $";
00026 
00027 int Haptic_v2::debug = 0;
00028 
00029 Haptic_v2::Haptic_v2(char* machine_name)
00030 : Sensor(Sensor::haptic_v2_sensor)
00031 {
00032     num_active = 1;
00033     
00034     hash = 0;
00035     my_space_t = new space_t();
00036     my_space_v = new space_v();
00037     my_space_i = new space_i();
00038     points_to_save = new space_i();
00039     my_space = my_space_v;
00040     
00041     // set up socket:
00042     buf = (char *)malloc(sizeof(char)*BUFSIZE);
00043     sizebuf=BUFSIZE;
00044     char server[80]; // = "maki.stanford.edu";
00045     strncpy(server, machine_name, 80);
00046     
00047     cerr << "making new socket to: " << server << endl;
00048     hsock = new Socket(server,Socket::tcp,HSOCK);
00049     cerr << "created socket" << endl;
00050     
00051     // send initial handshake
00052     sprintf(buf, "haptic v2.0");
00053     hsock->Send(buf,BUFSIZE);
00054     
00055     // Check to make sure haptic responded with an acknowledgement
00056     // (should ideally respond with what type of device it is, so we can
00057     // do device-specific things on this side (like remap coords, etc))
00058     hsock->Receive(buf,BUFSIZE);
00059     cerr << "BUF: [" << buf << "]\n";
00060     
00061     // test their response- do they support this version of the protocol?
00062     if (strncmp(buf,"ACK",3) != 0) {
00063         // no ACK, so an error has occured and we cannot connect
00064         cerr << "ERROR - denied connection" << endl;
00065     } else {
00066         // success
00067         cerr << "Connection made" << endl;
00068     }
00069     
00070 }
00071 
00072 Haptic_v2::~Haptic_v2()
00073 {
00074     us_t l;
00075     buf[0] = TYPE_QUIT;
00076     l.s = htonl(0);
00077     for(int i=0; i<2; i++)
00078         buf[1+i] = l.c[i];
00079     hsock->Send(buf, 3);
00080     delete hsock;
00081 }
00082 
00083 void Haptic_v2::Update()
00084 {
00085     
00086     char headers[3];
00087     char *oldbuf;
00088     unsigned short size;
00089     space_type new_type;
00090     
00091     //
00092     // Send the space if it changed.
00093     //
00094     
00095     if(this->spaceChanged())
00096     {
00097         //    cerr << "points_to_save: " << points_to_save->my_points->size() << endl;
00098         //    cerr << "my_space_i: " << my_space_i->my_points->size() << endl;
00099         //    cerr << "my_space_t: " << my_space_t->my_triangles->size() << endl;
00100         my_space->send_datas(hsock);
00101         my_space->print_datas();
00102     }
00103 #if 0
00104     if(this->spaceChanged() && (my_space != my_space_t))
00105     {
00106         my_space->send_datas(hsock);
00107     }
00108 #endif
00109     
00110     //
00111     // Get the position (and later, rotation matrix)
00112     //
00113     
00114     //  cout << "RECEIVED" << endl;
00115     // get the buffer
00116     
00117     while(hsock->InputQueueSize() >= 3)
00118     {
00119         if(hsock->Receive(headers, 3) == 3)
00120         {
00121             new_type = headers[0];
00122             receive_ushort_from_buf(headers + 1, &size);
00123             if(size > sizebuf)
00124             {
00125                 oldbuf = buf;
00126                 buf = (char *) realloc(oldbuf, size * sizeof(char));
00127                 if(buf)
00128                     sizebuf = size;
00129                 else
00130                 {
00131                     cerr << "problem of memory... I will go mad," << endl;
00132                     buf = oldbuf;
00133                 }
00134             }
00135             if(size <= sizebuf) {
00136                 if(new_type == SEND_QUIT) {
00137                     cerr << "The haptics did quit... I will go mad." << endl;
00138                 }
00139                 if(hsock->Receive(buf, size) != size) {
00140                     cerr << "I didn't receive a packet of the right length..." << endl;
00141                     cerr << "I will go mad," << endl;
00142                 }
00143                 else if (hsock->InputQueueSize() < 3) {
00144                     switch(headers[0])
00145                     {
00146                     case SEND_QUIT : break;
00147                     case SEND_COORD:
00148                         if(size == 56) {
00149                             int i,j;
00150                             double position[12];
00151                             double grip;
00152                             long touching;
00153                             char *index = buf;
00154                             for(i=0; i<12; i++)
00155                             {
00156                                 receive_float_from_buf(index, position + i);
00157                                 index+=4;
00158                             }
00159                             receive_float_from_buf(index, &grip); index+=4;
00160                             receive_long_from_buf(index, &touching); index+=4;
00161                             
00162                             // unpack the position
00163                             pos.x = position[0];
00164                             pos.y = position[1];
00165                             pos.z = position[2];
00166                             pos -= init_pos;    // center at user-given origin
00167                             pos *= scalefactor; // and scale it by the given factor
00168                             
00169                             // unpack the rotation matrix
00170                             for(i=0; i<3;i++)
00171                                 for(j=0;j<3;j++)
00172                                     m.m[i][j] = position[3 + j + 3*i];
00173                                 active[0] = float(grip);
00174                         }
00175                         else {
00176                             cerr << "I received a coords packets of the wrong size." << endl;
00177                             cerr << "Ignoring it." << endl;
00178                         } 
00179                     } // end switch
00180                 }
00181             }  // size <= sizebuf
00182         }
00183         else {
00184             cerr << "I just received a packet with size < 3..." << endl;
00185         }
00186     }
00187 #if 0
00188     {
00189         
00190         hsock->Receive(buf,BUFSIZE);
00191         
00192         // parse the position info
00193         Point3D hpos;
00194         double hm[3][3];
00195         double hactive;
00196         sscanf(buf,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
00197             &hpos.x, &hpos.y, &hpos.z,
00198             &hm[0][0], &hm[0][1], &hm[0][2], &hm[1][0], &hm[1][1], &hm[1][2],
00199             &hm[2][0], &hm[2][1], &hm[2][2], &hactive);
00200         
00201         // and remap it
00202         pos.x = hpos.y;
00203         pos.y = hpos.x;
00204         pos.z = -hpos.z;
00205         pos -= init_pos;        // center at user-given origin
00206         pos *= scalefactor;     // and scale it by the given factor
00207         {
00208             int i,j;
00209             for(i=0; i<3; i++)
00210                 for(j=0; j<3; j++)
00211                     m[i][j] = hm[i][j];
00212         }
00213         active = hactive;
00214         //  cout << "BUF: " << buf << endl;
00215         //****************************
00216     }
00217 #endif
00218 }
00219 
00220 int Haptic_v2::spaceChanged()
00221 {
00222     static long hash = 0;
00223     static long tmp;
00224     
00225     tmp = my_space->gethash();
00226     if(tmp != hash)
00227     {
00228         hash = tmp;
00229         return 1;
00230     }
00231     return 0;
00232 }

Generated on Thu Aug 30 11:03:14 2007 for SPRING Mac by  doxygen 1.5.3