Hi!
So I connected a motor controller that takes PWM input to my arduino such that as far as the arduino is concerned it is controlling a servo (0 negative thrust, 90 stop, 180 positive thrust).
I'm controlling this over pyserial and before I can send any command to my "servo" I have to give it a time.sleep() command of at least 1 second. Why is this necessary and how can I get around it?
Thanks in advance!
Here is my code thus far:
#!/usr/bin/env python
################################################
# Module: servo.py
# Created: 2 April 2008
# Author: Brian D. Wendt
# http://principialabs.com/
# Version: 0.3
# License: GPLv3
# http://www.fsf.org/licensing/
'''
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.usbmodem1411' #usb from arduino
# Set up serial baud rate
ser = serial.Serial(usbport, 9600, timeout=0)
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")
I can tell the robot to move with code like this:
import servo
import time
time.sleep(1)
servo.move(1, 90)