Lab 6

Lab Description
In this laboratory experiment, creating a combination of different phidgets is the goal. Previously, the features of a RFID device were observed using a sample code from the device’s website and programmed using Linux. The main feature of a RFID was to read tags on a chip.

Continuing from the last laboratory experiment, RFID must combine with another device. In this experiment, servo motors and sensors will be included. The initial plan was to create a similar model like cars. Due to idea theft, the focus was shifted. After manipulating the servo code to adjust to the flexible RFID code, the RFID activates the four servos once the RFID is read. The touch sensors first allowed two servo motors to move and the second time the sensors were activated, the full four servos were activated. The code however, was unable to perform the actions. In the end, the servo motors will move forward or backward like a mechanism similar to automatic doors with the help of the RFID phiget.

Code

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




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

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

	return 0;
}

int CCONV DetachHandlerRFID(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 ErrorHandlerRFID(CPhidgetHandle RFID, void *userptr, int ErrorCode, const char *unknown)
{
	printf("Error handled. %d - %s\n", ErrorCode, unknown);
	return 0;
}

int CCONV OutputChangeHandler(CPhidgetRFIDHandle RFID, void *usrptr, int Index, int State)
{
	if(Index == 0 || Index == 1)
	{
		printf("Output: %d > State: %d\n", Index, State);
	}
	return 0;
}
//needed for read...?
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);
	printf("Press Enter To continue \n");
	getchar();


	// get the two motors to move at the same time to 200 degrees
	set_servo();	

		return 0;
}
//needed for read...?
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);
	// get the four motors to move to 100 at the same time
	
	set_servo1();

	return 0;
}

//Display the properties of the attached phidget to the screen.  We will be displaying the name, serial number and version of the attached device.
//We will also display the nu,mber of available digital outputs
int display_propertiesRFID(CPhidgetRFIDHandle phid)
{
	int serialNo, version, numOutputs, antennaOn, LEDOn;
	const char* ptr;

	CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
	CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
	CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);

	CPhidgetRFID_getOutputCount (phid, &numOutputs);
	CPhidgetRFID_getAntennaOn (phid, &antennaOn);
	CPhidgetRFID_getLEDOn (phid, &LEDOn);


	printf("%s\n", ptr);
	printf("Serial Number: %10d\nVersion: %8d\n", serialNo, version);
	printf("# Outputs: %d\n\n", numOutputs);
	printf("Antenna Status: %d\nOnboard LED Status: %d\n", antennaOn, LEDOn);

	return 0;
}

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

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

	return 0;
}

int CCONV DetachHandlerADVServo(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 ErrorHandlerADVServo(CPhidgetHandle ADVSERVO, void *userptr, int ErrorCode, const char *Description)
{
	printf("Error handled. %d - %s\n", ErrorCode, Description);
	return 0;
}

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

//Display the properties of the attached phidget to the screen.  We will be displaying the name, serial number and version of the attached device.
int display_propertiesADVServo(CPhidgetAdvancedServoHandle phid)
{
	int serialNo, version, numMotors;
	const char* ptr;

	CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
	CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
	CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);

	CPhidgetAdvancedServo_getMotorCount (phid, &numMotors);

	printf("%s\n", ptr);
	printf("Serial Number: %10d\nVersion: %8d\n# Motors: %d\n", serialNo, version, numMotors);

	return 0;
}

int set_RFID()
{
	int result;
	const char *err;

	//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)rfid, AttachHandlerRFID, NULL);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)rfid, DetachHandlerRFID, NULL);
	CPhidget_set_OnError_Handler((CPhidgetHandle)rfid, ErrorHandlerRFID, 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;
	}

	//Display the properties of the attached RFID device
	display_propertiesRFID(rfid);

	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);

	//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, 0);

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

}
int set_servo1()
{
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);

	//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, AttachHandlerADVServo, NULL);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)servo, DetachHandlerADVServo, NULL);
	CPhidget_set_OnError_Handler((CPhidgetHandle)servo, ErrorHandlerADVServo, 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);

	//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;
	}

	//Display the properties of the attached device
	display_propertiesADVServo(servo);

	//read event data
	printf("Reading..... Servo\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, 0.00);
		CPhidgetAdvancedServo_setPosition (servo, 3, 0.00);
		CPhidgetAdvancedServo_setPosition (servo, 5, 0.00);
		CPhidgetAdvancedServo_setPosition (servo, 7, 0.00);
	CPhidgetAdvancedServo_setEngaged(servo, 0, 1);
	CPhidgetAdvancedServo_setEngaged(servo, 3, 1);
	CPhidgetAdvancedServo_setEngaged(servo, 5, 1);
	CPhidgetAdvancedServo_setEngaged(servo, 7, 1);

	printf("Move to position 100.00. Press any key to Continue\n");
	getchar();
	CPhidgetAdvancedServo_setPosition (servo, 0, 100.00);
	CPhidgetAdvancedServo_setPosition (servo, 3, 100.00);
	CPhidgetAdvancedServo_setPosition (servo, 5, 100.00);
	CPhidgetAdvancedServo_setPosition (servo, 7, 100.00);

}

int set_servo()
{
	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);

	//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, AttachHandlerADVServo, NULL);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)servo, DetachHandlerADVServo, NULL);
	CPhidget_set_OnError_Handler((CPhidgetHandle)servo, ErrorHandlerADVServo, 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);

	//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;
	}

	//Display the properties of the attached device
	display_propertiesADVServo(servo);

	//read event data
	printf("Reading.....Servo\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);
	CPhidgetAdvancedServo_setEngaged(servo, 3, 1);
	CPhidgetAdvancedServo_setEngaged(servo, 5, 1);
	CPhidgetAdvancedServo_setEngaged(servo, 7, 1);

	CPhidgetAdvancedServo_setPosition (servo, 0, 0.00);
	CPhidgetAdvancedServo_setPosition (servo, 3, 0.00);
	CPhidgetAdvancedServo_setPosition (servo, 5, 0.00);
	CPhidgetAdvancedServo_setPosition (servo, 7, 0.00);

}



int Project6()
{
	
	int c;
	
	
	set_RFID();
	set_servo();
	CPhidgetRFIDHandle rfid = 0;

	printf("This is a compile test \n Compiled \n");


	CPhidgetRFID_setAntennaOn(rfid, 1);
	printf("Reading.....\n");
	getchar();
	
	return 0;
}



int main()
{
	//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);

	Project6();
	printf("Closing...\n");
	CPhidget_close((CPhidgetHandle)servo);
	CPhidget_delete((CPhidgetHandle)servo);
	CPhidget_close((CPhidgetHandle)rfid);
	CPhidget_delete((CPhidgetHandle)rfid);
	
	return 0;

}


int Servo_Command_Left()
{
	CPhidgetAdvancedServoHandle servo = 0;
CPhidgetAdvancedServo_setEngaged(servo, 0, 1);
getchar();
CPhidgetAdvancedServo_setPosition (servo, 0, 30.00);

}

int Servo_Command_Right()
{
	CPhidgetAdvancedServoHandle servo = 0;
CPhidgetAdvancedServo_setEngaged(servo, 0, 1);
getchar();
CPhidgetAdvancedServo_setPosition (servo, 0, 330.00);
}
int Servo_Command_FullRight()
{
	CPhidgetAdvancedServoHandle servo = 0;
CPhidgetAdvancedServo_setEngaged(servo, 0, 1);
getchar();
CPhidgetAdvancedServo_setPosition (servo, 0, 360.00);
}

int Servo_Command_FullLeft()
{
	CPhidgetAdvancedServoHandle servo = 0;
CPhidgetAdvancedServo_setEngaged(servo, 0, 1);
getchar();
CPhidgetAdvancedServo_setPosition (servo, 0, 0.00);
}	

Screenshots

lab6_img

Leave a Reply

Your email address will not be published. Required fields are marked *