element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
BeagleBoard
  • Products
  • Dev Tools
  • Single-Board Computers
  • BeagleBoard
  • More
  • Cancel
BeagleBoard
Forum Get zero for clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join BeagleBoard to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 2 replies
  • Subscribers 101 subscribers
  • Views 782 views
  • Users 0 members are here
  • bbb
Related

Get zero for clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);

chrischristian14
chrischristian14 over 6 years ago

Hi,

I am using clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); function in my code on Ubuntu and it gives me correct time but the same code compiled for Pocketbeagle returns 0.

Any ideas ? I am making a PID controller and I need to know "last time" for I and D calculations.

Thanks

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 6 years ago in reply to chrischristian14 +2 verified
    Hi Chris, I've not tried it on BBB or PB, but I've used this code in the past on other machines.. not saying it is great code, but it worked at the time: #include <stdlib.h> #include <time.h> struct timespec…
  • chrischristian14
    0 chrischristian14 over 6 years ago

    Ok, I think that was a very poor explanation, please see the below code for the PID I am using , I get zero for millis() function on pocketbeagle

     

    #include <stdio.h>
    #include <time.h>
    #include <unistd.h>
    
    
    /*working variables*/
    unsigned long lastTime;
    double Input, Output, Setpoint; // Setpoint = desired pressure, Ouput is PWM value, Input is measured current value
    double errSum, lastErr;
    double kp, ki, kd;
    struct timespec start, stop;
    
    
    double millis()
    {
    double result = (stop.tv_sec - start.tv_sec) * 1e6 + (stop.tv_nsec - start.tv_nsec) / 1e3;    // in microseconds
    return result;
    }
    void Compute()
    {
      clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
       /*How long since we last calculated*/
       unsigned long now = millis();
       double timeChange = (double)(now - lastTime);
       printf("now is : %lu\n",now);
       printf("time change is : %f\n",timeChange);
    
       /*Compute all the working error variables*/
       double error = Setpoint - Input;  // Proportional error
       errSum += (error * timeChange); // Integral error
       double dErr = (error - lastErr) / timeChange; // Differential error
    
       /*Compute PID Output*/
       Output = kp * error + ki * errSum + kd * dErr;
    
       /*Remember some variables for next time*/
       lastErr = error;
       lastTime = now;
       clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
    }
    
    void SetTunings(double Kp, double Ki, double Kd)
    {
       kp = Kp;
       ki = Ki;
       kd = Kd;
    }
    
    
    int main()
    {
    printf("time diff is : %f\n",millis());
    while(1)
    {
    sleep(3);
    Compute();
    printf("time diff is : %f\n",millis());
    sleep(3);
    }
    return 0;
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 6 years ago in reply to chrischristian14

    Hi Chris,

     

    I've not tried it on BBB or PB, but I've used this code in the past on other machines.. not saying it is great code, but it worked at the time:

     

    #include <stdlib.h>
    #include <time.h>
    
    struct timespec ts_zero;
    
    void
    get_tzero(void)
    {
      clock_gettime(CLOCK_MONOTONIC, &ts_zero);
    }
    
    long
    get_time(void)
    {
      long nsec, sec;
      struct timespec tts;
      clock_gettime(CLOCK_MONOTONIC, &tts);
      sec=tts.tv_sec-ts_zero.tv_sec;
      nsec=tts.tv_nsec-ts_zero.tv_nsec;
      if (sec>0)
      {
        nsec=nsec+1E9; // we don't expect times greater than 1 sec
      }
      ts_zero=tts;
      return(nsec);
    }
    
    in your code:
    execute get_tzero() at the start of the event that you wish to time
    At the end of the event, use:
    long nsec;
    nsec=get_time();
    nsec will contain the duration of the event in nanosec.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube