Question about getting the current position

Hi,

I’m using Python (Serial, Pygame) to controll servos with a joystick.
My question now is if i can get the current position of a servo.
In the documentation only compact and pololu protocoll are listet for that function.

Is there a way for me to do this with the libraries I’m using ?

If not, what other libraries for python should i use ?

Thanks in advance.

Hello. What Pololu product are you asking about? --David

I’m talking about the micro meastro 6 channel usb servo controller.
Sry for not mentioning that in the initial post O_o

Ok. Presumably, at some level you are using a general purpose serial library (pyserial?) that can read and write bytes from a Maestro. If you get access to the object that represents the Maestro’s serial port, you can send the Get Position command that you found in the user’s guide and retrieve a response from the Maestro. I recommend using the Compact protocol since it sounds like you only have one device.

–David

We have some sample Maestro + Serial + Python code here, but it only sets the target:

–David

How are you sending the command to the servo in your script?
You can easily request the “position” of any channel on the maestro with the 0x90 command, but in the case of a servo, it’ll only return the position it’s being driven at - which you should already have, since you are sending that information…

Basically, there is no way to know what is the actual position of an RC servo - it’s calculated internally and never communicated. Unless you use a sensor of some sort, and connect it to the output of the servo (a potentiometer, for example).

Here a sample python code that’ll send the GET_POSITION command to channel 18, and convert the returned bytes into an integer from 0 to 1024:

[size=85]import serial
import struct #separate module, google python + struct to find it and install it
from struct import *

ser = serial.Serial(4) #open port on COM5 - make sure to change it to whatever is the port you’re using
ser.write(chr(0x90)+chr(0x12)) #send the get position command to channel 18
rawResult = ser.read()+ser.read() #read the bytes pair sent back from the maestro
tupleResult = struct.unpack(’<H’, rawResult) #make sense out of those bytes
intResult = tupleResult[0] #get an integer out of it

print "The value for channel 18 is: ", intResult[/size]

You can try that, making sure you change the COM port to the one you’re using, but I don’t see the point of requesting the position of a servo, since it’ll return you exactly what you are sending it… Unless I didn’t understand what you mean. I’ve been working with Python and the mini Maestro quite a bit, so maybe I can help.

Actually, I take that back (the uselessness of requesting a servo position)…

If you are using speed and/or acceleration parameters, then the GET_POSITION command will give you the current pulse width being sent by the maestro to the servo, which could be different than what you sent originally since values are interpolated in such case…

Thanks a lot for your detailed responses.
Actually i just wanted to get the position because i wanted to know where the servo is at when the scripts starts up but I guess it’d be easier just to move the servo to a standard position at startup. After that - you’re right - i do know where i send him to so i guess it really would be kinda pointless to get the position.