Run program even when a sensor has a saturation error

C, C++, and Visual C++
Post Reply
BosqueSeco1
Fresh meat
Posts: 1
Joined: Wed Sep 15, 2021 11:41 am
Contact:

Run program even when a sensor has a saturation error

Post by BosqueSeco1 »

We are running an experiment using load cells on trees to measure wind stress. Our program is written to attach and run 16 load cells. However, occasionally load cells will become displaced and show saturation errors. This is a problem when our program 'restarts' to begin a fresh data file but cannot run because one or more sensors have saturation errors.

Is it possible to edit my program so that when it restarts automatically data is still collected for all working sensors even if one or two are running saturation errors?

Thanks you for your help!

Code: Select all

#include <phidget22.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>

char* filename;
FILE *fp;

//Declare your Phidget channels and other variables
PhidgetVoltageRatioInputHandle voltageRatioInput0;
PhidgetVoltageRatioInputHandle voltageRatioInput1;
PhidgetVoltageRatioInputHandle voltageRatioInput2;
PhidgetVoltageRatioInputHandle voltageRatioInput3;
PhidgetVoltageRatioInputHandle voltageRatioInput4;
PhidgetVoltageRatioInputHandle voltageRatioInput5;
PhidgetVoltageRatioInputHandle voltageRatioInput6;
PhidgetVoltageRatioInputHandle voltageRatioInput7;
PhidgetVoltageRatioInputHandle voltageRatioInput8;
PhidgetVoltageRatioInputHandle voltageRatioInput9;
PhidgetVoltageRatioInputHandle voltageRatioInput10;
PhidgetVoltageRatioInputHandle voltageRatioInput11;
PhidgetVoltageRatioInputHandle voltageRatioInput12;
PhidgetVoltageRatioInputHandle voltageRatioInput13;
PhidgetVoltageRatioInputHandle voltageRatioInput14;
PhidgetVoltageRatioInputHandle voltageRatioInput15;
int vri_attached = 0;

static void CCONV onVoltageRatioChange(PhidgetVoltageRatioInputHandle ch, void * ctx, double voltageRatio) {
	int channel;
  double voltageRatio1, voltageRatio2, voltageRatio3, voltageRatio4, voltageRatio5, voltageRatio6, voltageRatio7, voltageRatio8, voltageRatio9, voltageRatio10, voltageRatio11, voltageRatio12, voltageRatio13, voltageRatio14, voltageRatio15 = 0.0;
	long ms;
	if (vri_attached == 16) {
		time_t t;
		t = time(NULL);
		struct tm timeinfo = *localtime(&t);
		struct timespec spec;
		clock_gettime(CLOCK_REALTIME, &spec);

		ms = spec.tv_nsec / 1000000;

    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput1, &voltageRatio1);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput2, &voltageRatio2);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput3, &voltageRatio3);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput4, &voltageRatio4);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput5, &voltageRatio5);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput6, &voltageRatio6);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput7, &voltageRatio7);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput8, &voltageRatio8);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput9, &voltageRatio9);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput10, &voltageRatio10);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput11, &voltageRatio11);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput12, &voltageRatio12);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput13, &voltageRatio13);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput14, &voltageRatio14);
    PhidgetVoltageRatioInput_getVoltageRatio(voltageRatioInput15, &voltageRatio15);
		if (voltageRatio1 > 5)	//initial value not yet received
			return;

		char* timestamp = (char *)malloc(sizeof(char) * 24);

		sprintf(timestamp, "%d-%02d-%02d %02d:%02d:%02d.%03d", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, ms);

		printf("%s,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",timestamp, voltageRatio, voltageRatio1, voltageRatio2, voltageRatio3, voltageRatio4, voltageRatio5, voltageRatio6, voltageRatio7, voltageRatio8, voltageRatio9, voltageRatio10, voltageRatio11, voltageRatio12, voltageRatio13, voltageRatio14, voltageRatio15);

		fp = fopen(filename, "a");
		fprintf(fp, "%s,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n", timestamp, voltageRatio, voltageRatio1, voltageRatio2, voltageRatio3, voltageRatio4, voltageRatio5, voltageRatio6, voltageRatio7, voltageRatio8, voltageRatio9, voltageRatio10, voltageRatio11, voltageRatio12, voltageRatio13, voltageRatio14, voltageRatio15);
		fclose(fp);
	}
}

static void CCONV onAttach(PhidgetHandle ch, void * ctx) {
	int channel;
	int hubPort;
	int deviceSerialNumber;

	Phidget_getChannel(ch, &channel);
	Phidget_getHubPort(ch, &hubPort);
	Phidget_getDeviceSerialNumber(ch, &deviceSerialNumber);
	printf("Attach [%d][%d][%d]!\n", deviceSerialNumber, hubPort, channel);

	fp = fopen(filename, "a");
	fprintf(fp, "Attach [%d][%d][%d]!\n", deviceSerialNumber, hubPort, channel);
	fclose(fp);

	PhidgetVoltageRatioInput_setDataInterval((PhidgetVoltageRatioInputHandle)ch, 250);
	PhidgetVoltageRatioInput_setVoltageRatioChangeTrigger((PhidgetVoltageRatioInputHandle)ch, 0);

	vri_attached++;
}

static void CCONV onDetach(PhidgetHandle ch, void * ctx) {
	int channel;
	int hubPort;
	int deviceSerialNumber;

	Phidget_getChannel(ch, &channel);
	Phidget_getHubPort(ch, &hubPort);
	Phidget_getDeviceSerialNumber(ch, &deviceSerialNumber);
	printf("Detach [%d][%d][%d]!\n", deviceSerialNumber, hubPort, channel);

	fp = fopen(filename, "a");
	fprintf(fp, "Detach [%d][%d][%d]!\n", deviceSerialNumber, hubPort, channel);
	fclose(fp);

	vri_attached--;
}

int main() {
	long ms;
	time_t t;
	t = time(NULL);
	struct tm timeinfo = *localtime(&t);
	struct timespec spec;
	clock_gettime(CLOCK_REALTIME, &spec);

	ms = spec.tv_nsec / 1000000;

	filename = (char *)malloc(sizeof(char) * 28);
	sprintf(filename, "logs_%d%02d%02d_%02d:%02d:%02d.csv", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);

  PhidgetVoltageRatioInput_create(&voltageRatioInput0) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput1) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput2) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput3) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput4) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput5) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput6) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput7) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput8) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput9) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput10) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput11) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput12) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput13) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput14) ;
  PhidgetVoltageRatioInput_create(&voltageRatioInput15) ;

  //Set addressing parameters to specify which channel to open (if any)
	Phidget_setHubPort((PhidgetHandle)voltageRatioInput0, 0);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput0, 0);
	Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput0, 608605);

	Phidget_setHubPort((PhidgetHandle)voltageRatioInput1, 0);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput1, 1);
	Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput1, 608605);

	Phidget_setHubPort((PhidgetHandle)voltageRatioInput2, 1);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput2, 0);
	Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput2, 608605);

	Phidget_setHubPort((PhidgetHandle)voltageRatioInput3, 1);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput3, 1);
	Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput3, 608605);

	Phidget_setHubPort((PhidgetHandle)voltageRatioInput4, 2);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput4, 0);
	Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput4, 608605);

	Phidget_setHubPort((PhidgetHandle)voltageRatioInput5, 2);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput5, 1);
	Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput5, 608605);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput6, 3);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput6, 0);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput6, 608605);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput7, 3);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput7, 1);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput7, 608605);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput8, 4);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput8, 0);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput8, 608605);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput9, 4);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput9, 1);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput9, 608605);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput10, 5);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput10, 0);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput10, 608605);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput11, 5);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput11, 1);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput11, 608605);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput12, 0);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput12, 0);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput12, 608516);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput13, 0);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput13, 1);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput13, 608516);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput14, 1);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput14, 0);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput14, 608516);

  Phidget_setHubPort((PhidgetHandle)voltageRatioInput15, 1);
  Phidget_setChannel((PhidgetHandle)voltageRatioInput15, 1);
  Phidget_setDeviceSerialNumber((PhidgetHandle)voltageRatioInput15, 608516);

  PhidgetVoltageRatioInput_setOnVoltageRatioChangeHandler(voltageRatioInput0, onVoltageRatioChange, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput0, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput0, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput1, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput1, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput2, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput2, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput3, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput3, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput4, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput4, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput5, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput5, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput6, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput6, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput7, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput7, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput8, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput8, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput9, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput9, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput10, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput10, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput11, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput11, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput12, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput12, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput13, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput13, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput14, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput14, onDetach, NULL);
  Phidget_setOnAttachHandler((PhidgetHandle)voltageRatioInput15, onAttach, NULL);
  Phidget_setOnDetachHandler((PhidgetHandle)voltageRatioInput15, onDetach, NULL);

  printf("Waiting for attachs\n");
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput1, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput2, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput3, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput4, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput5, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput6, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput7, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput8, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput9, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput10, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput11, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput12, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput13, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput14, 5000);
  Phidget_openWaitForAttachment((PhidgetHandle)voltageRatioInput15, 5000);
  Phidget_open((PhidgetHandle)voltageRatioInput0);

  while (1) { sleep(1); }

	printf("Ending program.\n");

  Phidget_close((PhidgetHandle)voltageRatioInput0);
  Phidget_close((PhidgetHandle)voltageRatioInput1);
  Phidget_close((PhidgetHandle)voltageRatioInput2);
  Phidget_close((PhidgetHandle)voltageRatioInput3);
  Phidget_close((PhidgetHandle)voltageRatioInput4);
  Phidget_close((PhidgetHandle)voltageRatioInput5);
  Phidget_close((PhidgetHandle)voltageRatioInput6);
  Phidget_close((PhidgetHandle)voltageRatioInput7);
  Phidget_close((PhidgetHandle)voltageRatioInput8);
  Phidget_close((PhidgetHandle)voltageRatioInput9);
  Phidget_close((PhidgetHandle)voltageRatioInput10);
  Phidget_close((PhidgetHandle)voltageRatioInput11);
  Phidget_close((PhidgetHandle)voltageRatioInput12);
  Phidget_close((PhidgetHandle)voltageRatioInput13);
  Phidget_close((PhidgetHandle)voltageRatioInput14);
  Phidget_close((PhidgetHandle)voltageRatioInput15);

  PhidgetVoltageRatioInput_delete(&voltageRatioInput0);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput1);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput2);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput3);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput4);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput5);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput6);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput7);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput8);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput9);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput10);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput11);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput12);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput13);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput14);
  PhidgetVoltageRatioInput_delete(&voltageRatioInput15);

  return 0;
  }
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Run program even when a sensor has a saturation error

Post by mparadis »

You need to be handling the exceptions so your program knows what to do when one of the load cells is in saturation. When you call getVoltageRatio, you should check the return value and if there's an error, handle it by printing a value that indicates saturation. You can read more on this page.

As a side note, you can use an array of Phidget handles to cut down on the repeated lines in your program. Have a look at this code snippet to get an idea of how it works in C:

Code: Select all

//Create an array for your Phidget channels
PhidgetDigitalOutputHandle ch[8];

for (int i = 0; i < 8; i++) {
    //Create the channels
    PhidgetDigitalOutput_create(&ch[i]);
    //Address, then open the channels
    Phidget_setChannel((PhidgetHandle)ch[i], i);
    Phidget_openWaitForAttachment((PhidgetHandle)ch[i], 5000);
}

PhidgetDigitalOutput_setState(ch[0], true);
PhidgetDigitalOutput_setState(ch[1], false);
PhidgetDigitalOutput_setState(ch[2], false);
PhidgetDigitalOutput_setState(ch[3], true);

//Close the channels when done
for (int i = 0; i < 8; i++) {
    Phidget_close((PhidgetHandle)ch[i]);
}
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests