|
| 1 | +#include "AS5311.h" |
| 2 | +#include "freertos/queue.h" |
| 3 | + |
| 4 | +volatile uint32_t AS5311::_pos_edg_0 = 0; |
| 5 | +volatile uint32_t AS5311::_pos_edg_1 = 0; |
| 6 | +volatile uint32_t AS5311::_neg_edg_0 = 0; |
| 7 | +volatile AS5311::PWM_Params AS5311::_pwm = {0, 0, 0}; |
| 8 | +volatile int AS5311::_edgeCounter = 0; |
| 9 | +volatile float AS5311::_position = 0.f; |
| 10 | +QueueHandle_t AS5311::dataQueue = nullptr; // Define the Queue handle globally. |
| 11 | + |
| 12 | +int AS5311::_pwmPin = 0; |
| 13 | +int AS5311::_interruptPin = 0; |
| 14 | + |
| 15 | +AS5311::AS5311(int pwmPin, int interruptPin) { |
| 16 | + AS5311::_pwmPin = pwmPin; |
| 17 | + _interruptPin = interruptPin; |
| 18 | +} |
| 19 | + |
| 20 | +void AS5311::begin() { |
| 21 | + pinMode(_pwmPin, INPUT_PULLDOWN); |
| 22 | + pinMode(_interruptPin, INPUT_PULLDOWN); |
| 23 | + |
| 24 | + attachInterrupt(digitalPinToInterrupt(_pwmPin), _Ext_PWM_ISR_handler, CHANGE); |
| 25 | + attachInterrupt(digitalPinToInterrupt(_interruptPin), _handleRisingEdge, RISING); |
| 26 | + |
| 27 | + // Create a queue to handle PWM and EdgeCounter data in a safe context |
| 28 | + dataQueue = xQueueCreate(10, sizeof(PWM_Params)); |
| 29 | + |
| 30 | + // Task creation should be added here |
| 31 | + xTaskCreate( |
| 32 | + handleDataTask, /* Task function. */ |
| 33 | + "HandleData", /* String with name of task. */ |
| 34 | + 10000, /* Stack size in bytes. */ |
| 35 | + NULL, /* Parameter passed as input to the task */ |
| 36 | + 1, /* Priority at which the task is created. */ |
| 37 | + NULL); /* Task handle. */ |
| 38 | +} |
| 39 | + |
| 40 | +float AS5311::readPosition() { |
| 41 | + return _position; |
| 42 | +} |
| 43 | + |
| 44 | +int AS5311::readEdgeCounter() { |
| 45 | + return _edgeCounter; |
| 46 | +} |
| 47 | + |
| 48 | +void IRAM_ATTR AS5311::_handleRisingEdge() { |
| 49 | + PWM_Params localData; |
| 50 | + localData.period = 0; |
| 51 | + localData.duty_cycle = 0; |
| 52 | + localData.edgeCounter = 1; |
| 53 | + BaseType_t xHigherPriorityTaskWoken = pdFALSE; |
| 54 | + xQueueSendFromISR(dataQueue, &localData, &xHigherPriorityTaskWoken); |
| 55 | + if (xHigherPriorityTaskWoken) { |
| 56 | + portYIELD_FROM_ISR(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void IRAM_ATTR AS5311::_Ext_PWM_ISR_handler() { |
| 61 | + uint32_t current_time = micros(); |
| 62 | + PWM_Params localData; |
| 63 | + localData.period = 0; |
| 64 | + localData.duty_cycle = 0; |
| 65 | + localData.edgeCounter = 0; |
| 66 | + |
| 67 | + if (digitalRead(_pwmPin) == HIGH) { |
| 68 | + _pos_edg_1 = current_time; |
| 69 | + localData.period = _pos_edg_1 - _pos_edg_0; |
| 70 | + _pos_edg_0 = _pos_edg_1; |
| 71 | + } else { |
| 72 | + _neg_edg_0 = current_time; |
| 73 | + localData.duty_cycle = _neg_edg_0 - _pos_edg_0; |
| 74 | + } |
| 75 | + |
| 76 | + BaseType_t xHigherPriorityTaskWoken = pdFALSE; |
| 77 | + xQueueSendFromISR(dataQueue, &localData, &xHigherPriorityTaskWoken); |
| 78 | + if (xHigherPriorityTaskWoken) { |
| 79 | + portYIELD_FROM_ISR(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void AS5311::handleDataTask(void *parameter) { |
| 84 | + PWM_Params receivedData; |
| 85 | + PWM_Params localData; |
| 86 | + localData = {0, 0, 0}; |
| 87 | + float position = 0.f; |
| 88 | + for (;;) { |
| 89 | + if (xQueueReceive(dataQueue, &receivedData, portMAX_DELAY)) { |
| 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 | + // At the 0/1 change the duty cycle gets noisy, so we need to average it over multiple senses |
| 115 | + _position = calculateRollingAverage((float) localData.duty_cycle / (float) localData.period); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +float AS5311::calculateRollingAverage(float newVal) { |
| 123 | + static float values[3] = {0.0, 0.0, 0.0}; |
| 124 | + static int insertIndex = 0; |
| 125 | + static float sum = 0.0; |
| 126 | + |
| 127 | + sum -= values[insertIndex]; |
| 128 | + values[insertIndex] = newVal; |
| 129 | + sum += newVal; |
| 130 | + |
| 131 | + insertIndex = (insertIndex + 1) % 3; |
| 132 | + |
| 133 | + return sum / 3.0; |
| 134 | +} |
0 commit comments