PWM
Not all the XBee protocols support pulse-width modulation (PWM) output handling, but the XBee Java Library provides functionality to manage them. When you configure an IO line as PWM output, you must use specific methods to set and read the duty cycle of the PWM.
For the set case, use the method setPWMDutyCycle(IOLine, double) and provide the IO line configured as PWM and the value of the duty cycle in % of the PWM. The duty cycle is the proportion of 'ON' time to the regular interval or 'period' of time. A high duty cycle corresponds to high power, because the power is ON for most of the time. The percentage parameter of the set duty cycle method is a double, which allows you to be more precise in the configuration.
Setting the duty cycle of an IO line configure as PWM
[...] // Set a duty cycle of 75% to the DIO10_PWM0 line (PWM output). myXBeeDevice.setPWMDutyCycle(IOLine.DIO10_PWM0, 75); [...]
The setPWMDutyCycle() method may fail for the following reasons:
- ACK of the command sent is not received in the configured timeout, throwing a TimeoutException.
- Other errors caught as XBeeException:
- The operating mode of the device is not API or API_ESCAPE, throwing an InvalidOperatingModeException.
- The response of the command is not valid, throwing an ATCommandException.
- There is an error writing to the XBee interface, throwing a generic XBeeException.
The getPWMDutyCycle(IOLine) method of a PWM line returns a double value with the current duty cycle percentage of the PWM.
Getting the duty cycle of an IO line configured as PWM
[...] // Get the duty cycle of the DIO10_PWM0 line (PWM output). double dutyCycle = myXBeeDevice.getPWMDutyCycle(IOLine.DIO10_PWM0); [...]
The getPWMDutyCycle() method may fail for the following reasons:
- ACK of the read command is not received in the configured timeout, throwing a TimeoutException.
- Other errors caught as XBeeException:
- If the operating mode of the device is not API or API_ESCAPE, throwing an InvalidOperatingModeException.
- If the received response does not contain the value for the given IO line, throwing a OperationNotSupportedException.
- If the response to the read command is not valid, throwing an ATCommandException.
- If there is an error writing to the XBee interface, throwing a generic XBeeException.
Note In both cases (get and set), the IO line provided must be PWM capable and must be configured as PWM output.