Set HAL-Pin Values by Python Script

More
19 Feb 2018 12:51 #106167 by Thom
Hi,
I try to Set a Hal-Pin Value in a Method of a Python script, associated with Glade VCP.
Example Python Script:
#!/usr/bin/env python
import gtk
import os
import hal
import hal_glib
import linuxcnc

class HandlerClass:

	def __init__(self, halcomp, builder, useropts):
		
		self.halcomp = halcomp
		self.builder = builder
#generate Hal Pin
		self.SPMCLAMP = hal_glib.GPin(halcomp.newpin('USR_SPMCLAMP', hal.HAL_BIT, hal.HAL_OUT))

#generate Button Click Event
		self.BTN_SPMUNCLAMP_auto = self.builder.get_object('BTN_SPMUNCLAMP_auto')
		self.BTN_SPMUNCLAMP_auto.connect("clicked", self._on_BTN_SPMUNCLAMP_auto_clicked)

	def _on_BTN_SPMCLAMP_auto_clicked(self, event) :
		print "Method is activ"				#work
		SPMCLAMP = True				#Dont`t work (No effekt)
		SPMCLAMP.set_active(True)			#Dont`t work (Error)
		SPMCLAMP.set_value(True)			#Dont`t work (Error)

What Method or Syntax could i use?

Please help me,
Thomas Kümmel

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

More
19 Feb 2018 13:55 #106169 by Todd Zuercher
Sorry I'm not going to be much help since I don't get programming Python. But I was able to bumble through creating a python script to work with my GladeVCP that does what I need it to and doesn't set too many error warnings by copying off and adapting of examples in the documentation and JT's tutorials. Mine creates a couple of hal pins. I don't understand the code I made any why it works, or why yours doesn't. I am not going to post my code unless you really want it, because I can't begin to explain it. The pins I needed to create were hal-ins that I am using to control the state of some radio buttons on the VCP.

Just curious why you need the hal pin out? Most of the hal widgets for GladeVCPs create their own hal pins automatically so you don't need to put them in the python script.
The following user(s) said Thank You: Thom

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

More
19 Feb 2018 14:23 #106171 by Thom
Thank you Todd for fast reply,

My Python Skills aren`t very well too.
My main calculations i write in a "HAL Component" in C.
but it is laborious to connect every Button, LED, ... in The PostGUI, the Hal File and define in the component File
But unfortunatualy i haven`t found a way to access Glade and the Hal-Layer with a C-Script direct.

For easy HALButtons, glade create automatic HAL-Pins,
But for eg. TreeView and ListStore Objects, they don`t.
But how to get These Values ( Bit and Int) to my C-HAL Component?

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

More
19 Feb 2018 21:40 - 19 Feb 2018 21:41 #106190 by cmorley
def _on_BTN_SPMCLAMP_auto_clicked(self, pin_reference) :
    print "Method is activ"          #work
    self.SPMCLAMP.set(True)   # this works with a absolute reference to the pin
    pin_reference.set(True)   # this works with an indirect reference to the pin (uses the pin_reference from the method)

use SOME_PIN_REFERENCE.get() for get the value of a pin

Chris M
Last edit: 19 Feb 2018 21:41 by cmorley.
The following user(s) said Thank You: Thom

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

More
19 Feb 2018 21:56 #106192 by cmorley

Thank you Todd for fast reply,

But unfortunatualy i haven`t found a way to access Glade and the Hal-Layer with a C-Script direct.

It's possible to do something like what you want- there are some recently documented commands for the python component.
linuxcnc.org/docs/2.7/html/hal/halmodule...l#_helpful_functions
hal.connect("pinname","signal_name")
hal.new_sig("signalname",hal.HAL_BIT)
but you would need a pretty good reason to use it - it's not as flexible as using HAL files.

For easy HALButtons, glade create automatic HAL-Pins,
But for eg. TreeView and ListStore Objects, they don`t.
But how to get These Values ( Bit and Int) to my C-HAL Component?


HALbuttons etc are specifically made general widgets for interfacing HAL to pyGTK.
there are a lot of HAL widgets, I haven't heard anyone wishing for more widgets in a while.
If there are lacking HAL widgets, one would need to build them. We are always interested in new code.
Treeview and liststore are used inside of a parent widget, so I'm not sure how one could add HAL pins to them in a general way.
Combobox widgets use liststores IIRC and I believe there is a HALified combobox - this would be the usual way.

Hard to elaborate more without know what you are trying to do :)

Chris M

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

More
20 Feb 2018 11:54 #106227 by Thom
Thank you a lot, cmorley

Thats the Key:
self.Pinname.set(Value)
Oh my God, it is so easy, if you know it :woohoo:

Here is my way to get the selected row of a Treeview in a HAL-Pin:
#!/usr/bin/env python

import gtk
import os
import hal
import hal_glib
import linuxcnc

class HandlerClass:

	def __init__(self, halcomp, builder, useropts):

#import objects from Glade File
		self.USR_Liststore = self.builder.get_object('USR_Liststore')
		self.USR_Treeview = self.builder.get_object('USR_Treeview')

#create Hal Pin
		self.HAL_selectedrow = hal_glib.GPin(halcomp.newpin('Test', hal.HAL_S32, hal.HAL_OUT))

#create Event Handler
		USR_Treeview_Select = self.USR_Treeview.get_selection()
		USR_Treeview_Select.connect("changed", self._on_USR_Treeview_selection_change)
		
#Global Variables
	selecteditem = 0

#Event Handler
	def _on_USR_Treeview_selection_change(self, tree_selection):
		model, treeiter = tree_selection.get_selected()
		if treeiter is not None :					# if any row is selected
			self.selecteditem = model[treeiter][0]	
			self.HAL_selectedrow.set(self.USR_Liststore[self.selecteditem][0])
		
#boiler code
def get_handlers(halcomp,builder,useropts):
	return [HandlerClass(halcomp,builder,useropts)]

The Treeview and the Liststore are created in Glade.
The vlaues of the Liststore are also preset in Glade.

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

More
20 Feb 2018 12:00 #106229 by Thom
I`ve heard about the funktions:

hal.connect("pinname","signal_name")
hal.new_sig("signalname",hal.HAL_BIT) , ...

but if i write this in Python or the following in the HAL File

net signalname pinname

does not make a difference

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

More
20 Feb 2018 19:05 #106246 by cmorley

I`ve heard about the funktions:

hal.connect("pinname","signal_name")
hal.new_sig("signalname",hal.HAL_BIT) , ...

but if i write this in Python or the following in the HAL File

net signalname pinname

does not make a difference


As I said one needs a good reason to use this - for instance Pncconf used it to automatically build a test screen on objects that could be different each time (in fact that is what they were added for)

I also wanted to point out the get() and set() functions work because you used hal_glib to make the pins.
if you make pins like in the manual here:
linuxcnc.org/docs/2.7/html/hal/halmodule.html
the get() and set() functions are not available.

Chris M
Chriis

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

Moderators: HansU
Time to create page: 0.097 seconds
Powered by Kunena Forum