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.
1 Answer 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.
-
I've tried your solution but the servo does not movesAlex– Alex2014年09月25日 16:01:13 +00:00Commented 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).joan– joan2014年09月25日 16:21:51 +00:00Commented Sep 25, 2014 at 16:21
-
sorry but I don't understand what are you saying :(Alex– Alex2014年09月25日 16:28:12 +00:00Commented Sep 25, 2014 at 16:28
-
Yes, the servo works if I write this on the terminal: echo 0=1000us > /dev/servoblasterAlex– Alex2014年09月25日 16:33:03 +00:00Commented Sep 25, 2014 at 16:33