Controlling Motion-Tracking Devices
by Mike Harrington


Example 1:

O1  30.12   0.23  11.23  10.01  30.01  30.02<cr><lf>
O1  31.24   0.27  12.01  10.34  30.15  30.12<cr><lf>
O1  32.36   0.21  12.44  10.44  30.16  30.13<cr><lf>
O1  33.48   0.23  13.11  10.54  30.14  30.16<cr><lf>
O1  34.58   0.22  13.13  10.54  30.13  30.17<cr><lf>
O1  35.79   0.29  13.15  10.56  30.11  30.18<cr><lf>



Listing One
/***********************************************************************
*    Description:  Sample Win95 rs232 tracker interface code
*    Author:       Yury Altshuler InterSense Inc.
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <process.h>
#include <winbase.h>
#include <time.h>

BOOL rs232InitCommunications(HANDLE *portHandle, UINT32 comPort,
                                                       UINT32 baudRate);
BOOL rs232DeinitCommunications( HANDLE *portHandle);

//********************** rs232InitCommunications *******************
BOOL rs232InitCommunications(HANDLE *portHandle, UINT32 comPort, 
                                                         UINT32 baudRate)
{
    COMMTIMEOUTS timeout;
    DCB dcb;
    const char openString[100]="9600,N,8,1";
    rs232DeinitCommunications(portHandle); // close port if already open
    FillMemory(&dcb, sizeof(dcb), 0);          //now open the serial port
    *portHandle=CreateFile((comPort == 1 ? "COM1" : (comPort == 2 ? "COM2" :
                            (comPort == 3 ? "COM3" : "COM4"))),
                            GENERIC_READ | GENERIC_WRITE,
                            0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
    if(*portHandle == INVALID_HANDLE_VALUE)
    {
        printf( "Failed to open communications port");
        return FALSE;
    }
// Set the timeouts for nonblocking reads and blocking writes
    if (!GetCommTimeouts(*portHandle,&timeout))
    {
        printf( "Could not get timeout info");
        return FALSE;
    }
    timeout.ReadIntervalTimeout = MAXDWORD;
    timeout.ReadTotalTimeoutMultiplier = 0;
    timeout.ReadTotalTimeoutConstant = 0;
    timeout.WriteTotalTimeoutMultiplier = 10;
    timeout.WriteTotalTimeoutConstant = 100;

    if(!SetCommTimeouts(*portHandle, &timeout))

    {
        printf( "Could not set timeout info");
        return FALSE;
    }
        // now set up comm port with baud rate and other port parameters
    FillMemory(&dcb, sizeof(dcb), 0);
    dcb.DCBlength = sizeof(dcb);

    if (!GetCommState(*portHandle, &dcb))
    {
        printf( "Failed to get communications state");
        return FALSE;
    }
    BuildCommDCB(openString, &dcb);
    dcb.DCBlength = sizeof(dcb);
    dcb.BaudRate = baudRate;    // use the passed baud rate
    dcb.fNull = FALSE;
    dcb.fBinary = TRUE;
    dcb.fAbortOnError = FALSE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
    dcb.fOutxCtsFlow = FALSE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fRtsControl = RTS_CONTROL_DISABLE;

    if (!SetCommState(*portHandle, &dcb))
    {
        printf( "Failed to set communications state");
        return FALSE;
    }
    return TRUE;
}
//******************** rs232DeinitCommunications *********************
BOOL rs232DeinitCommunications(HANDLE *portHandle)
{
    if(*portHandle != NULL)
    {
        CloseHandle(*portHandle);
        *portHandle = NULL;
    }
    return TRUE;
}


Listing Two
itSendCommand(portHandle,"O1,2,4,1\r\n");// setup output record format
                                         // station 1 for Eulers and Pos
itSendCommand(portHandle,"f");           // binary mode
itSendCommand(portHandle,"U");           // Set position to inches
itSendCommand(portHandle,"l1,1\r\n ");   // Turn on station 1
itSendCommand(portHandle,"l2,0\r\n ");   // Turn off station 2 
itSendCommand(portHandle,"l3,0\r\n ");   // Turn off station 3 
itSendCommand(portHandle,"l4,0\r\n ");   // Turn off station 4
itSendCommand(portHandle,"C");           // Put the system in continuous mode
                                         //     to start sending data

Listing Three
void  Render(void)
{
RECT    rect;
DDBLTFX bltFx;
    updateTracker();    // read in the most current tracker data record
                        // use most current record to update users viewpoint
updateOrientation(Orientation[0][0],Orientation[0][1], Orientation[0][2]);
updatePosition(Position[0][0],Position[0][1],Position[0][2]);
    rect.left   = 0;    // Clear the buffer we are about to render to
    rect.top    = 0;
    rect.right  = 640;
    rect.bottom = 480;
 
    ZeroMemory(&bltFx,sizeof(DDBLTFX));
    bltFx.dwSize = sizeof(DDBLTFX);
    bltFx.dwFillColor = D3DRGB(1.0,1.0,1.0); // White background
    backBuffer->Blt(NULL,NULL,NULL,DDBLT_COLORFILL | DDBLT_WAIT ,&bltFx);

    // Update the direct3D Retained Mode scene
    scene->Move(D3DVALUE(1.0));
    view->Clear();
    view->Render(scene);
    dev->Update();
    
// Update primary surface by doing a flip when vertical sync comes along
    while(frontBuffer->Flip( NULL,0 ) == DDERR_WASSTILLDRAWING);
}



3


