Skip to content

[SOLVED][BUG] Issue with more than one HallSensor instance rotating in different directions #324

Closed
@Everymann

Description

@Everymann

hello there,

I'll briefly explain the problem and how you can fix it.

[Problem]
If more than one HallSensor is used, all modes that use getVelocity() from HallSensor go crazy.

You can check it if the test from the examples/utils/sensor_test/hall_sensors folder is used and at least two HallSensors are initialized. If you turn the motors in the opposite direction of rotation (from the sensors point of view), you will very often get a value of zero in the sensor.getVelocity() Print.

[Reason]
in a line of the HallSensor.cpp file the variable old_direction is created as static. This passes the same value to all HallSensor instances.

[FIX]
You can fix the problem easily by deleting line 56

int8_t new_electric_sector = ELECTRIC_SECTORS[hall_state];
static Direction old_direction;
if (new_electric_sector - electric_sector > 3) {

  static Direction old_direction;

from the function void HallSensor::updateState() in the src/sensors/HallSensor.cpp file.

After this adding the following line in HallSensor.h to the class HallSensor in file src/sensors/HallSensor.h:

    Direction old_direction;

add it to line 65 for example

// whether last step was CW (+1) or CCW (-1).
Direction direction;

now the variable old_direction is used for its own instance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions