Recording data from Spatial 1044 and logging it to file

Any hardware type questions or problems for all other Phidget devices.
Manuel Miranda
Fresh meat
Posts: 3
Joined: Fri Jul 06, 2018 10:10 am
Contact:

Recording data from Spatial 1044 and logging it to file

Postby Manuel Miranda » Fri Jul 06, 2018 10:53 am

Hi there,
I have a few questions regarding the way the Spatial 1044 streams data through the USB connection. I implemented a C program similar to the one you show here https://www.phidgets.com/?view=articles&article=SimpleSpatialExperiments, but modified it to use the phidgets22 library, in a similar way as your spatial example code. The relevant code is shown at the end of the message.

Anyway, when I set the data interval to the minimum (which is 4 ms for the Spatial 3/3/3), the output I get is shown below. Notice the first event fires at 0.004 seconds, which is a bit odd so I'm guessing it should be discarded. Thereafter, events fire at 28.74, 28.748, 28.756, 28.764, 28.772 seconds, etc., which means that the data interval is 8 ms for several events before it switches down to 4ms, where it remains for almost all the rest of the recording. Out of about 7500 events that are logged (30 seconds of recording), about 0.7% skip an event (data interval 8 ms instead of 4 ms). This means that 99.3% of events are firing at the desired interval of 4 ms.

I've repeated this several times and I get very similar results. So my questions are:

(1) Is this expected behavior or am I doing something wrong? I'm especially curious as to why the first few events are firing at a slower rate. Should I discard them?
(2) How is the zero of the timestamp defined? Is there a way to set it to zero before logging data?

Any help and suggestions would be much appreciated. Thanks for a great product and a powerful API.

Creating logfile...current time is Tue Jul 3 01:37:54 2018
Channel 0 on device 416115 attached
Sampling interval set to 0.004000 seconds
Gathering data for 30 seconds...
Time, Accel_X, Accel_Y, Accel Z, Ang_X, Ang_Y, Ang_Z, Mag_X, Mag_Y, Mag_Z
0.004,-0.01297,0.00066,0.98452,-0.39612,0.11414,-0.66208,-0.68478,-0.10302,0.20301
28.74,-0.01133,0.00092,0.98518,-0.50354,0.22156,-0.86563,-0.68478,-0.10605,0.20301
28.748,-0.01148,0.00221,0.98236,-0.38269,0.39276,-0.55024,-0.68478,-0.10605,0.20301
28.756,-0.01419,0.00607,0.98671,-0.45319,0.44312,-0.62406,-0.68478,-0.10605,0.20301
28.764,-0.01274,-0.00237,0.99716,-0.51361,0.39276,-0.47643,-0.68478,-0.10302,0.20301
28.772,-0.01427,-0.00599,0.97626,-0.39276,0.1712,-0.89247,-0.68478,-0.10605,0.20301
28.78,-0.01556,-0.00191,0.9901,-0.43304,0.27191,-0.74484,-0.68175,-0.10302,0.20301
28.788,-0.01633,0.00191,0.98633,-0.52368,0.36255,-0.68445,0,0,0
28.792,-0.01259,0.00198,0.98366,-0.52368,0.26184,-0.83208,-0.68478,-0.10605,0.20301
28.796,-0.00984,0.00191,0.97992,-0.54382,0.14099,-0.81866,0,0,0
28.8,-0.00786,0.00427,0.98007,-0.62439,0.28198,-0.8455,-0.68478,-0.10605,0.20301
28.804,-0.00702,0.00572,0.98404,-0.54382,0.32227,-0.65761,0,0,0
28.808,-0.00801,0.0071,0.98785,-0.60425,0.46326,-0.72471,-0.68478,-0.10302,0.20907
28.812,-0.01083,0.00648,0.98747,-0.54382,0.40283,-0.83208,0,0,0
28.816,-0.01259,0.00351,0.98526,-0.30212,0.08057,-0.63077,-0.68478,-0.10302,0.20301
28.82,-0.01343,0.00038,0.98969,-0.46326,0.34241,-0.57708,0,0,0




Code: Select all

/*
 * This event reports the most recent values that the channel has measured, which occurs when the sampling interval has elapsed
 */
static void CCONV
onSpatialDataHandler(PhidgetSpatialHandle ch, void *ctx, const double* acceleration, const double* angularRate, const double* magneticField, double timestamp) {

    FILE *file = (FILE *) ctx;

    fprintf(file, "%f,", timestamp/1000.0);
    fprintf(file, "%8f,%8f,%8f,", acceleration[0], acceleration[1], acceleration[2]);
    fprintf(file, "%8f,%8f,%8f,", angularRate[0], angularRate[1], angularRate[2]);
    if (magneticField[0] == PUNK_DBL) {
        fprintf(file, "%8f,%8f,%8f\n", 0.000, 0.000, 0.000);
    } else {
        fprintf(file, "%8f,%8f,%8f\n", magneticField[0], magneticField[1], magneticField[2]);
    }
    fflush(file); 
}

/*
 * Main program here
 * -----------------
 */
int
main(int argc, char **argv) {
    PhidgetSpatialHandle ch;
    PhidgetReturnCode res;
    const char *errs;
    int user_res;

    FILE *file = fopen("spatial_data.csv","w");
    if (file == NULL) {
        fprintf(stderr, "failed to open file \"spatial_data.csv\"\n");
        exit(1);
    }
    user_res = initializeLogFileWithDateStamp((void *) file);
    if (user_res == 1) {
        fprintf(stderr, "failed to initialize log file with date stamp\n");
        exit(1);
    }
    res = PhidgetSpatial_create(&ch);
    if (res != EPHIDGET_OK) {
        fprintf(stderr, "failed to create Spatial channel\n");
        exit(1);
    }
    res = initChannel((PhidgetHandle)ch, (void *) file);
    if (res != EPHIDGET_OK) {
        Phidget_getErrorDescription(res, &errs);
        fprintf(stderr, "failed to initialize channel:%s\n", errs);
        exit(1);
    }
    res = PhidgetSpatial_setOnSpatialDataHandler(ch, onSpatialDataHandler, (void *) file);
    if (res != EPHIDGET_OK) {
        Phidget_getErrorDescription(res, &errs);
        fprintf(stderr, "failed to set spatial data change handler: %s\n", errs);
        goto done;
    }
/*  Open the channel synchronously: waiting a maximum of 5 seconds (5000 milliseconds) */
    res = Phidget_openWaitForAttachment((PhidgetHandle)ch, 5000);
    if (res != EPHIDGET_OK) {
        if (res == EPHIDGET_TIMEOUT) {
            fprintf(file, "Channel did not attach after 5 seconds: please check that the device is attached\n");
        } else {
            Phidget_getErrorDescription(res, &errs);
            fprintf(stderr, "failed to open channel:%s\n", errs);
        }
        goto done;
    }
    fprintf(file, "Gathering data for %d seconds...\n", n_record_size);
    fprintf(file, "Time, Accel_X, Accel_Y, Accel Z, Ang_X, Ang_Y, Ang_Z, Mag_X, Mag_Y, Mag_Z\n");
    fflush(file);
    ssleep(n_record_size);

done:
/*  Finish everything and clean up  */
    Phidget_close((PhidgetHandle)ch);
    PhidgetSpatial_delete(&ch);
    user_res = finalizeLogFileWithDateStamp((void *) file);
    if (user_res == 1) {
        fprintf(stderr, "failed to finalize log file with date stamp\n");
    }
    fclose(file);
    exit(res);
}

Manuel Miranda
Fresh meat
Posts: 3
Joined: Fri Jul 06, 2018 10:10 am
Contact:

Re: Recording data from Spatial 1044 and logging it to file

Postby Manuel Miranda » Mon Jul 16, 2018 12:44 pm

Update:
After much experimentation it seems that the best option is to just throw out the first second of data. Regarding skipped events, this may be an issue with the device that was being tested, which has been used for a while. I repeated the experiment with brand new 1033 devices and did not see any skipped events in my log file, even though the sampling rate is 4 times higher.


Return to “All Other Phidgets”

Who is online

Users browsing this forum: No registered users and 1 guest