Products for USB Sensing and Control
It is currently Wed Jun 19, 2013 9:50 am

All times are UTC - 7 hours [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Fri Apr 27, 2012 9:58 am 
Offline
Phidgetsian

Joined: Fri Apr 27, 2012 9:53 am
Posts: 5
Will not attach attachment, which is the encoder. Encoder works and measures angle and duration in Phidgets GUI. OS used:

Mac OS X 10.6.8 (10K549)
Kernel Version: Darwin 10.8.0

Having trouble getting this code to run, getting error:

"Problem waiting for attachment: Given timeout has been exceeded."


Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#include <phidget21.h>

#define CONTROL_RATE 8 // ms
#define SENSOR_RATE 8 //ms
#define START_DELAY 500 //ms
#define PI 3.14159

long ustarttime = 0;
int running = 0;
int spring=6;
int mode;
double theta = 0;

void *startControlThread();

int msleep(long milisec)
{
   if(milisec > 0)
   {
      struct timespec req={0};
      time_t sec=(int)(milisec/1000);
      milisec=milisec-(sec*1000);
      req.tv_sec=sec;
      req.tv_nsec=milisec*1000000L;
      while(nanosleep(&req,&req)==-1)
         continue;
   }
   return 1;
}

int microsleep(long microsec)
{
   if(microsec > 0)
   {
      struct timespec req={0};
      time_t sec=(int)(microsec/1000000);
      microsec=microsec-(sec*1000000);
      req.tv_sec=sec;
      req.tv_nsec=microsec*1000;
      while(nanosleep(&req,&req)==-1)
         continue;
   }
   return 1;
}

long getutime() // get time in microseconds
{
   struct timeval tv;
   gettimeofday(&tv, 0);
   return( (long)(tv.tv_sec * 1000000 + tv.tv_usec)-ustarttime );
}
long getmtime() // get time in milliseconds
{
   return( round(getutime()/1000) );
}
long getstime() // get time in seconds
{
   return( round(getutime()/1000000) );
}

int ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
{
   printf("Error handled. %d - %s", ErrorCode, unknown);
   return 0;
}


int main(int argc, char* argv[])
{
   
   char keypress;
   
   printf("Put wheel in zero position and press any key.\n");
   printf("define the mode you want: f-force feeback,  .....");
   keypress = getchar();   // add code to define mode here
   
   if ( keypress== 'z' || keypress == 'Z')
   {
      mode=spring;
   }
   
    /*---------------------------------------------------------------------------------------------*/
   /*-------------------------------------- Spawn Control Thread ---------------------------------*/
   /*---------------------------------------------------------------------------------------------*/
   
   pthread_t control_thread_id;
   pthread_attr_t control_attr;
   struct sched_param control_param;
   
   pthread_attr_init(&control_attr);
   pthread_attr_setinheritsched(&control_attr, PTHREAD_EXPLICIT_SCHED);
   pthread_create(&control_thread_id, &control_attr, startControlThread, NULL);
   
   while(keypress != 'q' & keypress != 'Q')
   {
      msleep(5);
      keypress = getchar();
   }
   running = 0;
   sleep(1);
   
   return 0;
}

void *startControlThread(){
   int result;
   const char *err;
   
    printf("here 1\n");
   
   // setup motor controller
   CPhidgetMotorControlHandle motoControl = 0;
   CPhidgetMotorControl_create(&motoControl);
   CPhidget_open((CPhidgetHandle)motoControl, -1);   
   printf("here 2\n");
    // setup encoder
   CPhidgetEncoderHandle encoder = 0;
   CPhidgetEncoder_create(&encoder);
   CPhidget_open((CPhidgetHandle)encoder, 1);
   printf("here 3\n");
   //   if((result = CPhidget_waitForAttachment((CPhidgetHandle)motoControl, 10000)))
   //   {
   //      CPhidget_getErrorDescription(result, &err);
   //      printf("Problem waiting for attachment: %s\n", err);
   //      return 0;
   //   }
   printf("here 4\n");
   if((result = CPhidget_waitForAttachment((CPhidgetHandle)encoder, 10000)))
   {
      CPhidget_getErrorDescription(result, &err);
      printf("Problem waiting for attachment: %s\n", err);
      return 0;   
   }   printf("here 5\n");
   
   int output;
   CPhidgetEncoder_getPosition(encoder, 0, &output);
   
   CPhidgetMotorControl_setAcceleration (motoControl, 0, 100.00);
   CPhidgetMotorControl_setVelocity (motoControl, 0, 0.00);
   
   struct sched_param sp;
   sp.sched_priority=63;
   if (pthread_setschedparam(pthread_self(), SCHED_RR, &sp)  == -1) {
      printf("Failed to change priority.\n");
   }
   FILE *file_c;
   file_c = fopen("haptics_cursor.txt", "w");   
   long startTime = getutime() + START_DELAY*1000;
   double torque;
   
   double rate = 1.46;
   printf("\n\n\n");
   double theta_error = 0.0;
   double theta_error_prev = 0.0;
   double Kp;
   double theta = 0.0;
   double theta_prev = 0.0;
   double theta_give = 0.0;
   
    printf("here");
   
   
   while(running)
   {
      // sleep until the next startTime following this point
      while(startTime < getutime())
      {
         startTime = startTime+CONTROL_RATE*1000;
      }
      microsleep(startTime - getutime());
        printf("Mode = %d",mode);   
         
      if(mode == spring)
      {
         CPhidgetMotorControl_setAcceleration (motoControl, 0, 100.00);
         CPhidgetEncoder_getPosition(encoder, 0, &output);
         theta_prev=theta;
         theta = -output/1441.8*360.0;      
         theta_give=0;
         theta_error = (theta_give - theta);
         Kp =.75;
         torque =  Kp * theta_error;
         if (torque>100)
         {
            torque=100;
         }
         if (torque<-100)
         {
            torque=-100;
         }
         fprintf(file_c,"%2.1f\t %2.1f %2.1f %2.1f\n",theta_give,theta,theta_error,startTime/1000000.0);
         CPhidgetMotorControl_setVelocity (motoControl, 0, torque);
      }
      
   }

      CPhidgetMotorControl_setAcceleration (motoControl, 0, 0);
      CPhidgetMotorControl_setVelocity (motoControl, 0, 0);
      //fprintf(file_c,"%2.1f\t %2.1f %2.1f\n",theta_give,theta,theta_error);   
      fclose(file_c);
      printf("\n\nControl loop stopping\n");
   }


Top
 Profile Send private message  
 
PostPosted: Mon Apr 30, 2012 7:33 am 
Offline
Human-Cyborg Relations
User avatar

Joined: Tue Sep 27, 2011 2:37 pm
Posts: 333
Location: Calgary
Which motor controller are you using, the 1064 or 1065? And which encoder interface are you using?

_________________
Brian Burley
403-282-7335 ext. 6003
support@phidgets.com


Top
 Profile Send private message  
 
PostPosted: Mon Apr 30, 2012 10:27 am 
Offline
Phidgetsian

Joined: Fri Apr 27, 2012 9:53 am
Posts: 5
Thanks so much for you response... here is what we are using :

http://www.phidgets.com/products.php?ca ... _id=3264_0

http://www.phidgets.com/products.php?product_id=1057_0


Top
 Profile Send private message  
 
PostPosted: Tue May 01, 2012 7:23 am 
Offline
Human-Cyborg Relations
User avatar

Joined: Tue Sep 27, 2011 2:37 pm
Posts: 333
Location: Calgary
So you are using the 1057 to read to encoder, but what about the controller for the motor?

_________________
Brian Burley
403-282-7335 ext. 6003
support@phidgets.com


Top
 Profile Send private message  
 
PostPosted: Tue May 01, 2012 11:39 am 
Offline
Phidgetsian

Joined: Fri Apr 27, 2012 9:53 am
Posts: 5
Sorry about that, this is it...
http://www.phidgets.com/products.php?ca ... _id=1064_1


Top
 Profile Send private message  
 
PostPosted: Tue May 01, 2012 11:41 am 
Offline
Phidgetsian

Joined: Fri Apr 27, 2012 9:53 am
Posts: 5
We are trying to write a code for a haptic steering wheel. We want the wheel to turn in the opposite direction of turn when the wheel reaches +- 30 degrees from centerline(straight ahead). I think it should be a simple program, but we are not very good at programming. THANKS!


Top
 Profile Send private message  
 
PostPosted: Tue May 01, 2012 2:34 pm 
Offline
Human-Cyborg Relations
User avatar

Joined: Tue Sep 27, 2011 2:37 pm
Posts: 333
Location: Calgary
Does the device attach and function normally under the Phidget Control Panel application?

_________________
Brian Burley
403-282-7335 ext. 6003
support@phidgets.com


Top
 Profile Send private message  
 
PostPosted: Tue May 01, 2012 3:15 pm 
Offline
Phidgetsian

Joined: Fri Apr 27, 2012 9:53 am
Posts: 5
It shows activity under systems prefs on the mac.... Will emailing you be a better form of communication? This is due in 2 days and really need some advice. thanks.


Top
 Profile Send private message  
 
PostPosted: Wed May 02, 2012 7:59 am 
Offline
Human-Cyborg Relations
User avatar

Joined: Tue Sep 27, 2011 2:37 pm
Posts: 333
Location: Calgary
Oh. I see. You didn't use -1 as the serial number you used +1. I am assuming your 1057 doesn't have a serial number of 1.

_________________
Brian Burley
403-282-7335 ext. 6003
support@phidgets.com


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 7 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group