#!/usr/bin/env python # //////////////////////////////////////// # // servoMotor.py # // Drive a simple servo motor back and forth on P9_16 pin # // Wiring: # // Setup: config-pin P9_16 pwm # // See: # //////////////////////////////////////// import time import signal import sys pwmPeriod = '20000000' # Period in ns, (20 ms) pwm = '1' # pwm to use channel = 'b' # channel to use PWMPATH='/dev/bone/pwm/'+pwm+'/'+channel low = 0.8 # Smallest angle (in ms) hi = 2.4 # Largest angle (in ms) ms = 250 # How often to change position, in ms pos = 1.5 # Current position, about middle ms) step = 0.1 # Step size to next position def signal_handler(sig, frame): print('Got SIGINT, turning motor off') f = open(PWMPATH+'/enable', 'w') f.write('0') f.close() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print('Hit ^C to stop') f = open(PWMPATH+'/period', 'w') f.write(pwmPeriod) f.close() f = open(PWMPATH+'/enable', 'w') f.write('1') f.close() f = open(PWMPATH+'/duty_cycle', 'w') while True: pos += step # Take a step if(pos > hi or pos < low): step *= -1 duty_cycle = str(round(pos*1000000)) # Convert ms to ns # print('pos = ' + str(pos) + ' duty_cycle = ' + duty_cycle) f.seek(0) f.write(duty_cycle) time.sleep(ms/1000) # | Pin | pwm | channel # | P9_31 | 0 | a # | P9_29 | 0 | b # | P9_14 | 1 | a # | P9_16 | 1 | b # | P8_19 | 2 | a # | P8_13 | 2 | b