#!/usr/bin/env python3 """Demonstrates simultaneous control of two servos on the hat. One servo uses the simple default configuration, the other servo is tuned to ensure the full range is reachable. """ from time import sleep from gpiozero import Servo from aiy.pins import PIN_A from aiy.pins import PIN_B # Create a default servo that will not be able to use quite the full range. simple_servo = Servo(PIN_A) # Create a servo with the custom values to give the full dynamic range. tuned_servo = Servo(PIN_B, min_pulse_width=.0005, max_pulse_width=.0019) # Move the Servos back and forth until the user terminates the example. while True: simple_servo.min() tuned_servo.max() sleep(1) simple_servo.mid() tuned_servo.mid() sleep(1) simple_servo.max() tuned_servo.min() sleep(1)
from gpiozero import Button from gpiozero import Servo from time import sleep from signal import pause button_up = Button(21) button_down = Button(20) servo = Servo(14) servo.min() while True: button_up.when_pressed = servo.max() button_down.when_pressed = servo.mix() sleep(3)
from gpiozero import Servo from time import sleep myGPIO = 18 servo = Servo(myGPIO) while True: servo.mid() print("mid") sleep(0.5) servo.min() print("min") sleep(1) servo.mid() print("mid") sleep(0.5) servo.max() print("max") sleep(1)
class Robot: def __init__(self, websocket, servo_pin1=PWM_PIN1, servo_pin2=PWM_PIN2, max_pulse=DEFAULT_MAX_PULSE, min_pulse=DEFAULT_MIN_PULSE): self.init_at = datetime.datetime.now().time() self.left_servo = Servo(PWM_PIN1, frame_width=20 / 1000, max_pulse_width=max_pulse, min_pulse_width=min_pulse) self.right_servo = Servo(servo_pin2, frame_width=20 / 1000, max_pulse_width=max_pulse, min_pulse_width=min_pulse) self.command_type_to_method = { 'movement': self.execute_movement, 'playback': self.handle_playback_task, 'reverse': self.handle_reverse_task, 'upload': self.save_command_sets, 'sonar': self.handle_sonar } self.movement_commands = { 'ArrowUp': self.move_forward, 'ArrowRight': self.turn_right, 'ArrowLeft': self.turn_left, 'ArrowDown': self.move_backwards, 'stop': self.stop } self.command_database_url = "" self.database_path = "" self.websocket = websocket self.sonic_sensor = DistanceSensor(echo=17, trigger=4) self.angular_servo = AngularServo(21, max_pulse_width=2 / 1000, min_pulse_width=1 / 10000) self.current_background_tasks = {} def handle_sonar(self, message): if message == 'start': sonar_task = asyncio.ensure_future(self.transmit_sonar_data()) self.current_background_tasks['sonar'] = sonar_task elif message == 'stop': self.current_background_tasks['sonar'].cancel() async def transmit_sonar_data(self): self.angular_servo.min() await asyncio.sleep(1) while True: for i in chain(range(-90, 91), range(90, -81, -1)): self.angular_servo.angle = i await asyncio.sleep(.024) # fastest is like .004 angle_and_distance = json.dumps( (i, round(self.sonic_sensor.distance * 100, 2))) await self.websocket.send(angle_and_distance) def move_forward(self): self.left_servo.max() self.right_servo.min() def turn_right(self): self.right_servo.min() self.left_servo.min() def turn_left(self): self.left_servo.max() self.right_servo.max() def move_backwards(self): self.left_servo.min() self.right_servo.max() def stop(self): self.left_servo.mid() self.right_servo.mid() def handle_reverse_task(self, command_set): reverse_task = asyncio.ensure_future(self.execute_reverse(command_set)) self.current_background_tasks['movement'] = reverse_task async def execute_reverse(self, command_set): reversed_directions = { 'ArrowUp': 'ArrowDown', 'ArrowDown': 'ArrowUp', 'ArrowRight': 'ArrowLeft', 'ArrowLeft': 'ArrowRight' } direction_duration_tups = RobotUtils.process_command_set( command_set) #[('ArrowRight',3411),('ArrowDown',5000)] direction_duration_tups.reverse() for direction, duration in direction_duration_tups: direction = reversed_directions[direction] print("Executing {} for {} ms".format(direction, duration)) self.movement_commands[direction]() await asyncio.sleep(duration / 1000) self.stop() def handle_playback_task(self, command_set): playback_task = asyncio.ensure_future( self.execute_playback(command_set)) self.current_background_tasks['movement'] = playback_task async def execute_playback(self, command_set): direction_duration_tups = RobotUtils.process_command_set( command_set) #[('ArrowRight',3411),('ArrowDown',5000)] for direction, duration in direction_duration_tups: print("Executing {} for {} ms".format(direction, duration)) self.movement_commands[direction]() await asyncio.sleep(duration / 1000) self.stop() def execute_movement(self, direction): if 'movement' in self.current_background_tasks and self.current_background_tasks[ 'movement']: self.current_background_tasks['movement'].cancel() self.current_background_tasks['movement'] = None correct_method = self.movement_commands[direction] correct_method() def save_command_sets(self, command_set): with open(self.database_path, 'w') as database: database.write(command_set) def send_command_sets(self): with open(self.database_path) as database: return database.read() def process_message(self, message): message_type, message_data = message['type'], message['data'] correct_method = self.command_type_to_method[ message_type] #returns a method return correct_method( message_data ) #this can potentially return a coroutine which can be cancelled in the main loop
#!/usr/bin/env python3 """Demonstrates simultaneous control of two servos on the hat. One servo uses the simple default configuration, the other servo is tuned to ensure the full range is reachable. """ from time import sleep from gpiozero import Servo from aiy.vision.pins import PIN_A from aiy.vision.pins import PIN_B # Create a default servo that will not be able to use quite the full range. simple_servo = Servo(PIN_A) # Create a servo with the custom values to give the full dynamic range. tuned_servo = Servo(PIN_B, min_pulse_width=.0005, max_pulse_width=.0019) # Move the Servos back and forth until the user terminates the example. while True: simple_servo.min() tuned_servo.max() sleep(1) simple_servo.mid() tuned_servo.mid() sleep(1) simple_servo.max() tuned_servo.min() sleep(1)
try: from picamera import PiCamera except: print("fail load camera") from gpiozero import Servo from time import sleep servoOne = Servo(22) servoTwo = Servo(27) while True: value = input() print(value) servoOne.value = value servoTwo.min() camera = PiCamera() camera.start_preview() sleep(0.5) camera.capture('picture.jpg') camera.stop_preview()
def openServo(): servo = Servo(21) servo.min() sleep(0.2)
from gpiozero import Servo from time import sleep my_servo = Servo('gpio26',min_pulse_width=900/1000000, max_pulse_width=2100/1000000, frame_width=100/1000) while True: my_servo.min() sleep(2) my_servo.max() sleep(2)
from gpiozero import Servo from time import sleep xGPIO = 4 yGPIO = 17 xServo = Servo(xGPIO) yServo = Servo(yGPIO) print("Using GPIO4 for xServo") print("Using GPIO5 for yServo") print("Using Gpiozero defaults for the servo class") while True: xServo.mid() yServo.mid() print("Set to middle position") sleep(1) xServo.min() yServo.min() print("Set to minimum position") sleep(1) xServo.mid() yServo.mid() print("Set to middle position") sleep(1) xServo.max() yServo.max() print("Set to maximum position") sleep(1)
print(valList) if len(valList) == 10: if abs(valList[0] - valList[9]) > 0.1: myServo.value = valList[9] print(valList[9]) else: myServo.value = None valList.clear() else: pass #print(xList, "and", avg) xList.clear() else: pass #print(" Error = {0}, Mapped = {1}, Value = {2}".format(error, mappedError,value)) #calibrat the servo and test the bounds myServo.max() time.sleep(5) myServo.min() time.sleep(5) myServo.mid() time.sleep(5) #operation part while True: orient(targetTilt) fire()
from gpiozero import Servo from time import sleep s = Servo(14) while True: s.min() sleep(0.5) print( "frame_width: %s | min_pulse_width: %s | pulse_width: %s | max_pulse_width: %s | value: %s" % (s.frame_width, s.min_pulse_width, s.pulse_width, s.max_pulse_width, s.value)) s.mid() sleep(0.75) #This ratio should be kept! print( "frame_width: %s | min_pulse_width: %s | pulse_width: %s | max_pulse_width: %s | value: %s" % (s.frame_width, s.min_pulse_width, s.pulse_width, s.max_pulse_width, s.value)) s.value = None
from gpiozero import Servo from time import sleep servo1 = Servo(12) servo2 = Servo(13) while True: servo1.min() servo2.min() sleep(2) servo1.mid() servo2.mid() sleep(2) servo1.max() servo2.max() sleep(2)
""" Servo Controller (test only) """ import time from gpiozero import Servo if __name__ == '__main__': l = Servo(7) l.min() time.sleep(5) l.max() time.sleep(1)