use of com.robo4j.hw.rpi.i2c.pwm.Servo in project robo4j by Robo4J.
the class PWMPCA9685DeviceTest method main.
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Creating device...");
PWMPCA9685Device device = new PWMPCA9685Device();
device.setPWMFrequency(SERVO_FREQUENCY);
Servo servo0 = new Servo(device.getChannel(0));
Servo servo1 = new Servo(device.getChannel(1));
PWMChannel motor0 = device.getChannel(2);
PWMChannel motor1 = device.getChannel(3);
System.out.println("Setting start conditions...");
servo0.setInput(0);
servo1.setInput(0);
motor0.setPWM(0, MOTOR_MIN);
motor1.setPWM(0, MOTOR_MIN);
System.out.println("Press enter to run loop!");
System.in.read();
System.out.println("Running perpetual loop...");
while (true) {
servo0.setInput(-1);
servo1.setInput(-1);
motor0.setPWM(0, MOTOR_MEDIUM);
motor1.setPWM(0, MOTOR_MEDIUM);
Thread.sleep(500);
servo0.setInput(1);
;
servo1.setInput(1);
;
motor0.setPWM(0, MOTOR_MAX);
motor1.setPWM(0, MOTOR_MAX);
Thread.sleep(500);
servo0.setInput(0);
servo1.setInput(0);
motor0.setPWM(0, MOTOR_MIN);
motor1.setPWM(0, MOTOR_MIN);
Thread.sleep(1000);
}
}
use of com.robo4j.hw.rpi.i2c.pwm.Servo 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));
}
Aggregations