Lab # 6 Creating C++ Application that integreates both a RFID and Sensors

Lab Description:

In this lab we are working with another team using different Phidgets and develop a C++/C application together that integrates both a RFID and the touching or rotational sensor that our group did. The purpose of this lab is once the motor is activated due to the amount of force applied to the touch sensor, it will read the sensor and the sensor will activate accordingly. We unfortunately ran out of time and therefore removed the sensor because it was conflicting problems. But when we removed the sensors we were able to solve the problem to an extent where is was working a lot more. The sensors aspect of the project is supposedly suppose to act like a secondary option for the door lock project; sort of like an iPhone where unlocking the phone consists of 2 options, either from the finger print system or from the unlocking code.

What the other group did was they also had problems. Problems was that they couldn’t get the servo to change directions after introducing the tag to the RFID.

Lab Codes

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

Leave a Reply

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