I've been studying the Servo library that ships with Galileo and have developed a new version that addresses a couple of issues that I've discovered.
If you'd like to try the new version:
1. Download the latest archive at Releases · mikalhart/galileo-Servo · GitHub
2. Unzip the archive and use the two contained files (Servo.h and Servo.cpp) to overwrite those in ../hardware/arduino/x86/libraries/Servo.
3. Exit the Arduino IDE software and restart it.
First, a bit of background about why the Servo experience on Galileo is a little less than optimal, even after my changes. A typical servo expects to see a series of pulses at 50Hz on its control line. The width ("duty cycle") of each pulse determines whether the servo goes to 0 degrees (2.7% duty), 180 degrees (12% duty), or somewhere in between. On Galileo, this pulse stream is generated by an internal PWM ("pulse width modulation") mechanism, but because this is quite low resolution--the duty cycle is defined by just 8 bits--the effective accuracy of the library for off-the-shelf servos can only be guaranteed to within about 10 degrees.
Now the authors of the original Servo library provide an option, enabled by default, to increase the Servo angular resolution by shrinking the width of the pulse from 20ms (50Hz) to about 5.3ms. While this is an admirable idea in many ways, it doesn't work at all for any of the servos I tried. These sub-$10 servos can't deal with the out-of-spec pulse widths, so they just sit there twitching. And so one of my library changes is to make this "hi resolution", 188Hz mode not the default.
My overriding goal for these changes and the other mods I'm offering is to make it so a person coming from Uno to Galileo experiences as little discomfort as possible. One of the mottoes of the group I work for is "It just works." If we can give a new Galileo user as close as possible to the "it just works" experience, well, so much the better.
Details of changes
- The default min/max pulse widths in Uno (544us and 2400us) have been restored.
- Removed code that recenters (moves to 90 degrees) the servo whenever it is attach()ed to the library or changes resolution. The AVR version of the library does not recenter, and it's bad form to assume that 90 degrees is a safe or even possible position for a given application.
- Default to low-resolution 48Hz mode, because 188Hz mode is not usable by many servos and we don't want 'panicked twitching' to be the default experience.
- Removed a diagnostic line that floods the Serial monitor and resets the minimum pulse with to an incorrect value.
- Replace hard constants 0 and 180 with symbols defined in Servo.h
Comments and change suggestions encouraged.
LIbrary at Releases · mikalhart/galileo-Servo · GitHub.