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

# Platform/Aperios1.3.2/GTCamTimeSyncHandler.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file  Platform/Aperios1.3.2/GTCamTimeSyncHandler.cpp
    00003 * @author Matthias Meyer
    00004 *
    00005 */
    00006
    00007 #include "GTCamTimeSyncHandler.h"
    00008
    00009 /**
    00010 * Constructor
    00011 */
    00012 GTCamTimeSyncHandler::GTCamTimeSyncHandler() :
    00013 UDPEndpoint(2048,2048)
    00014 {
    00015   robotTimeRequestWasSent = -1;
    00016   robotTimeRequestWasReceived = -1;
    00017   serverTimeInResponse = -1;
    00018   received2Packages = false;
    00019   timeServer = "127.0.0.1";
    00020   //cout << "GTCAM: Generating TimeSyncHandler\n";
    00021 }
    00022
    00023 /**
    00024 * Init the handler
    00025 */
    00026 void GTCamTimeSyncHandler::initGTCamTimeSyncHandler(GTCamWorldState* myState, Image* myImage)
    00027 {
    00028   int camport = GTCAMPORT;//std port
    00029   int syncport = GTCAMSYNCPORT;
    00030   double version;
    00031
    00032   //read port
    00033   InConfigFile file("gtcam.cfg");
    00034   // format:
    00035   // Version x.x
    00036   // CAMPORT x
    00037   // SYNCPORT x
    00038   // spaces and nl are ignored
    00039   if(file.exists())
    00040   {
    00041     char buf[250];
    00042     while(!file.eof())
    00043     {
    00044       file >> buf;
    00045       if ( strcmp(buf,"Version")==0 )
    00046       {
    00047         file >> version;
    00048       }else
    00049       if ( strcmp(buf,"CAMPORT")==0 )
    00050       {
    00051         file >> camport;
    00052       }
    00053       if ( strcmp(buf,"SYNCPORT")==0 )
    00054       {
    00055         file >> syncport;
    00056       }
    00057     }
    00058   }
    00059
    00060   bind(syncport); // GTCamTimeSync data
    00061   state = myState;    // The worldstate
    00062   image = myImage;
    00063   //cout << "GTCAM: Binding to Port " << GTCAMSYNCPORT << "\n";
    00064 }
    00065
    00066
    00067 /**
    00068 * Receive data
    00069 */
    00070 void GTCamTimeSyncHandler::onReceive(void* data, int size)
    00071 {
    00072   // Do we have 2 packages ?
    00073   if (serverTimeInResponse != -1) {
    00074     received2Packages = true;
    00075   }
    00076
    00077   // copy input to serverTimeResponse
    00078   memcpy(&serverTimeInResponse,data,size);
    00079   robotTimeRequestWasReceived = SystemCall::getCurrentSystemTime();
    00080   frameNumberTimeRequestWasReceived = image->frameNumber;
    00081   calculateOffset();
    00082   //cout << "GTCAM: New TimeOffset" << offset << "\n";
    00083 }
    00084
    00085 /**
    00086 * calculate the offset
    00087 */
    00088 void GTCamTimeSyncHandler::calculateOffset() {
    00089   offset = serverTimeInResponse - (robotTimeRequestWasSent / 2) - (robotTimeRequestWasReceived / 2);
    00090   buffer.add((double)offset);
    00091   // cout << "GTCAM: New TimeOffset2 : " << offset << "\n";
    00092   // cout << buffer.getSum() << "\n";
    00093   offset = (long)(buffer.getSum() / buffer.getNumberOfEntries());
    00094
    00095
    00096
    00097   frameOffset = serverTimeInResponse / 8 -
    00098                 (frameNumberTimeRequestWasSent / 2) - (frameNumberTimeRequestWasReceived / 2);
    00099
    00100  /* cout << "GTCAM: " << frameOffset << " = " << serverTimeInResponse / 8 << " - " <<
    00101     frameNumberTimeRequestWasSent / 2 << " - " << frameNumberTimeRequestWasReceived / 2 << " ----- ";*/
    00102   frameNumberBuffer.add(frameOffset);
    00103
    00104   frameOffset = frameNumberBuffer.getSum() / frameNumberBuffer.getNumberOfEntries();
    00105   //cout << frameOffset << " \n";
    00106
    00107
    00108
    00109 }
    00110
    00111 /**
    00112 * Start the syncing
    00113 */
    00114 void GTCamTimeSyncHandler::startTimeSync() {
    00115   double id = (double)getPlayer().getPlayerNumber();
    00116   send(&id,sizeof(id),timeServer,GTCAMSYNCPORT);
    00117   robotTimeRequestWasSent = SystemCall::getCurrentSystemTime();
    00118   frameNumberTimeRequestWasSent = image->frameNumber;
    00119 }