ESP32 Support, General refactoring for architecture-independant code, Zero position write support.#6
ESP32 Support, General refactoring for architecture-independant code, Zero position write support.#6paucarre wants to merge 10 commits intoZoetropeLabs:masterfrom
Conversation
…p (removed unused class variables, proper C++ initialization, simplified write...). Added methods to get rotation in degrees and radians. Updated sketches adding esp32 example.
|
@paucarre, this is fantastic! Most of the team is away at the moment, including me, however I will make sure that this gets tested and merged over the next couple of weeks. Thanks ever so much for this and I think it's a really good contribution to the community |
|
No problem, take as much time as you need, just wanted to know if there were people notified of PR. So far it's working well for my purpose https://drive.google.com/file/d/1DPY4aPaDYzX_SdBavivNrGoniEyXbJqI/view |
|
|
||
| static const float AS5048A_MAX_VALUE = 8191.0; | ||
| static const float AS5048A_TWICE_MAX_VALUE = AS5048A_MAX_VALUE * 2.0; | ||
| static const float AS5048A_PI = 3.14159265358979323846; |
There was a problem hiding this comment.
This variable is not necessary. You can use PI instead.
An example program would be:
void setup() {
Serial.begin(9600);
Serial.print(PI,20);
}
void loop() {}
And you will obtain as result 3.14159274101257324218.
You can use M_PI but it's not very precise.
|
|
||
| /** | ||
| * Constructor | ||
| * Constructor usign response delay (ESP32 and similars) |
lib/AS5048A/AS5048A.cpp
Outdated
| data = AS5048A::getRawRotation(); | ||
| rotation = (int)data - (int)position; | ||
| uint16_t data = AS5048A::getRawRotation(); | ||
| int32_t rotation = (int32_t)data - (int32_t)position; |
There was a problem hiding this comment.
Consider using C++ casting static_cast<int32_t>(var).
lib/AS5048A/AS5048A.cpp
Outdated
| uint16_t data = AS5048A::getRawRotation(); | ||
| int32_t rotation = (int32_t)data - (int32_t)position; | ||
| if(rotation > 8191) rotation = -((0x3FFF)-rotation); //more than -180 | ||
| //if(rotation < -0x1FFF) rotation = rotation+0x3FFF; |
|
|
||
| float AS5048A::getRotationInRadians(){ | ||
| int32_t rotation = getRotation(); | ||
| float degrees = AS5048A_PI * (rotation + AS5048A_MAX_VALUE) / AS5048A_MAX_VALUE; |
lib/AS5048A/AS5048A.cpp
Outdated
| @@ -245,12 +276,12 @@ word AS5048A::write(word registerAddress, word data) { | |||
| SPI.transfer(left_byte); | |||
There was a problem hiding this comment.
Why didn't you apply the same logic as in read()?
lib/AS5048A/AS5048A.cpp
Outdated
|
|
||
| //Send a NOP to get the new data in the register | ||
| digitalWrite(_cs, LOW); | ||
| left_byte =-SPI.transfer(0x00); |
There was a problem hiding this comment.
Is this the reason why you didn't apply the 16-bit logic?
| /** | ||
| * Get the rotation of the sensor relative to the zero position. | ||
| * | ||
| * @return {int} between -2^13 and 2^13 |
| Serial.print("Got rotation of: 0x"); | ||
| Serial.println(val, HEX); | ||
| Serial.print("State: "); | ||
| angleSensor.printState(); |
There was a problem hiding this comment.
If you don't set Serial.begin(), this line will crash the app.
| rotation = (int)data - (int)position; | ||
| uint16_t data = AS5048A::getRawRotation(); | ||
| int32_t rotation = (int32_t)data - (int32_t)position; | ||
| if(rotation > 8191) rotation = -((0x3FFF)-rotation); //more than -180 |
Added support to write the zero position using SPI General refactoring of the library speeding up parity bit computation as well as reducing the total number of lines increasing code-clarity
The library doesn't work using ESP32 microcontrollers, the pull request resolves the issue. The reason is pretty simple. For fast microcontrollers it seems when sending a transfer, it's necessary to wait for the sensor to have data to be received.
I found several other problems, such as the use of word or int types which are not architecture-independant. I also seized the opportunity to remove a few class variables not used, and modernize the code a bit (constructor assignment, constants, variable declaration...)
Furthermore, I simplified the write method, removed all the code that splits and merges 16 bits variables into 8 bits. Same shall be done for write, which I will probably do later on.
Finally I added methods to get the rotation in degrees and radians.
Thanks for the library by the way!
... two more things:
The code is tested with Arduino UNO and ESP32. It seems to work for both.
I added sketches for both and renamed them so that they fit specifically the microcontroller type.