Topic:
Is it possible to command the EPOS4, IDX, 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, EPOS2, or IDX.
Based on the feedback by lots of Python and EPOS4 users we recommend to use "ctypes" for Python application programming based on the "EPOS Command Library".
Solution: "ctypes" library
The only thing which is needed to command the EPOS4, IDX, or EPOS2 by Python is the "ctypes" library and maxon's "EPOS Command library".
1.) ctypes
ctypes is a function library which provides C compatible data types and allows calling functions in DLLs (like maxon's "EPOS Command library"). It is installed by most Python programming environments for Windows and Linux 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
- Take care: Data type conversion -> Stumbling block!
Variable types of pass values, return values and return pointers of the 'C'-based "EPOS Command Library" functions must be correctly converted to the corresponding Python and ctypes and data types, i.e. the corresponding matching ctypes data declarations must be used, e.g. ...
- pNbOfBytesRead=c_uint()
pData=c_uint()
pPositionIs=c_long()
pErrorCode=c_uint() - Find "ctypes" documentation dealing with data types here:
- 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:
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. (in case of Linux and a Raspberry Pi):
path='/home/pi/src/python/libEposCmd.so.6.7.1.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.
Windows + ctypes + EPOS Command Library
- DLL version 32 bit / 64 bit:
Please ensure that you have the EposCmd64.dll in use in case of a Windows 64 Bit system. Otherwise you will face a Win32 error by Python. - Documentation & sample project:
Please find below the link to a very professional information, documentation, and sample code by a maxon user and software engineer who successfully used Python and the "EPOS Command Library" by a Windows system:
-> Interfacing and Controlling Maxon Motors Using Python | by M Khorasani | May, 2021 | Medium
-> https://github.com/mkhorasani/maxon_python_windows_64
Thanks a lot to Mohammad Khorasani.
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?
- If you have any questions, please submit a ticket by maxon's global technical Support Center.
Comments
0 comments
Article is closed for comments.