nathankellenicki / node-poweredup

A Javascript module to interface with LEGO Powered Up components.
https://nathankellenicki.github.io/node-poweredup/
MIT License
477 stars 59 forks source link

Tachomotor & Absolutemotor #153

Open firien opened 2 years ago

firien commented 2 years ago

I ran into trouble controlling a Technic Large Motor; while struggling to reset zero angle, and absolutely rotate to a desired angle, i found:


I do not think Tachomotor rotateByDegrees should allow a negative number for degrees as writeUInt32LE will yield unexpected results.

https://github.com/nathankellenicki/node-poweredup/blob/e4c68e5749be0ac96bc4296c87ff10b28b96af6a/src/devices/tachomotor.ts#L157


I am not sure what normalizeAngle does, but it contains a bunch of math that eventually just cancels out. https://github.com/nathankellenicki/node-poweredup/blob/e4c68e5749be0ac96bc4296c87ff10b28b96af6a/src/utils.ts#L36

const normalizeAngle = (angle) => {
  if (angle >= 180) {
      return angle - (360 * ((angle + 180) / 360));
  } else if (angle < -180) {
      return angle + (360 * ((180 - angle) / 360));
  }
  return angle;
};

// remove (360 / 360) from equation
const normalizeAngle1 = (angle) => {
  if (angle >= 180) {
      return angle - (((angle + 180)));
  } else if (angle < -180) {
      return angle + (((180 - angle)));
  }
  return angle;
};

// remove (angle - angle) from equation
const normalizeAngle2 = (angle) => {
  if (angle >= 180) {
      return -180;
  } else if (angle < -180) {
      return 180;
  }
  return angle;
};

for (let a = -400; a < 400; a = a + 10) {
  console.log([a, normalizeAngle(a), normalizeAngle1(a), normalizeAngle2(a) ])
}

Would you consider opening up Discussions?. I am looking for help using the Technic Large Motor to steer the off road buggy - but don't want to clutter up Issues with non-bugs.


I used this library to make a web page to control kid's duplo train a few years ago, and it's still kicking. Just updated your lib from 1.x => 8.x. So thanks!