Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

How to control servo with Autohotkey

Hello some Newbie questions here. I have a servo that I want to control via the left and right arrow keys on the keyboard.
For example I want to keep on pushing the left or right arrows to get it to move in small increments. I don’t want to push once and have my servo move all the way to the left or right and stop.
I found this script but it moves all the way to the left or to the right then stops!
Left::
RunWait usccmd --accel 0,12,,Hide RunWait usccmd --speed 0,12,Hide
Run usccmd --servo 0,8000,,Hide return Right:: RunWait usccmd --accel 0,0,Hide
RunWait usccmd --speed 0,0,,Hide Run usccmd --servo 0,4000,Hide
return

Thanks

Hello, Hamdroid.

Since the Maestro does not have a command to move relative to the current position like you are describing, you would have to read the current position, add or subtract your small increment, then set that as the new position. The easiest way to do that is probably by writing a couple subroutines on the Maestro (one for forward and one for reverse), then calling those from your AutoHotkey program like shown in the Start a Sequence section of the Using AutoHotkey with Pololu USB Products application note.

The script for doing something like that would look like this:

sub forward
  0 get_position  #read position of servo 0
  100 plus  #add some amount
  0 servo  #set servo 0 to the new target value
quit

sub reverse
  0 get_position  #read position of servo 0
  100 minus  #subtract some amount
  0 servo  #set servo 0 to the new target value
quit

Then, in your AutoHotkey program Run usccmd --sub 0,,Hide would call the first subroutine (i.e. forward) and Run usccmd --sub 1,,Hide would call the second subroutine (i.e. reverse).

Brandon

Hello, Brandon

Thanks very much this is what I’m looking for. However I get an error(UscCmd has stopped working) from Autohotkey when going in the left movement, The right movement works fine without issues.
This is the Autohotkey Script:
Left::
Run usccmd --sub 0,Hide
Right::
Run usccmd --sub 1,Hide

The Script in the Maestro is the same except I changed all the 0’s to 8.
Thanks

OK working great now, I just added a Return.
Left::
Run usccmd --sub 0,Hide
return
Right::
Run usccmd --sub 1,Hide
return

On another note sometimes I get the UscCmd has stopped working error. This seems to be when I enter a combination of keys to quickly. Is there any way to clear it automatically or hide it?
Thanks again

I suspect your error is caused by multiple instances trying to open and connect to the Maestro at the same time. Does the problem go away if you use AutoHotKey’s RunWait command instead of Run?

Brandon

The RunWait command works fine now. It’s all working great thanks again for the help.

1 Like