Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add update funktion for speed and cumulative position #67

@Chris-42

Description

@Chris-42

whats about introducing an update method which calculates speed and position? So the I2C register has only to be read once?
then one can have update called in loop and use siple getters for getCumulativePosition and getAngularSpeed.
maybe with a #define USE_UPDATE ...

float AS5600::getAngularSpeed(uint8_t mode) {
  //  return radians, RPM or degrees.
  if (mode == AS5600_MODE_RADIANS)
  {
    return _angular_speed * AS5600_RAW_TO_RADIANS;
  }
  if (mode == AS5600_MODE_RPM)
  {
    return _angular_speed * AS5600_RAW_TO_RPM;
  }
  //  default return degrees
  return _angular_speed * AS5600_RAW_TO_DEGREES;
}

int32_t AS5600::getCumulativePosition() {
  return _position;
}

void AS5600::update() {
  uint32_t now  = micros();
  int16_t angle = readAngle();
  if (_error != AS5600_OK) return;
  // update cummulative position
  //  whole rotation CW?
  //  less than half a circle
  if ((_lastPosition > 2048) && ( angle < (_lastPosition - 2048))) {
    _position = _position + 4096 - _lastPosition + angle;
  }
  //  whole rotation CCW?
  //  less than half a circle
  else if ((angle > 2048) && ( _lastPosition < (angle - 2048))) {
    _position = _position - 4096 - _lastPosition + angle;
  }
  else {
    _position = _position - _lastPosition + angle;
  }
  _lastPosition = angle;

  uint32_t deltaT  = now - _lastMeasurement;
  int      deltaA  = angle - _lastAngle;

  //  assumption is that there is no more than 180° rotation
  //  between two consecutive measurements.
  //  => at least two measurements per rotation (preferred 4).
  if (deltaA >  2048) deltaA -= 4096;
  if (deltaA < -2048) deltaA += 4096;
  _angular_speed = (deltaA * 1e6) / deltaT;

  //  remember last time & angle
  _lastMeasurement = now;
  _lastAngle       = angle;

}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions