Page 1 of 1

Help compiling C++ sample file?

Posted: Tue Jul 29, 2008 10:21 am
by preet
I downloaded the C++ Win32 command line VC8/2005 sample file, opened it up in VC and tried to compile it. But I get an error:

Cannot open include file: 'atlbase.h'

It appears I don't have one of the required header files... any ideas as to how I can remedy this issue?

Re: Help compiling C++ sample file?

Posted: Tue Jul 29, 2008 1:22 pm
by Birch
Are you trying to open the project and compile using the standard Visual Studio 2005 install? I'm not sure whether express will work or not.

Re: Help compiling C++ sample file?

Posted: Fri Aug 01, 2008 11:05 am
by preet
You were right, it needed VC Studio Standard.
Since I have a SmartNAV camera, the command line sample doesn't really help me out at all.

The win32 gui app worked fine though. I'm still having trouble writing my own sample application...

I can't seem to get any of the sample code on this forum to work after compiling, without getting a "This program has performed an illegal op and must close" or having the camera just return null frames.

As a starting point I wanted to just get some feedback from the camera as in, at least display that it's detecting something. Here's the chopped up version of the sample file that I'm trying to use.

Code: Select all

//========================================================================-----
//== OptiTrack WIN32 Sample Application
//== Copyright 20067 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"

//== SAMPLE APPLICATION ENTRY POINT ======================================-----

int _tmain(int argc, _TCHAR* argv[])
{
	printf("OptiTrack WIN32 Sample Application ============================================\n");
	printf("Copyright 2007 NaturalPoint =============================================------\n\n");

	//== Initialize Microsoft COM Interop ================----
	CoInitialize(NULL);

	//== Initialize OptiTrack COM Component ==============----
	CComPtr<INPCameraCollection> cameraCollection;
    CComPtr<INPCamera>           camera;
	CComPtr<INPCameraFrame>		 frame;

    cameraCollection.CoCreateInstance(CLSID_NPCameraCollection);

	//== Enumerate (Identify) Available Cameras ==========----
	cameraCollection->Enum();

	long cameraCount  = 0;
	int  frameCounter = 0;
	VARIANT vOption;
	vOption.boolVal = VARIANT_TRUE;

	//== Determine Available Cameras =====================----
    cameraCollection->get_Count(&cameraCount);

	printf("%d Camera(s) Detected:\n\n", cameraCount);
	
	//== Display Camera Information for All Cameras ======----
	for(int index=0; index<cameraCount; index++)
	{
		cameraCollection->Item(index, &camera);

		long serial,width,height,model,revision,rate;

		camera->get_SerialNumber(&serial);
		camera->get_Width       (&width);
		camera->get_Height      (&height);
		camera->get_Model       (&model);
		camera->get_Revision    (&revision);
		camera->get_FrameRate   (&rate);

		printf("  Camera %d\n",serial);
		printf("  =========================\n");
		printf("  Resolution: %dx%d\n",width,height);
		printf("  Revision  : 0x%8x\n",revision);
		printf("  Model     : 0x%8x\n",model);
		printf("  Frame rate: %d\n\n" ,rate);

		//== Always Clean-up COM Objects ================----
		camera.Release();
	}

	//== Open the first camera ==========================----
	
	if(cameraCount>0)
	{

		printf("\nPress Return To Exit\n\n");

		cameraCollection->Item(0, &camera);
		
			camera->Open();
			camera->SetOption(NP_OPTION_STATUS_GREEN_ON_TRACKING, vOption);


			camera->Start();

			    while(!_kbhit())
			    {
				    Sleep(50);

					
	                MSG msg;
                    if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
                    {
                        if(GetMessage( &msg, NULL, 0, 0 ) )
                        {
                            TranslateMessage(&msg);
                            DispatchMessage(&msg);
                        }
                    }
					
					camera->GetFrame(5, &frame);

					if (frame != NULL)
					{
						printf("*");
					}

					else
					{
						printf("0");
					}

						//frame->Free();
						//frame.Release();
			    }			
            
			camera->Stop();
			camera->Close();
		

		camera.Release();
	}


	//== Always Clean-up COM Objects ================----
	cameraCollection.Release();

	//== Uninitialize Microsoft COM Interop =============----
	CoUninitialize();

	printf("Application Exit.\n");
	getchar();
	return 0;
}


