Skip to content

Categories:

Lab 6

Lab Description:

In this lab we needed to work with another team using different Phidgets and develop a C/C++ application together that integrates these different devices. My team the Servo needed to work with the team that has the RFID. We needed to create any created idea using these two devices together. One of them is to add a listener to the RFID and when RFID is scanned the temperature reading will be display or something unique. In this project we tried to make the RFID work with the servo motor, our goal was to  make the RIFD be activated and when activated the servo motor would turn a certain degree , we tried  solving the problem  by implementing if and else commands, using function to call the  RFID sensor  in to our C program but it failed to work  we tried to make the sensor work when the RFID sensor was activated. Our code below activates and runs both phidget devices together with the same code but  it runs separately and both of them have no control over each other. Our goal was to make it work together and  make each other be able to work with each other.

Code:

#include <stdio.h>
#include <Phidget21/phidget21.h>

int CCONV AttachHandler(CPhidgetHandle RFID, void *userptr)
{
	int serialNo;
	const char *name;

	CPhidget_getDeviceName (RFID, &name);
	CPhidget_getSerialNumber(RFID, &serialNo);
	printf("%s %10d attached!\n", name, serialNo);

	return 0;
}

int CCONV DetachHandler(CPhidgetHandle RFID, void *userptr)
{
	int serialNo;
	const char *name;

	CPhidget_getDeviceName (RFID, &name);
	CPhidget_getSerialNumber(RFID, &serialNo);
	printf("%s %10d detached!\n", name, serialNo);

	return 0;
}

int CCONV ErrorHandler(CPhidgetHandle RFID, void *userptr, int ErrorCode, const char *unknown)
{
	printf("Error handled. %d - %s\n", ErrorCode, unknown);
	return 0;
}

int CCONV AttachHandler1(CPhidgetHandle ADVSERVO, void *userptr)
{
	int serialNo;
	const char *name;

	CPhidget_getDeviceName (ADVSERVO, &name);
	CPhidget_getSerialNumber(ADVSERVO, &serialNo);
	printf("%s %10d attached!\n", name, serialNo);

	return 0;
}

int CCONV DetachHandler1(CPhidgetHandle ADVSERVO, void *userptr)
{
	int serialNo;
	const char *name;

	CPhidget_getDeviceName (ADVSERVO, &name);
	CPhidget_getSerialNumber(ADVSERVO, &serialNo);
	printf("%s %10d detached!\n", name, serialNo);

	return 0;
}

int CCONV ErrorHandler1(CPhidgetHandle ADVSERVO, void *userptr, int ErrorCode, const char *Description)
{
	printf("Error handled. %d - %s\n", ErrorCode, Description);
	return 0;
}

int CCONV TagHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto)
{
	//turn on the Onboard LED
	CPhidgetRFID_setLEDOn(RFID, 1);

	printf("Tag Read: %s\n", TagVal);
	return 0;
}

int CCONV TagLostHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto)
{
	//turn off the Onboard LED
	CPhidgetRFID_setLEDOn(RFID, 0);

	printf("Tag Lost: %s\n", TagVal);
	return 0;
}

int CCONV OutputChangeHandler(CPhidgetRFIDHandle RFID, void *usrptr, int Index, int State)
{
	if(Index == 1 || Index == 1)
	{
		printf("Output: %d > State: %d\n", Index, State);
	}
	return 0;
}

int CCONV PositionChangeHandler(CPhidgetAdvancedServoHandle ADVSERVO, void *usrptr, int Index, double Value)
{
	printf("Motor: %d > Current Position: %f\n", Index, Value);
	return 0;
}

int servo_rfid()
{
	int result;
	double curr_pos;
	const char *err;
	double minAccel, maxVel;

    //Declare an advanced servo handle
	CPhidgetAdvancedServoHandle servo = 0;

	//create the advanced servo object
	CPhidgetAdvancedServo_create(&servo);

	//Declare an RFID handle
	CPhidgetRFIDHandle rfid = 0;

	//create the RFID object
	CPhidgetRFID_create(&rfid);

    //Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)servo, AttachHandler1, NULL);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)servo, DetachHandler1, NULL);
	CPhidget_set_OnError_Handler((CPhidgetHandle)servo, ErrorHandler1, NULL);

    //Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)rfid, AttachHandler, NULL);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)rfid, DetachHandler, NULL);
	CPhidget_set_OnError_Handler((CPhidgetHandle)rfid, ErrorHandler, NULL);

    //Registers a callback that will run when the motor position is changed.
	//Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
	CPhidgetAdvancedServo_set_OnPositionChange_Handler(servo, PositionChangeHandler, NULL);

    //Registers a callback that will run if an output changes.
	//Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
    CPhidgetRFID_set_OnOutputChange_Handler(rfid, OutputChangeHandler, NULL);

	//Registers a callback that will run when a Tag is read.
	//Requires the handle for the PhidgetRFID, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
	CPhidgetRFID_set_OnTag2_Handler(rfid, TagHandler, NULL);

	//Registers a callback that will run when a Tag is lost (removed from antenna read range).
	//Requires the handle for the PhidgetRFID, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
	CPhidgetRFID_set_OnTagLost2_Handler(rfid, TagLostHandler, NULL);

    //open the RFID for device connections
	CPhidget_open((CPhidgetHandle)rfid, -1);

	//get the program to wait for an RFID device to be attached
	printf("Waiting for RFID to be attached....");
	if((result = CPhidget_waitForAttachment((CPhidgetHandle)rfid, 10000)))
	{
		CPhidget_getErrorDescription(result, &err);
		printf("Problem waiting for attachment: %s\n", err);
		return 0;
	}

    CPhidgetRFID_setAntennaOn(rfid, 1);

	//read RFID event data
	printf("Reading.....\n");

	//keep displaying RFID event data until user input is read
	printf("Press any key to continue\n");
	getchar();

	//toggle the digital output (when making this example I had an LED plugged into the digital output index 0
	CPhidgetRFID_setOutputState(rfid, 0, 1);

    //toggle the digital output (when making this example I had an LED plugged into the digital output index 0
	CPhidgetRFID_setOutputState(rfid, 0, 0);

    //open the device for connections
	CPhidget_open((CPhidgetHandle)servo, -1);

	//get the program to wait for an advanced servo device to be attached
	printf("Waiting for Phidget to be attached....");
	if((result = CPhidget_waitForAttachment((CPhidgetHandle)servo, 10000)))
	{
		CPhidget_getErrorDescription(result, &err);
		printf("Problem waiting for attachment: %s\n", err);
		return 0;
	}

	//read event data
	printf("Reading.....\n");

	//This example assumes servo motor is attached to index 0

	//Set up some initial acceleration and velocity values
	CPhidgetAdvancedServo_getAccelerationMin(servo, 0, &minAccel);
	CPhidgetAdvancedServo_setAcceleration(servo, 0, minAccel*2);
	CPhidgetAdvancedServo_getVelocityMax(servo, 0, &maxVel);
	CPhidgetAdvancedServo_setVelocityLimit(servo, 0, maxVel/2);

	//display current motor position
	if(CPhidgetAdvancedServo_getPosition(servo, 0, &curr_pos) == EPHIDGET_OK)
		printf("Motor: 0 > Current Position: %f\n", curr_pos);

	//keep displaying servo event data until user input is read
	printf("Press any key to continue\n");
	getchar();

	//change the motor position
	//valid range is -23 to 232, but for most motors ~30-210
	//we'll set it to a few random positions to move it around

	//Step 1: Position 40.00 - also engage servo
	printf("Move to position 40.00 and engage. Press any key to Continue\n");
	getchar();

	CPhidgetAdvancedServo_setPosition (servo, 0, 40.00);
	CPhidgetAdvancedServo_setEngaged(servo, 0, 1);

	//Step 2: Position 50.00
	printf("Move to position 50.00. Press any key to Continue\n");
	getchar();

	CPhidgetAdvancedServo_setPosition (servo, 0, 50.00);

	//Step 3: Position 100.00
	printf("Move to position 100.00. Press any key to Continue\n");
	getchar();

	CPhidgetAdvancedServo_setPosition (servo, 0, 100.00);

	//Step 4: Position 150.00
	printf("Move to position 150.00. Press any key to Continue\n");
	getchar();

	CPhidgetAdvancedServo_setPosition (servo, 0, 150.00);

	//Step 5: Position 200.00
	printf("Move to position 200.00. Press any key to Continue\n");
	getchar();

	CPhidgetAdvancedServo_setPosition (servo, 0, 200.00);

	//Step 6: Position 70.00
	printf("Move to position 70.00. Press any key to Continue\n");
	getchar();

	CPhidgetAdvancedServo_setPosition (servo, 0, 70.00);

	//Step 7: Disengage
	printf("Disengage Servo. Press any key to Continue\n");
	getchar();

	CPhidgetAdvancedServo_setEngaged(servo, 0, 0);

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

	//since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
	printf("Closing...\n");
	CPhidget_close((CPhidgetHandle)rfid);
	CPhidget_delete((CPhidgetHandle)rfid);
    CPhidget_close((CPhidgetHandle)servo);
	CPhidget_delete((CPhidgetHandle)servo);
	//all done, exit
	return 0;
}

int main(int argc, char* argv[])
{
	servo_rfid();
	return 0;
}

Screenshots:

Screen Shot 2014-05-15 at 8.50.58 AM Screen Shot 2014-05-15 at 8.51.15 AM Screen Shot 2014-05-15 at 8.51.35 AM Screen Shot 2014-05-15 at 8.51.51 AM Screen Shot 2014-05-15 at 8.52.08 AM Screen Shot 2014-05-15 at 8.52.30 AM


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.