adafruit / Adafruit-PWM-Servo-Driver-Library

Adafruit PWM Servo Driver Library
Other
477 stars 311 forks source link

Add a `writeMicroseconds()` function #62

Closed photodude closed 5 years ago

photodude commented 5 years ago

The function is based on the example servo sketch's setServoPulse() method

ladyada commented 5 years ago

hiya thanks for the contribution

before we can merge, please fix up travis so it compiles/passes :) https://travis-ci.com/adafruit/Adafruit-PWM-Servo-Driver-Library/builds/133676811 also please add a short example demonstrating how you expect this function to be used

photodude commented 5 years ago

@ladyada travis is fixed now (I thought I had read over the code better than that before submitting, c'est la vie)

As for an example. I'm rewriting the RB-ENantel/RC_ESC to use the Adafruit PCA9685 PWM servo driver. The RB-ENantel/RC_ESC library currently uses the Arduino servo library to talk to Electronic Speed Controllers (ESC) and all of the functions are dependent on the Arduino servo library writeMicroseconds() by including a writeMicroseconds() function here it makes modifying libraries that depend on the Arduino servo library a simpler process.

function is used by calling pwm.writeMicroseconds(Pin, microseconds); to set the pulse length in microseconds and not ticks. Example pwm.writeMicroseconds(0, 1000); would make a 1000 microsecond pulse on pin 0 of the PWM/Servo driver the library is attached to (0x40).

pwm.writeMicroseconds(15, 2500); would make a 2500 microsecond pulse on pin 15 of the PWM/Servo driver the library is attached to (0x40).

this mimics the arduino servo.writeMicroseconds(1000); method which would write a 1000us pulse to the pin the library instance was attached to.

photodude commented 5 years ago

There is probably a better way to calculate the frequency from the prescale, I just attempted to reverse what was done in the setPWMFreq() function.

ladyada commented 5 years ago

awesome! great work :) please ping us when the example is added

photodude commented 5 years ago

@ladyada I take it from your response I need include something in the code examples folder showing use of this function.

Would it be sufficient if I extend the existing servo example with this new function?

ladyada commented 5 years ago

yep! that sounds good

photodude commented 5 years ago

@ladyada the Servo example is now extended to include writeMicroseconds().

ladyada commented 5 years ago

nice! what hardware did you test with?

photodude commented 5 years ago

@ladyada Tested with an arduino Uno, a Flycolor ESC, a 950KV motor. Was able to correctly arm and control the motor speed.