# Don't use device before ipcon is connected # Configure two servos with voltage 5.5V # Servo 1: Connected to port 0, period of 19.5ms, pulse width of 1 to 2ms # and operating angle -100 to 100° # # Servo 2: Connected to port 5, period of 20ms, pulse width of 0.95 # to 1.95ms and operating angle -90 to 90° servo.set_output_voltage(5500) servo.set_degree(0, -10000, 10000) servo.set_pulse_width(0, 1000, 2000) servo.set_period(0, 19500) servo.set_acceleration(0, 1000) # Slow acceleration servo.set_velocity(0, 0xFFFF) # Full speed servo.set_degree(5, -9000, 9000) servo.set_pulse_width(5, 950, 1950) servo.set_period(5, 20000) servo.set_acceleration(5, 0xFFFF) # Full acceleration servo.set_velocity(5, 0xFFFF) # Full speed servo.set_position(0, 10000) # Set to most right position servo.enable(0) servo.set_position(5, -9000) # Set to most left position servo.enable(5) raw_input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect()
# Use position reached callback to swing back and forth def cb_reached(servo_num, position): if position == 9000: print('Position: 90°, going to -90°') servo.set_position(servo_num, -9000) elif position == -9000: print('Position: -90°, going to 90°') servo.set_position(servo_num, 9000) else: print('Error') # Can only happen if another program sets position if __name__ == "__main__": ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Register "position reached callback" to cb_reached # cb_reached will be called every time a position set with # set_position is reached servo.register_callback(servo.CALLBACK_POSITION_REACHED, cb_reached) # Set velocity to 100°/s. This has to be smaller or equal to # maximum velocity of the servo, otherwise cb_reached will be # called too early servo.set_velocity(0, 10000) servo.set_position(0, 9000) servo.enable(0) raw_input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect()
servo = Servo(SERVOUID) # Create device object ipcon.add_device(servo) # Add device to IP connection # Don't use device before it is added to a connection servo.set_degree(motor, -9000, 9000) servo.set_pulse_width(motor, 950, 1950) servo.set_period(motor, 20000) servo.set_acceleration(motor, 7000) servo.set_velocity(motor, 0xFFFF) # Full speed servo.set_degree(steeringsrv, -3600, 3600) servo.set_pulse_width(steeringsrv, 955, 2000) servo.set_period(steeringsrv, 20000) servo.set_acceleration(steeringsrv, 7000) # Full acceleration 0xFFFF servo.set_velocity(steeringsrv, 0xFFFF) # Full speed servo.set_position(motor, stop) servo.set_position(steeringsrv, mid) servo.enable(motor) servo.enable(steeringsrv) # Set Period for coordinates callback to 1s (1000ms) # Note: The callback is only called every second if the # coordinates have changed since the last call! gps.set_coordinates_callback_period(500) gps.set_status_callback_period(50) # Register current callback to function cb_current gps.register_callback(gps.CALLBACK_COORDINATES, cb_coordinates) gps.register_callback(gps.CALLBACK_STATUS, cb_status)