Minimal necessary C++ code for using device.

Post Reply
kozcan
Posts: 2
Joined: Wed Jan 25, 2006 5:00 am

Minimal necessary C++ code for using device.

Post by kozcan » Thu Jan 26, 2006 4:18 am

Hi guys;

First of all, i apologize for my english.

I'm using OptiTrack SDK (1.0.028) with TIR3PRO for a while. The VB.NET sample is very clear and easy-to-use. I can do eveything with it. But it is hard to say same things for the VC.NET sample. Header files, References, Resources, AFX declerations(i don't know what it is) etc. they are all mixed. I couldn't determine which code lines and header files are vital to communicate with the device. I want to integrate OptiTrack( or TrackIR ) into a game engine and i need a base reference code to communicate with the device(i don't need a GUI and for simplicity i prefer a console application). Can anyone provide a real simple vc6 or vc.net sample code? Thanks in advance.

Anonemous
Posts: 6
Joined: Thu Jan 26, 2006 5:00 am
Location: USA

Re: Minimal necessary C++ code for using device.

Post by Anonemous » Thu Jan 26, 2006 11:25 am

Here's some rough code that I used:

code:#include "stdafx.h"

#include "optitrack.h"

INPCameraCollection *pCameraCollection;
INPCamera *pCamera;
INPCameraFrame *pFrame;
INPVector *pVector;

#define HRCHECK(message) if (FAILED(hr)) { fprintf(stderr, "%s\n", message); return 0; }

int main(int argc, char* argv[])
{
HRESULT hr;
LONG count;
LONG misc;
LONG waittime;

VARIANT vx, vy, vz;
VARIANT vyaw, vpitch, vroll;
VARIANT voption;

int running = 1;
int toerrors = 0;

printf("Hello World!\n");

// probably not needed, but doesn't hurt:
VariantInit(&vx);
VariantInit(&vy);
VariantInit(&vz);
VariantInit(&vyaw);
VariantInit(&vpitch);
VariantInit(&vroll);
VariantInit(&voption);

CoInitialize(NULL); // Startup Microsoft crap

// create CameraCollection and Vector objects:

hr = CoCreateInstance(__uuidof(NPCameraCollection), NULL, CLSCTX_ALL,
__uuidof(INPCameraCollection), (void **) &pCameraCollection);
HRCHECK("CoCreateInstance for CameraCollection");

hr = CoCreateInstance(__uuidof(NPVector), NULL, CLSCTX_ALL,
__uuidof(INPVector), (void **) &pVector);
HRCHECK("CoCreateInstance for Vector");

// find all the cameras:
hr = pCameraCollection->Enum();
HRCHECK("CameraCollection->Enum");

hr = pCameraCollection->get_Count(&count);
HRCHECK("CameraCollection->get_Count");

if (count < 1) {
fprintf(stderr, "camera count < 1\n");
return 0;
}

// get one:
hr = pCameraCollection->Item(0, &pCamera);
HRCHECK("CameraCollection->Item");

hr = pCamera->get_SerialNumber(&misc);
HRCHECK("Camera->get_SerialNumber");

printf("serial number = %ld\n", misc);

hr = pCamera->Open();
HRCHECK("Camera->Open");

// turn on illumination LEDs
hr = pCamera->SetLED(NP_LED_ONE, VARIANT_TRUE);
HRCHECK("Camera->SetLED");

#if 0
voption.vt = VT_BOOL;
voption.boolVal = VARIANT_TRUE;
hr = pCamera->SetOption(NP_OPTION_SEND_EMPTY_FRAMES, voption);
HRCHECK("Camera->SetOption");
#endif

hr = pCamera->Start();
HRCHECK("Camera->Start");

waittime = 10; // value in milliseconds; 0 = don't wait; INFINITE = wait till data

pVector->Reset();
HRCHECK("Vector->Reset");

running = 6000; // run ~1 minute for waittime=10 ms

while(running--) {

hr = pCamera->GetFrame(waittime, &pFrame);

if (FAILED(hr)) {
// fprintf(stderr,"Camera->GetFrame %x ", hr);
switch(hr) {
case S_OK:
fprintf(stderr, "OK\n"); break;
case E_POINTER:
fprintf(stderr, "E_POINTER\n"); break;
case NP_E_DEVICE_DISCONNECTED:
fprintf(stderr, "NP_E_DEVICE_DISCONNECTED\n"); break;
case NP_E_DEVICE_NOT_SUPPORTED:
fprintf(stderr, "NP_E_DEVICE_NOT_SUPPORTED\n"); break;
case HRESULT_FROM_WIN32(ERROR_TIMEOUT):
/*fprintf(stderr, "HRESULT_FROM_WIN32(ERROR_TIMEOUT)\n");*/
toerrors++;
break;
default:
fprintf(stderr, "default\n"); break;
}
continue;
}

hr = pVector->Update(pCamera, pFrame);
HRCHECK("Vector->Update");

hr = pVector->get_X(&vx);
HRCHECK("Vector->get_X");

hr = pVector->get_Y(&vy);
HRCHECK("Vector->get_Y");

hr = pVector->get_Z(&vz);
HRCHECK("Vector->get_Z");

hr = pVector->get_Yaw(&vyaw);
HRCHECK("Vector->get_Yaw");

hr = pVector->get_Pitch(&vpitch);
HRCHECK("Vector->get_Pitch");

hr = pVector->get_Roll(&vroll);
HRCHECK("Vector->get_Roll");

printf("x=%.3f y=%.3f z=%.3f yaw=%.3f pitch=%.3f roll=%.3f \r",
vx.dblVal, vy.dblVal, vz.dblVal, vyaw.dblVal, vpitch.dblVal, vroll.dblVal);

hr = pFrame->Free();

}

printf("\n\n%d timeouts\n", toerrors);

printf("\nFin\n");

// shut everything down
pCamera->SetLED(NP_LED_ONE, VARIANT_FALSE);
pCamera->Stop();
pCamera->Close();

CoUninitialize();

return 0;
}
[/code]

kozcan
Posts: 2
Joined: Wed Jan 25, 2006 5:00 am

Re: Minimal necessary C++ code for using device.

Post by kozcan » Fri Jan 27, 2006 2:33 am

Thank you very much Anonemous. You are great !!! Everything is clear now.

marie
Posts: 4
Joined: Tue Dec 05, 2006 1:52 pm

Re: Minimal necessary C++ code for using device.

Post by marie » Wed Dec 06, 2006 1:03 pm

Hi, there.

Thanks for your generous sharing. I am a newbie of OptiTrack and COM. I tried to run your basic code in VC6 environment on WinXP platform, but in less than 30 seconds, it will report an error "OptiTrackTest.exe encountered a problem and needs close" and halt.

Could anyone kindly help me out?
Thanks a lot!

ymaksyuta
Posts: 1
Joined: Thu Dec 21, 2006 8:05 am
Location: Russia

Re: Minimal necessary C++ code for using device.

Post by ymaksyuta » Fri Dec 22, 2006 6:00 am

minimal code using connection points.
but infinite message loop!

Code: Select all

#include <windows.h>
#include <atlbase.h>
#include <atlcom.h>
#include <conio.h>

#import  "optitrack.tlb"
#include "optitrack.h"

#define CALL( x ) _cprintf("%s: "#x"\n", ((x)==S_OK) ? "passed" : "FAILED");

// --- ATL initialization ---
class COptiTrackClientModule : public CAtlExeModuleT< COptiTrackClientModule >
{
public:
   HRESULT PreMessageLoop (int) throw( );
   HRESULT PostMessageLoop(   ) throw( );
   static HRESULT InitializeCom  () throw() { return CoInitialize(NULL);     }
   static HRESULT UnInitializeCom() throw() { CoUninitialize(); return S_OK; }
};
COptiTrackClientModule theModule;

extern "C" int WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int nShowCmd)
{
    return theModule.WinMain(nShowCmd);
}
// --- ATL initialization ---

// --- connection points definitions ---
class CameraCollectionEvents :
    public IDispEventImpl<0, CameraCollectionEvents, &DIID__INPCameraCollectionEvents, &LIBID_OptiTrack, 1, 0>
{
public:
   STDMETHOD_(void, OnDeviceRemoval)(INPCamera * pCamera) { HandleDeviceRemoval(pCamera); }
   STDMETHOD_(void, OnDeviceArrival)(INPCamera * pCamera) { HandleDeviceArrival(pCamera); }
   void HandleDeviceRemoval(INPCamera * pCamera) {_cprintf("device removed\n");}
   void HandleDeviceArrival(INPCamera * pCamera) {_cprintf("device arrived\n");}
   BEGIN_SINK_MAP(CameraCollectionEvents)
      SINK_ENTRY_EX(0, DIID__INPCameraCollectionEvents, 1, OnDeviceRemoval)
      SINK_ENTRY_EX(0, DIID__INPCameraCollectionEvents, 2, OnDeviceArrival)
   END_SINK_MAP()
};

class CameraEvents :
    public IDispEventImpl<0, CameraEvents, &DIID__INPCameraEvents, &LIBID_OptiTrack, 1, 0>
{
public:
   STDMETHOD_(void, OnFrameAvailable)(INPCamera * pCamera) { HandleFrameAvailable(pCamera); }
   STDMETHOD_(void, OnSwitchChange)(INPCamera * pCamera, LONG lNewSwitchState) { HandleSwitchChange(pCamera, lNewSwitchState); }
   void HandleFrameAvailable(INPCamera * pCamera);
   void HandleSwitchChange(INPCamera * pCamera, LONG lNewSwitchState) {_cprintf("switch changed\n");}
   BEGIN_SINK_MAP(CameraEvents)
      SINK_ENTRY_EX(0, DIID__INPCameraEvents, 1, OnFrameAvailable)
      SINK_ENTRY_EX(0, DIID__INPCameraEvents, 2, OnSwitchChange)
   END_SINK_MAP()
};
// --- connection points definitions ---

// --- global variables ---
CComPtr<INPCameraCollection> collection;
CameraCollectionEvents       collectionEvents;
CComPtr<INPCamera>           camera;
CameraEvents                 cameraEvents;
CComPtr<INPVector2>          vector;
// --- global variables ---

HRESULT COptiTrackClientModule::PreMessageLoop(int)
{
   AllocConsole();

   CALL( vector.CoCreateInstance(CLSID_NPVector) );

   CALL( collection.CoCreateInstance(CLSID_NPCameraCollection) );
   CALL( collectionEvents.DispEventAdvise(collection) );
   CALL( collection->Enum() );

   CALL( collection->Item(0, &camera) );
   CALL( cameraEvents.DispEventAdvise(camera) );
   CALL( camera->Open()  );
   CALL( camera->Start() );
   return S_OK;
}

HRESULT COptiTrackClientModule::PostMessageLoop()
{
   CALL( camera->Stop()  );
   CALL( camera->Close() );
   CALL( cameraEvents.DispEventUnadvise(camera) );
   CALL( collectionEvents.DispEventUnadvise(collection) );

   collection.Release();

   vector.Release();

   FreeConsole();
   return S_OK;
}

void CameraEvents::HandleFrameAvailable(INPCamera * pCamera)
{
   CComPtr<INPCameraFrame> frame;
   pCamera->GetFrame(0,&frame);
   vector->Update( pCamera, frame );
   frame->Free();
   VARIANT value;
   double x,y,z;
   vector->get_X(&value); x = value.dblVal;
   vector->get_Y(&value); y = value.dblVal;
   vector->get_Z(&value); z = value.dblVal;
   _cprintf("frame available: (%f,%f,%f)\n", x,y,z);
}

Queeny
Posts: 26
Joined: Tue Jun 03, 2008 10:06 am

Re: Minimal necessary C++ code for using device.

Post by Queeny » Tue Jun 03, 2008 10:55 am

Hello,

So what is the minimal C++ code for using the device running on the most latest version??

If you do provide some code, could you please insert some comments or explanations.

Thanks!
Q.

Post Reply