60fps Limit + GENERAL_DEFAULT_WIRED_MODE can be set in sdkconfig

This commit is contained in:
PhosphorosVR
2025-08-26 01:39:40 +02:00
parent 06394f9654
commit cb569c9e47
12 changed files with 108 additions and 57 deletions

View File

@@ -48,7 +48,7 @@ void CameraManager::setupCameraPinout()
ESP_LOGI(CAMERA_MANAGER_TAG, "CAM_BOARD");
#endif
#if CONFIG_GENERAL_WIRED_MODE
#if CONFIG_GENERAL_DEFAULT_WIRED_MODE
xclk_freq_hz = CONFIG_CAMERA_USB_XCLK_FREQ;
#endif
@@ -82,7 +82,7 @@ void CameraManager::setupCameraPinout()
.jpeg_quality = 7, // 0-63, for OV series camera sensors, lower number means higher quality // Below 6 stability problems
.fb_count = 2, // When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode.
.fb_location = CAMERA_FB_IN_DRAM,
.grab_mode = CAMERA_GRAB_WHEN_EMPTY,
.grab_mode = CAMERA_GRAB_LATEST, //CAMERA_GRAB_WHEN_EMPTY
};
}
@@ -196,7 +196,7 @@ bool CameraManager::setupCamera()
return false;
}
#if CONFIG_GENERAL_WIRED_MODE
#if CONFIG_GENERAL_DEFAULT_WIRED_MODE
const auto temp_sensor = esp_camera_sensor_get();
// Thanks to lick_it, we discovered that OV5640 likes to overheat when

View File

@@ -35,7 +35,17 @@ struct DeviceMode_t : BaseConfigModel
void load()
{
int stored_mode = this->pref->getInt("mode", 0);
// Default mode can be controlled via sdkconfig:
// - If CONFIG_GENERAL_DEFAULT_WIRED_MODE is enabled, default to UVC
// - Otherwise default to AUTO
int default_mode =
#if CONFIG_GENERAL_DEFAULT_WIRED_MODE
static_cast<int>(StreamingMode::UVC);
#else
static_cast<int>(StreamingMode::AUTO);
#endif
int stored_mode = this->pref->getInt("mode", default_mode);
this->mode = static_cast<StreamingMode>(stored_mode);
ESP_LOGI("DeviceMode", "Loaded device mode: %d", stored_mode);
}

View File

@@ -91,6 +91,31 @@ static uvc_fb_t *UVCStreamHelpers::camera_fb_get_cb(void *cb_ctx)
return nullptr;
}
//--------------------------------------------------------------------------------------------------------------
// Pace frames to exactly 60 fps (drop extras). Uses fixed-point accumulator
// to achieve an exact average of 60.000 fps without drifting.
static int64_t next_deadline_us = 0;
static int rem_acc = 0; // remainder accumulator for 1e6 % fps distribution
constexpr int target_fps = 60;
constexpr int64_t us_per_sec = 1000000LL;
constexpr int base_interval_us = us_per_sec / target_fps; // 16666
constexpr int rem_us = us_per_sec % target_fps; // 40
const int64_t now_us = esp_timer_get_time();
if (next_deadline_us == 0)
{
// First frame: allow immediately and schedule next slot from now
next_deadline_us = now_us;
}
if (now_us < next_deadline_us)
{
// Too early for next frame: drop this camera buffer
esp_camera_fb_return(s_fb.cam_fb_p);
s_fb.cam_fb_p = nullptr;
return nullptr;
}
//--------------------------------------------------------------------------------------------------------------
s_fb.uvc_fb.buf = s_fb.cam_fb_p->buf;
s_fb.uvc_fb.len = s_fb.cam_fb_p->len;
s_fb.uvc_fb.width = s_fb.cam_fb_p->width;
@@ -105,6 +130,20 @@ static uvc_fb_t *UVCStreamHelpers::camera_fb_get_cb(void *cb_ctx)
esp_camera_fb_return(s_fb.cam_fb_p);
return nullptr;
}
//--------------------------------------------------------------------------------------------------------------
// Schedule the next allowed frame time: base interval plus distributed remainder
rem_acc += rem_us;
int extra_us = 0;
if (rem_acc >= target_fps)
{
rem_acc -= target_fps;
extra_us = 1;
}
// Accumulate from the previous deadline to avoid drift; if we are badly late, catch up from now
const int64_t base_next = next_deadline_us + base_interval_us + extra_us;
next_deadline_us = (base_next < now_us) ? now_us : base_next;
//--------------------------------------------------------------------------------------------------------------
return &s_fb.uvc_fb;
}
@@ -119,7 +158,7 @@ static void UVCStreamHelpers::camera_fb_return_cb(uvc_fb_t *fb, void *cb_ctx)
esp_err_t UVCStreamManager::setup()
{
#ifndef CONFIG_GENERAL_WIRED_MODE
#ifndef CONFIG_GENERAL_DEFAULT_WIRED_MODE
ESP_LOGE(UVC_STREAM_TAG, "The board does not support UVC, please, setup WiFi connection.");
return ESP_FAIL;
#endif