How to run a component in a non-realtime task

More
09 Mar 2023 16:27 - 09 Mar 2023 16:27 #266220 by BaxEDM
Hi,
I've got several M-code executable bash scrips that I call from within my g-code. I would like to add a button that can call the same script. So I've written a component that uses system() to execute my script upon a button press. Sometimes when I press the button I get an unexpected realtime delay in my servo task, since the servo thread is also the one executing my script, which apparently takes too long occasionally.
I do not need to execute the script in realtime and would rather run it in a non-realtime thread, which would fix the problem.
I've been diving into the documentation but cannot find a non realtime approach of setting up a new thread and adding my component to it.

Any examples? 
Last edit: 09 Mar 2023 16:27 by BaxEDM.

Please Log in or Create an account to join the conversation.

More
09 Mar 2023 21:15 - 09 Mar 2023 21:24 #266233 by BaxEDM
figured it out. The relevant HAL command is loadusr
the component code is different than what you would write for a RT component. My RT components are written in C and I use halcompile for those.

The simplest way to write a non realtime component is python. Here's an example for a file called my_component.py, which you can then load with:

loadusr ~/linuxcnc/configs/myconfig/my_component.py

Make sure the python script is executable with sudo chmod +x my_component.py

Here's the contents of my_component.py :

#!/usr/bin/env python

import hal

class MyComponent(object):
    def __init__(self):
        self.halcomp = hal.component("my_component")
        self.halcomp.newpin("input", hal.HAL_FLOAT, hal.HAL_IN)
        self.halcomp.newpin("output", hal.HAL_FLOAT, hal.HAL_OUT)
        self.halcomp.ready()

    def run(self):
        self.halcomp["output"] = self.halcomp["input"] * 2

if __name__ == "__main__":
    comp = MyComponent()
    while True:
        comp.run()

 
Last edit: 09 Mar 2023 21:24 by BaxEDM.

Please Log in or Create an account to join the conversation.

More
11 Mar 2023 23:37 #266442 by andypugh
For the benefit of folk finding this thread in the future, PYthion Userpace component creation is documented here:

linuxcnc.org/docs/stable/html/hal/halmodule.html

Please Log in or Create an account to join the conversation.

Time to create page: 0.077 seconds
Powered by Kunena Forum