Store current time in float variable

This commit is contained in:
Oleg Kalachev 2023-05-26 16:23:30 +03:00
parent c5323f5723
commit c1aa0d9869
5 changed files with 31 additions and 35 deletions

View File

@ -53,7 +53,7 @@ static const struct Param {
{"yi", &yawRatePID.i, nullptr}, {"yi", &yawRatePID.i, nullptr},
{"yd", &yawRatePID.d, nullptr}, {"yd", &yawRatePID.d, nullptr},
{"ss", &stepsPerSecond, nullptr}, {"ss", &loopFreq, nullptr},
// {"m", &mode, nullptr}, // {"m", &mode, nullptr},
}; };

View File

@ -21,10 +21,9 @@
#define MOTOR_FRONT_RIGHT 2 #define MOTOR_FRONT_RIGHT 2
#define MOTOR_REAR_RIGHT 1 #define MOTOR_REAR_RIGHT 1
uint32_t stepTime; // current step time float t = NAN; // current step time, s
uint32_t steps; // total steps count float dt; // time delta from previous step, s
float stepsPerSecond; // steps per last second float loopFreq; // loop frequency, Hz
float dt; // time delta from previous step
uint16_t channels[16]; // raw rc channels uint16_t channels[16]; // raw rc channels
float controls[RC_CHANNELS]; // normalized controls in range [-1..1] ([0..1] for throttle) float controls[RC_CHANNELS]; // normalized controls in range [-1..1] ([0..1] for throttle)
uint32_t rcFailSafe, rcLostFrame; uint32_t rcFailSafe, rcLostFrame;

View File

@ -5,21 +5,22 @@
const int LOG_RATE = 100; const int LOG_RATE = 100;
const int LOG_DURATION = 10; const int LOG_DURATION = 10;
const int LOG_PERIOD = 1000000 / LOG_RATE; const float LOG_PERIOD = 1 / LOG_RATE;
const int LOG_SIZE = LOG_DURATION * LOG_RATE; const int LOG_SIZE = LOG_DURATION * LOG_RATE;
const int LOG_COLUMNS = 14; const int LOG_COLUMNS = 14;
static float logBuffer[LOG_SIZE][LOG_COLUMNS]; // * 4 (float) static float logBuffer[LOG_SIZE][LOG_COLUMNS]; // * 4 (float)
static int logPointer = 0; static int logPointer = 0;
static uint32_t lastLog = 0;
void logData() void logData()
{ {
if (!armed) return; if (!armed) return;
if (stepTime - lastLog < LOG_PERIOD) return;
lastLog = stepTime;
logBuffer[logPointer][0] = stepTime; static float logTime = 0;
if (t - logTime < LOG_PERIOD) return;
logTime = t;
logBuffer[logPointer][0] = t;
logBuffer[logPointer][1] = rates.x; logBuffer[logPointer][1] = rates.x;
logBuffer[logPointer][2] = rates.y; logBuffer[logPointer][2] = rates.y;
logBuffer[logPointer][3] = rates.z; logBuffer[logPointer][3] = rates.z;

View File

@ -1,28 +1,25 @@
// Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com> // Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com>
// Repository: https://github.com/okalachev/flix // Repository: https://github.com/okalachev/flix
const uint32_t MS = 1000; #define FREQ_WINDOW 1
const uint32_t S = 1000000;
static uint32_t stepsPerSecondCurrent; void step()
static uint32_t stepsPerSecondCurrentLast; {
float now = micros() / 1000000.0;
dt = now - t; // dt is NAN on first step
t = now;
void step() { computeLoopFreq();
steps++;
auto time = micros();
if (stepTime == 0) { // first step
stepTime = time;
} }
dt = (time - stepTime) / 1000000.0; void computeLoopFreq()
stepTime = time; {
static float windowStart = 0;
// compute steps per seconds static uint32_t freq = 0;
stepsPerSecondCurrent++; freq++;
if (time - stepsPerSecondCurrentLast >= 1000000) { if (t - windowStart >= FREQ_WINDOW) {
stepsPerSecond = stepsPerSecondCurrent; loopFreq = freq;
stepsPerSecondCurrent = 0; windowStart = t;
stepsPerSecondCurrentLast = time; freq = 0;
} }
} }

View File

@ -15,11 +15,9 @@
#define MOTOR_FRONT_RIGHT 2 #define MOTOR_FRONT_RIGHT 2
#define MOTOR_REAR_RIGHT 1 #define MOTOR_REAR_RIGHT 1
uint32_t startTime; float t;
uint32_t stepTime;
uint32_t steps;
float stepsPerSecond;
float dt; float dt;
float loopFreq;
float motors[4]; float motors[4];
int16_t channels[16]; // raw rc channels WARNING: unsigned on hardware int16_t channels[16]; // raw rc channels WARNING: unsigned on hardware
float controls[RC_CHANNELS]; float controls[RC_CHANNELS];
@ -27,7 +25,8 @@ Vector acc;
Vector rates; Vector rates;
Quaternion attitude; Quaternion attitude;
// control // declarations
void computeLoopFreq();
void control(); void control();
void interpretRC(); void interpretRC();
static void controlAttitude(); static void controlAttitude();