Would appreciate any help... TiA

-Preet

Re: Help compiling C++ sample file?

Posted: Sat Aug 02, 2008 12:10 am
by Birch
The frame needs to have Free and Release called if it came back as valid. After making that change I was able to run your code without getting a crash on exit.

btw, SmartNav 4s do have the ability to run in grayscale mode. you'd need to slow the frame rate down or select a reduced spatial decimation setting to get it to work though.

Code: Select all

if (frame != NULL)
{
  printf("*");

  frame->Free();
  frame.Release();
}
else
{
  printf("0");
}

Re: Help compiling C++ sample file?

Posted: Tue Aug 05, 2008 6:43 am
by preet
Hey, thanks for the quick reply. After adding the frame->Free() and frame.Release() lines, the program doesn't crash any more. But I never receive a frame that isn't null.

I'm not really sure on this, but if I wave reflective tape (the little stickers that came with the camera, for example) in front of the camera, I should be receiving valid frames right?

edit!: Disconnecting and reconnecting the USB cable solved this problem!

Thank you for all the help!

Re: Help compiling C++ sample file?

Posted: Tue Aug 05, 2008 12:55 pm
by Birch
Glad you got it working.

You might want to explicitly set the video mode at the beginning of your program in case the camera is in a different mode than you expect when its started up.

Re: Help compiling C++ sample file?

Posted: Fri Aug 15, 2008 12:20 pm
by preet
Hey,

I've run into another small problem. I can't seem to exit the program without it crashing on me. I think the last few lines are the culprit... maybe I'm forgetting to uninitialize something?

I also have a quick question... how do I use functions in my program when I need to refer to CComPtr objects (like a frame) within the function? Visual Studio always gives me the error that those corresponding objects are undefined. I just make the CComPtr declerations outside of my main to make them global, but I'm not sure that's the right way to do it.

Thanks,
-Preet

Code: Select all

//	____________________________________________________________________
//
//	OUTPUT_XY
//	X,Y Coordinate Output for a Single Object using the SmartNAV Camera
//	____________________________________________________________________

//	____________________________________________________________________
//	Standard Includes and OptiTrack API Includes

#include "stdafx.h"
#include <tchar.h>
#include <conio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string>

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

using namespace std;

struct vDat
{
	double yaw;
	double pitch;
	double roll;
	double pointData [3][3];
};

void updateVectorInfo(INPCameraFrame *frame, struct vDat &vectorData);

	CComPtr<INPCameraCollection>	cameraCollection;
    CComPtr<INPCamera>				camera;
	CComPtr<INPCameraFrame>			frame;
	CComPtr<INPVector2>				pVector;
	CComPtr<INPPoint>				pPoint;
	


//	____________________________________________________________________
//	Start program

