Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAL_Linux: fixed time function to use integer maths #26104

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/AP_HAL_Linux/OpticalFlow_Onboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void OpticalFlow_Onboard::push_gyro(float gyro_x, float gyro_y, float dt)
_integrated_gyro.x += (gyro_x - _gyro_bias.x) * dt;
_integrated_gyro.y += (gyro_y - _gyro_bias.y) * dt;
sample.gyro = _integrated_gyro;
sample.time_us = 1.0e6 * (ts.tv_sec + (ts.tv_nsec*1.0e-9));
sample.time_us = ts.tv_sec*1000000ULL + ts.tv_nsec/1000ULL;

_gyro_ring_buffer->push(sample);
}
Expand Down
28 changes: 12 additions & 16 deletions libraries/AP_HAL_Linux/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
#include <AP_HAL/AP_HAL.h>
#include <AP_HAL/system.h>
#include <AP_HAL_Linux/Scheduler.h>
#include <AP_Math/div1000.h>

extern const AP_HAL::HAL& hal;

namespace AP_HAL {

static struct {
struct timespec start_time;
uint64_t start_time_ns;
} state;

static uint64_t ts_to_nsec(struct timespec &ts)
{
return ts.tv_sec*1000000000ULL + ts.tv_nsec;
}

void init()
{
clock_gettime(CLOCK_MONOTONIC, &state.start_time);
struct timespec ts {};
clock_gettime(CLOCK_MONOTONIC, &ts);
state.start_time_ns = ts_to_nsec(ts);
}

void WEAK panic(const char *errormsg, ...)
Expand Down Expand Up @@ -59,24 +67,12 @@ uint64_t micros64()

struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return 1.0e6*((ts.tv_sec + (ts.tv_nsec*1.0e-9)) -
(state.start_time.tv_sec +
(state.start_time.tv_nsec*1.0e-9)));
return uint64_div1000(ts_to_nsec(ts) - state.start_time_ns);
}

uint64_t millis64()
{
const Linux::Scheduler* scheduler = Linux::Scheduler::from(hal.scheduler);
uint64_t stopped_usec = scheduler->stopped_clock_usec();
if (stopped_usec) {
return stopped_usec / 1000;
}

struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return 1.0e3*((ts.tv_sec + (ts.tv_nsec*1.0e-9)) -
(state.start_time.tv_sec +
(state.start_time.tv_nsec*1.0e-9)));
return uint64_div1000(micros64());
}

} // namespace AP_HAL
20 changes: 12 additions & 8 deletions libraries/AP_HAL_SITL/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ using HALSITL::Scheduler;
namespace AP_HAL {

static struct {
struct timeval start_time;
uint64_t start_time_ns;
} state;

static uint64_t ts_to_nsec(struct timespec &ts)
{
return ts.tv_sec*1000000000ULL + ts.tv_nsec;
}

void init()
{
gettimeofday(&state.start_time, nullptr);
struct timespec ts {};
clock_gettime(CLOCK_MONOTONIC, &ts);
state.start_time_ns = ts_to_nsec(ts);
}

#if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(CYGWIN_BUILD)
Expand Down Expand Up @@ -170,12 +177,9 @@ uint64_t micros64()
return stopped_usec;
}

struct timeval tp;
gettimeofday(&tp, nullptr);
uint64_t ret = 1.0e6 * ((tp.tv_sec + (tp.tv_usec * 1.0e-6)) -
(state.start_time.tv_sec +
(state.start_time.tv_usec * 1.0e-6)));
return ret;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return uint64_div1000(ts_to_nsec(ts) - state.start_time_ns);
}

uint64_t millis64()
Expand Down
Loading