Skip to content

Commit 06e1cca

Browse files
committed
Add 60deg setting for hall sensors
1 parent 395b6cd commit 06e1cca

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/sensors/HallSensor.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
#include "HallSensor.h"
22

3+
// seq 1 > 5 > 4 > 6 > 2 > 3 > 1 000 001 010 011 100 101 110 111
4+
const int8_t ELECTRIC_SECTORS_120[8] = { -1, 0, 4, 5, 2, 1, 3 , -1 };
5+
6+
// seq 1 > 5 > 4 > 6 > 2 > 3 > 1 000 001 010 011 100 101 110 111
7+
const int8_t ELECTRIC_SECTORS_60[8] = { 0, 5, 1, 2, 5, 4, 2 , 3 };
38

49
/*
510
HallSensor(int hallA, int hallB , int cpr, int index)
611
- hallA, hallB, hallC - HallSensor A, B and C pins
712
- pp - pole pairs
813
*/
9-
HallSensor::HallSensor(int _hallA, int _hallB, int _hallC, int _pp){
14+
HallSensor::HallSensor(int _hallA, int _hallB, int _hallC, int _pp, bool _hall_60deg){
1015

1116
// hardware pins
1217
pinA = _hallA;
1318
pinB = _hallB;
1419
pinC = _hallC;
20+
hall_60deg = _hall_60deg;
1521

1622
// hall has 6 segments per electrical revolution
1723
cpr = _pp * 6;
@@ -50,7 +56,15 @@ void HallSensor::updateState() {
5056
long new_pulse_timestamp = _micros();
5157
hall_state = new_hall_state;
5258

53-
int8_t new_electric_sector = ELECTRIC_SECTORS[hall_state];
59+
int8_t new_electric_sector;
60+
if (hall_60deg)
61+
{
62+
new_electric_sector = ELECTRIC_SECTORS_60[hall_state];
63+
}
64+
else
65+
{
66+
new_electric_sector = ELECTRIC_SECTORS_120[hall_state];
67+
}
5468
int8_t electric_sector_dif = new_electric_sector - electric_sector;
5569
if (electric_sector_dif > 3) {
5670
//underflow

src/sensors/HallSensor.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include "../common/foc_utils.h"
77
#include "../common/time_utils.h"
88

9-
// seq 1 > 5 > 4 > 6 > 2 > 3 > 1 000 001 010 011 100 101 110 111
10-
const int8_t ELECTRIC_SECTORS[8] = { -1, 0, 4, 5, 2, 1, 3 , -1 };
119

1210
class HallSensor: public Sensor{
1311
public:
@@ -17,9 +15,9 @@ class HallSensor: public Sensor{
1715
@param encB HallSensor B pin
1816
@param encC HallSensor C pin
1917
@param pp pole pairs (e.g hoverboard motor has 15pp and small gimbals often have 7pp)
20-
@param index index pin number (optional input)
18+
@param hall_60deg Indicate if the hall sensors are 60 degrees apart electrically (means that they can all be one or off at the same time). In 60deg mode, B needs to lead, so you may need to swap the connections until you find one that works
2119
*/
22-
HallSensor(int encA, int encB, int encC, int pp);
20+
HallSensor(int encA, int encB, int encC, int pp, bool hall_60deg = false);
2321

2422
/** HallSensor initialise pins */
2523
void init();
@@ -47,7 +45,8 @@ class HallSensor: public Sensor{
4745
int pinA; //!< HallSensor hardware pin A
4846
int pinB; //!< HallSensor hardware pin B
4947
int pinC; //!< HallSensor hardware pin C
50-
int use_interrupt; //!< True if interrupts have been attached
48+
bool use_interrupt; //!< True if interrupts have been attached
49+
bool hall_60deg; //!< Hall sensors are 60 degrees apart electrically (means that they can all be one or off at the same time)
5150

5251
// HallSensor configuration
5352
Pullup pullup; //!< Configuration parameter internal or external pullups
@@ -93,7 +92,6 @@ class HallSensor: public Sensor{
9392
void (*onSectorChange)(int sector) = nullptr;
9493

9594
volatile long pulse_diff;
96-
9795
};
9896

9997

0 commit comments

Comments
 (0)