@@ -6,19 +6,23 @@ volatile uint32_t AS5311::_pos_edg_1 = 0;
66volatile uint32_t AS5311 ::_neg_edg_0 = 0 ;
77volatile AS5311 ::PWM_Params AS5311 ::_pwm = {0 , 0 , 0 };
88volatile int AS5311 ::_edgeCounter = 0 ;
9+ volatile float AS5311 ::_position = 0 .f;
910QueueHandle_t AS5311 ::dataQueue = nullptr ; // Define the Queue handle globally.
1011
12+ int AS5311 ::_pwmPin = 0 ;
13+ int AS5311 ::_interruptPin = 0 ;
14+
1115AS5311 ::AS5311 (int pwmPin, int interruptPin) {
12- _pwmPin = pwmPin; // Set static members in the constructor
16+ AS5311 :: _pwmPin = pwmPin;
1317 _interruptPin = interruptPin;
1418}
1519
1620void 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
3640float AS5311::readPosition () {
37- // This method may need adjustment according to your new data handling logic.
38- return -1 .;
41+ return _position;
3942}
4043
4144int 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
8083void 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}
0 commit comments