Skip to content

Improvements to SmoothingSensor and LinearHall #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/encoders/smoothing/smoothing.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
*
* Hall sensor velocity motion control example, modified to demonstrate usage of SmoothingSensor
* Hall sensor velocity motion control example, modified to demonstrate usage of SmoothingSensor.
* The only changes are the declaration of the SmoothingSensor, passing it to motor.linkSensor
* instead of the HallSensor instance, and the added Commander code to switch between the two.
*
* Steps:
* 1) Configure the motor and sensor
* 2) Run the code
Expand Down Expand Up @@ -63,8 +66,6 @@ void setup() {
sensor.enableInterrupts(doA, doB); //, doC);
// software interrupts
PciManager.registerListener(&listenerIndex);
// set SmoothingSensor phase correction for hall sensors
smooth.phase_correction = -_PI_6;
// link the SmoothingSensor to the motor
motor.linkSensor(&smooth);

Expand Down
4 changes: 3 additions & 1 deletion src/encoders/linearhall/LinearHall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ LinearHall::LinearHall(int _hallA, int _hallB, int _pp){
pinA = _hallA;
pinB = _hallB;
pp = _pp;
electrical_rev = 0;
prev_reading = 0;
}

float LinearHall::getSensorAngle() {
Expand Down Expand Up @@ -84,7 +86,7 @@ void LinearHall::init(FOCMotor *motor) {
// move one mechanical revolution forward
for (int i = 0; i <= 2000; i++)
{
float angle = _3PI_2 + _2PI * i * pp / 2000.0f;
float angle = _3PI_2 + _2PI * i * motor->pole_pairs / pp / 2000.0f;
motor->setPhaseVoltage(motor->voltage_sensor_align, 0, angle);

ReadLinearHalls(pinA, pinB, &lastA, &lastB);
Expand Down
8 changes: 6 additions & 2 deletions src/encoders/smoothing/SmoothingSensor.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include "SmoothingSensor.h"
#include "common/foc_utils.h"
#include "common/time_utils.h"
#include "sensors/HallSensor.h"


SmoothingSensor::SmoothingSensor(Sensor& s, const FOCMotor& m) : _wrapped(s), _motor(m)
{
}

SmoothingSensor::SmoothingSensor(HallSensor& s, const FOCMotor& m) : _wrapped(s), _motor(m) {
phase_correction = -_PI_6;
}

void SmoothingSensor::update() {
// Update sensor, with optional downsampling of update rate
Expand All @@ -27,8 +31,8 @@ void SmoothingSensor::update() {

// Apply phase correction if needed
if (phase_correction != 0) {
if (_motor.shaft_velocity < -0) angle_prev -= _motor.sensor_direction * phase_correction / _motor.pole_pairs;
else if (_motor.shaft_velocity > 0) angle_prev += _motor.sensor_direction * phase_correction / _motor.pole_pairs;
if (_motor.shaft_velocity < -0.001) angle_prev -= _motor.sensor_direction * phase_correction / _motor.pole_pairs;
else if (_motor.shaft_velocity > 0.001) angle_prev += _motor.sensor_direction * phase_correction / _motor.pole_pairs;
}

// Handle wraparound of the projected angle
Expand Down
1 change: 1 addition & 0 deletions src/encoders/smoothing/SmoothingSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SmoothingSensor : public Sensor
@param m Motor that the SmoothingSensor will be linked to
*/
SmoothingSensor(Sensor& s, const FOCMotor& m);
SmoothingSensor(class HallSensor& s, const FOCMotor& m); // Automatically sets phase_correction

void update() override;
float getVelocity() override;
Expand Down
Loading