PyFirmata servo control issue (Arduino with Python)

48 Views Asked by At

I am trying to control my servo with PyFirmata2 (Using an Arduino Nano). It works half the times and stops at random angles the other half. Why aren't the results consistent and reliable?

Same result with and without using delays.

Can someone please help rectify this, and is it viable to just switch to PySerial instead?


PYTHON CODE:

import pyfirmata2
from pyfirmata2 import Arduino
import time

board= Arduino("COM14")
iter8 = pyfirmata2.util.Iterator(board)
iter8.start()

servo_pinX = board.get_pin('d:9:s') #Servo-x to pin 9 
servo_pinY = board.get_pin('d:10:s') #Servo-y to pin 10         

def move_servoX(): 
    servo_pinX.write(servoPos[0])
    time.sleep(0.2)

def move_servoY():
    servo_pinY.write(servoPos[1])
    time.sleep(0.2)

servoPos = [18.0,42.0]
move_servoX()
move_servoY()

servoPos = [93.0,0.0]
move_servoX()
move_servoY()

servoPos = [89.0,100.0]
move_servoX()
move_servoY()

servoPos = [8.0,170.0]
move_servoX()
move_servoY()

servoPos = [3.0,83.0]
move_servoX()
move_servoY()

servoPos = [149.0,0.0]
move_servoX()
move_servoY()

servoPos = [90.0,90.0]
move_servoX()
move_servoY()
board.exit()

Expecting the servo to react to all commands but works for the first few angles and then stops :(

Thanks in advance!!

1

There are 1 best solutions below

2
Guillius On

I don't know why you wrote your code the way you did, but I would suggest the following.

See the servo example from the pyfirmata2 library and follow it as closely as possible. You can find it here: https://github.com/berndporr/pyFirmata2/blob/master/examples/servo.py

It seems weird to me that you call board.digital[9].write(10) especially since the example would result in something like this:

servo_5 = board.get_pin('d:5:s')

v = 90.0 # example value

# Set the duty cycle
servo_5.write(v)