1

I'm working on a RPI controlled quadcopter. The PWM is generated by ServoBlaster. I've written a Python script to control the motors' speed. Assume that I'm using GPIO4 to control the motor. If I want to set the pulse wifth to 1000us I have to write this echo 0=1000us > /dev/servoblaster to the terminal (where 0 is the reference to the GPIO4). How can I run this command (writing 0=1000us to /dev/servoblaster) from a Python script in an effective way. The "effective" is important, because I've been using subprocess.call(["echo 0=1000us > /dev/servoblaster"], shell=True) , but this uses much CPU time.

I've also tried: python myScript > /dev/servoblaster, where myScript is:

print "0=1000us"

but it does nothing.

EDIT: I've tried this way:

dev = open('/dev/servoblaster', 'w')
dev.write('0=1000us\n')

but the servo does not moves.

asked Sep 25, 2014 at 15:35

1 Answer 1

1

Open /dev/servoblaster as you would any other file for writing.

Then write 0=1000us to the file.

You'll probably have to terminate every command with a new line, i.e write "0=1000us\n" to the file. If you don't terminate with a newline, the command won't be recognized as complete.

I'd keep the file open for the duration of your script rather than open, write, close for every change in pulse width.

Morgan Courbet
3,7033 gold badges24 silver badges38 bronze badges
answered Sep 25, 2014 at 15:48
4
  • I've tried your solution but the servo does not moves Commented Sep 25, 2014 at 16:01
  • Check that the same command works from the command line. I've just checked with a simple Python script and it works for me (not with servoblaster, with /dev/pigpio). Commented Sep 25, 2014 at 16:21
  • sorry but I don't understand what are you saying :( Commented Sep 25, 2014 at 16:28
  • Yes, the servo works if I write this on the terminal: echo 0=1000us > /dev/servoblaster Commented Sep 25, 2014 at 16:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.