python hal component userspace

63 views
Skip to first unread message

mngr

unread,
May 16, 2019, 10:53:25 AM5/16/19
to Machinekit
Hi,

I am trying to build userspace hal components, and I am trying to build them with python.

I can find a lot of examples in the repository calling machinekit.rtapi.loadrt, but I have the vague idea of using machinekit.hal.loadusr

Until now the only solution i have found is something structured like this
#comp file
from machinekit import hal

comp
=hal.component("name")
comp
.newpin("a", HAL.IN , HAL.BIT)

while 1:
  comp
["a"]=comp["b"]
  time
.sleep(1)



and loading

hal.loadusr("comp")



Is this the way it should be used?

I know the hal.addf function, I would like to use it to load function on threads. Is it possible to export function from userspace components? I haven't found any way of exporting functions from python

I would like to write something like that
#comp file
from machinekit import hal

comp
=hal.component("name")
#comp.newpin...

def foo():
  comp
["a"]=comp["b"]

hal
.export(foo)


hal.loadusr("comp")
hal
.addf("foo","servo-thread")



Can you please give me some advice?

mngr












































Bas de Bruijn

unread,
May 16, 2019, 11:15:14 AM5/16/19
to Machinekit


On Thursday, May 16, 2019 at 4:53:25 PM UTC+2, mngr wrote:
Hi,

I am trying to build userspace hal components, and I am trying to build them with python.

I can find a lot of examples in the repository calling machinekit.rtapi.loadrt, but I have the vague idea of using machinekit.hal.loadusr

Until now the only solution i have found is something structured like this
#comp file
from machinekit import hal

comp
=hal.component("name")
comp
.newpin("a", HAL.IN , HAL.BIT)

while 1:
  comp
["a"]=comp["b"]
  time
.sleep(1)



and loading

hal.loadusr("comp")



Is this the way it should be used?

I think you're mixing concepts.

a python userspace hal component is a component created in Python (in this case) which exposes its pins in HAL. It's a userspace component. So you initiate setting output pins and read input pins from within your python code. So it's not executed in the same rigid RT time like the realtime components. You have to take care that you create a loop to do so if you plan to do it more than once.
 

I know the hal.addf function, I would like to use it to load function on threads. Is it possible to export function from userspace components? I haven't found any way of exporting functions from python

You do not expose a userland function and add it to a HAL thread as with a realtime component. You can't, do not want and do not need that. You execute your code from python. It's running independent from the realtime thread. You read/write your userspace component pins once, twice, many times, or not at all. It is not related to the realtime HAL thread..

hal.addf("some_rt_component.funct", "servothread") is the same as using `halcmd addf some_rt_component.funct servothread`


I would like to write something like that
#comp file
from machinekit import hal

comp
=hal.component("name")
#comp.newpin...

def foo():
  comp
["a"]=comp["b"]

hal
.export(foo)


hal.loadusr("comp")
hal
.addf("foo","servo-thread")


hal.loadusr("something") is used for loading a userspace script/program from HAL (you can load halscope for example in this way). In your situation this has nothing to do with your python script functionality, other perhaps than starting your python script from HAL, and your python script then should take care of creating a userspace component with pins, looping and whatnot.
 


Can you please give me some advice?

hope it helped :)


mngr













































 

schoo...@gmail.com

unread,
May 16, 2019, 11:16:44 AM5/16/19
to machi...@googlegroups.com
userspace components are just HAL aware normal binaries, using normal scheduling.
Their polling, within the constraints of the scheduling allowed to them, is set in the main function loop.
(in your example the sleep call)

You cannot attach one to a HAL thread.

If you say what you are trying to do, rather than how you are trying to do it, we may be able to help.

There is no compelling reason to use python at all, writing comp components is probably simpler in terms
of usability, 'comp --install component.comp' and voila, it is available like any other component.
--
website: http://www.machinekit.io blog: http://blog.machinekit.io github: https://github.com/machinekit
---
You received this message because you are subscribed to the Google Groups "Machinekit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to machinekit+...@googlegroups.com.
Visit this group at https://groups.google.com/group/machinekit.
To view this discussion on the web visit https://groups.google.com/d/msgid/machinekit/321cb3b5-7379-447e-8532-063f2e50e47c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages