After setting up the PWM outputs on BeagleBoneBlack, I created a binding for OpenHAB. In this way, I can create items and widgets that controls servos and are fully integrated into the OpenHAB UI.
What I basically did was to make some changes in the existing GPIO binding to accomodate some new configuration keywords.
The original GPIO binding accepts the following configuration settings
gpio="pin:PIN_NUMBER [debounce:DEBOUNCE_INTERVAL] [activelow:yes|no]"
After the changes, the binding accepts the following
gpio="pin:PIN_NUMBER [debounce:DEBOUNCE_INTERVAL] [activelow:yes|no] [name:PIN_NAME] pwm:[PWM period]"
The name is the name of the pin configured for PWM as I talked about in my previous post. When either a name is supplied, the output is assumed to be a PWM output
The pwm parameter allows user to set PWM period in microseconds. By default, this parameter is set to 20000 us, that is to say 20 ms which is the typical PWM period for controlling a servo
For example, on my BBB I set
gpio="pin:13 name:pwm_test_P8_13.11"
I also made some changes to the library
- org.openhab.io.gpio
I added some new function overrides to accept an integer parameter that represent the duty cycle to set. Both the PWM and duty cycle are set by writing the values to the corresponding files. For example, to set the PWM the following code is executed
Files.write(runPath, "0".getBytes()); Files.write(periodPath, Long.valueOf(this.pwmPeriod).toString().getBytes()); Files.write(runPath, "1".getBytes());
In the same way, to set duty cycle the following code is executed
Files.write(runPath, "0".getBytes()); Files.write(dutyPath, value.toString().getBytes()); Files.write(runPath, "1".getBytes());
Note that both PWM and duty cycle values are passed to the class functions expressed in microseconds and hence they are multiplied by 1000 before being written.
runPath, dutyPath and periodPath are respectively set to
/sys/devices/ocp.3/<pin name>/run
/sys/devices/ocp.3/<pin name>/duty
/sys/devices/ocp.3/<pin name>/period
I finally created a test configuration with a single item
Number Home_Servo "Servo [%.0f]" {gpio="pin:13 name:pwm_test_P8_13.11"}
and a page with two controls: a setpoint to change the PWM duty cycle and a Text to provide feedback about the current PWM duty cycle
sitemap demo label="Main Menu" { Frame label="Home"{ Setpoint item=Home_Servo label="PWM" minValue=1500 maxValue=2500 step=100 Text item=Home_Servo } }