Replace millis with esp timer

This commit is contained in:
Rune Harlyk
2025-10-09 17:49:36 +02:00
parent 12e1f80830
commit bc31b1b2dd
6 changed files with 32 additions and 26 deletions
+2 -1
View File
@@ -2,6 +2,7 @@
#define MotionService_h
#include <ArduinoJson.h>
#include "esp_timer.h"
#include <kinematics.h>
#include <peripherals/gesture.h>
@@ -61,7 +62,7 @@ class MotionService {
float dir[12] = {1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1};
unsigned long lastUpdate = millis();
int64_t lastUpdate = esp_timer_get_time();
};
#endif
+16 -14
View File
@@ -1,14 +1,16 @@
#ifndef TIMING_H
#define TIMING_H
#include "esp_timer.h"
#define CONCAT(a, b) a##b
#define UNIQUE_VAR(base) CONCAT(base, __LINE__)
#define EXECUTE_EVERY_N_MS(n, code) \
do { \
static volatile unsigned long UNIQUE_VAR(lastExecution_) = 0; \
unsigned long currentMillis = millis(); \
static volatile uint64_t UNIQUE_VAR(lastExecution_) = 0; \
uint64_t currentMillis = esp_timer_get_time() / 1000; \
if (UNIQUE_VAR(lastExecution_) == 0 || currentMillis - UNIQUE_VAR(lastExecution_) >= n) { \
code; \
UNIQUE_VAR(lastExecution_) = currentMillis; \
@@ -17,26 +19,26 @@
#define TIME_IT(code) \
{ \
uint32_t time_it_start = micros(); \
uint64_t time_it_start = esp_timer_get_time(); \
code; \
uint32_t time_it_elapsed = micros() - time_it_start; \
uint64_t time_it_elapsed = esp_timer_get_time() - time_it_start; \
if (time_it_elapsed < 1000) { \
ESP_LOGI("Time It", "Time elapsed: %lu microseconds", time_it_elapsed); \
ESP_LOGI("Time It", "Time elapsed: %llu microseconds", time_it_elapsed); \
} else if (time_it_elapsed < 1000000) { \
ESP_LOGI("Time It", "Time elapsed: %lu milliseconds", time_it_elapsed / 1000); \
ESP_LOGI("Time It", "Time elapsed: %llu milliseconds", time_it_elapsed / 1000); \
} else { \
ESP_LOGI("Time It", "Time elapsed: %.2f seconds", time_it_elapsed / 1000000.0); \
} \
}
#define CALLS_PER_SECOND(name) \
static unsigned long name##_count = 0; \
static unsigned long last_time = 0; \
name##_count++; \
if (millis() - last_time >= 1000) { \
Serial.printf("%s: %lu calls per second\n", #name, name##_count); \
name##_count = 0; \
last_time = millis(); \
#define CALLS_PER_SECOND(name) \
static uint64_t name##_count = 0; \
static uint64_t last_time = 0; \
name##_count++; \
if (esp_timer_get_time() / 1000 - last_time >= 1000) { \
ESP_LOGI("Calls", "%s: %llu calls per second", #name, name##_count); \
name##_count = 0; \
last_time = esp_timer_get_time() / 1000; \
}
#endif
+3 -2
View File
@@ -1,4 +1,5 @@
#include <ap_service.h>
#include "esp_timer.h"
#include <string>
static const char *TAG = "APService";
@@ -37,14 +38,14 @@ APNetworkStatus APService::getAPNetworkStatus() {
}
void APService::reconfigureAP() {
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
_lastManaged = esp_timer_get_time() / 1000 - MANAGE_NETWORK_DELAY;
_reconfigureAp = true;
_recoveryMode = false;
}
void APService::recoveryMode() {
ESP_LOGI(TAG, "Recovery Mode needed");
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
_lastManaged = esp_timer_get_time() / 1000 - MANAGE_NETWORK_DELAY;
_recoveryMode = true;
_reconfigureAp = true;
}
+2 -2
View File
@@ -63,8 +63,8 @@ void MotionService::handleGestures(const gesture_t ges) {
bool MotionService::update(Peripherals *peripherals) {
handleGestures(peripherals->takeGesture());
if (!state) return false;
unsigned long now = millis();
float dt = (now - lastUpdate) / 1000.0f;
int64_t now = esp_timer_get_time();
float dt = (now - lastUpdate) / 1000000.0f; // Convert microseconds to seconds
lastUpdate = now;
state->updateImuOffsets(peripherals->angleY(), peripherals->angleX());
state->step(body_state, dt);
+3 -2
View File
@@ -1,4 +1,5 @@
#include "system_service.h"
#include "esp_timer.h"
#include <string>
namespace system_service {
@@ -108,11 +109,11 @@ void status(JsonObject &root) {
root["fs_used"] = ESP_FS.usedBytes();
root["core_temp"] = temperatureRead();
root["cpu_reset_reason"] = resetReason(esp_reset_reason());
root["uptime"] = millis() / 1000;
root["uptime"] = esp_timer_get_time() / 1000000;
}
void metrics(JsonObject &root) {
root["uptime"] = millis() / 1000;
root["uptime"] = esp_timer_get_time() / 1000000;
root["free_heap"] = ESP.getFreeHeap();
root["total_heap"] = ESP.getHeapSize();
root["min_free_heap"] = ESP.getMinFreeHeap();
+6 -5
View File
@@ -1,5 +1,6 @@
#include <Arduino.h>
#include <unity.h>
#include "esp_timer.h"
#include "gait/state.h"
#include "gait/bezier_state.h"
@@ -10,17 +11,17 @@ void test_gaitPlanner_calculateStep_time() {
CommandMsg command = {0, 0, 0, 0, 0, 0, 0};
const int num_steps = 1000;
unsigned long start = millis();
uint64_t start = esp_timer_get_time() / 1000;
for (int i = 0; i < num_steps; i++) {
gaitPlanner.step(body_state, command, 0.02f);
}
unsigned long end = millis();
uint64_t end = esp_timer_get_time() / 1000;
unsigned long duration = end - start;
unsigned long max_duration = num_steps / 2; // Minimum 0.5 ms per step
uint64_t duration = end - start;
uint64_t max_duration = num_steps / 2; // Minimum 0.5 ms per step
char message[50];
snprintf(message, sizeof(message), "The step calculation took: %lu ms (%lu ms per iter)", duration,
snprintf(message, sizeof(message), "The step calculation took: %llu ms (%llu ms per iter)", duration,
duration / num_steps);
ESP_LOGI("Test planner", message);
TEST_ASSERT_MESSAGE(duration <= max_duration, message);