Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to C library wrapper #23

Merged
merged 11 commits into from
Nov 18, 2023
74 changes: 51 additions & 23 deletions acspy/acsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,29 @@ def getMotorState(hcomm, axis, wait=SYNCHRONOUS):
return mst


def getMotorError(hcomm, axis, wait=SYNCHRONOUS):
"""Get the motor error for disabling."""
error = ctypes.c_int()
call_acsc(acs.acsc_GetMotorError, hcomm, axis, byref(error), wait)
error = error.value
return error

def getErrorString(hcomm, error:int):
"""Retrieves the explanation of an error code."""
err_lng = int32()
s = create_string_buffer(256)
call_acsc(
acs.acsc_GetErrorString,
hcomm,
error,
s,
int32(ctypes.sizeof(s)),
byref(err_lng)
)
error_string = s.value.decode("ascii")
return error_string


def getAxisState(hcomm, axis, wait=SYNCHRONOUS):
"""Gets the axis state. Returns a dictionary with the following keys
* "lead"
Expand Down Expand Up @@ -277,27 +300,20 @@ def toPointM(hcomm, flags, axes, target, wait=SYNCHRONOUS):
call_acsc(acs.acsc_ToPointM, hcomm, flags, axes_c, target_c, wait)


def enable(hcomm, axis, wait=SYNCHRONOUS):
def enableMotor(hcomm, axis:int, wait=SYNCHRONOUS):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't rename this in case others are depending on it (like some of my apps!) You can create an alias if you'd like, but I've just been mirroring the ACS function names, which here is just enable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Pete, ok! On it. Thanks for your answer.

"""The function activates a motor."""
call_acsc(acs.acsc_Enable, hcomm, int32(axis), wait)


def commutate(
hcomm,
axis,
current=DEFAULT,
settle=DEFAULT,
slope=DEFAULT,
wait=SYNCHRONOUS,
):
call_acsc(
acs.acsc_CommutExt,
hcomm,
int32(axis),
float_(current),
int32(settle),
int32(slope),
wait,
)
petebachant marked this conversation as resolved.
Show resolved Hide resolved
def enableMotors(hcomm, axes:list, wait=SYNCHRONOUS):
"""The function activates several motors."""
axes_array = (ctypes.c_int * len(axes))(*axes)
call_acsc(acs.acsc_EnableM, hcomm, axes_array, wait)


def commutateMotor(hcomm, axis:int, current=DEFAULT, settle=DEFAULT, slope=DEFAULT, wait=SYNCHRONOUS):
"""This function initiates a motor commutation."""
call_acsc(acs.acsc_CommutExt, hcomm, int32(axis), float_(current), int32(settle), int32(slope), wait)


def waitCommutated(hcomm, axis, timeout=INFINITE):
Expand All @@ -310,10 +326,21 @@ def waitCommutated(hcomm, axis, timeout=INFINITE):
)


def disable(hcomm, axis, wait=SYNCHRONOUS):
def disableMotor(hcomm, axis, wait=SYNCHRONOUS):
"""The function shuts off a motor."""
call_acsc(acs.acsc_Disable, hcomm, int32(axis), wait)


def disableAllMotors(hcomm,wait=SYNCHRONOUS):
"""The function shuts off all motors."""
call_acsc(acs.acsc_DisableAll, hcomm, wait)


def disableMotors(hcomm, axes:list, wait=SYNCHRONOUS):
"""The function shuts off several specified motors."""
axes_array = (ctypes.c_int * len(axes))(*axes)
call_acsc(acs.acsc_DisableM, hcomm, axes_array, wait)

def getRPosition(hcomm, axis, wait=SYNCHRONOUS):
pos = double()
call_acsc(acs.acsc_GetRPosition, hcomm, axis, p(pos), wait)
Expand Down Expand Up @@ -586,9 +613,7 @@ def uploadDataFromController(
def loadBuffer(hcomm, buffnumber, program, count=512, wait=SYNCHRONOUS):
"""Load a buffer into the ACS controller."""
prgbuff = ctypes.create_string_buffer(str(program).encode(), count)
call_acsc(
acs.acsc_LoadBuffer, hcomm, buffnumber, byref(prgbuff), count, wait
)
call_acsc(acs.acsc_LoadBuffer, hcomm, buffnumber, byref(prgbuff), count, wait)


def loadBuffersFromFile(hcomm, filename, wait=SYNCHRONOUS):
Expand Down Expand Up @@ -704,6 +729,9 @@ def call_acsc(func, *args, **kwargs):
raise AcscError(err)
return rv

def cancelOperation(hcomm, wait=SYNCHRONOUS):
"""Cancels all of the waiting and non-waiting calls."""
call_acsc(acs.acsc_CancelOperation, hcomm, wait)

if __name__ == "__main__":
"""Some testing can go here"""
Expand All @@ -712,4 +740,4 @@ def call_acsc(func, *args, **kwargs):
print(getOutput(hc, 1, 16))
closeComm(hc)

call_acsc(acs.acsc_Halt, hc, 0, SYNCHRONOUS)
call_acsc(acs.acsc_Halt, hc, 0, SYNCHRONOUS)