Arduino-FOC icon indicating copy to clipboard operation
Arduino-FOC copied to clipboard

[BUG] Velocity doesnt go to 0 when stationary

Open Quenom opened this issue 6 months ago • 3 comments

Describe the bug when motor is stationary velocity doesnt go to 0

SC60228 sensor pipico

Platformio

seems like this code is to blame

if (fabsf(delta_angle) > 1e-8f) {
    velocity = delta_angle / Ts;

    vel_angle_prev = angle_prev;
    vel_full_rotations = full_rotations;
    vel_angle_prev_ts = angle_prev_ts;
}

return velocity;

in Sensor.cpp:GetVelocity()

Quenom avatar May 13 '26 17:05 Quenom

Indeed, it should have else velocity = 0;

dekutree64 avatar May 23 '26 18:05 dekutree64

I can confirm that this is an important error that we have in the library!

askuric avatar May 27 '26 14:05 askuric

Host regression validation of the existing fix, rather than another production patch:

I checked master b3d8f3931342ae68a980d0140e24b9048eba098b and dev ae7b3add93b7ddb863cbb21ed31aae44330ae216. The two commits already linked here (5d1005c and 9608741) are present on dev. The PR template also explicitly requires dev as the target branch.

A host suite compiling the actual Sensor.cpp with a fake sensor and mocked Arduino clock gives:

Implementation Result
Unmodified master 6 of 10 checks fail
Master with only a zero-velocity else branch 3 of 10 checks fail
Current dev, unchanged All 10 checks pass

The checks cover moving, stopping, continued stationary samples, restart, minimum elapsed time, reversal, sub-threshold movement and angle wrap. They explicitly exercise 9608741's decision to advance the reference sample even when velocity is zero. For example, 0.01 rad at 1 ms, unchanged positions at 2/3 ms, then 0.03 rad at 4 ms produces 20 rad/s on dev; retaining the old reference gives approximately 6.67 rad/s instead.

The low-motion check also distinguishes an advancing baseline from an accumulated sub-threshold baseline. This records dev's current behavior, rather than claiming that a host fake sensor establishes the best estimator for every quantized physical encoder.

The inline reproduction below contains the C++11 test and minimal Arduino timing shim. Compiler used: Zig 0.14.1 C++, Windows x86-64, -O2 -Wall -Wextra -Werror -std=c++11. No MCU build, physical sensor/closed-loop validation or 32-bit timestamp-wrap testing is claimed. AI assistance was used for source analysis and test preparation; the builds and before/after comparisons were executed locally.

Would a regression-only PR targeting dev be useful, or is there a preferred host-test location/framework? The source fix is already there; this would preserve coverage of the stop/restart behavior.

Reproducible host test

Save the following as tests/host/Arduino.h:

#pragma once

#include <cmath>
#include <cstdlib>

using std::abs;

void delay(unsigned long ms);
void delayMicroseconds(unsigned int us);

Save as tests/host/sensor_velocity.cpp:

#include <cmath>
#include <cstdio>

#include "common/base_classes/Sensor.h"

static unsigned long clock_us;

unsigned long _micros() { return clock_us; }
void delay(unsigned long ms) { clock_us += ms * 1000; }
void delayMicroseconds(unsigned int us) { clock_us += us; }

class FakeSensor : public Sensor {
public:
    float position = 0.0f;
    float getSensorAngle() override { return position; }
    void sample(float angle, unsigned long timestamp) {
        position = angle;
        clock_us = timestamp;
        update();
    }
};

static unsigned int failures;
static void expect(const char *name, float actual, float expected) {
    const float tolerance = 1e-7f + 1e-4f * std::fabs(expected);
    if (!std::isfinite(actual) || std::fabs(actual - expected) > tolerance) {
        std::printf("FAIL %s: got %.9g, expected %.9g\n", name, actual, expected);
        failures++;
    }
}

int main() {
    FakeSensor sensor;
    sensor.sample(0.01f, 1000);
    expect("moving", sensor.getVelocity(), 10.0f);
    sensor.sample(0.01f, 2000);
    expect("stopped", sensor.getVelocity(), 0.0f);
    sensor.sample(0.01f, 3000);
    expect("still stopped", sensor.getVelocity(), 0.0f);
    sensor.sample(0.03f, 4000);
    expect("resume uses latest sample interval", sensor.getVelocity(), 20.0f);
    sensor.sample(0.0301f, 4050);
    expect("minimum sample interval", sensor.getVelocity(), 20.0f);
    sensor.sample(0.02f, 5000);
    expect("direction reversal", sensor.getVelocity(), -10.0f);

    FakeSensor slow;
    slow.min_elapsed_time = 0.0f;
    slow.sample(0.001f, 1000);
    expect("slow initial movement", slow.getVelocity(), 1.0f);
    const float small_step = 0.001f + 5e-9f;
    slow.sample(small_step, 2000);
    expect("subthreshold movement", slow.getVelocity(), 0.0f);
    slow.sample(0.001f + 2e-8f, 3000);
    expect("subthreshold sample advances baseline", slow.getVelocity(),
           (slow.position - small_step) / 0.001f);

    FakeSensor wrap;
    wrap.sample(6.28f, 1000);
    wrap.getVelocity();
    wrap.sample(0.01f, 2000);
    expect("angle wrap", wrap.getVelocity(),
           (6.28318530718f + 0.01f - 6.28f) / 0.001f);
    std::printf("%s: 10 sensor velocity checks\n", failures ? "FAIL" : "PASS");
    return failures ? 1 : 0;
}

From the repository root, build and run:

zig c++ -O2 -Wall -Wextra -Werror -std=c++11 -Itests/host -Isrc tests/host/sensor_velocity.cpp src/common/base_classes/Sensor.cpp -o sensor-test.exe
./sensor-test.exe

pacatomic avatar Nov 02 '26 11:11 pacatomic