int _tmain(int argc, _TCHAR* argv[])
{
	printf("X,Y Coordinate Output for a Single Object (SmartNAV Camera)\n\n");

//	________________________________________________
//	Start MS COM Library on thread
	CoInitialize(NULL);

//	________________________________________________
//	Initialize COM Objects for Camera, Frame, etc


    cameraCollection.CoCreateInstance(CLSID_NPCameraCollection);
	pVector.CoCreateInstance(CLSID_NPVector);
	pPoint.CoCreateInstance(CLSID_NPPoint);


//	________________________________________________
//	Enumerate all cameras on the system
	cameraCollection->Enum();

	long cameraCount  = 0;
	int  frameCounter = 0;

	VARIANT vOptionTrue;
	vOptionTrue.boolVal = VARIANT_TRUE;

	VARIANT vOptionFalse;
	vOptionFalse.boolVal = VARIANT_FALSE;

//	________________________________________________
//	Return number of cameras available
    cameraCollection->get_Count(&cameraCount);

	printf("%d Camera(s) Detected:\n\n", cameraCount);
	
//	________________________________________________
//	Show camera information (assumed only 1 camera)
		
		int index = 0;
		cameraCollection->Item(index, &camera);

		long serial,width,height,model,revision,rate;

		camera->get_SerialNumber(&serial);
		camera->get_Width       (&width);
		camera->get_Height      (&height);
		camera->get_Model       (&model);
		camera->get_Revision    (&revision);
		camera->get_FrameRate   (&rate);

		printf("  Camera %d\n",serial);
		printf("  =========================\n");
		printf("  Resolution: %dx%d\n",width,height);
		printf("  Revision  : 0x%8x\n",revision);
		printf("  Model     : 0x%8x\n",model);
		printf("  Frame rate: %d\n\n" ,rate);

//	________________________________________________
//	Release COM Object
		camera.Release();

//	________________________________________________
//	Prompt user to start aquisition
		printf("\n\n Press any key to start aquisition");
		printf("\n Press any key once aquisition has started to exit.\n\n");
		_getch();

//	________________________________________________
//	Open/Start Camera
	if(cameraCount == 1)
	{
//		________________________________________________
//		Assign the first camera to the 'camera' COM object
		cameraCollection->Item(0, &camera);
		
		camera->Open();
		camera->SetOption(NP_OPTION_STATUS_LED_ON_START, vOptionFalse);
		camera->SetOption(NP_OPTION_STATUS_GREEN_ON_TRACKING, vOptionTrue);

		camera->Start();
//		________________________________________________
//		(Press any key to terminate program)
	while(!_kbhit())
	{
				    Sleep(50);				//Delay between frame grabs

					
	                MSG msg;
                    if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
                    {
                        if(GetMessage( &msg, NULL, 0, 0 ) )
                        {
                            TranslateMessage(&msg);
                            DispatchMessage(&msg);
                        }
                    }
					
//		________________________________________________
//		Get frame from camera


					camera->GetFrame(INFINITE, &frame);
					vDat vectorData;
					int written = 0;

					if (frame != NULL)
					{
						//Check if there is ONE object detected by camera
						LONG oCount = 0;
						frame->get_Count(&oCount);

						if (oCount < 3)
						{
							
							cout << "\rToo few objects to create vector";

							//Print out the coordinates

							written = 1;
						}

						else if (oCount == 3)
						{
	
							updateVectorInfo(frame, vectorData);
							cout << "\r3 objects in frame";
							//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);
							written = 1;
						}

						//Frames must be freed after processing ASAP
						frame->Free();
						frame.Release();
					}

					else
					{
						printf("\rToo many objects");
						written = 1;
					}

			    }	//ends while (!kbhit())			
//		________________________________________________
//		Stop camera
			camera->Stop();
			camera->Close();
		
		camera.Release();
	}


//	________________________________________________
//	Release COM Objects
	cameraCollection.Release();

//	________________________________________________
//	Uninitialize COM
	CoUninitialize();

	printf("\n\nApplication Exit.\n");
	return 0;
}


void updateVectorInfo(INPCameraFrame * frame, struct vDat &vectorData)
{
   CComVariant varYaw, varPitch, varRoll, varX, varY, varZ, varEmpty;

   // Process the vector data
   pVector->Update(camera, frame);

   pVector->get_Yaw(&varYaw);
   pVector->get_Pitch(&varPitch);
   pVector->get_Roll(&varRoll);

   // Assign vector data to struct
   vectorData.yaw = varYaw.dblVal;
   vectorData.pitch = varPitch.dblVal;
   vectorData.roll = varRoll.dblVal;


   // Get Point number 0 on the clip
   pVector->GetPoint(0, &pPoint);
   pPoint->get_X(&varX);
   pPoint->get_Y(&varY);
   pPoint->get_Z(&varZ);
   vectorData.pointData[0][0] = varX.dblVal;
   vectorData.pointData[0][1] = varY.dblVal;
   vectorData.pointData[0][2] = varZ.dblVal;

   pPoint.Release();

   // Get Point number 1 on the clip
   pVector->GetPoint(1, &pPoint);
   pPoint->get_X(&varX);
   pPoint->get_Y(&varY);
   pPoint->get_Z(&varZ);
   vectorData.pointData[1][0] = varX.dblVal;
   vectorData.pointData[1][1] = varY.dblVal;
   vectorData.pointData[1][2] = varZ.dblVal;

   pPoint.Release();

   // Get Point number 2 on the clip
   pVector->GetPoint(1, &pPoint);
   pPoint->get_X(&varX);
   pPoint->get_Y(&varY);
   pPoint->get_Z(&varZ);
   vectorData.pointData[2][0] = varX.dblVal;
   vectorData.pointData[2][1] = varY.dblVal;
   vectorData.pointData[2][2] = varZ.dblVal;

   pPoint.Release();

}