Куча всего.

Добавлена интерполяция по таблице датчиков
Структурирован проект в матлаб
This commit is contained in:
2025-11-14 18:03:44 +03:00
parent e4f05bdf6a
commit 2cdcebeffa
10 changed files with 172 additions and 60 deletions

View File

@@ -26,8 +26,61 @@
#include <windows.h>
#include <process.h>
#include <intrin.h>
#include "mex.h"
static unsigned long long get_timer_frequency(void)
{
#ifdef USE_CPU_TIMER
HKEY hKey;
DWORD frequency = 0;
DWORD size = sizeof(DWORD);
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
0, KEY_READ, &hKey) == ERROR_SUCCESS) {
if (RegQueryValueExA(hKey, "~MHz", NULL, NULL, (LPBYTE)&frequency, &size) == ERROR_SUCCESS) {
RegCloseKey(hKey);
return (unsigned long long)frequency * 1000000ULL; // MHz -> Hz
}
RegCloseKey(hKey);
}
#elif defined(USE_QPF_TIMER)
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return frequency.QuadPart;
#endif
}
static double ticksToNanoseconds(unsigned long long start, unsigned long long end)
{
#if defined(USE_CPU_TIMER) || defined(USE_QPF_TIMER)
unsigned long long elapsed = end - start;
unsigned long long frequency = get_timer_frequency();
return (double)elapsed * 1e9 / (double)frequency;
#endif
}
static void InitializeHighPrecisionTimer(void)
{
#ifdef USE_QPF_TIMER
QueryPerformanceFrequency(&hmcu.dTimer.Frequency);
hmcu.dTimer.TimerResolutionNs = 1e9 / (double)(hmcu.dTimer.Frequency);
#endif
}
static unsigned long long read_timer(void)
{
#ifdef USE_CPU_TIMER
return __rdtsc();
#elif defined(USE_QPF_TIMER)
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
return counter.QuadPart;
#endif
}
#define MDL_UPDATE ///< для подключения mdlUpdate()
/**
* @brief Update S-Function at every step of simulation
@@ -40,12 +93,32 @@
*/
static void mdlUpdate(SimStruct* S, int_T tid)
{
// Расчет периода (время между вызовами)
if (hmcu.dTimer.call_count > 0 && hmcu.dTimer.SFuncPrevTime != 0) {
hmcu.dSFuncPeriod = ticksToNanoseconds(
hmcu.dTimer.SFuncPrevTime,
hmcu.dTimer.SFuncStartTime) / 1000.0; // в микросекундах
}
// Сохраняем текущее время для следующего расчета периода
hmcu.dTimer.SFuncPrevTime = hmcu.dTimer.SFuncStartTime;
// Текущий вызов становится началом S-Function
hmcu.dTimer.SFuncStartTime = read_timer();
// get time of simulation
time_T TIME = ssGetT(S);
//---------------SIMULATE MCU---------------
// Измерение времени выполнения MCU_Step_Simulation
hmcu.dTimer.MCUStepStartTime = read_timer();
MCU_Step_Simulation(S, TIME); // SIMULATE MCU
hmcu.dTimer.MCUStepEndTime = read_timer();
//------------------------------------------
// Расчет времени выполнения в микросекундах
hmcu.dMCUStepTime = ticksToNanoseconds(
hmcu.dTimer.MCUStepStartTime,
hmcu.dTimer.MCUStepEndTime) / 1000.0;
}//end mdlUpdate
/**
@@ -59,6 +132,20 @@ static void mdlUpdate(SimStruct* S, int_T tid)
static void mdlOutputs(SimStruct* S, int_T tid)
{
SIM_writeOutputs(S);
// Измерение времени окончания выполнения mdlOutputs
hmcu.dTimer.SFuncEndTime = read_timer();
// Общее время выполнения S-Function в микросекундах
if (hmcu.dTimer.SFuncStartTime)
{
hmcu.dSFuncTime = ticksToNanoseconds(
hmcu.dTimer.SFuncStartTime,
hmcu.dTimer.SFuncEndTime) / 1000.0;
}
// Накопление статистики
hmcu.dTimer.call_count++;
}//end mdlOutputs
#define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
@@ -158,6 +245,9 @@ static void mdlInitializeSizes(SimStruct* S)
*/
static void mdlStart(SimStruct* S)
{
// Инициализация высокоточного таймера
InitializeHighPrecisionTimer();
SIM_Initialize_Simulation(S);
}
#endif // MDL_START