Creating custom HAL pin in QtPyVCP

More
22 Jul 2023 19:17 #276013 by DauntlessA
I'm trying to create a hal pin that indicates the current coordinate system. This pin does not exist in HALUI. There was some discussion of the creation of this pin here:
forum.linuxcnc.org/24-hal-components/454...or-coordinate-system
'If you converted it to one of the others then I think that the Python handler code could poll the linuxcnc.status() and update itself.'
(The value to retrieve is g5x_index.)

This link explains how to use the Python Interface:
linuxcnc.org/docs/2.7/html/config/python...ding_linuxcnc_status

This link explains how to use GStat to create a custom HAL pin:
linuxcnc.org/docs/html/gui/GStat.html#_sample_gstat_code

I was wondering if I chose to go down this route whether this is the simplest way to do this in QtPyVCP?, or am I missing something and there is a better way? Is there anything else that I should be aware of?

The purpose of this hal pin is create a message whenever the coordinate system is changed in auto mode, primarily used to indicate when M5 or M30 change the coordinate system. I recently destroyed a touch probe by accidentally ending a handwritten probing routine with M30, causing me to accidentally run the program in wrong coordinate system the second time, and rapid the probe into a fixture.

Any help is much appreciated!

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

More
23 Jul 2023 01:22 - 23 Jul 2023 01:23 #276045 by KCJ
You mention GStat...
Are you using QtVCP which is part of LinuxCNC, or QtPyVCP which is a separate project?

If using QtPyVCP it should be easy to add such a HAL pin using the HAL interface and the LinuxCNC status plugin.

Connect a handler to the status.g5x_index datachannel and set the HAL pin the value passed to the handler.

www.qtpyvcp.com/hal.html
www.qtpyvcp.com/designer/plugins/status.html

I'll see if I can provide an example.
Last edit: 23 Jul 2023 01:23 by KCJ.
The following user(s) said Thank You: Lcvette, DauntlessA

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

More
23 Jul 2023 01:40 - 23 Jul 2023 01:51 #276046 by KCJ
This is the code you would add to your VCP mainwindow.py file to add the HAL pin
from qtpyvcp import hal
from qtpyvcp.plugins import getPlugin

STATUS = getPlugin('status')

class MainWindow(VCPMainWindow):
"""Main window class for the VCP."""
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)

# connect status signals
STATUS.g5x_index.notify(self.ong5xIndexChanged)

# create a new component and add some pins
self.hal_comp = hal.getComponent()
self.hal_comp.addPin("g5x_index", "float", "out")

def ong5xIndexChanged(self, g5x_index):
self.hal_comp.getPin("g5x_index").value = g5x_index


 
Attachments:
Last edit: 23 Jul 2023 01:51 by KCJ.
The following user(s) said Thank You: Lcvette, DauntlessA

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

More
23 Jul 2023 02:16 - 23 Jul 2023 23:15 #276048 by DauntlessA
Thanks so much for the help! Somehow managed to not spot www.qtpyvcp.com/hal.html.

I'm using probe_basic, so I've added your example to my probe_basic.py. 
            
I still can't see the qtpyvcp.g5x_index pin under Hal Show, and I can't add it to a signal in my custom_postgui.hal file. Have I missed anything?
Last edit: 23 Jul 2023 23:15 by DauntlessA. Reason: Removed code insert due to excessive length and the fact it wasn't correct, adding nothing to the post. Correct code posted below.

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

More
23 Jul 2023 23:13 - 23 Jul 2023 23:47 #276114 by DauntlessA
Solved, the issue was with indentation, and using tabs instead of spaces. The following worked!

Thank you, this should be very useful!

I've got a bit more work to do as g5x_index currently initialises with a value of 0 (indicating G53) and it would be preferable if it initialised with a value indicating the active work coordinate system (which is persistent across restarts in my case). This means that initialising with the stored value could be an option (although there may be simpler ways).
 
Attachments:
Last edit: 23 Jul 2023 23:47 by DauntlessA. Reason: Discussing followup without cluttering thread
The following user(s) said Thank You: Lcvette

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

More
24 Jul 2023 17:33 #276158 by KCJ
Glad it worked!

You can initialize the HAL pin value like this:
self.hal_comp.getPin("g5x_index").value = STATUS.g5x_index.value

This should work if placed at the end of your __init__ method.
The following user(s) said Thank You: Lcvette, blauerklappstuhl, DauntlessA

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

Moderators: KCJLcvette
Time to create page: 0.908 seconds
Powered by Kunena Forum