TUsb Respiration Belt plugin 1.0

src/RespirationBelt.cpp

Go to the documentation of this file.
00001 
00012 #include "Plugin.h"
00013 
00014 
00018 RespirationBelt::RespirationBelt()
00019 {
00020   // Create a new TUsb COM controller
00021         m_pIMonCom = new IT_USBComPtr(__uuidof(Controller)); 
00022 
00023   // Launch update loop thread
00024   m_isConnected = false;
00025   exitRequested = false;
00026   comState = false;
00027 
00028   // Set refresh loop rate according to acquisition frequency
00029   int fps = static_cast<int> (1000.0f / 60.0f);
00030   loopThread = new boost::thread(boost::bind(&RespirationBelt::GoThread, this), boost::posix_time::millisec(fps));
00031 
00032   // Initialize class members
00033   SetChestExtension(0.0f);
00034   SetBatteryLevel(0.0f);
00035 }
00036 
00037 RespirationBelt::~RespirationBelt()
00038 {
00039   // Stop update loop thread
00040   exitRequested = true;
00041   comState = false;
00042   if (loopThread != 0)
00043     loopThread->join();
00044   
00045   // Close connection to TUsb module
00046   Disconnect();
00047 }
00048 
00049 
00053 double RespirationBelt::GetChestExtension()
00054 {
00055   return m_chestExtension;
00056 }
00057 
00058 float RespirationBelt::GetBatteryLevel()
00059 {
00060   return m_batteryLevel;
00061 }
00062 
00063 void RespirationBelt::SetChestExtension(double extensionPercent)
00064 {
00065   m_chestExtension = extensionPercent;
00066 }
00067 
00068 void RespirationBelt::SetBatteryLevel(float batteryPercent)
00069 {
00070   m_batteryLevel = batteryPercent;
00071 }
00072 
00073 
00077 bool RespirationBelt::Connect(long comPort, long carryingFrequency)
00078 {
00079   // Set COM port and connect to TUsb
00080   MMechostr(MSKDEBUG, "COM connection attempt on port [%d]\n", comPort);
00081         int ret = m_pIMonCom->setPortCom(comPort);
00082         if (ret == 1)
00083         {
00084                 // Connection successful
00085                 MMechostr(MSKDEBUG, "TUSB is now connected on port [%ld] \nNow searching for TSens...", comPort);
00086 
00087     // Set carryinq frequency for data acquisition
00088           ret = m_pIMonCom->setCarryingWaveFrequency(carryingFrequency);
00089 
00090     // Create a one dimensional SafeArray of integers
00091     SAFEARRAY *pSA;
00092     SAFEARRAYBOUND aDim[1];
00093     aDim[0].lLbound = 0;
00094     aDim[0].cElements = 6;
00095     pSA = SafeArrayCreate(VT_I4, 1, aDim);
00096 
00097     // Scan all the sensors connected to the TUsb module
00098     long nbDetectedTSens = m_pIMonCom->scan(&pSA);
00099     MMechostr(MSKDEBUG, "%d T-Sens detected\n", nbDetectedTSens);
00100         
00101     // Set up real-time data recording
00102     ret = m_pIMonCom->recConfig(1, 0, "");
00103     if (ret < 0)
00104     {
00105             // Error launching data recording
00106             MMechostr(MSKDEBUG, "TUSB didn't manage to configure the paramater for the record... Press RETURN to exit\n");
00107             return false;
00108     }
00109     MMechostr(MSKDEBUG, "Record is ready to start... \n");
00110 
00111     // Retrieve battery level
00112     SetBatteryLevel(m_pIMonCom->getBattery(1));
00113     PostMessage(HScol, TUSB_BATTERY_LEVEL_CB, (int)this, (LPARAM)(GetBatteryLevel()));
00114 
00115     // Start data acquisition
00116     ret = m_pIMonCom->startRec();
00117     if (ret < 0)                
00118     {
00119             // Error launching data recording
00120             MMechostr(MSKDEBUG, "TUSB didn't manage to launch record... Press RETURN to exit\n");
00121             return false;
00122     }
00123     MMechostr(MSKDEBUG, "Record has started!\n");
00124 
00125     // Send notification to Scol handle
00126     m_isConnected = true;
00127     PostMessage(HScol, TUSB_CONNECTED_CB, (int)this, (LPARAM)NULL);
00128     return true;
00129         }
00130         else
00131         {
00132                 // Connection failed
00133                 MMechostr(MSKDEBUG, "Connexion attempt failed! (error %d)\n", ret);
00134                 return false;
00135         }
00136 }
00137 
00138 bool RespirationBelt::Disconnect()
00139 {
00140   // Stop data acquisition
00141   int ret = m_pIMonCom->stopRec();
00142         if (ret < 0)
00143         {
00144                 MMechostr(MSKDEBUG, "TUSB failed to stop recording...\n");
00145     return false;
00146         }
00147         MMechostr(MSKDEBUG, "Record has been stopped.\n");
00148 
00149   // Close COM port
00150   ret = m_pIMonCom->closePortCom();
00151   if (ret == -1)
00152   {
00153     MMechostr(MSKDEBUG, "Failed to close COM port...\n");
00154     return false;
00155   }
00156         MMechostr(MSKDEBUG, "COM port has been successfully closed.\n");
00157   m_isConnected = false;
00158 
00159   // Send notification to Scol handle
00160   PostMessage(HScol, TUSB_DISCONNECTED_CB, (int)this, (LPARAM)NULL);
00161 
00162   return true;
00163 }
00164 
00168 void RespirationBelt::ParseData()
00169 {
00170   // Check if respiration belt is still connected
00171   if (m_pIMonCom->IsTSensConnected(1))
00172   {
00173     // Check if new data is available for TUsb module
00174     int nbData = m_pIMonCom->isDataRTAvailable(1);
00175     if (nbData > 0)
00176     {
00177       // Create 1D one and two dimensional arrays for data acquisition
00178       SAFEARRAY *TStime, *TSdata;
00179       SAFEARRAYBOUND timeDim[1], dataDim[2];
00180       timeDim[0].lLbound = dataDim[0].lLbound = dataDim[1].lLbound = 0;
00181       timeDim[0].cElements = dataDim[0].cElements = dataDim[1].cElements = nbData;
00182       TStime = SafeArrayCreate(VT_R8, 1, timeDim);
00183       TSdata = SafeArrayCreate(VT_R8, 2, dataDim);
00184 
00185       // Get new TUsb module data, including time stamp
00186       m_pIMonCom->getDataRT(1, &TSdata, &TStime, nbData);
00187       double beltExtension = 0.0f;
00188       long ix[2];
00189 
00190       // Create a data structure to retrieve new values of belt extension
00191       BeltData *beltData = new BeltData();
00192       for (int j=0; j<=nbData-1; j++)
00193       {
00194         ix[0] = 0;
00195         ix[1] = j;
00196 
00197         // Get current belt extension value
00198         SafeArrayGetElement(TSdata, ix, (void*)&beltExtension);
00199 
00200         // Add new data to the list
00201         beltData->AddData(static_cast<float>(beltExtension));
00202       }
00203 
00204       // Send message to Scol handle
00205       PostMessage(HScol, TUSB_NEW_DATA_CB, (int)this, (LPARAM)beltData);
00206     }
00207   }
00208 }
00209 
00210 
00214 void RespirationBelt::GoThread()
00215 {
00216   while(!exitRequested)
00217   {
00218     if (!m_isConnected)
00219     {
00220       // Enumerate the list of available COM ports
00221       std::list<unsigned int> lport = LSerie::enumAvailablePorts();
00222       std::list<unsigned int>::iterator it = lport.begin();
00223       while (!comState && (it != lport.end()))
00224       {
00225         // Establish a safe COM connection
00226         if (Connect(*it, CARRYING_FREQ))    // NOTE: The carrying frequency mustn't be changed, it is specific to the TUsb device
00227           comState = true;
00228         it++;
00229       }
00230     }
00231     else
00232     {
00233       // Lock during operation
00234       boost::mutex::scoped_lock lockNewData(mutexNewData);
00235 
00236       // Retrieve new data
00237       ParseData();
00238     }
00239   }
00240 }