SPYSpheres
ZRCommRoutines.cpp
Go to the documentation of this file.
00001 //Basic SPHERES communications
00002 
00003 #include "ZRGame.h"
00004 
00005 //SPHERES C includes
00006 extern "C" {
00007 #include "spheres_types.h"
00008 #include "comm.h"
00009 }
00010 
00011 void ZeroRoboticsGameImpl::processRXData(default_rfm_packet packet)
00012 {
00013   if (packet[PKT_CM] == COMM_CMD_DBG_SHORT_SIGNED)
00014   {
00015     dbg_short_packet DebugVecShort;
00016     memcpy(DebugVecShort, &packet[PKT_DATA], sizeof(dbg_short_packet));
00017     if (ctrlManeuverNumGet() > 2)
00018     {
00019       for (int i = 0; i < NUM_ITEMS; i++){
00020         challInfo.other.mpTime[i] = DebugVecShort[i+1];
00021 
00022       }
00023       // copy general data during game run
00024       challInfo.other.mirrorTime = DebugVecShort[14];
00025       challInfo.other.message = DebugVecShort[15];
00026     }
00027     else {
00028       // initialization data for items
00029       if (DebugVecShort[0] == 0){
00030         #if (SPHERE_ID == SPHERE1)
00031           // get data from SPHERE2
00032           for (int i = 0; i < NUM_ITEMS - 5; i++){
00033                   challInfo.mpLoc[i + 5][0] = DebugVecShort[3*i + 1] / 10000.0f;
00034                   challInfo.mpLoc[i + 5][1] = DebugVecShort[3*i + 2] / 10000.0f;
00035                   challInfo.mpLoc[i + 5][2] = DebugVecShort[3*i + 3] / 10000.0f;
00036           }
00037         #else
00038           // get data from SPHERE1
00039           for (int i = 0; i < 5; i++){
00040                   challInfo.mpLoc[i][0] = DebugVecShort[3*i + 1] / 10000.0f;
00041                   challInfo.mpLoc[i][1] = DebugVecShort[3*i + 2] / 10000.0f;
00042                   challInfo.mpLoc[i][2] = DebugVecShort[3*i + 3] / 10000.0f;
00043           }
00044 
00045         #endif
00046       }
00047     }
00048   }
00049   else if (packet[PKT_CM] == COMM_CMD_DBG_SHORT_UNSIGNED)
00050   {
00051     dbg_ushort_packet DebugVecUShort;
00052     memcpy(DebugVecUShort, &packet[PKT_DATA], sizeof(dbg_ushort_packet));
00053       int cmn = ctrlManeuverNumGet();
00054       // printf("cmn %d\n",cmn);
00055       // recieve initialization packet (control maneuver 1)
00056       if (cmn < 3){
00057         #if (SPHERE_ID == SPHERE2)
00058           // printf("Init DebugVecShort Received by Sphere 2: %d (should be 0), %d\n", DebugVecShort[0], cmn);
00059           // make sure its one of our initialization packets, all other debugVecShort[0] are time * 10, so they are larger than 0
00060           if (DebugVecUShort[0] == 0)
00061           {
00062             challInfo.light.nextSwitchTime = (int) DebugVecUShort[1];
00063           }
00064 
00065         #endif
00066       }
00067 
00068     //challInfo.other.mpTime[0] = DebugVecUShort[1];
00069     //challInfo.other.mpTime[1] = DebugVecUShort[2];
00070   }
00071   else if (packet[PKT_CM] == COMM_CMD_DBG_FLOAT)
00072   {
00073     dbg_float_packet DebugVecFloat;
00074     memcpy(DebugVecFloat, &packet[PKT_DATA], sizeof(dbg_float_packet));
00075     int cmn = ctrlManeuverNumGet();
00076     if (cmn < 3) {
00077         #if (SPHERE_ID == SPHERE2)
00078           //printf("Init DebugVecFloat Received by Sphere 2: %f (should be -1), %d\n", DebugVecFloat[0], cmn);
00079           // make sure its one of our initialization packets. tstep should be nonnegative, I think.
00080         if (DebugVecFloat[0] == 0.0f)
00081         {
00082           challInfo.light.center = DebugVecFloat[1];
00083           challInfo.light.direction = DebugVecFloat[2];
00084 
00085           return;
00086         }
00087         #endif
00088     }
00089     challInfo.other.score = DebugVecFloat[1];
00090     challInfo.other.energy = DebugVecFloat[3];
00091   }
00092 }
00093 
00094 void ZeroRoboticsGameImpl::sendDebug()
00095 {
00096   dbg_short_packet  DebugVecShort; // short[16]
00097   dbg_ushort_packet DebugVecUShort; // ushort[16]
00098   dbg_float_packet  DebugVecFloat; // float[8]
00099   unsigned int tstep;
00100 
00101   // send debug after estimator convergence
00102   if (ctrlManeuverNumGet() < 3)
00103   {
00104     sendInit();
00105   }
00106   if (ctrlManeuverNumGet() > 1)
00107   {
00108     // normal game packages
00109     tstep = apiImpl.api->getTime();
00110 
00111     if (!tstep) return;   // only send starting with time 1, since time 0 can cause problems with initialization
00112 
00113     // initialize all packets to 0
00114     memset(DebugVecShort,  0, sizeof(dbg_short_packet));
00115     memset(DebugVecUShort, 0, sizeof(dbg_ushort_packet));
00116     memset(DebugVecFloat,  0, sizeof(dbg_float_packet));
00117 
00118     // debug short: POIs currently visible, other sat message, flare status
00119     DebugVecShort[0]  = (short)(tstep*10);            //Timestamp
00120     DebugVecShort[13] = game->getMirrorTimeRemaining();
00121     DebugVecShort[14] = challInfo.me.mirrorTime;
00122 
00123     //items
00124     for (int i = 0; i < NUM_ITEMS; i++){
00125       DebugVecShort[i + 1] = (unsigned short) challInfo.me.mpTime[i];
00126     }
00127 
00128     DebugVecShort[15] = challInfo.me.message;                   //message sent between players
00129 
00130     // unsigned short debug packet: status of game variables, known flare times
00131     DebugVecUShort[0] = (unsigned short)(tstep*10); //Timestamp
00132 
00133 
00134     DebugVecUShort[7] = (unsigned short) (sphereInLight(challInfo.me.zrState));
00135     DebugVecUShort[7] <<= 1;
00136     DebugVecUShort[7] += (unsigned short) (sphereInDark(challInfo.me.zrState));
00137 
00138     DebugVecUShort[8] = (challInfo.light.direction > 0);
00139     DebugVecUShort[9] = (unsigned short) (challInfo.camera.tookPicture);
00140     DebugVecUShort[10] = (unsigned short) (challInfo.camera.uploadedPictures);
00141     DebugVecUShort[11] = (unsigned short) (challInfo.camera.cameraOn);
00142     DebugVecUShort[13] = (unsigned short) (challInfo.camera.memorySize);
00143     DebugVecUShort[14] = (unsigned short) (challInfo.camera.memoryFilled);
00144 
00145     //Float debug packet: score, fuel, forces
00146     DebugVecFloat[0] = (float)tstep;
00147     DebugVecFloat[1] = game->getScore();
00148     DebugVecFloat[2] = game->getFuelRemaining() / ((double)PROP_ALLOWED_SECONDS);
00149     DebugVecFloat[3] = challInfo.me.energy;
00150     DebugVecFloat[4] = challInfo.light.center;
00151 
00152     memcpy(&DebugVecFloat[5], challInfo.me.userForces, 3*sizeof(float)); //Forces for reference
00153 
00154     //Send packets to other SPHERES/ground/sim; do not modify below this line
00155     commSendPacket(COMM_CHANNEL_STL, BROADCAST, 0, COMM_CMD_DBG_SHORT_SIGNED, (unsigned char *) DebugVecShort,0);
00156     commSendPacket(COMM_CHANNEL_STL, BROADCAST, 0, COMM_CMD_DBG_FLOAT, (unsigned char *) DebugVecFloat,0);
00157     commSendPacket(COMM_CHANNEL_STL, BROADCAST, 0, COMM_CMD_DBG_SHORT_UNSIGNED, (unsigned char *) DebugVecUShort,0);
00158 
00159   #ifdef ZRSIMULATION
00160     apiImpl.ZRUserDbgVec[0] = (float)tstep;
00161     commSendPacket(COMM_CHANNEL_STL, GROUND, sysIdentityGet(), COMM_CMD_DBG_ZRUSER, (unsigned char *) apiImpl.ZRUserDbgVec,0);
00162   #endif
00163   }
00164 }
00165 
00166 void ZeroRoboticsGameImpl::sendInit()
00167 {
00168   #if (SPHERE_ID == SPHERE1)
00169       dbg_short_packet DebugVecShort;
00170       dbg_ushort_packet DebugVecUShort;
00171       dbg_float_packet DebugVecFloat;
00172 
00173     // sends light information to second satellite
00174     memset(DebugVecShort,  0, sizeof(dbg_short_packet));    // initialize packets to 0
00175     memset(DebugVecUShort,  0, sizeof(dbg_ushort_packet));
00176     memset(DebugVecFloat,  0, sizeof(dbg_float_packet));
00177 
00178     DebugVecShort[0] = 0;
00179 
00180     int upTo = 5;
00181     if (NUM_ITEMS < upTo) {
00182       upTo = NUM_ITEMS;
00183     }
00184 
00185     for (int i = 0; i < upTo; i++) {
00186       DebugVecShort[3*i + 1] = 10000 * challInfo.mpLoc[i][0];
00187       DebugVecShort[3*i + 2] = 10000 * challInfo.mpLoc[i][1];
00188       DebugVecShort[3*i + 3] = 10000 * challInfo.mpLoc[i][2];
00189     }
00190 
00191     DebugVecUShort[0] = 0;
00192     DebugVecUShort[1] = (short)0 + (challInfo.light.nextSwitchTime);
00193 
00194     #ifdef ALLIANCE
00195       DebugVecUShort[2] = 3;
00196     #else
00197       #ifdef ZR3D
00198         DebugVecUShort[2] = 2;
00199       #else
00200         #ifdef ZR2D
00201           DebugVecUShort[2] = 1;
00202         #else
00203           DebugVecUShort[2] = 0;
00204         #endif
00205       #endif
00206     #endif
00207 
00208     DebugVecUShort[3] = NUM_ITEMS;
00209 
00210     for (int i = 0; i < NUM_ITEMS; i++)
00211     {
00212       DebugVecUShort[4 + i] = ITEM_TYPES[i];
00213     }
00214 
00215       DebugVecFloat[0] = 0.0f;
00216       DebugVecFloat[1] = challInfo.light.center;
00217       DebugVecFloat[2] = challInfo.light.direction;
00218 
00219     commSendPacket(COMM_CHANNEL_STL, BROADCAST, 0, COMM_CMD_DBG_SHORT_SIGNED, (unsigned char *) DebugVecShort,0);
00220     commSendPacket(COMM_CHANNEL_STL, BROADCAST, 0, COMM_CMD_DBG_SHORT_UNSIGNED, (unsigned char *) DebugVecUShort,0);
00221     commSendPacket(COMM_CHANNEL_STL, BROADCAST, 0, COMM_CMD_DBG_FLOAT, (unsigned char *) DebugVecFloat,0);
00222   #endif
00223   #if (SPHERE_ID == SPHERE2)
00224       dbg_short_packet DebugVecShort;
00225       DebugVecShort[0] = 0;
00226 
00227       for (int i = 0; i < NUM_ITEMS - 5; i++)
00228       {
00229         DebugVecShort[3*i + 1] = 10000 * challInfo.mpLoc[i+5][0];
00230         DebugVecShort[3*i + 2] = 10000 * challInfo.mpLoc[i+5][1];
00231         DebugVecShort[3*i + 3] = 10000 * challInfo.mpLoc[i+5][2];
00232       }
00233 
00234       commSendPacket(COMM_CHANNEL_STL, BROADCAST, 0, COMM_CMD_DBG_SHORT_SIGNED, (unsigned char *) DebugVecShort,0);
00235   #endif
00236 }