nebrius / j5-io

An abstract library for creating Raspi IO plugins
MIT License
6 stars 2 forks source link

Question re: servoWrite functionality #4

Closed dtex closed 6 years ago

dtex commented 7 years ago

Hi @nebrius

I'm working on a change in Johnny-Five that takes advantage of an existing feature in firmata.js that will give better resolution for servo positioning and allows us to support 90, 360 degree and multi-turn servos.

I'm marching through all the IO plugins to make sure they already match firmata's functionality, or am submitting a PR to add it, but before I make a change here I have a question: With these values I would expect the actual range of standard 180° servos to be limited to a bit more than 90°:

const SOFTWARE_PWM_FREQUENCY = 50;
...
const DEFAULT_SERVO_MIN = 1000;
const DEFAULT_SERVO_MAX = 2000;

Is there something peculiar to the Pi and the pulse width that gets output?

nebrius commented 7 years ago

There be magic numbers ahead! If you look at the code, you'll notice this piece of code:

const period = 1000000 / pinInstance.peripheral.frequency; // in us
const pulseWidth = (pinInstance.min + (value / 180) * (pinInstance.max - pinInstance.min)); // in us
pinInstance.peripheral.write(pulseWidth / period);

raspi-io-core's purpose here is to take the values that J5 sends in and convert it to a float between 0 and 1, corresponding to a duty cycle of 0% and 100%, respectively. I got this to work through experimentation over the years. I honestly don't remember why those values are what they are anymore, and it doesn't really matter either since it's just an intermediate representation.

What this does mean though, is that if you want to change what's sent from J5, it's easy, since it's nothing but math to convert to a float anyways.

EDIT: The important part, back to your question, though is the (value / 180) part. If you want to use a servo that is some other amount, we'll need a way to provide this value via the API so I can replace the 180 constant with that variable.

nebrius commented 6 years ago

@dtex are there any updates on this? Do we still need this issue open?

dtex commented 6 years ago

Yes. Rick reminded me just a couple of days ago. I'm working on servoConfig for tessel-io this week. Once that's done I'll be making a PR with changes in J5 for multi-turn (and 90° and 360°) servos that will depend on the ability to send servoWrite value params > 544 and have that value handled as microseconds instead of degrees.

dtex commented 6 years ago

Just an update. These things have landed in tessel-io, linux-io, and beaglebone-io.

Currently working on imp-io/tyrion and particle-io/voodoospark.

Still to do: rasp-io, edison-io (galileo-io), and nino-io.

nebrius commented 6 years ago

I believe this can be closed now, correct @dtex?

dtex commented 6 years ago

Yes it can :-)