Modified win32 sample hangs

beckdo
Posts: 520
Joined: Tue Jan 02, 2007 2:02 pm

Re: Modified win32 sample hangs

Post by beckdo »

Actually, in your code you need to make sure not to call NPVector->Update with a NULL camera frame (pFrame==NULL)

Bracket the frame processing with a check like this...

Code: Select all

if(pFrame!=NULL)
{
    hr = pVector->Update(pCamera, pFrame);

    ...your processing code here...

    hr = pFrame->Release();
}

Once I added that, your code started running just fine.
niallm
Posts: 23
Joined: Tue Feb 22, 2005 5:00 am
Location: London Ontario Canada

Re: Modified win32 sample hangs

Post by niallm »

Hi,

I found with the new build of the SDK specifying a timeout value other than zero crashes the program.

Niall
dlecoutre
Posts: 6
Joined: Wed Apr 18, 2007 3:23 pm

Re: Modified win32 sample hangs

Post by dlecoutre »

The problem is fix this is the new code. Thanks very much to the support team. If it doesn't work, you should ask for the SDK beta.

Code: Select all

//========================================================================-----
//== OptiTrack WIN32 Sample Application
//== Copyright 2007 NaturalPoint
//== 
//== This sample is intended to show how to properly access and control
//== OptiTrack cameras.  The application does the following:
//==
//== 1. Initializes the OptiTrack COM Component.
//== 2. Displays information about all connected cameras.
//== 3. Starts the first camera in the system and displays ongoing info.
//== 4. Unintializes the OptiTrack cameras, COM, and terminates.

//== INCLUDES ============================================================-----

#include "stdafx.h"
#include <conio.h>    //== For sample simplicity: kbhit()

//== NECESSARY OPTITRACK INCLUDES AND DEFINITIONS ========================-----

#include <objbase.h>
#include <atlbase.h>
#include "optitrack.h"
#import  "optitrack.tlb"


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

double ncompteur;

#define HRCHECK(message) if (FAILED(hr)) { fprintf(stderr, "%s\n", message); return 0; }
//== SAMPLE APPLICATION ENTRY POINT ======================================-----

int main(int argc, char* argv[])
{
	HRESULT hr;
	LONG count;
	LONG misc;
	LONG waittime;
	ncompteur=1;
	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 = 0; // value in milliseconds; 0 = don't wait; INFINITE = wait till data
	//waittime = INFINITE;

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

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

    //  while(running--) {
    while(1) 
    {
        Sleep(5);
        pFrame=NULL;
        hr = pCamera->GetFrame(waittime, &pFrame);


        if (FAILED(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;
            } //switch
        }


        if(pFrame!=NULL)
        {
            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 c=%.1f  \r",
            vx.dblVal, vy.dblVal, vz.dblVal, vyaw.dblVal, vpitch.dblVal, vroll.dblVal,ncompteur);
            ncompteur=ncompteur+1;
            hr = pFrame->Release();
        }
    }

  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;
}  
  
[color:"red"] [/color] [color:"red"] [/color]
beckdo
Posts: 520
Joined: Tue Jan 02, 2007 2:02 pm

Re: Modified win32 sample hangs

Post by beckdo »

Another way to solve the 'hang' problem without needing the Beta SDK is to put this code in your main loop:

Code: Select all

        MSG msg;
        while( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
        {
            if(GetMessage( &msg, NULL, 0, 0 ) )
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

Since it's a console application, there is nothing pumping messages and the message queue for the thread overflows.
Post Reply