urgTicksCtrl.cpp

00001 /*
00002   URGCtrl における時間同期機能の実装
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "urgCtrl.h"
00008 #include "timeUtils.h"
00009 
00010 
00011 enum {
00012   Timeout = 150,
00013 };
00014 
00015 
00016 void URGCtrl::initTicksInfo(void) {
00017   beginTimeAdjust();
00018   pre_module_ticks = static_cast<unsigned short>(getURGTimestamp());
00019 
00020   ignore_beginTimeAdjust = true;
00021   adjustOwnTicks(0);
00022 }
00023 
00024 
00025 unsigned long URGCtrl::getHostTicks(void) {
00026   return VXV::GetTicks();
00027 }
00028 
00029 
00030 void URGCtrl::skipReply(int n) {
00031   char buffer[16];
00032 
00033   for (int i = 0; i < n; ++i) {
00034     //fprintf(stderr, "%d: \n", i);
00035     con->recv_line(buffer, 15, Timeout);
00036   }
00037 }
00038 
00039 
00040 void URGCtrl::beginTimeAdjust(void) {
00041   if (ignore_beginTimeAdjust) {
00042     ignore_beginTimeAdjust = false;
00043     return;
00044   }
00045   con->send("TM0\r", 4);
00046   skipReply(3);
00047 }
00048 
00049 
00050 void URGCtrl::endTimeAdjust(void) {
00051   con->send("TM2\r", 4);
00052   skipReply(3);
00053 
00054   //fprintf(stderr, "laser\n");
00055   con->laser(true);
00056 }
00057 
00058 
00059 int URGCtrl::updateTicksDiff(unsigned short ticks) {
00060   total_msec += ticks - pre_module_ticks;
00061   pre_module_ticks = ticks;
00062 
00063   return total_msec;
00064 }
00065 
00066 
00067 long URGCtrl::decode(const char* data, int data_byte) {
00068   long value = 0;
00069   for (int i = 0; i < data_byte; ++i) {
00070     value <<= 6;
00071     value &= ~0x3f;
00072     value |= data[i] - 0x30;
00073   }
00074   return value;
00075 }
00076 
00077 
00078 unsigned long URGCtrl::getURGTimestamp(void) {
00079   con->send("TM1\r", 4);
00080   skipReply(2);
00081 
00082   char ticks_buffer[8] = { 0, 0, 0, 0, 0, 0, '\0', '\0' };
00083   int n = con->recv_line(ticks_buffer, 6, Timeout);
00084   //fprintf(stderr, "n = %d\n", n);
00085   //fprintf(stderr, "%c%c%c%c%c\n", ticks_buffer[0],ticks_buffer[1],ticks_buffer[2],ticks_buffer[3], ticks_buffer[4]);
00086   //fprintf(stderr, "%02x %02x %02x %02x %02x\n", ticks_buffer[0],ticks_buffer[1],ticks_buffer[2],ticks_buffer[3], ticks_buffer[4]);
00087   unsigned long raw_ticks = 0;
00088   if (n >= 5) {
00089     raw_ticks = decode(ticks_buffer, 4);
00090     //fprintf(stderr, "raw_ticks: %d\n", raw_ticks);
00091   }
00092   skipReply(1);
00093 
00094   return raw_ticks;
00095 }
00096 
00097 
00098 unsigned long URGCtrl::getModuleRawTicks(void) {
00099   return updateTicksDiff(static_cast<unsigned short>(getURGTimestamp()));
00100 }
00101 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1