rwaldron / tessel-io

A Tessel 2 IO Plugin for Johnny-Five JavaScript Robotics programs
http://johnny-five.io
50 stars 11 forks source link

tessel-io servoWrite does not match firmata.js servoWrite functionality #20

Closed dtex closed 6 years ago

dtex commented 7 years ago

In firmata.js:

Board.prototype.servoWrite = function(pin, value) {
  // Values less than 544 will be treated as angles in degrees
  // (valid values in microseconds are handled as microseconds)
  this.analogWrite.apply(this, arguments);
};

The logic for degrees vs microseconds is handled in the Arduino Servo Library. If we add similar logic to all the other io classes we can substantially increase the servo resolution in Johnny-Five.

I'm working on adding support in Johnny-Five for multi-turn servos. The servo I have for testing turns 6.75 rotations (2430 degrees) with a pwm range of 600 - 2370. If I map everything to 0 - 180 and only pass integers to servoWrite (the current Johnny-Five way), than I can only move the servo in 13.4 degree increments. If Johnny-Five instead mapped to the servo's PWM range and sent microseconds we could move the servo in 1.4 degree increments.

It is also worth noting that for standard hobby servos we can currently only move in one degree steps. If we mapped to the PWM range and sent microseconds then we could move in 0.18 degree steps (assuming the pots on the servos are up to the task).

The Johnny-Five Servo class is engineered in a way that this would be an easy change to make there. Firmata.js, the firmata protocol and firmata all already support it. I see it would be easy to add here in tessel-io and I assume it would be equally easy to add into the other IO plugins.

Can you think of any reason not to do this (or what am I missing)?

dtex commented 7 years ago

Checked on the pot question. Most standard hobby servos can be positioned in 2-8 us increments depending on the quality of the servo. That means that currently J5 is the limiting factor on all but the lowest quality servos (assuming I understand all this correctly).

rwaldron commented 7 years ago

I guess it's worth experimenting. My one request is that current behavior is preserved.

dtex commented 7 years ago

Of course, this would be non-breaking and requires no changes to the public API. You would still have the option to pass degrees.

It also requires changes to ESC and the PCA-9685 Expander in Johnny-Five, but I've already done all that. I'll submit a PR here tonight and will audit the IO classes to see what repos need to be addressed.