Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Problem with ClockPeriod(): (1 Item)
   
Problem with ClockPeriod()  
Hi
I am using a 800 MHz PIII SBC with QNXNeutrino6.3.2. I need 250 microseconds thread for which I am unable to program the
 clock with microseconds resolution
Though I programmed 10 microseconds, but the resolution is around 9.219 microseconds and the calculations go haywire 
with this amount of drift.
I have given the code below. Kindly help me.
Shyamala

/* Thread1 is expected to run every 250us ,thread2 every 500us and thread3 every 1sec and all the 3 threads should 
perennially run.*/

#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/neutrino.h>
#include <pthread.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/siginfo.h>
#include <sys/wait.h>
// diamond driver includes
#include "dscud.h"

float cosval[80],sinval[80];

DSCADSCAN adscan;
DSCB dscb; int i=0;
int j=0;
int j1=0;
int j2=0;
float vol;
void sampling(void);
void measurand(void);
extern float Vr1;
pthread_mutex_t mutex;
void *T1(void *ignored) {      struct timespec ts;
       struct itimerspec tsr;
    struct sched_param p;
   clock_gettime(CLOCK_REALTIME,&ts);

      p.sched_priority=20;
  pthread_setschedparam(pthread_self(),SCHED_FIFO,&p);
   while(1)
{
   pthread_mutex_lock(&mutex);
    sampling();
    pthread_mutex_unlock(&mutex);
   ts.tv_nsec =250000;
   ts.tv_sec =0;
clock_nanosleep(CLOCK_REALTIME,NULL,&ts,NULL);
   if (j==0) {dscDIOOutputBit(dscb,1,0,1);j=1;}
   else {dscDIOOutputBit(dscb,1,0,0);j=0;}
   }
return (void *)0;
}

void *T2(void *ignored)    // 1 msec Thread for calculatig the measurands and Protection functions
{      struct timespec ts1;
   clock_gettime(CLOCK_REALTIME,&ts1);
   while(1)
   {       measurand();
delay(1);
       if (j1==0) {dscDIOOutputBit(dscb,1,1,1);j1=1;}
       else {dscDIOOutputBit(dscb,1,1,0);
j1=0;}
   }
return (void *)0;
}


void *T3(void *ignored)  {   //FILE *fp1;
     while(1)
   {         sleep(5);
       printf("%f \n",Vr1);
      if (j2==0) {
dscDIOOutputBit(dscb,1,2,1);j2=1;}
      else {dscDIOOutputBit(dscb,1,2,0);j2=0;}

         }
     return (void *)0;
}

int main(void)
{  
   pthread_t thread1, thread2,thread3;
   void *thread_rc;
   pthread_attr_t attr1,attr2,attr3;
   struct _clockperiod hercules;
   DSCCB dsccb;
     DSCADSETTINGS adsettings;
   BYTE confbyte;
     dscInit(DSC_VERSION);
   memset(&dsccb, 0, sizeof(DSCCB));
   dsccb.base_address = 0x240;
   dsccb.int_level = 5;
   dscInitBoard(DSC_HERCEBX, &dsccb, &dscb);
     confbyte=0x1e;
   dscDIOSetConfig(dscb,&confbyte);
     for (i=0;i<80;i++) {
           cosval[i]=0.025*cos(i*0.078571429);sinval[i]=0.025*sin(i*0.078571429);
   }
         memset(&adsettings, 0, sizeof(DSCADSETTINGS));
       adsettings.range = RANGE_10;
       adsettings.polarity = BIPOLAR;
       adsettings.gain = GAIN_1;
       adsettings.load_cal = TRUE;
       adsettings.scan_interval = SCAN_INTERVAL_9;
       dscADSetSettings(dscb, &adsettings);
       memset(&adscan, 0, sizeof(DSCADSCAN));
       adscan.low_channel = 0;
       adscan.high_channel = 0;
           hercules.nsec=10000;
   hercules.fract = 0;
   ClockPeriod(CLOCK_REALTIME,&hercules,NULL,0);
 pthread_attr_init(&attr1);
    pthread_attr_setdetachstate(&attr1,PTHREAD_CREATE_DETACHED);
    pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&attr1,SCHED_FIFO);
    attr1.param.sched_priority=10;
       pthread_attr_init(&attr2);
    pthread_attr_setdetachstate(&attr2,PTHREAD_CREATE_DETACHED);
    pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&attr2,SCHED_RR);
    attr2.param.sched_priority=10;
       pthread_attr_init(&attr3);
    pthread_attr_setdetachstate(&attr3,PTHREAD_CREATE_DETACHED);
    pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);
   ...
View Full Message