Skip to content

Commit 6d96569

Browse files
Merge pull request #53 from dekutree64/dev
Improvements to SmoothingSensor and LinearHall
2 parents 5ed156f + 8818ad9 commit 6d96569

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

examples/encoders/smoothing/smoothing.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/**
22
*
3-
* Hall sensor velocity motion control example, modified to demonstrate usage of SmoothingSensor
3+
* Hall sensor velocity motion control example, modified to demonstrate usage of SmoothingSensor.
4+
* The only changes are the declaration of the SmoothingSensor, passing it to motor.linkSensor
5+
* instead of the HallSensor instance, and the added Commander code to switch between the two.
6+
*
47
* Steps:
58
* 1) Configure the motor and sensor
69
* 2) Run the code
@@ -63,8 +66,6 @@ void setup() {
6366
sensor.enableInterrupts(doA, doB); //, doC);
6467
// software interrupts
6568
PciManager.registerListener(&listenerIndex);
66-
// set SmoothingSensor phase correction for hall sensors
67-
smooth.phase_correction = -_PI_6;
6869
// link the SmoothingSensor to the motor
6970
motor.linkSensor(&smooth);
7071

src/encoders/linearhall/LinearHall.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ LinearHall::LinearHall(int _hallA, int _hallB, int _pp){
1313
pinA = _hallA;
1414
pinB = _hallB;
1515
pp = _pp;
16+
electrical_rev = 0;
17+
prev_reading = 0;
1618
}
1719

1820
float LinearHall::getSensorAngle() {
@@ -84,7 +86,7 @@ void LinearHall::init(FOCMotor *motor) {
8486
// move one mechanical revolution forward
8587
for (int i = 0; i <= 2000; i++)
8688
{
87-
float angle = _3PI_2 + _2PI * i * pp / 2000.0f;
89+
float angle = _3PI_2 + _2PI * i * motor->pole_pairs / pp / 2000.0f;
8890
motor->setPhaseVoltage(motor->voltage_sensor_align, 0, angle);
8991

9092
ReadLinearHalls(pinA, pinB, &lastA, &lastB);

src/encoders/smoothing/SmoothingSensor.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#include "SmoothingSensor.h"
22
#include "common/foc_utils.h"
33
#include "common/time_utils.h"
4+
#include "sensors/HallSensor.h"
45

56

67
SmoothingSensor::SmoothingSensor(Sensor& s, const FOCMotor& m) : _wrapped(s), _motor(m)
78
{
89
}
910

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

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

2832
// Apply phase correction if needed
2933
if (phase_correction != 0) {
30-
if (_motor.shaft_velocity < -0) angle_prev -= _motor.sensor_direction * phase_correction / _motor.pole_pairs;
31-
else if (_motor.shaft_velocity > 0) angle_prev += _motor.sensor_direction * phase_correction / _motor.pole_pairs;
34+
if (_motor.shaft_velocity < -0.001) angle_prev -= _motor.sensor_direction * phase_correction / _motor.pole_pairs;
35+
else if (_motor.shaft_velocity > 0.001) angle_prev += _motor.sensor_direction * phase_correction / _motor.pole_pairs;
3236
}
3337

3438
// Handle wraparound of the projected angle

src/encoders/smoothing/SmoothingSensor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SmoothingSensor : public Sensor
2222
@param m Motor that the SmoothingSensor will be linked to
2323
*/
2424
SmoothingSensor(Sensor& s, const FOCMotor& m);
25+
SmoothingSensor(class HallSensor& s, const FOCMotor& m); // Automatically sets phase_correction
2526

2627
void update() override;
2728
float getVelocity() override;

0 commit comments

Comments
 (0)