AS5048A PWM issue

Hey Guys,

I just started using SimpleFOC a short while ago… so first of all many thanks! Amazing work!!

Still, I believe that there is an issue with the PWM angle / velocity reading in MagneticSensorPWM.
My setup is as follows:

-> Gimble Motor with integrated AS5048A, that I would like to use with PWM-based output
-> Arduino Uno, PWM connected to DigitalPin2
-> Running the “find_raw_min_max example”

The Sensor seems to be fine, checked with an scope and I am getting a nice PWM Signal between ~5us and ~920us with a magnitude of 3.3V:

In init everything is calculated correctly, then it doesnt update the values in the loop:
image

I am unfortunately not a programming pro, hope for some hints :slight_smile:

What I found is:
-> is_interrupt_based is correctly set to true
-> So I guess “pulse_length_us” is calculated in doPWM since it shall be interrupt based…getRawCount() only returns it since the if-statement is skipped.

Thanks upfront!
BR
Razo

Are you using an UNO or nano (or similar chip)? The code in the sensor.enableInterrupt() function has purposefully chosen to not set up interrupt for atmega chips but I’m not sure why.
Can you manually enable the interrupt? e.g. add a line after sensor.enableInterrupt(doPWM) to your setup.

void setup() {
  // monitoring port
  Serial.begin(115200);

  // initialise magnetic sensor hardware
  sensor.init();
   // comment out to use sensor in blocking (non-interrupt) way
  sensor.enableInterrupt(doPWM);
  attachInterrupt(digitalPinToInterrupt(2), doPWM, CHANGE);


  Serial.println("Sensor ready");
  _delay(1000);
}

@Owen_Williams many thanks for the fast reply! Working like a charm with manual activation.

Would still be interesting to know, why the interrupt shall be not used at the Atmega chips. …Maybe Performance?

I will continue a bit and see if I run into other issues than :slight_smile: Already ordered an ESP32, guess thats be better choice here anyway :slight_smile:

So, Thanks!!

1 Like

Haven’t a clue - I expect the macro check can be removed, which will mean you wouldn’t need to manually register interrupt

Hey guys,

Just to answer your question, why is it disabled.
Its due to the performance issues.

Atmega328 and atmega2560 have a lot of trouble with pwm sensors. Actually the biggest issue are not he pwm interrupts but the micors() function.
This function is calculated in the timer1 for both chips and since timer1 often has some pwm pins that we need we usually set the maximal possible timer frequency for the timer1 which makes the microsecond reading resolution pretty bad.

So this was, at least in my head, a way to make people use blocking way and avoid interrupt based approach for these architectures. Blocking approach uses pulseln() function which handles this situation a bit better.

But as I did not document or explain it properly it just adds to the confusion, so I’ll remove this constraint for the next library version. :smiley: