2022-07-20 11:20:01 +08:00
|
|
|
#ifndef _UNITREE_ARM_TIMER_H_
|
|
|
|
#define _UNITREE_ARM_TIMER_H_
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
|
2022-11-16 16:23:36 +08:00
|
|
|
namespace UNITREE_ARM {
|
|
|
|
|
2022-07-20 11:20:01 +08:00
|
|
|
inline long long getSystemTime(){
|
|
|
|
struct timeval t;
|
|
|
|
gettimeofday(&t, NULL);
|
|
|
|
return 1000000 * t.tv_sec + t.tv_usec;
|
|
|
|
}
|
2022-11-16 16:23:36 +08:00
|
|
|
|
2022-07-20 11:20:01 +08:00
|
|
|
inline double getTimeSecond(){
|
|
|
|
double time = getSystemTime() * 0.000001;
|
|
|
|
return time;
|
|
|
|
}
|
2022-11-16 16:23:36 +08:00
|
|
|
|
2022-07-20 11:20:01 +08:00
|
|
|
inline void absoluteWait(long long startTime, long long waitTime){
|
|
|
|
if(getSystemTime() - startTime > waitTime){
|
|
|
|
std::cout << "[WARNING] The waitTime=" << waitTime << " of function absoluteWait is not enough!" << std::endl
|
|
|
|
<< "The program has already cost " << getSystemTime() - startTime << "us." << std::endl;
|
|
|
|
}
|
|
|
|
while(getSystemTime() - startTime < waitTime){
|
|
|
|
usleep(50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
waitTimeS = 0 means do not care time out
|
|
|
|
*/
|
|
|
|
class AbsoluteTimer{
|
|
|
|
public:
|
|
|
|
AbsoluteTimer(double waitTimeS);
|
|
|
|
~AbsoluteTimer();
|
|
|
|
void start();
|
|
|
|
bool wait();
|
|
|
|
private:
|
|
|
|
void _updateWaitTime(double waitTimeS);
|
|
|
|
int _timerFd;
|
|
|
|
uint64_t _missed;
|
|
|
|
double _waitTime;
|
|
|
|
double _startTime;
|
|
|
|
double _leftTime;
|
|
|
|
double _nextWaitTime;
|
|
|
|
itimerspec _timerSpec;
|
|
|
|
};
|
2022-11-16 16:23:36 +08:00
|
|
|
}
|
2022-07-20 11:20:01 +08:00
|
|
|
#endif
|