function res = getServoPosition(index)
%GETSERVOPOSITION  Get the current position of a servo motor.
%   res = getServoPosition(index) returns the position of the servo motor
%   at the specified index.

global ADVSERVO;

if index < 0 || index > 7
    error('index must be in the range 0 to 7');
end

valPtr = libpointer('doublePtr', 0);
code = calllib('phidget21', 'CPhidgetAdvancedServo_getPosition', ADVSERVO, index, valPtr);
if code == 0
    res = valPtr.Value;
else
    %error(phidgetError(code));
    disp('getServoPosition error: Is the servo engaged?');
end
