halcmd setp equivalent in python

124 views
Skip to first unread message

Michael Brown

unread,
May 17, 2020, 2:31:13 PM5/17/20
to Machinekit
Is it possible to set module parameters directly in python based hal configs without using the shell ?
if I attempt doing:
    
    hal.Pin('hm2_5i25.0.gpio.018.invert_output').set(true)

I get an error about missing pin.

So far below construct is all I can get to work:

    os.system('halcmd setp hm2_5i25.0.gpio.018.invert_output true')

justin White

unread,
May 17, 2020, 4:09:47 PM5/17/20
to Machinekit
import hal and:

hal.set_p("pinname","value")

set_p or "setp" should be universal between pins and parameters. hal.Pin is specifying the Pin class, but you are trying to set a parameter. hal.Param may also work in the same vein as your original attempt on a parameter.

Michael Brown

unread,
May 18, 2020, 8:37:39 PM5/18/20
to Machinekit
Here are all the combinations I have tried with cossospoding error messages:

    hal.Param('hm2_5i25.0.gpio.070.is_output').set(true)
AttributeError: 'module' object has no attribute 'Param'

    hal.set_p("hm2_5i25.0.gpio.070.is_output","true")
AttributeError: 'module' object has no attribute 'set_p'

    hal.setp("hm2_5i25.0.gpio.070.is_output","true")
AttributeError: 'module' object has no attribute 'setp'

    hal.Setp("hm2_5i25.0.gpio.070.is_output","true")
AttributeError: 'module' object has no attribute 'Setp'

there are 2 python files:
'/home/mib/Developer/the-snowwhite_git/machinekit-hal/lib/python/hal.py' 
'/home/mib/Developer/the-snowwhite_git/machinekit-hal/src/hal/lib/hal.py' 
containing:

class Pin(_ItemWrap):
    def __init__(self, item):
        _ItemWrap.__init__(self, item)
        if not item.is_pin():
            raise TypeError("Must be constructed from pin object")

class Param(_ItemWrap):
    def __init__(self, item):
        _ItemWrap.__init__(self, item)
        if item.is_pin():
            raise TypeError("Must be constructed from param object")

However it seems like the python library used is in a different location:

    hal.Pin('hm2_5i25.0.gpio.070.is_output').set(true)
  File "hal/cython/machinekit/hal_pin.pyx", line 23, in machinekit.hal._Pin.__cinit__ (hal/cython/machinekit/hal.c:5368)
  File "hal/cython/machinekit/hal_pin.pyx", line 29, in machinekit.hal._Pin.__cinit__ (hal/cython/machinekit/hal.c:4935)

???

b...@basdebruijn.com

unread,
May 19, 2020, 3:50:43 AM5/19/20
to Michael Brown, Machinekit

The Cython bindings do not support Params. I do not know about the original python bindings that came from the LCNC fork.

If you have a pin (not param), then you can set it with hal.Pin(‘pinname’).set(value)

If the params are needed in a python program, the best way would be to change the params to pins in the components source.

.

b...@basdebruijn.com

unread,
May 19, 2020, 3:55:51 AM5/19/20
to Michael Brown, Machinekit

Forgot to mention what has cost me a lot of time in the past.

IIRC then in python ‘import hal’ imports the ‘legacy’ LCNC python bindings.

If you use ‘from machinekit import hal’ then you use the Machinekit cython generated bindings.

If I’m not right about this I hope to get corrected.

Michael Brown

unread,
May 19, 2020, 9:32:11 AM5/19/20
to Machinekit
Well this seems to be doable, I just don't know if its currently possible to compile in the machinekit-cnc or lcnc part with the current machinekit-hal status
(I'm running a cnc router with a machinekit-hal/-cnc combo from november or so) 

Perhaps I should rephrase my question:

What I would like to construct is something like this (where i can specify the max spindle rpm [12000] in the ini file)
    hal.Param('hm2_5i25.0.pwmgen.00.scale').set(c.find('SPINDLE', 'MAXRPM'))
or
    hal.Pin('hm2_5i25.0.pwmgen.00.scale').set(c.find('SPINDLE', 'MAXRPM'))

What I have is this:
    os.system('halcmd setp hm2_5i25.0.pwmgen.00.scale 12000')

where I would like to replace the hard coded 12000 with a c.find('SPINDLE', 'MAXRPM') construction, that probes it from the .ini file 
I just can't figure out how ?

justin White

unread,
May 19, 2020, 11:00:57 AM5/19/20
to Machinekit
Yeah, I forgot Machinekit is in a perpetual state of "this might work the way it used to".....or "maybe not". I've recompiled components to swap parameters for pins but I don't think I'd try with a hm2 driver.

If all you want to do is set a parameter from an .ini line you don't have to do anything with Python whatsoever (unless there's some reason you really want to). All you need to do is:

