SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
HallSensor.h
Go to the documentation of this file.
1#ifndef HALL_SENSOR_LIB_H
2#define HALL_SENSOR_LIB_H
3
4#include "Arduino.h"
5#include "../common/base_classes/Sensor.h"
6#include "../common/foc_utils.h"
7#include "../common/time_utils.h"
8
9// seq 1 > 5 > 4 > 6 > 2 > 3 > 1 000 001 010 011 100 101 110 111
10const int8_t ELECTRIC_SECTORS[8] = { -1, 0, 4, 5, 2, 1, 3 , -1 };
11
12class HallSensor: public Sensor{
13 public:
14 /**
15 HallSensor class constructor
16 @param encA HallSensor A pin
17 @param encB HallSensor B pin
18 @param encC HallSensor C pin
19 @param pp pole pairs (e.g hoverboard motor has 15pp and small gimbals often have 7pp)
20 @param index index pin number (optional input)
21 */
22 HallSensor(int encA, int encB, int encC, int pp);
23
24 /** HallSensor initialise pins */
25 void init();
26 /**
27 * function enabling hardware interrupts for the HallSensor channels with provided callback functions
28 * if callback is not provided then the interrupt is not enabled
29 *
30 * @param doA pointer to the A channel interrupt handler function
31 * @param doB pointer to the B channel interrupt handler function
32 * @param doIndex pointer to the Index channel interrupt handler function
33 *
34 */
35 void enableInterrupts(void (*doA)() = nullptr, void(*doB)() = nullptr, void(*doC)() = nullptr);
36
37 // HallSensor interrupt callback functions
38 /** A channel callback function */
39 void handleA();
40 /** B channel callback function */
41 void handleB();
42 /** C channel callback function */
43 void handleC();
44
45
46 // pins A and B
47 int pinA; //!< HallSensor hardware pin A
48 int pinB; //!< HallSensor hardware pin B
49 int pinC; //!< HallSensor hardware pin C
50 int use_interrupt; //!< True if interrupts have been attached
51
52 // HallSensor configuration
53 Pullup pullup; //!< Configuration parameter internal or external pullups
54 int cpr;//!< HallSensor cpr number
55
56 // Abstract functions of the Sensor class implementation
57 /** Interrupt-safe update */
58 void update() override;
59 /** get current angle (rad) */
60 float getSensorAngle() override;
61 /** get current angular velocity (rad/s) */
62 float getVelocity() override;
63
64 // whether last step was CW (+1) or CCW (-1).
67
68 void attachSectorCallback(void (*onSectorChange)(int a) = nullptr);
69
70 // the current 3bit state of the hall sensors
71 volatile int8_t hall_state;
72 // the current sector of the sensor. Each sector is 60deg electrical
73 volatile int8_t electric_sector;
74 // the number of electric rotations
75 volatile long electric_rotations;
76 // this is sometimes useful to identify interrupt issues (e.g. weak or no pullup resulting in 1000s of interrupts)
77 volatile long total_interrupts;
78
79 // variable used to filter outliers - rad/s
80 float velocity_max = 1000.0f;
81
82 private:
83
84 Direction decodeDirection(int oldState, int newState);
85 void updateState();
86
87 volatile unsigned long pulse_timestamp;//!< last impulse timestamp in us
88 volatile int A_active; //!< current active states of A channel
89 volatile int B_active; //!< current active states of B channel
90 volatile int C_active; //!< current active states of C channel
91
92 // function pointer for on sector change call back
93 void (*onSectorChange)(int sector) = nullptr;
94
95 volatile long pulse_diff;
96
97};
98
99
100#endif
const int8_t ELECTRIC_SECTORS[8]
Definition HallSensor.h:10
Pullup
Definition Sensor.h:19
Direction
Definition Sensor.h:9
volatile int8_t electric_sector
Definition HallSensor.h:73
void handleA()
int cpr
HallSensor cpr number.
Definition HallSensor.h:54
int pinB
HallSensor hardware pin B.
Definition HallSensor.h:48
int use_interrupt
True if interrupts have been attached.
Definition HallSensor.h:50
float velocity_max
Definition HallSensor.h:80
void attachSectorCallback(void(*onSectorChange)(int a)=nullptr)
float getVelocity() override
float getSensorAngle() override
Direction direction
Definition HallSensor.h:65
void handleC()
volatile long total_interrupts
Definition HallSensor.h:77
void enableInterrupts(void(*doA)()=nullptr, void(*doB)()=nullptr, void(*doC)()=nullptr)
volatile long electric_rotations
Definition HallSensor.h:75
int pinC
HallSensor hardware pin C.
Definition HallSensor.h:49
void update() override
void handleB()
Pullup pullup
Configuration parameter internal or external pullups.
Definition HallSensor.h:53
int pinA
HallSensor hardware pin A.
Definition HallSensor.h:47
Direction old_direction
Definition HallSensor.h:66
volatile int8_t hall_state
Definition HallSensor.h:71
float a
Definition foc_utils.cpp:59