detach and attach continuously when testing with an iTouch

Supporting 10.7 and up
Kimo19

detach and attach continuously when testing with an iTouch

Postby Kimo19 » Mon Sep 20, 2010 9:42 am

Hi,

I have an application running on my iTouch (2nd gen) using Phidgets Library.
the application works fine and perfect with the web services when using the iPhone simulator. but when I try to test my application on my iTouch, it works not as expected and it keeps printing these errors continuously :

Phidget InterfaceKit 8/8/8 95392 detached!
Phidget Advanced Servo Controller 8-motor 88648 detached!
Phidget Advanced Servo Controller 8-motor 88648 attached!
Phidget InterfaceKit 8/8/8 95392 attached!
...
...
...

Could please help and check why I have this issue ?

Thank you,
Karim

Kimo19

Re: detach and attach continuously when testing with an iTou

Postby Kimo19 » Mon Sep 20, 2010 10:46 am

I want to add that when using InterfaceKit alone, it works fine,
the problem comes when using AdvancedServo (only when testing on the iTouch)

User avatar
Patrick
Lead Developer
Posts: 3098
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: detach and attach continuously when testing with an iTou

Postby Patrick » Mon Sep 20, 2010 11:11 am

It's hard to guess what could be happening without knowing what your code is doing.

-Patrick

Kimo19

Re: detach and attach continuously when testing with an iTou

Postby Kimo19 » Mon Sep 20, 2010 12:09 pm

Hi Patrick.

I tried to reproduce the error with the minimum code and this is what I have
This code will connect to AdvancedServo and then try to turn the servo continuously.

for the initialization and handler function, I didn't change the code example that Phidgets provides.
The problem occurs at the end of the "startRobot" function: when I try to turn the servo continuously from 50° to 170°.

Code: Select all

bugAppDelegate.m

static CPhidgetAdvancedServoHandle servo = 0;
static double ServoPos;


int 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 DetachHandler(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 ErrorHandler(CPhidgetHandle ADVSERVO, void *userptr, int ErrorCode, const char *Description)
{
   return 0;
}

int PositionChangeHandler(CPhidgetAdvancedServoHandle ADVSERVO, void *usrptr, int Index, double Value)
{
   return 0;
}


- (void)StartRobot{
   
   /*Advanced Servo Variables*/
   int Aresult;
   const char *Aerr;
   double minAccel, maxVel;
      
   //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, 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);

   CPhidget_openRemoteIP((CPhidgetHandle)servo,88648, "192.168.1.3", 5001, "phidgetdemo");
   //   CPhidget_openRemote((CPhidgetHandle)servo, 88648, "kimo", "demo");      
   //get the program to wait for an interface kit device to be attached
   NSLog(@"Waiting for servo to be attached....");
   if((Aresult = CPhidget_waitForAttachment((CPhidgetHandle)servo, 20000)))
   {
      CPhidget_getErrorDescription(Aresult, &Aerr);
      printf("Problem waiting for Advanced Servo attachment: %s\n", Aerr);
      return 0;
   }
   //read event data
   printf("servo: Reading.....\n");
   
   CPhidgetAdvancedServo_setEngaged(servo, 0, 1);
   
   CPhidgetAdvancedServo_getAccelerationMin(servo, 0, &minAccel);
   CPhidgetAdvancedServo_setAcceleration(servo, 0, minAccel*200);
   
   CPhidgetAdvancedServo_getVelocityMax(servo, 0, &maxVel);
   CPhidgetAdvancedServo_setVelocityLimit(servo, 0, maxVel);
   
   CPhidgetAdvancedServo_setPosition (servo, 0,110);
   CPhidgetAdvancedServo_getPosition(servo, 0, &ServoPos) ;
   
   while (ServoPos!=110) CPhidgetAdvancedServo_getPosition(servo, 0, &ServoPos) ;
      
   while (1) {
      CPhidgetAdvancedServo_setPosition (servo, 0,50);
      while (ServoPos!=50) CPhidgetAdvancedServo_getPosition(servo, 0, &ServoPos) ;
      CPhidgetAdvancedServo_setPosition (servo, 0,170);
      while (ServoPos!=170) CPhidgetAdvancedServo_getPosition(servo, 0, &ServoPos) ;
   }
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

    [window makeKeyAndVisible];
   [self StartRobot];
}




Again, with the simulator, it works fine. But when I try it on the device it keeps printing Detach and Attach
Thank you

Best Regards,
Karim

User avatar
Patrick
Lead Developer
Posts: 3098
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: detach and attach continuously when testing with an iTou

Postby Patrick » Mon Sep 20, 2010 12:27 pm

Well this may not be the problem, but you have coded busy loops that will eat up 100% CPU, and this may be lowering latency in the system to the point of detach. You need to add sleeps.

Also, it may be better to poll the stopped property instead of waiting for a specific position.

Code: Select all

...
   CPhidgetAdvancedServo_setPosition (servo, 0, 110);
   int stopped = FALSE;
   
      while (!stopped) {
      CPhidgetAdvancedServo_getStopped(servo, 0, &stopped);
      usleep(10000); //10ms sleep
   }
     
   while (1) {
      CPhidgetAdvancedServo_setPosition (servo, 0, 50);
      stopped = FALSE;
         while (!stopped) {
         CPhidgetAdvancedServo_getStopped(servo, 0, &stopped);
         usleep(10000); //10ms sleep
      }
      CPhidgetAdvancedServo_setPosition (servo, 0, 170);
      stopped = FALSE;
         while (!stopped) {
         CPhidgetAdvancedServo_getStopped(servo, 0, &stopped);
         usleep(10000); //10ms sleep
      }
   }
}

-Patrick

Kimo19

Re: detach and attach continuously when testing with an iTou

Postby Kimo19 » Mon Sep 20, 2010 3:12 pm

Thank you for the help, but the problem still persist.
even if I remove all the "while" statement (the code is just used to open remote connection) I still have the "attach" and "detach".
I also try to open AdvancedServo in a new thread. but still the same problem.

~Karim


Return to “macOS”

Who is online

Users browsing this forum: No registered users and 2 guests