Trouble getting tracking data

115 views
Skip to first unread message

Philip Degarmo

unread,
Aug 23, 2012, 5:25:33 PM8/23/12
to libfre...@googlegroups.com
I'm having trouble using the freespace library to interface with the USB2 motion sensor. I am using sample code (gameloop_example2.c) and am able to get the code to act differently based on if the sensor is present or not (the sample code prints an error when no devices are detected.) I also have the callback getting hit when the sensor is unplugged. So I know the library is working to at least some degree, and I know my callback is registered. This is again the stock example code except that I removed the appControlHandler calls.

I'm working in VS2008, and I have version 0.6 of the SDK. I'm hoping there is a stupidly simple reason why this isn't working. Full source listed below but again, it's pretty much a copy of gameloop_example2.c.

Thanks
Philip

----

#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>

#ifdef WIN32
#include <windows.h>
#include <stdio.h>
#else
#include <unistd.h>
#endif


#include <freespace/freespace.h>

struct freespace_BodyFrame cachedBodyFrame;

static void receiveMessageCallback(FreespaceDeviceId id,
                            struct freespace_message* message,
                            void* cookie,
                            int result) {
    if (result == FREESPACE_SUCCESS && message != NULL && message->messageType == FREESPACE_MESSAGE_BODYFRAME) {
        cachedBodyFrame = message->bodyFrame;
    }
}

static FreespaceDeviceId initializeFreespace() {
    struct freespace_message message;
    FreespaceDeviceId device;
    int numIds;
    int rc;

    // Initialize the freespace library
    rc = freespace_init();
    if (rc != FREESPACE_SUCCESS) {
        printf("Initialization error. rc=%d\n", rc);
return 1;
    }

    /** --- START EXAMPLE INITIALIZATION OF DEVICE -- **/
    rc = freespace_getDeviceList(&device, 1, &numIds);
    if (numIds == 0) {
        printf("freespaceInputThread: Didn't find any devices.\n");
        exit(1);
    }

    rc = freespace_openDevice(device);
    if (rc != FREESPACE_SUCCESS) {
        printf("freespaceInputThread: Error opening device: %d\n", rc);
        exit(1);
    }
    freespace_setReceiveMessageCallback(device, receiveMessageCallback, NULL);

    rc = freespace_flush(device);
    if (rc != FREESPACE_SUCCESS) {
        printf("freespaceInputThread: Error flushing device: %d\n", rc);
        exit(1);
    }

    memset(&cachedBodyFrame, 0, sizeof(cachedBodyFrame));

    memset(&message, 0, sizeof(message));
    if (FREESPACE_SUCCESS == freespace_isNewDevice(device)) {
        message.messageType = FREESPACE_MESSAGE_DATAMODECONTROLV2REQUEST;
        message.dataModeControlV2Request.packetSelect = 2;
        message.dataModeControlV2Request.modeAndStatus |= 0 << 1;
    } else {
        message.messageType = FREESPACE_MESSAGE_DATAMODEREQUEST;
        message.dataModeRequest.enableBodyMotion = 1;
        message.dataModeRequest.inhibitPowerManager = 1;
    }
    rc = freespace_sendMessage(device, &message);
    if (rc != FREESPACE_SUCCESS) {
        printf("freespaceInputThread: Could not send message: %d.\n", rc);
    }
    /** --- END EXAMPLE INITIALIZATION OF DEVICE -- **/

    return device;
}

static void finalizeFreespace(FreespaceDeviceId device) {
    struct freespace_message message;
    int rc;

    /** --- START EXAMPLE FINALIZATION OF DEVICE --- **/
    printf("\n\nfreespaceInputThread: Cleaning up...\n");
    memset(&message, 0, sizeof(message));
    if (FREESPACE_SUCCESS == freespace_isNewDevice(device)) {
        message.messageType = FREESPACE_MESSAGE_DATAMODECONTROLV2REQUEST;
        message.dataModeControlV2Request.packetSelect = 1;
    } else {
        message.messageType = FREESPACE_MESSAGE_DATAMODEREQUEST;
        message.dataModeRequest.enableMouseMovement = 1;
    }
    rc = freespace_sendMessage(device, &message);
    if (rc != FREESPACE_SUCCESS) {
        printf("freespaceInputThread: Could not send message: %d.\n", rc);
    }

    freespace_closeDevice(device);
    /** --- END EXAMPLE FINALIZATION OF DEVICE --- **/

    freespace_exit();
}

int main(int argc, char* argv[]) {
    struct freespace_BodyFrame body;
    FreespaceDeviceId device;

    //printVersionInfo(argv[0]);

    memset(&body, 0, sizeof(struct freespace_BodyFrame));

    //addControlHandler();
    device = initializeFreespace();

    // Run the game loop
    while (true) {
        // Get input.
        freespace_perform();

        body = cachedBodyFrame;

        // Run game logic.

        // Render.
        printf("\r%d: Current accel = %d, %d, %d          ",
               body.sequenceNumber,
               body.linearAccelX,
               body.linearAccelY,
               body.linearAccelZ);
        fflush(stdout);

        // Wait for "vsync"
#ifdef WIN32
        Sleep(1);
#else
        usleep(16000);
#endif
    }

    finalizeFreespace(device);
    return 0;
}

Merrill

unread,
Aug 27, 2012, 3:05:19 PM8/27/12
to libfre...@googlegroups.com
Hi Philip,

There's an issue with the current libfreespace-examples and the FSRK-USB-2.  We use a function called freespace_isNewDevice() to determine which data control message to use.  The problem is that this freespace_isNewDevice() does not recognize the USB-2 as a new device and the USB-2 does not support the older message.  You can fix your code easily through one of two ways:

- Remove the conditionals in the examples so that you do not check for freespace_isNewDevice() and instead just send the DataModeControlV2Request message

- In the libfreespace library code, modify freespace_deviceTable.c as follows and rebuild the library:

     In freespace_newDeviceAPITable[] struct, add the following to the list:

     { "USB Freespace Module (MV)", 0x1d5a, 0xc080, 2 }

   So, the correct code for freespace_newDeviceAPITable[] is:

   const struct FreespaceDeviceInfo freespace_newDeviceAPITable[] = {

        { "Smart USB RF Transceiver v2 (MKCV)", 0x1d5a, 0xc0b3, 2 },

        { "USB Freespace Module (MV)", 0x1d5a, 0xc080, 2 }

   };


There's an open bug for this that should be resolved in the next release.

Thanks,
-Merrill
Reply all
Reply to author
Forward
0 new messages