Use double for storing time instead of float

Float precision may be not enough after some time of operating
This commit is contained in:
Oleg Kalachev 2025-01-12 19:58:36 +03:00
parent d4e04c46cd
commit 70f5186c1b
10 changed files with 16 additions and 14 deletions

View File

@ -6,7 +6,7 @@
The main loop is running at 1000 Hz. All the dataflow is happening through global variables (for simplicity):
* `t` *(float)* — current step time, *s*.
* `t` *(double)* — current step time, *s*.
* `dt` *(float)* — time delta between the current and previous steps, *s*.
* `gyro` *(Vector)* — data from the gyroscope, *rad/s*.
* `acc` *(Vector)* — acceleration data from the accelerometer, *m/s<sup>2</sup>*.

View File

@ -8,7 +8,7 @@
extern const int MOTOR_REAR_LEFT, MOTOR_REAR_RIGHT, MOTOR_FRONT_RIGHT, MOTOR_FRONT_LEFT;
extern float loopRate, dt;
extern float t;
extern double t;
extern int rollChannel, pitchChannel, throttleChannel, yawChannel, armedChannel, modeChannel;
const char* motd =

View File

@ -6,7 +6,7 @@
#define RC_LOSS_TIMEOUT 0.2
#define DESCEND_TIME 3.0 // time to descend from full throttle to zero
extern float controlsTime;
extern double controlsTime;
extern int rollChannel, pitchChannel, throttleChannel, yawChannel;
void failsafe() {

View File

@ -10,7 +10,7 @@
#define SERIAL_BAUDRATE 115200
#define WIFI_ENABLED 1
float t = NAN; // current step time, s
double t = NAN; // current step time, s
float dt; // time delta from previous step, s
int16_t channels[16]; // raw rc channels
float controls[16]; // normalized controls in range [-1..1] ([0..1] for throttle)

View File

@ -10,6 +10,7 @@
#define LOG_PERIOD 1.0 / LOG_RATE
#define LOG_SIZE LOG_DURATION * LOG_RATE
float tFloat;
Vector attitudeEuler;
Vector attitudeTargetEuler;
@ -19,7 +20,7 @@ struct LogEntry {
};
LogEntry logEntries[] = {
{"t", &t},
{"t", &tFloat},
{"rates.x", &rates.x},
{"rates.y", &rates.y},
{"rates.z", &rates.z},
@ -39,6 +40,7 @@ const int logColumns = sizeof(logEntries) / sizeof(logEntries[0]);
float logBuffer[LOG_SIZE][logColumns];
void prepareLogData() {
tFloat = t;
attitudeEuler = attitude.toEulerZYX();
attitudeTargetEuler = attitudeTarget.toEulerZYX();
}
@ -46,7 +48,7 @@ void prepareLogData() {
void logData() {
if (!armed) return;
static int logPointer = 0;
static float logTime = 0;
static double logTime = 0;
if (t - logTime < LOG_PERIOD) return;
logTime = t;

View File

@ -13,7 +13,7 @@
#define MAVLINK_CONTROL_SCALE 0.7f
#define MAVLINK_CONTROL_YAW_DEAD_ZONE 0.1f
extern float controlsTime;
extern double controlsTime;
extern int rollChannel, pitchChannel, throttleChannel, yawChannel, armedChannel, modeChannel;
void processMavlink() {
@ -22,8 +22,8 @@ void processMavlink() {
}
void sendMavlink() {
static float lastSlow = 0;
static float lastFast = 0;
static double lastSlow = 0;
static double lastFast = 0;
mavlink_message_t msg;
uint32_t time = t * 1000;

View File

@ -113,7 +113,7 @@ bool setParameter(const char *name, const float value) {
}
void syncParameters() {
static float lastSync = 0;
static double lastSync = 0;
if (t - lastSync < 1) return; // sync once per second
if (motorsActive()) return; // don't use flash while flying, it may cause a delay
lastSync = t;

View File

@ -14,7 +14,7 @@ int yawChannel = 3;
int armedChannel = 4;
int modeChannel = 5;
float controlsTime; // time of the last controls update
double controlsTime; // time of the last controls update
float channelNeutral[16] = {NAN}; // first element NAN means not calibrated
float channelMax[16];

View File

@ -6,7 +6,7 @@
float loopRate; // Hz
void step() {
float now = micros() / 1000000.0;
double now = micros() / 1000000.0;
dt = now - t;
t = now;
@ -18,7 +18,7 @@ void step() {
}
void computeLoopRate() {
static float windowStart = 0;
static double windowStart = 0;
static uint32_t rate = 0;
rate++;
if (t - windowStart >= 1) { // 1 second window

View File

@ -12,7 +12,7 @@
#define WIFI_ENABLED 1
float t = NAN;
double t = NAN;
float dt;
float motors[4];
int16_t channels[16]; // raw rc channels