Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getCumulativePosition messed up by http.POST #62

Closed
ivukotic opened this issue Mar 17, 2024 · 20 comments
Closed

getCumulativePosition messed up by http.POST #62

ivukotic opened this issue Mar 17, 2024 · 20 comments
Assignees
Labels
bug Something isn't working

Comments

@ivukotic
Copy link

Hi,
not sure if this is a bug or a feature. I am reading getCumulativePosition from 8 sensors every 100 ms.
After some time, I upload the data using http.POST (ESP Async Web Server). If sensor moved during the upload, getCumulativePosition skips (didn't notice the movement).
Best,
I

@RobTillaart RobTillaart self-assigned this Mar 17, 2024
@RobTillaart
Copy link
Owner

Thanks for the issue,

The context:

int32_t AS5600::getCumulativePosition()
{
  int16_t value = readReg2(AS5600_ANGLE) & 0x0FFF;     <<<<<<<<<<< (1)

  //  whole rotation CW?
  //  less than half a circle
  if ((_lastPosition > 2048) && ( value < (_lastPosition - 2048)))
  {
    _position = _position + 4096 - _lastPosition + value;
  }
  //  whole rotation CCW?
  //  less than half a circle
  else if ((value > 2048) && ( _lastPosition < (value - 2048)))
  {
    _position = _position - 4096 - _lastPosition + value;
  }
  else _position = _position - _lastPosition + value;   <<<<<<<<<<< (3)
  _lastPosition = value;

  return _position;
}

....

uint16_t AS5600::readReg2(uint8_t reg)
{
  _error = AS5600_OK;
  _wire->beginTransmission(_address);
  _wire->write(reg);
  if (_wire->endTransmission() != 0)
  {
    _error = AS5600_ERROR_I2C_READ_2;
    return 0;      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (2)
  }
  uint8_t n = _wire->requestFrom(_address, (uint8_t)2);
  if (n != 2)
  {
    _error = AS5600_ERROR_I2C_READ_3;
    return 0;  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (2)
  }
  uint16_t _data = _wire->read();
  _data <<= 8;
  _data += _wire->read();
  return _data;
}

GetCumulativePosition() does not check if (1) the readReg2() call works or not.
The ESP webserver may disrupt this I2C read (2) which returns 0 and does not see any movement.
If this happens a few times in a row, GetCumulativePosition() will read zero movement.
Then it will execute line (3) with both _lastPosition and value zero ==> no movement.

No, this is not a feature, so it classifies as a bug ==> need to be fixed.

@RobTillaart RobTillaart added the bug Something isn't working label Mar 17, 2024
@RobTillaart
Copy link
Owner

GetCumulativePosition() should check _error to catch the incorrect read.
The best it can do is

  • not update _lastPosition so it can be used when there is a correct read, assuming within half rotation.
  • return _position.
  • not clear the error state so you can check lastError() if getCumulativePosition() is updated or not,
    and optionally call it again.

in code this would add one line in the library.

int32_t AS5600::getCumulativePosition()
{
  int16_t value = readReg2(AS5600_ANGLE) & 0x0FFF;
  if (_error != AS5600_OK) return _position;

  //  whole rotation CW?
  //  less than half a circle
  if ((_lastPosition > 2048) && ( value < (_lastPosition - 2048)))
  {
    _position = _position + 4096 - _lastPosition + value;
  }
  //  whole rotation CCW?
  //  less than half a circle
  else if ((value > 2048) && ( _lastPosition < (value - 2048)))
  {
    _position = _position - 4096 - _lastPosition + value;
  }
  else _position = _position - _lastPosition + value;   <<<<<<<<<<< (3)
  _lastPosition = value;

  return _position;
}

@RobTillaart
Copy link
Owner

Can you verify if the proposed patch works for you?

@ivukotic
Copy link
Author

Hi,

thanks for the very quick reply. I tried your fix but it did not help.
_error != AS5600_OK is never true.

Best,
I

@RobTillaart
Copy link
Owner

Mmm,
Need to investigate if another scenario exists that can explain what happened. Will be difficult.

Can you add a check for lastError() after your calls to getCumulativePosition() to see if it fails and if so what the error code is.

@ivukotic
Copy link
Author

I added lastError call . It always return 0.
I

@RobTillaart
Copy link
Owner

I added lastError call . It always return 0.

So that excludes the scenario above. To be continued.

@RobTillaart
Copy link
Owner

FYI,
not found a cause yet as it is unclear why there is not rotation seen.

Can you add a print statement in the function to see what values are read from the sensor?

int32_t AS5600::getCumulativePosition()
{
  int16_t value = readReg2(AS5600_ANGLE) & 0x0FFF;
  Serial.println(value);

@ivukotic
Copy link
Author

Hi,
it is really hard to see from a printout as I have 8 sensors read every 100ms.
but my application takes always covers the same angular range and I can plot min and max values of the range. Normally range is ~5900 counts. If I plot the values, I see that range increases:
image

@RobTillaart
Copy link
Owner

Still can't explain the behaviour.

Could it be that the wifi signal distorts the sensor?

Have you tried to make a plot of raw reads?
That might help to see if it is a sensor problem .

@ivukotic
Copy link
Author

it is not wifi signal... When I turn off http post part and leave only part that sends data as events to the website (again via wifi), everything is fine. Here actual counts:
image
you may notice that normally I sleep 100ms after each read out. But then suddenly at 31:20.333 delay is 900 ms and count jumped outside normal range...

@RobTillaart
Copy link
Owner

it is not wifi signal... When I turn off http post part and leave only part that sends data as events to the website (again via wifi), everything is fine.

Too fast a conclusion imho,
these events probably have different data size, use other timing, which might be less interfering.

Q1: What data do you send over the HTTP POST?
Q2: How long does the POST take?

So far I do see problems in the library (except for the read error not handled before).

Maybe it is time to post your question on the Arduino forum or so

@RobTillaart
Copy link
Owner

Have you read #28 ?
might be same problem.

@RobTillaart
Copy link
Owner

RobTillaart commented Mar 31, 2024

Created a develop branch + PR for the patch for getCumulativePosition() + some pending things.
(update - build succeeded)

@ivukotic
Copy link
Author

ivukotic commented Apr 1, 2024

I will try suggestions from #28 and let you know what happens.

RobTillaart added a commit that referenced this issue Apr 2, 2024
- improve **getCumulativePosition()**, catch I2C error, see #62
- update readme.md (incl reorder future work).
- update GitHub actions
- minor edits
@RobTillaart
Copy link
Owner

@ivukotic
FYI: Merged the develop branch into release 0.6.1

@RobTillaart
Copy link
Owner

I will try suggestions from #28 and let you know what happens.

Any new insights?

@RobTillaart
Copy link
Owner

@ivukotic
As there is no feedback, I assume the problem is solved so I close this issue.
Feel free to reopen it if needed.

@ivukotic
Copy link
Author

Hi Rob,
sorry for not commenting earlier. At the end I circumvented the issue by not posting from ESP32 but from the client side (client gets the same data via SSE).
best,
i

@RobTillaart
Copy link
Owner

Good to hear you found a way to work around / solve the issue. Still I can not explain why / what happened that caused the issue in the first place.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants