Alert.png

Notice: This page contains information for the legacy Phidget21 Library.

Phidget21 is out of support. Bugfixes may be considered on a case by case basis.

Phidget21 does not support VINT Phidgets, or new USB Phidgets released after 2020. We maintain a selection of legacy devices for sale that are supported in Phidget21.

We recommend that new projects be developed against the Phidget22 Library.


Click on the 2phidget22.jpg button in the menu bar to go to the Phidget22 version of this page.

Alert.png

Simple Spatial Experiments: Difference between revisions

From Phidgets Legacy Support
Line 39: Line 39:
==Code==
==Code==


Our code measures data by writing it every time we have a 'new data' event.  Here we call the function that handles that event {{Code|SpatialDataHandler}}.
Our code measures data by writing it every time we have a 'new data' event.  Here we call the function that handles that event {{Code|SpatialDataHandler}}. 
That function receives an event at the default timing (about every 8 ms) and then writes the received data to a file called {{Code|spatial_data.csv}}.
 
You can either peruse the code and modify it to your needs, or simply copy and paste it into a file.  We called our file {{Code|sample_code.c}}.


<div class="source">
<div class="source">
Line 107: Line 110:
</syntaxhighlight>
</syntaxhighlight>
</div>
</div>
If you have your spatial plugged


==Putting it All Together==
==Putting it All Together==


==Extra Credit==
==Extra Credit==

Revision as of 19:08, 15 March 2012

The project described here is a simple data recording program for the Phidget Spatial. We play with the program and record data to learn things about our environment.


Practical concepts covered are (click on links to see other projects on that topic):  


As with any of our described projects, Phidgets takes care of the electrical component design. Here, we also provide some simple code so you can play around with your Spatial and save the data to plot later.

Time: About two hours (It is perfect for a Saturday morning, perhaps with your kids?)

Special Needed Tools: A Phidget Spatial, USB cord, and your computer.

  • You should have already worked through our Phidget Spatial Getting Started Guide for your Spatial Device - this will show you how to get the the Phidget Libraries installed for your operating system.
  • Your computer should have a C compiler we support (including gcc, MinGW, Borland, Visual Studio, and many more)
  • If you would like to follow along with the graphing examples, you can install the R Statistical package, which is free and available for Windows, Mac, and Linux.


Introduction

A Phidget Spatial can measure acceleration, and some models can also measure gyroscopic motion and magnetic fields.

We start with a Phidget spatial just on our desk, and then play around with it and plot the data.

Phidgets

All you need is a Phidget Spatial - this page shows use of the 1056 - PhidgetSpatial 3/3/3. Hook it up using USB to your computer as shown in the getting started guide for your Spatial Device

Code

Our code measures data by writing it every time we have a 'new data' event. Here we call the function that handles that event SpatialDataHandler. That function receives an event at the default timing (about every 8 ms) and then writes the received data to a file called spatial_data.csv.

You can either peruse the code and modify it to your needs, or simply copy and paste it into a file. We called our file sample_code.c.

#include <stdio.h>
#include <stdlib.h>
#include <phidget21.h>

// Our function that gets called at the set data rate (default of 8 ms)
// Different rates can be set using CPhidgetSpatialHandle_setDataRate(milliseconds)
int SpatialDataHandler(CPhidgetSpatialHandle spatial, void *userptr, CPhidgetSpatial_SpatialEventDataHandle *data, int count)
{
    int i;
    printf(".");
    fflush(stdout);
    // Sometimes we get more than one packet per event, this counts through them
    for(i = 0; i < count; i++) {
        FILE *file = (FILE *) userptr;
        // Our header was already printed on opening the file: 
        // Timestamp, then Accel X,Y,Z, then Ang Rate X,Y,Z then Mag Field X,Y,Z
        fprintf(file, "%f,", data[i]->timestamp.seconds + (data[i]->timestamp.microseconds)/1000000.0);
        fprintf(file, "%6f,%6f,%6f,", data[i]->acceleration[0], data[i]->acceleration[1], data[i]->acceleration[2]);
	fprintf(file, "%6f,%6f,%6f,", data[i]->angularRate[0], data[i]->angularRate[1], data[i]->angularRate[2]);
        if (data[i]->magneticField[0] == PUNK_DBL) {
	    fprintf(file, "%6f,%6f,%6f\n", 0.0, 0.0, 0.0);
        } else {
    	    fprintf(file, "%6f,%6f,%6f\n", data[i]->magneticField[0], data[i]->magneticField[1], data[i]->magneticField[2]);
        }
        fflush(file);
    }
    return 0;
}

int main(int argc, char* argv[]) {

    int result;
    char character;

    FILE *file = fopen("spatial_data.csv","w");

	CPhidgetSpatialHandle spatial = 0;

    // Print the header at the top of the file
    fprintf(file, "Time,Accel_X,Accel_Y,Accel_Z,Ang_X,Ang_Y,Ang_Z,Mag_X,Mag_Y,Mag_Z\n");

    CPhidgetSpatial_create(&spatial);

    // Hook our file-writing function above into the data stream events
    CPhidgetSpatial_set_OnSpatialData_Handler(spatial, SpatialDataHandler, (void *) file);

    CPhidget_open((CPhidgetHandle)spatial, -1);

    result = CPhidget_waitForAttachment((CPhidgetHandle)spatial, 10000);
    if (result) {
        printf("No Device!\n");
        return 0;
    }

    printf("Press any key to end\n");
    getchar();

    CPhidget_close((CPhidgetHandle)spatial);
    CPhidget_delete((CPhidgetHandle)spatial);
    fclose(file);
    return 0;
}

If you have your spatial plugged

Putting it All Together

Extra Credit