common: C++ RateKeeper (#29374)

* c++ RateKeeper

* add to files_common

* use util::random_int

* improve monotor_time

* remove ~ratekeeper
old-commit-hash: 3eef63af9b4f4a25bc4c3a0d6ad450bba9f62205
This commit is contained in:
Dean Lee
2023-08-21 12:27:02 +08:00
committed by GitHub
parent bd27be2b78
commit 97631ec362
10 changed files with 97 additions and 23 deletions
+4 -1
View File
@@ -1,6 +1,7 @@
#include <sys/resource.h>
#include "common/ratekeeper.h"
#include "common/util.h"
#include "system/proclogd/proclog.h"
@@ -9,13 +10,15 @@ ExitHandler do_exit;
int main(int argc, char **argv) {
setpriority(PRIO_PROCESS, 0, -15);
RateKeeper rk("proclogd", 0.5);
PubMaster publisher({"procLog"});
while (!do_exit) {
MessageBuilder msg;
buildProcLogMessage(msg);
publisher.send("procLog", msg);
util::sleep_for(2000); // 2 secs
rk.keepTime();
}
return 0;
+4 -4
View File
@@ -9,6 +9,7 @@
#include "cereal/messaging/messaging.h"
#include "common/i2c.h"
#include "common/ratekeeper.h"
#include "common/swaglog.h"
#include "common/timing.h"
#include "common/util.h"
@@ -173,10 +174,10 @@ int sensor_loop(I2CBus *i2c_bus_imu) {
std::thread lsm_interrupt_thread(&interrupt_loop, std::ref(lsm_interrupt_sensors),
std::ref(sensor_service));
RateKeeper rk("sensord", 100);
// polling loop for non interrupt handled sensors
while (!do_exit) {
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
for (Sensor *sensor : sensors) {
MessageBuilder msg;
if (!sensor->get_event(msg)) {
@@ -190,8 +191,7 @@ int sensor_loop(I2CBus *i2c_bus_imu) {
pm_non_int.send(sensor_service[sensor].c_str(), msg);
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::milliseconds(10) - (end - begin));
rk.keepTime();
}
for (Sensor *sensor : sensors) {