Using HAL from Python
25 Jan 2023 09:19 #262865
by juniorfi
Using HAL from Python was created by juniorfi
Hi!
I'm tying to be able to get and change pins values from Python.
I have been trying it for a while Cython but haven't got it working. The best near approach I have made, was today re-using HAL Python library that LinuxCNC includes by default on the images provided.
Files used and placed under /usr/lib/....:
- _hal.arm-linux-gnueabihf.so
- hal.py
- hal.pyc
- hal_glib.py
- hal_glib.pyc
- liblinuxcnchal.so.0 (Would like to know how compile it)
---
My setup is with a RPi4 + EtherCAT modules. -All working nicely- using HAL-CORE right now:
github.com/grotius-cnc/hal-core
This is my runtest file:
The issue I'm facing right now is when using this code, is the halrun version code mismatch:
The code I used:
My idea is to be able to use it in real-time (I know Python isn't the best for it) but approximately will be great. Because actually I'm calling halcmd every time I want to do a movement, and that's not the best way of doing it.
Best regards!
I'm tying to be able to get and change pins values from Python.
I have been trying it for a while Cython but haven't got it working. The best near approach I have made, was today re-using HAL Python library that LinuxCNC includes by default on the images provided.
Files used and placed under /usr/lib/....:
- _hal.arm-linux-gnueabihf.so
- hal.py
- hal.pyc
- hal_glib.py
- hal_glib.pyc
- liblinuxcnchal.so.0 (Would like to know how compile it)
---
My setup is with a RPi4 + EtherCAT modules. -All working nicely- using HAL-CORE right now:
github.com/grotius-cnc/hal-core
This is my runtest file:
#!/usr/bin/bash
# Startup hal-core
cd /opt/hal-core/scripts/ && . ./rip-environment
cd /opt/hal-core/bin
halcmd stop
halcmd loadrt threads name1=base-thread fp1=0 period1=1000000
#name2=servo-thread period2=1
# Unix command to load the ethercat .xml config
/opt/hal-core/rtlib/./lcec_conf /opt/hal-core/rtlib/ethercat-conf.xml &
halcmd loadrt lcec
halcmd net ec-slaves-responding <= lcec.slaves-responding
halcmd net ec-link-up <= lcec.link-up
halcmd net ec-all-op <= lcec.all-op
halcmd addf lcec.read-all base-thread
halcmd addf lcec.write-all base-thread
halcmd start
The issue I'm facing right now is when using this code, is the halrun version code mismatch:
HAL: ERROR: version code mismatch
HAL: ERROR: could not init shared memory
Traceback (most recent call last):
File "test", line 3, in <module>
h = hal.component("lcec")
hal.error: Invalid argument
The code I used:
#!/usr/bin/env python
import hal, time
h = hal.component("lcec")
h.newpin("in", hal.HAL_FLOAT, hal.HAL_IN)
h.newpin("out", hal.HAL_FLOAT, hal.HAL_OUT)
h.ready()
try:
while 1:
time.sleep(1)
h['out'] = h['in']
except KeyboardInterrupt:
raise SystemExit
My idea is to be able to use it in real-time (I know Python isn't the best for it) but approximately will be great. Because actually I'm calling halcmd every time I want to do a movement, and that's not the best way of doing it.
Best regards!
Please Log in or Create an account to join the conversation.
25 Jan 2023 09:25 #262866
by juniorfi
Replied by juniorfi on topic Using HAL from Python
The actual Cython ( file_name.pyx ) code is the following in case someone want to have a look:
#!python
#cython: language_level=3
cdef extern from "rtapi_app.h":
cdef int hal_init(char* name)
cdef void hal_ready(int comp_id)
cdef void *hal_malloc(int size)
cdef int hal_pin_float_new(char* name, void* ptr, int comp_id)
cdef void hal_pin_float_set(int pin, double value)
cdef double my_pin_value
def main():
cdef int comp_id
comp_id = hal_init("lcec.0.3.din-0-not")
my_pin_value = hal_malloc(sizeof(double))
hal_pin_float_new("lcec.0.3.din-0-not", my_pin_value, comp_id)
hal_ready(comp_id)
return <object>my_pin_value
if __name__ == "__main__":
main()
Please Log in or Create an account to join the conversation.
25 Jan 2023 09:49 #262868
by juniorfi
Replied by juniorfi on topic Using HAL from Python
I just saw that there was already kind of Cython implementation from a LinuxCNC fork:
github.com/machinekit/machinekit/tree/master/src/hal/cython
github.com/machinekit/machinekit/tree/master/src/hal/cython
Please Log in or Create an account to join the conversation.
28 Jan 2023 17:15 #263095
by andypugh
Replied by andypugh on topic Using HAL from Python
I am not sure quite why you are using cython?
The best way to do this would be to make a Python HAL component, create an output pin in that component, and net that (In HAL) to the pin you want to change the value of.
linuxcnc.org/docs/stable/html/hal/halmodule.html
However, it might be better (and a _lot_ more realtime, to do it in a C realtime component:
This link will take you to some examples that show just how little programming can be needed for a simple HAL component.
linuxcnc.org/docs/stable/html/hal/comp.html#_sincos
Though you would need to read the rest to do it...
The best way to do this would be to make a Python HAL component, create an output pin in that component, and net that (In HAL) to the pin you want to change the value of.
linuxcnc.org/docs/stable/html/hal/halmodule.html
However, it might be better (and a _lot_ more realtime, to do it in a C realtime component:
This link will take you to some examples that show just how little programming can be needed for a simple HAL component.
linuxcnc.org/docs/stable/html/hal/comp.html#_sincos
Though you would need to read the rest to do it...
The following user(s) said Thank You: juniorfi
Please Log in or Create an account to join the conversation.
28 Jan 2023 21:11 #263120
by cmorley
github.com/LinuxCNC/linuxcnc/commits/python3_cython
Are you trying to speed python up?
Replied by cmorley on topic Using HAL from Python
also here, though it didn;t go anywhere:I just saw that there was already kind of Cython implementation from a LinuxCNC fork:
github.com/machinekit/machinekit/tree/master/src/hal/cython
github.com/LinuxCNC/linuxcnc/commits/python3_cython
Are you trying to speed python up?
The following user(s) said Thank You: juniorfi
Please Log in or Create an account to join the conversation.
30 Jan 2023 14:48 #263239
by juniorfi
Replied by juniorfi on topic Using HAL from Python
My goal is to be able to communicate with the pins with ROS.
www.ros.org/
To be able to do that, I need to use Python or C++, because those are the language levels that ROS uses natively.
I was thinking in Cython, because I could use the RTAPI_APP libraries to communicate with the pins already created after -halcmd start- + wrapped in Python.
Thanks for the link you sent me.
I was today thinking to modify halcmd and try to add there the C++ ROS code there. But probably that will not be best way to access to the pins already created and modify it.
Thanks again for the information provided.
Regards
www.ros.org/
To be able to do that, I need to use Python or C++, because those are the language levels that ROS uses natively.
I was thinking in Cython, because I could use the RTAPI_APP libraries to communicate with the pins already created after -halcmd start- + wrapped in Python.
Thanks for the link you sent me.
I was today thinking to modify halcmd and try to add there the C++ ROS code there. But probably that will not be best way to access to the pins already created and modify it.
Thanks again for the information provided.
Regards
Please Log in or Create an account to join the conversation.
30 Jan 2023 14:50 #263240
by juniorfi
Replied by juniorfi on topic Using HAL from Python
Thanks for the link. I haven't see that branch before...
My goal is to be able to communicate with the pins from ROS ( www.ros.org/ ).
Python and C++ is already supported, but not C. I will need to read how to wrap C++ in C code to do that I guess.
Regards
My goal is to be able to communicate with the pins from ROS ( www.ros.org/ ).
Python and C++ is already supported, but not C. I will need to read how to wrap C++ in C code to do that I guess.
Regards
Please Log in or Create an account to join the conversation.
30 Jan 2023 16:00 #263244
by Grotius
Replied by Grotius on topic Using HAL from Python
Hi,
was today thinking to modify halcmd and try to add there the C++ ROS code there. But probably that will not be best way to access to the pins already created and modify it.
Modifying halcmd is a good practice.
I would problably write a tiny python-c++ binding class to achieve goal.
that uses a method like : www.geeksforgeeks.org/how-to-call-c-c-from-python/
Another example that uses this method:
github.com/grotius-cnc/XCreator/blob/mai...ient/patch/skynet.py
Background info : github.com/tdegeus/pybind11_examples/issues/11
was today thinking to modify halcmd and try to add there the C++ ROS code there. But probably that will not be best way to access to the pins already created and modify it.
Modifying halcmd is a good practice.
I would problably write a tiny python-c++ binding class to achieve goal.
that uses a method like : www.geeksforgeeks.org/how-to-call-c-c-from-python/
Another example that uses this method:
github.com/grotius-cnc/XCreator/blob/mai...ient/patch/skynet.py
Background info : github.com/tdegeus/pybind11_examples/issues/11
Please Log in or Create an account to join the conversation.
30 Jan 2023 16:06 #263246
by Grotius
Replied by Grotius on topic Using HAL from Python
- liblinuxcnchal.so.0 (Would like to know how compile it)
You can just recompile lcnc.
Or here is a cmake file for just only the liblinuxcnchal.so
github.com/grotius-cnc/linuxcnc/blob/mai...nchal/CMakeLists.txt
If you should open : github.com/grotius-cnc/linuxcnc/blob/main/cmake/CMakeLists.txt
in qt. You will see the bird view of lcnc, And you can compile each item you want to.
You can just recompile lcnc.
Or here is a cmake file for just only the liblinuxcnchal.so
github.com/grotius-cnc/linuxcnc/blob/mai...nchal/CMakeLists.txt
If you should open : github.com/grotius-cnc/linuxcnc/blob/main/cmake/CMakeLists.txt
in qt. You will see the bird view of lcnc, And you can compile each item you want to.
Please Log in or Create an account to join the conversation.
07 Mar 2023 19:16 #266056
by juniorfi
Replied by juniorfi on topic Using HAL from Python
Thanks for the information you provided.
Now I will change my focus on C/C++ instead Python.
I think I will get more flexibility and compatibility overall.
Regards.
Now I will change my focus on C/C++ instead Python.
I think I will get more flexibility and compatibility overall.
Regards.
Please Log in or Create an account to join the conversation.
Time to create page: 0.106 seconds