Phidget Devices RFID

Phidget Devices RFID

For this lab we had to team up with other group from class and make our servo motor working there sensor. so we are working with RFID and touch sensor to make the servo motor move. our initially project was to use motor as the lock and unlock doors at school or bank but we wanted to go a set more and add the touch sensor to make specific motor to rotate. however we were unable to mesh all the code to make all three device to work.

The device our team choose was the Servo Motor, the model number of the motor is HS-422 . which one of the most durable , reliable , high impact resin gear train and high performance circuitry servos is offered by Prof Dr jose Reyes. Most of the time these type of servo are use for or as  actuator for different mechanical systems such as the steering control for cars , doors on a airplane, elevator, and many more.

This servo motor is a limited rotation servo that has a range of rotation of 180° at maximum speed of 286°/s that produces 3 kg·cm (.22 IB-ft ) of torque and its required 5 volts of battery to run all that glory.

We did had a lot of problems, to being with our USB wasn’t readable by our computer which we running in virtual box even when we allowed it, so we hard installed the Linux in to our computer. After that we couldn’t add the required library to run phidget devices. Finally, after fix everything we couldn’t get the servo to run we then later on find out that we had run the command as a “sudo” in order to work.

The team member are Fredrick Jah, Farjana Lopa and Prina Presila.

 

#include 
#include 

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

}
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.....\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.....\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);

}

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

pic

3510_ServosMovebut_notag_read

Leave a Reply

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