Skip to content

Commit 9052f7a

Browse files
committed
Adding Queue Structure to Encoder readout
1 parent 83f55fb commit 9052f7a

4 files changed

Lines changed: 47 additions & 19 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

ESP32/AS5311-PWM-LIB-QUEUE/AS5311-PWM-LIB-QUEUE.ino

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
AS5311 sensor(GPIO_NUM_26, GPIO_NUM_27);
44

55
void setup() {
6-
//Serial.begin(115200);
7-
//sensor.begin();
6+
Serial.begin(115200);
7+
sensor.begin();
88
}
99

1010
void loop() {
11-
/*
11+
1212
float position = sensor.readPosition();
1313
int edgeCount = sensor.readEdgeCounter();
1414
if (position != -1.0f) {
15-
Serial.println(position);
15+
Serial.println((edgeCount+position));
1616
Serial.println(edgeCount);
1717
}
1818
delay(10);
19-
*/
2019
}

ESP32/AS5311-PWM-LIB-QUEUE/AS5311.cpp

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ volatile uint32_t AS5311::_pos_edg_1 = 0;
66
volatile uint32_t AS5311::_neg_edg_0 = 0;
77
volatile AS5311::PWM_Params AS5311::_pwm = {0, 0, 0};
88
volatile int AS5311::_edgeCounter = 0;
9+
volatile float AS5311::_position = 0.f;
910
QueueHandle_t AS5311::dataQueue = nullptr; // Define the Queue handle globally.
1011

12+
int AS5311::_pwmPin = 0;
13+
int AS5311::_interruptPin = 0;
14+
1115
AS5311::AS5311(int pwmPin, int interruptPin) {
12-
_pwmPin = pwmPin; // Set static members in the constructor
16+
AS5311::_pwmPin = pwmPin;
1317
_interruptPin = interruptPin;
1418
}
1519

1620
void AS5311::begin() {
1721
pinMode(_pwmPin, INPUT_PULLDOWN);
18-
attachInterrupt(digitalPinToInterrupt(_pwmPin), _Ext_PWM_ISR_handler, CHANGE);
19-
2022
pinMode(_interruptPin, INPUT_PULLDOWN);
21-
attachInterrupt(digitalPinToInterrupt(_interruptPin), _handleRisingEdge, RISING);
23+
24+
attachInterrupt(digitalPinToInterrupt(_pwmPin), _Ext_PWM_ISR_handler, CHANGE);
25+
attachInterrupt(digitalPinToInterrupt(_interruptPin), _handleRisingEdge, RISING);
2226

2327
// Create a queue to handle PWM and EdgeCounter data in a safe context
2428
dataQueue = xQueueCreate(10, sizeof(PWM_Params));
@@ -34,8 +38,7 @@ void AS5311::begin() {
3438
}
3539

3640
float AS5311::readPosition() {
37-
// This method may need adjustment according to your new data handling logic.
38-
return -1.;
41+
return _position;
3942
}
4043

4144
int AS5311::readEdgeCounter() {
@@ -54,12 +57,12 @@ void IRAM_ATTR AS5311::_handleRisingEdge() {
5457
}
5558
}
5659

57-
void IRAM_ATTR AS5311::_Ext_PWM_ISR_handler() {
60+
void IRAM_ATTR AS5311::_Ext_PWM_ISR_handler() {
5861
uint32_t current_time = micros();
5962
PWM_Params localData;
6063
localData.period = 0;
6164
localData.duty_cycle = 0;
62-
localData.edgeCounter = 1;
65+
localData.edgeCounter = 0;
6366

6467
if (digitalRead(_pwmPin) == HIGH) {
6568
_pos_edg_1 = current_time;
@@ -79,13 +82,38 @@ void IRAM_ATTR AS5311::_Ext_PWM_ISR_handler() {
7982

8083
void AS5311::handleDataTask(void *parameter) {
8184
PWM_Params receivedData;
85+
PWM_Params localData;
86+
localData = {0, 0, 0};
87+
float position = 0.f;
8288
for (;;) {
8389
if (xQueueReceive(dataQueue, &receivedData, portMAX_DELAY)) {
84-
Serial.println(receivedData.period);
85-
Serial.println(receivedData.edgeCounter);
86-
//_edgeCounter = receivedData.edgeCounter;
87-
//Serial.println(receivedData);
88-
// You might process the received data here, or update other variables accordingly.
90+
if (receivedData.edgeCounter != 0) {
91+
if (_position>.5){
92+
_edgeCounter = _edgeCounter +1;
93+
}
94+
else{
95+
_edgeCounter = _edgeCounter -1;
96+
}
97+
//Serial.print("Edge Counter: ");
98+
//Serial.println(_edgeCounter);
99+
}
100+
101+
if(receivedData.period != 0) {
102+
//Serial.print("Period: ");
103+
//Serial.println(receivedData.period);
104+
localData.period = receivedData.period;
105+
}
106+
if (receivedData.duty_cycle != 0) {
107+
//Serial.print("Duty Cycle: ");
108+
//Serial.println(receivedData.duty_cycle);
109+
localData.duty_cycle = receivedData.duty_cycle;
110+
}
111+
112+
// Calculate position
113+
if (localData.period !=0 and localData.duty_cycle <= localData.period){
114+
_position = (float) localData.duty_cycle / (float) localData.period;
115+
}
116+
89117
}
90118
}
91119
}

ESP32/AS5311-PWM-LIB-QUEUE/AS5311.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class AS5311 {
77
public:
88
AS5311(int pwmPin, int interruptPin);
9+
910
void begin();
1011
float readPosition();
1112
int readEdgeCounter();
@@ -14,6 +15,7 @@ class AS5311 {
1415
static bool writing_counter;
1516
static volatile uint32_t _pos_edg_0, _pos_edg_1, _neg_edg_0;
1617
static volatile int _edgeCounter;
18+
static volatile float _position;
1719
static volatile bool _time_2_print;
1820
static QueueHandle_t dataQueue; // Define the Queue handle globally.
1921
static void handleDataTask(void *parameter);
@@ -24,7 +26,6 @@ class AS5311 {
2426
int edgeCounter;
2527
} PWM_Params;
2628

27-
2829
static volatile PWM_Params _pwm;
2930

3031
static void IRAM_ATTR _handleRisingEdge();

0 commit comments

Comments
 (0)