Phidget Devices

Phidget Devices

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.

Main command:

Gcc lab5.c -of lab5 -l Phidgets 21

Sudo ./ lab5



//Lab 5 

#include //Phidget library 

int CCONV AttachHandler(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 DetachHandler(CPhidgetHandle ADVSERVO, void *userptr) 
    int serialNo;//Declares Serial number 
    const char *name; 

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

    return 0; 

int CCONV ErrorHandler(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;//Displays motor position 

//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_properties(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);//Displays the characteristics of the servo 

    return 0; 

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

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

    //Creates the Advanced Servo 

    //Set the handlers to be ran when the it is plugged in unplugged or closed , or when there is an error. 
    CPhidget_set_OnAttach_Handler((CPhidgetHandle)servo, AttachHandler, NULL); 
    CPhidget_set_OnDetach_Handler((CPhidgetHandle)servo, DetachHandler, NULL); 
    CPhidget_set_OnError_Handler((CPhidgetHandle)servo, 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); 

    //Opens the phidget servo 
    CPhidget_open((CPhidgetHandle)servo, -1); 

    //Waits for phidget 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 

    //Reads the data 

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

    //Displays current motor position to end 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 Enter to continue\n"); 

    //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: Begin and engage servo at position 20 
    printf("Press Enter to activate the Motor and start from position 20\n"); 

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

    //Step 2: Position 50.00 
    printf("Press Enter to go to postion 50\n"); 

    CPhidgetAdvancedServo_setPosition (servo, 0, 50.00);//20-50 

    //Step 3: Position 100.00 
    printf("Press Enter to go to postion 100\n"); 

    CPhidgetAdvancedServo_setPosition (servo, 0, 100.00); 

    //Step 4: Position 150.00 
    printf("Press Enter to go to postion 150\n"); 

    CPhidgetAdvancedServo_setPosition (servo, 0, 150.00); 

    //Step 5: Position 200.00 
    printf("Press Enter to go to postion 200\n"); 

    CPhidgetAdvancedServo_setPosition (servo, 0, 200.00); 

    //Step 6: Position 70.00 
    printf("Press Enter to go to postion 70\n"); 

    CPhidgetAdvancedServo_setPosition (servo, 0, 70.00); 

    //Step 7: Position 80.00 
    printf("Press Enter to go to postion 80\n"); 

    CPhidgetAdvancedServo_setPosition (servo, 0, 80); 

    //Step 8: Disengage 
    printf("Press Enter to Disengage\n"); 

    CPhidgetAdvancedServo_setEngaged(servo, 0, 0); 

    printf("Press Enter to close program\n"); 

    printf("End of Program\n"); 
    CPhidget_close((CPhidgetHandle)servo);//close the phidget 
    CPhidget_delete((CPhidgetHandle)servo);//delete input data 

    return 0; 

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




Leave a Reply

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