- Posts: 1108
- Thank you received: 3
Connect PyQt Button to HAL
2 years 8 months ago #292986
by JT
Connect PyQt Button to HAL was created by JT
I'm a bit stumped on how to connect a PyQt button clicked signal to a HAL pin.
I can create a HAL pin and see it in showhal.
After a lot of grepping in src/emc/usr_intf I'm still at a loss how to connect the hal component to the clicked signal of a QPushButton.
After creating the hal component do you need to install it?
JT
I can create a HAL pin and see it in showhal.
Code:
parent.hal_test = hal.component('test')
parent.hal_test.newpin('out', hal.HAL_BIT, hal.HAL_OUT)
parent.hal_test.ready()
After a lot of grepping in src/emc/usr_intf I'm still at a loss how to connect the hal component to the clicked signal of a QPushButton.
After creating the hal component do you need to install it?
JT
Please Log in to join the conversation.
2 years 8 months ago #292991
by JT
Replied by JT on topic Connect PyQt Button to HAL
I did just figure out that my component is loaded and I can make a net connection to it in the postgui.hal file.
JT
Attachment not found
JT
Please Log in to join the conversation.
2 years 8 months ago #293038
by cmorley
Replied by cmorley on topic Connect PyQt Button to HAL
How are you defining the clicked signal? from designer? in code?
Can you post your sample code for reference?
usually something like this:
button.clicked.connect(my_function)
def my_function(state):
print(state)
if state:
parent.hal_test.out.set(10)
else:
parent.hal_test.out.set(0)
Can you post your sample code for reference?
usually something like this:
button.clicked.connect(my_function)
def my_function(state):
print(state)
if state:
parent.hal_test.out.set(10)
else:
parent.hal_test.out.set(0)
Please Log in to join the conversation.
2 years 8 months ago #293046
by JT
Replied by JT on topic Connect PyQt Button to HAL
Hi Chris,
I'm trying to connect a PyQt native signal from a QPushButton like clicked, pressed etc and have that set a HAL pin.
I "think" you have given me enough clues to try and make this work. I'll give it a whirl in the morning.
Thanks
JT
I'm trying to connect a PyQt native signal from a QPushButton like clicked, pressed etc and have that set a HAL pin.
I "think" you have given me enough clues to try and make this work. I'll give it a whirl in the morning.
Thanks
JT
Please Log in to join the conversation.
2 years 8 months ago #293084
by JT
Replied by JT on topic Connect PyQt Button to HAL
With some help from the IRC this is some code:
and a video
JT
Code:
#!/usr/bin/env python3
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
from PyQt6.QtWidgets import QSpinBox
import hal, linuxcnc
'''
HAL_BIT
HAL_FLOAT
HAL_U32
HAL_S32
HAL_U64
HAL_S64
'''
class TestWidget(QWidget):
def __init__(self):
super().__init__()
self.halcomp = hal.component('mytest')
self.halcomp.newpin('out', hal.HAL_BIT, hal.HAL_OUT)
self.halcomp.newpin('in', hal.HAL_BIT, hal.HAL_IN)
self.halcomp.newpin('value', hal.HAL_U32, hal.HAL_IN)
self.halcomp.ready()
self.setWindowTitle('Test Widget')
self.setGeometry(300, 300, 250, 150)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.test_out = QPushButton('Test Out')
self.test_out.setCheckable(True)
self.test_out.clicked.connect(self.set_out_pin)
self.test_in = QPushButton('Test In')
self.test_in.setCheckable(True)
self.test_in.clicked.connect(self.set_in_pin)
self.number_in = QSpinBox()
self.number_in.valueChanged.connect(lambda: hal.set_p('mytest.value', f'{self.number_in.value()}'))
self.layout.addWidget(self.test_out)
self.layout.addWidget(self.test_in)
self.layout.addWidget(self.number_in)
self.test_hal = QPushButton('Test HAL')
self.test_hal.clicked.connect(lambda: hal.set_p('halui.mode.teleop', "true"))
self.layout.addWidget(self.test_hal)
self.show()
def set_out_pin(self, data):
self.halcomp['out'] = data
def set_in_pin(self, data):
self.halcomp['in'] = data
def closeEvent(self, event):
event.accept()
self.halcomp.exit()
app = QApplication([])
w = TestWidget()
app.exec()
and a video
JT
Please Log in to join the conversation.
2 years 8 months ago #293088
by JT
Replied by JT on topic Connect PyQt Button to HAL
I tried this minimal example using your suggestion:
And I get this error:
If you don't have PyQt6 just use PyQt5 and change app.exec() to app.exec_().
Thanks for your time and help
JT
Code:
#!/usr/bin/env python3
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
import hal
class test(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.hal_test = hal.component('test')
self.hal_test.newpin('out', hal.HAL_BIT, hal.HAL_OUT)
self.hal_test.ready()
self.test_out = QPushButton('Test Out')
self.test_out.setCheckable(True)
self.test_out.clicked.connect(self.set_out_pin)
self.layout.addWidget(self.test_out)
self.show()
def set_out_pin(self, state):
print(state)
if state:
self.hal_test.out.set(10)
else:
self.hal_test.out.set(0)
def closeEvent(self, event):
event.accept()
self.hal_test.exit()
app = QApplication([])
x = test()
app.exec()
And I get this error:
Code:
john@cave:~/github/hal$ ./hal_test_2.py
True
Traceback (most recent call last):
File "/home/john/github/hal/./hal_test_2.py", line 28, in set_out_pin
self.hal_test.out.set(10)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'bool' object has no attribute 'set'
Aborted
If you don't have PyQt6 just use PyQt5 and change app.exec() to app.exec_().
Thanks for your time and help
JT
Please Log in to join the conversation.
2 years 8 months ago #293090
by JT
Replied by JT on topic Connect PyQt Button to HAL
This is working
Looks like the The HAL Python module docs are not correct...
JT
Code:
#!/usr/bin/env python3
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
import hal
class test(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.hal_test = hal.component('test')
self.hal_test.newpin('out', hal.HAL_BIT, hal.HAL_OUT)
self.hal_test.newpin('speed', hal.HAL_U32, hal.HAL_IN)
self.hal_test.ready()
print(f'test exists {hal.component_exists("test")}')
print(f'test ready {hal.component_is_ready("test")}')
hal.new_sig("out_signal",hal.HAL_BIT)
hal.connect('test.out','out_signal')
#print(f'Pin Dir {test.out.get_dir()}')
self.test_value = QPushButton('Test Value')
self.test_value.setCheckable(True)
self.test_value.clicked.connect(self.set_value)
self.layout.addWidget(self.test_value)
self.test_state = QPushButton('Test State')
self.test_state.setCheckable(True)
self.test_state.clicked.connect(self.set_state)
self.layout.addWidget(self.test_state)
self.show()
def set_value(self, state):
print(state)
if state:
self.hal_test['speed'] = 10
else:
self.hal_test['speed'] = 0
def set_state(self, state):
print(state)
if state:
self.hal_test['out'] = True
else:
self.hal_test['out'] = False
def closeEvent(self, event):
print('by by')
event.accept()
self.hal_test.exit()
app = QApplication([])
x = test()
app.exec()
Looks like the The HAL Python module docs are not correct...
JT
Please Log in to join the conversation.
Time to create page: 0.083 seconds
