Topic:
Is it possible to command the EPOS4 (or EPOS2) by Python?
Basics:
maxon does not provide documentation, specific libraries, sample code, or support to command the EPOS controllers by Python. Anyway it is easily possible to use the "Epos Command library" by Python to command an EPOS4 (resp. EPOS2).
Based on user's feedback there are two solutions mainly in use to command an EPOS by Python.
- Solution 1: ctypes
A solution which is favored by most users is the "ctypes" library for Python.
The "ctypes" library is installed by most Python environment by default. "ctypes" offers "C" compatible data types conversions and enables calling up of DLL functions (like in use by the "EPOS Command Library").
The usage of "ctypes" is explained below, see section "Solution: ctypes library".
The attached sample code was shared by a EPOS4 and Python user. - Solution 2: pythonnet
Another possible solution is the usage of "pythonnet" (which might be mainly intended for Windows based systems).
The usage of "pythonnet" is explained by the following article:
EPOS2 / EPOS4: Commanding by Python -> pythonnet
Solution: "ctypes" library
The only thing which is needed to command the EPOS4 (or EPOS2) by Python is the "ctypes" library and the "EPOS Command library".
1.) ctypes
The ctypes library is a foreign function library which provides C compatible data types and allows calling functions in DLLs (like maxon "EPOS Command library"). It is installed as one part of most Python programming environments by default.
Detailed documentation about the usage of the "ctypes" Library can be found on the Internet, e.g. ...
- https://docs.python.org/3/library/ctypes.html
- https://pgi-jcns.fz-juelich.de/portal/pages/using-c-from-python.html
The attached two sample programs (for RaspberryPi 3) show nicely how to use ctypes. The keypoints are:
- Import ctypes:
- from ctypes import *
- from ctypes import *
- Copy the "EPOS Command library" into your Python environment and configure the path information properly, e.g.
- path='/home/alg_sys/python/libEposCmd.so.6.6.1.0'
- path='/home/alg_sys/python/libEposCmd.so.6.6.1.0'
- Load the library
- cdll.LoadLibrary(path)
epos=CDLL(path)
- cdll.LoadLibrary(path)
- Define a variable NodeID and configure it according to your EPOS4 node ID setting, e.g.
- NodeID = 1
- NodeID = 1
- Define the data type of return pointers of "EPOS Command library" function calls properly matching the ctypes data types, e.g.
- pNbOfBytesRead=c_uint()
pData=c_uint()
pPositionIs=c_long()
pErrorCode=c_uint() - Find "ctypes" documentation dealing with data types here:
https://docs.python.org/3/library/ctypes.html?highlight=c_uint#ctypes-fundamental-data-types-2
- pNbOfBytesRead=c_uint()
- Use the ctypes "byref" for return pointers of "EPOS Command library" function calls, e.g.
- keyhandle=epos.VCS_OpenDevice('EPOS4', 'MAXON SERIAL V2', 'USB', 'USB0', byref(pErrorCode) )
- ret=epos.VCS_GetPositionIs(keyhandle, NodeID, byref(pPositionIs), byref(pErrorCode))
- ret=epos.VCS_MoveToPosition(keyhandle, NodeID, 20000, 0, 0, byref(pErrorCode))
- Find "ctypes" documentation dealing with "byref" here:
https://docs.python.org/3/library/ctypes.html?highlight=c_uint#passing-pointers-or-passing-parameters-by-reference
2.) "EPOS Command library"
The "EPOS Command library" is installed by the "EPOS Setup" package on your local drive automatically too. The library as well as its documentation "EPOS Command Library.pdf" is part of the following directories by default:
- C:\Program Files (x86)\maxon motor ag\EPOS IDX\EPOS4\04 Programming\Linux Library
- C:\Program Files (x86)\maxon motor ag\EPOS IDX\EPOS4\04 Programming\Windows DLL
Additional important notes:
Compatibility issues in between Python 2 and Python 3
Please be aware that Python 2.x and Python 3.x are not fully compatible anymore. There are some commands which have been added to Python 3.x. Other commands might be missing or not be fully identical anymore. There are some hints and documents about such Python incompatibility issues present by the Internet, e.g.
-> Porting Python 2 Code to Python 3 — Python 3.9.2 documentation
Example:
- The parameters for opening the EPOS4 interface look like this by Python 2.x:
keyhandle=epos.VCS_OpenDevice('EPOS4', 'MAXON SERIAL V2', 'USB', 'USB0', byref(pErrorCode) ) - The same has to look like this by Python 3.x:
keyhandle=epos.VCS_OpenDevice(b'EPOS4', b'MAXON SERIAL V2', b'USB', b'USB0', byref(pErrorCode) )
Please note the Python version number present by the file name of the attached sample code.
Configuration of the library path:
Please take care that the directory path which holds the DLL is configured properly, e.g. path='/home/pi/src/python/libEposCmd.so.6.6.2.0'
Please ensure that you have the latest "EPOS Command Library" version in use which is present by your "EPOS Studio" installation or maxon's EPOS4 websites.
Remark:
- Please note that maxon cannot provide support or training for Python, ctypes, or any application specific adaptation of the program example.
- The sample program code can be shared, modified, and used for free without any restrictions.
- maxon disclaims any liability and warranty based on the usage of the sample code.
- We are pleased about your feedback if this information was helpful and you managed to command the EPOS4 by Python.
Comments
0 comments
Article is closed for comments.