Skip to content

Commit

Permalink
Use double for storing time instead of float
Browse files Browse the repository at this point in the history
Float precision may be not enough after some time of operating
  • Loading branch information
okalachev committed Jan 12, 2025
1 parent d4e04c4 commit 70f5186
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/firmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>*.
Expand Down
2 changes: 1 addition & 1 deletion flix/cli.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion flix/failsafe.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion flix/flix.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions flix/log.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LOG_PERIOD 1.0 / LOG_RATE
#define LOG_SIZE LOG_DURATION * LOG_RATE

float tFloat;
Vector attitudeEuler;
Vector attitudeTargetEuler;

Expand All @@ -19,7 +20,7 @@ struct LogEntry {
};

LogEntry logEntries[] = {
{"t", &t},
{"t", &tFloat},
{"rates.x", &rates.x},
{"rates.y", &rates.y},
{"rates.z", &rates.z},
Expand All @@ -39,14 +40,15 @@ const int logColumns = sizeof(logEntries) / sizeof(logEntries[0]);
float logBuffer[LOG_SIZE][logColumns];

void prepareLogData() {
tFloat = t;
attitudeEuler = attitude.toEulerZYX();
attitudeTargetEuler = attitudeTarget.toEulerZYX();
}

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;

Expand Down
6 changes: 3 additions & 3 deletions flix/mavlink.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion flix/parameters.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion flix/rc.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
4 changes: 2 additions & 2 deletions flix/time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
float loopRate; // Hz

void step() {
float now = micros() / 1000000.0;
double now = micros() / 1000000.0;
dt = now - t;
t = now;

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gazebo/flix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70f5186

Please sign in to comment.