Skip to content
This repository was archived by the owner on Sep 23, 2021. It is now read-only.

ESP32 Support, General refactoring for architecture-independant code, Zero position write support.#6

Open
paucarre wants to merge 10 commits intoZoetropeLabs:masterfrom
paucarre:esp32_support
Open

ESP32 Support, General refactoring for architecture-independant code, Zero position write support.#6
paucarre wants to merge 10 commits intoZoetropeLabs:masterfrom
paucarre:esp32_support

Conversation

@paucarre
Copy link

@paucarre paucarre commented Dec 16, 2017

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.

…p (removed unused class variables, proper C++ initialization, simplified write...). Added methods to get rotation in degrees and radians. Updated sketches adding esp32 example.
@benhowes
Copy link
Contributor

@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

@paucarre
Copy link
Author

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

Copy link

@eborghi10 eborghi10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your work.
I left a couple of comments, PTAL.

BTW, I tested on my ESP32 and it's not working.


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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using

data = AS5048A::getRawRotation();
rotation = (int)data - (int)position;
uint16_t data = AS5048A::getRawRotation();
int32_t rotation = (int32_t)data - (int32_t)position;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using C++ casting static_cast<int32_t>(var).

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove commented code.


float AS5048A::getRotationInRadians(){
int32_t rotation = getRotation();
float degrees = AS5048A_PI * (rotation + AS5048A_MAX_VALUE) / AS5048A_MAX_VALUE;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float radians

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also remove the variable.

@@ -245,12 +276,12 @@ word AS5048A::write(word registerAddress, word data) {
SPI.transfer(left_byte);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you apply the same logic as in read()?


//Send a NOP to get the new data in the register
digitalWrite(_cs, LOW);
left_byte =-SPI.transfer(0x00);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{int32_t}

Serial.print("Got rotation of: 0x");
Serial.println(val, HEX);
Serial.print("State: ");
angleSensor.printState();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (rotation > AS5048A_MAX_VALUE)

Pau Carre and others added 2 commits January 5, 2020 15:27
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
@paucarre paucarre changed the title Esp32 support ESP32 Support, General refactoring for architecture-independant code, Zero position write support. Jan 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants