Search in sources :

Example 1 with I2CEndPoint

use of com.robo4j.units.rpi.I2CEndPoint in project robo4j by Robo4J.

the class PCA9685ServoUnit method onInitialization.

/**
	 *
	 * @param configuration
	 *            - unit configuration
	 * @throws ConfigurationException
	 */
@Override
protected void onInitialization(Configuration configuration) throws ConfigurationException {
    super.onInitialization(configuration);
    Object pwmDevice = I2CRegistry.getI2CDeviceByEndPoint(new I2CEndPoint(getBus(), getAddress()));
    PWMPCA9685Device pcaDevice = null;
    try {
        if (pwmDevice == null) {
            pcaDevice = new PWMPCA9685Device(getBus(), getAddress());
            I2CRegistry.registerI2CDevice(pcaDevice, new I2CEndPoint(getBus(), getAddress()));
            pcaDevice.setPWMFrequency(50);
        } else {
            pcaDevice = (PWMPCA9685Device) pwmDevice;
        }
    } catch (IOException e) {
        throw new ConfigurationException("Could not initialize hardware", e);
    }
    channel = configuration.getInteger(CONFIGURATION_KEY_CHANNEL, -1);
    if (channel == -1) {
        throw ConfigurationException.createMissingConfigNameException(CONFIGURATION_KEY_CHANNEL);
    }
    servo = new Servo(pcaDevice.getChannel(channel));
    servo.setTrim(configuration.getFloat(CONFIGURATION_KEY_TRIM, 0f));
    servo.setInverted(configuration.getBoolean(CONFIGURATION_KEY_INVERTED, false));
    servo.setDualRate(configuration.getFloat(CONFIGURATION_KEY_DUAL_RATE, 1.0f));
    servo.setExpo(configuration.getFloat(CONFIGURATION_KEY_EXPO, 0.0f));
}
Also used : ConfigurationException(com.robo4j.core.ConfigurationException) Servo(com.robo4j.hw.rpi.i2c.pwm.Servo) I2CEndPoint(com.robo4j.units.rpi.I2CEndPoint) PWMPCA9685Device(com.robo4j.hw.rpi.i2c.pwm.PWMPCA9685Device) IOException(java.io.IOException)

Aggregations

ConfigurationException (com.robo4j.core.ConfigurationException)1 PWMPCA9685Device (com.robo4j.hw.rpi.i2c.pwm.PWMPCA9685Device)1 Servo (com.robo4j.hw.rpi.i2c.pwm.Servo)1 I2CEndPoint (com.robo4j.units.rpi.I2CEndPoint)1 IOException (java.io.IOException)1