I just want to drop a quick note, I discovered an error that prevents PWM sensors from operating properly (the hard way :().
There appears to be at least 2 errors in the file.
one is on line 48, I forget what it used to say , I think it divided the pulse length by the counts per rotation. However the problem is that the pulse length is never exactly right, thus the maximal rotational angle can easily exceed the counts per rotation, even when you use the sensor pulse length min/max checking example.
This leads to errors later on as the getrawAngle thing returns a rotation of more than 2pi rotations pre revolution sometimes.
This leads to a NaN when the sensor calibration is used, in particular, which propagates and makes everything else break…
I was able to correct it by going
if (pulse_length_us>(cpr-1)){
pulse_length_us = (cpr-1);
}
cpr is for counts per rotation, and is the number of microseconds a full length pulse is supposed to be. I made it cpr-1 just in case of rounding issues when things get converted to a float or whatever.
The second error appears to be on line 61, it said
if (!digitalRead(pinPWM)) pulse_length_us = now_us - last_call_us;
But I’m thinking it should say
if (!digitalRead(pinPWM)) pulse_length_us = (now_us - last_call_us)-min_raw_count;
The sensor doesn’t give a pulse length from zero to full, it has a minimal pulse length that corresponds to zero angle. Thus, what we want is surely the difference between the measured pulse and the minimum, I think looking at the datasheet of the AS5600 this is right yes?