In the .ini

[SPINDLE]
MAXRPM = 12000

In the hal file:
setp hm2_5i25.0.pwmgen.00.scale [SPINDLE]MAXRPM

That's the pretty standard usage, or am I missing something?

b...@basdebruijn.com

unread,
May 19, 2020, 11:01:20 AM5/19/20
to Michael Brown, Machinekit

Snip from a previous project (3 yrs ago) where I used configparser:

 

import Configparser

 

# taken out of a class somewhere, adapt as needed, the config will be loaded in self.cfg_hardware

# somewhere setup_ini() is called, which does the actual loading

        # in the instance:

        self.cfg_hardware = ConfigParser.ConfigParser()

 

    def setup_ini(self):

        self.read_hardware_config()

        for key, (field, joint) in enumerate(self.joints.iteritems()):

            for (key, val) in self.cfg_hardware.items(joint.name):

                joint.add_hardware_setting(key, val)

 

    def read_hardware_config(self):

        os.chdir(os.path.dirname(os.path.realpath(__file__)))

        directory = os.getcwd() + '/config'

        configfile = directory + '/hardware.ini'

        self.cfg_hardware.read(configfile)

 

instead of the for loop for (key, val) in self.cfg_hardware.items(joint.name): you would get a value something like so: myval=cfg_hardware[‘SPINDLE’][‘MAXRPM’]

then use myval to set the pin

b...@basdebruijn.com

unread,
May 19, 2020, 11:05:42 AM5/19/20
to justin White, Machinekit

 

 

From: machi...@googlegroups.com <machi...@googlegroups.com> On Behalf Of justin White
Sent: Tuesday, 19 May 2020 17:01
To: Machinekit <machi...@googlegroups.com>
Subject: Re: [Machinekit] Re: halcmd setp equivalent in python

 

 

Yeah, I forgot Machinekit is in a perpetual state of "this might work the way it used to".....or "maybe not". I've recompiled components to swap parameters for pins but I don't think I'd try with a hm2 driver.

 

If all you want to do is set a parameter from an .ini line you don't have to do anything with Python whatsoever (unless there's some reason you really want to). All you need to do is:

 

In the .ini

 

[SPINDLE]

MAXRPM = 12000

 

In the hal file:

setp hm2_5i25.0.pwmgen.00.scale [SPINDLE]MAXRPM

 

That's the pretty standard usage, or am I missing something?

 

 

 

Yes, we were talking of doing this from python (see subject)

Michael Brown

unread,
May 19, 2020, 11:47:05 AM5/19/20
to Machinekit
Perhaps I will clarify if I point directly to the exact file I'm attempting to rectify:

I'm trying to replace the hard coded 12000 below with the parameter 
os.system('halcmd setp hm2_5i25.0.pwmgen.00.scale 12000')

I cannot see how to make this work:

os.system('halcmd setp hm2_5i25.0.pwmgen.00.scale c.find('SPINDLE', 'MAXRPM')')

Michael Brown

unread,
May 19, 2020, 12:14:15 PM5/19/20
to Machinekit
Well seems like my brain started working again and I found out that this construction fits that task:

    os.system("halcmd setp hm2_5i25.0.pwmgen.00.scale %s "%(c.find('SPINDLE', 'MAXRPM')))

On a side note according to this post:
https://www.mail-archive.com/search?l=machi...@googlegroups.com&q=subject:%22%5C%5BMachinekit%5C%5D+Machinekit+Advanced+HAL+Tutorial%22&o=newest&f=1
Parameters are depreciated in machinekit-hal so,
It could seem like a great idea to convert all the Params in hm2 to pins ?
Unless there are worries about breaking too many existing Machinekit mesa hm2 configs ?

Best wishes
Michael Brown

ce...@tuta.io

unread,
May 19, 2020, 4:20:33 PM5/19/20
to Michael Brown, Machinekit

May 19, 2020, 18:14 by mib.hol...@gmail.com:

> Well seems like my brain started working again and I found out that this construction fits that task:
>
>     os.system("halcmd setp hm2_5i25.0.pwmgen.00.scale %s "%(c.find('SPINDLE', 'MAXRPM')))
>
> On a side note according to this post:
> https://www.mail-archive.com/search?l=machi...@googlegroups.com&q=subject:%22%5C%5BMachinekit%5C%5D+Machinekit+Advanced+HAL+Tutorial%22&o=newest&f=1
> Parameters are depreciated in machinekit-hal so,
> It could seem like a great idea to convert all the Params in hm2 to pins ?
> Unless there are worries about breaking too many existing Machinekit mesa hm2 configs ?
>
Look at https://github.com/machinekit/machinekit-hal/issues/260 - the idea to delete lower functions and make the Parameters C API simply call Pin API probably has the best chance at being passable.

Cern.

>
> Best wishes
> Michael Brown
>
>
>
>
> On Tuesday, May 19, 2020 at 5:47:05 PM UTC+2, Michael Brown wrote:
>
>> Perhaps I will clarify if I point directly to the exact file I'm attempting to rectify:
>> https://github.com/the->> snowwhite/Hm2-soc_FDM/blob/>> work/Cramps/PY/OX/cramps.py#>> L91
>>
>> I'm trying to replace the hard coded 12000 below with the parameter 
>> MAXRPM from the .ini file <https://github.com/the-snowwhite/Hm2-soc_FDM/blob/work/Cramps/PY/OX/ox.ini#L382>
>> os.system('halcmd setp hm2_5i25.0.pwmgen.00.scale 12000')
>>
>> I cannot see how to make this work:
>>
>> os.system('halcmd setp hm2_5i25.0.pwmgen.00.scale >> c>> .>> f>> ind>> (>> 'SPINDLE'>> , >> 'MAXRPM'>> )>> ')
>>
>>
>> On Tuesday, May 19, 2020 at 5:05:42 PM UTC+2, Bas de Bruijn wrote:
>>
>>>
>>>  
>>>
>>>
>>>  
>>>
>>>
>>> From:>>> >>> machi...@googlegroups.com <>>>> <>>> machi...@googlegroups.com <>>>> > >>> On Behalf Of >>> justin White
>>> Sent:>>> Tuesday, 19 May 2020 17:01
>>> To:>>> Machinekit <>>> machi...@googlegroups.com <>>>> >
>>> Subject:>>> Re: [Machinekit] Re: halcmd setp equivalent in python
>>>
>>>  
>>>
>>>
>>>  
>>>
>>>
>>> Yeah, I forgot Machinekit is in a perpetual state of "this might work the way it used to".....or "maybe not". I've recompiled components to swap parameters for pins but I don't think I'd try with a hm2 driver.
>>>
>>>
>>>  
>>>
>>>
>>> If all you want to do is set a parameter from an .ini line you don't have to do anything with Python whatsoever (unless there's some reason you really want to). All you need to do is:
>>>
>>>
>>>  
>>>
>>>
>>> In the .ini
>>>
>>>
>>>  
>>>
>>>
>>> [SPINDLE]
>>>
>>>
>>> MAXRPM = 12000
>>>
>>>
>>>  
>>>
>>>
>>> In the hal file:
>>>
>>>
>>> setp hm2_5i25.0.pwmgen.00.scale [SPINDLE]MAXRPM
>>>
>>>
>>>  
>>>
>>>
>>> That's the pretty standard usage, or am I missing something?
>>>
>>>
>>>  
>>>
>>>
>>>  
>>>
>>>
>>>  
>>>
>>>
>>> Yes, we were talking of doing this from python (see subject)
>>>
>>>
>
>
>
> --
> 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> .
> To view this discussion on the web visit > https://groups.google.com/d/msgid/machinekit/05d8021e-c605-4c46-90ba-8eaddc03fee8%40googlegroups.com <https://groups.google.com/d/msgid/machinekit/05d8021e-c605-4c46-90ba-8eaddc03fee8%40googlegroups.com?utm_medium=email&utm_source=footer>> .
>

Reply all
Reply to author
Forward
0 new messages