#!/usr/bin/env python ################################################ # Module: servo.py # Created: 2 April 2008 # Updated: 12 May 2020 # Author: Brian D. Wendt # Version: 0.3 # License: MIT ''' Provides a serial connection abstraction layer for use with Arduino "MultipleSerialServoControl" sketch. ''' ################################################ import serial # Assign Arduino's serial port address # Windows example # usbport = 'COM3' # Linux example # usbport = '/dev/ttyUSB0' # MacOSX example # usbport = '/dev/tty.usbserial-FTALLOK2' usbport = '/dev/tty' # Set up serial baud rate try: ser = serial.Serial(usbport, 9600, timeout=1) except: print("\nCannot connect to serial on port:", usbport) exit() def move(servo, angle): '''Moves the specified servo to the supplied angle. Arguments: servo the servo number to command, an integer from 1-4 angle the desired servo angle, an integer from 0 to 180 (e.g.) >>> servo.move(2, 90) ... # "move servo #2 to 90 degrees"''' if (0 <= angle <= 180): ser.write(chr(255)) ser.write(chr(servo)) ser.write(chr(angle)) else: print("Servo angle must be an integer between 0 and 180.\n")