Setting HAL Signals from Python

709 views
Skip to first unread message

Kirk Wallace

unread,
Jul 11, 2015, 1:44:12 PM7/11/15
to machi...@googlegroups.com
I am trying to create an m-code, M41, for my lathe using remapping. This
is with LinuxCNC 2.6, but I'm posting here because there are remapping
experts here (maybe?).

Currently, I have an M10x.ngc file which is a shell script something like:

M102
~~~~~~~~~~~~~
#!/bin/bash
# Sets spindle clutches to Low
halcmd setp parport.0.pin-05-out False
halcmd setp parport.0.pin-06-out True
exit 0
~~~~~~~~~~~~~

So it is invoked in g-code with "M102".

What I would like to do is use M41. To do this, I edited my .ini file:


lathe.ini
~~~~~~~~~~~~~
...
[RS274NGC]
REMAP=M41 modalgroup=10 py=m41
...
[PYTHON]
TOPLEVEL=/home/kwallace/linuxcnc-dev/configs/python/toplevel.py
PATH_APPEND=/home/kwallace/linuxcnc-dev/configs/python
...
~~~~~~~~~~~~~


toplevel.py (just the one line)
~~~~~~~~~~~~~
import remap
~~~~~~~~~~~~~


remap.py
~~~~~~~~~~~~~
def m41(self, **words):
...
I would like a command that does what halcmd did in the shell
script above
...

return
~~~~~~~~~~~~~


I can invoke g-code commands with 'execute()', or emccanon to invoke
canon commands, but neither of these allow setting a HAL signal or pin,
as for as I know. Also, I would prefer to not set the parport pin but
connect it in the .ini file to a HAL signal, such as spindleLow, then
set the signal in remap.py , similar to the built-in signal for coolant.

Any comments are welcome. Thank you.


--
Kirk Wallace
http://www.wallacecompany.com/machine_shop/
http://www.wallacecompany.com/E45/

Alexander Rössler

unread,
Jul 11, 2015, 2:26:10 PM7/11/15
to Kirk Wallace, machi...@googlegroups.com
Kirk Wallace writes:

> I am trying to create an m-code, M41, for my lathe using remapping. This
> is with LinuxCNC 2.6, but I'm posting here because there are remapping
> experts here (maybe?).
>
> Currently, I have an M10x.ngc file which is a shell script something like:
>
> M102
> ~~~~~~~~~~~~~
> #!/bin/bash
> # Sets spindle clutches to Low
> halcmd setp parport.0.pin-05-out False
> halcmd setp parport.0.pin-06-out True
> exit 0
> ~~~~~~~~~~~~~
>
> So it is invoked in g-code with "M102".
>
> What I would like to do is use M41. To do this, I edited my .ini file:
Remapping does only work for MCodes 100-999
--
Alexander

Michael Haberler

unread,
Jul 11, 2015, 2:43:49 PM7/11/15
to Alexander Rössler, Kirk Wallace, machi...@googlegroups.com

> Am 11.07.2015 um 20:25 schrieb Alexander Rössler <ma...@roessler.systems>:
>
> Kirk Wallace writes:
>
>> I am trying to create an m-code, M41, for my lathe using remapping. This
>> is with LinuxCNC 2.6, but I'm posting here because there are remapping
>> experts here (maybe?).
>>
>> Currently, I have an M10x.ngc file which is a shell script something like:
>>
>> M102
>> ~~~~~~~~~~~~~
>> #!/bin/bash
>> # Sets spindle clutches to Low
>> halcmd setp parport.0.pin-05-out False
>> halcmd setp parport.0.pin-06-out True
>> exit 0
>> ~~~~~~~~~~~~~
>>
>> So it is invoked in g-code with "M102".
>>
>> What I would like to do is use M41. To do this, I edited my .ini file:
> Remapping does only work for MCodes 100-999


that is incorrect, see the set of remappable codes in the docs

the range of shell escapes is limited, but that wasnt what Kirk was asking
> --
> 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.
> Visit this group at http://groups.google.com/group/machinekit.
> For more options, visit https://groups.google.com/d/optout.

Michael Haberler

unread,
Jul 11, 2015, 2:48:34 PM7/11/15
to Kirk Wallace, machi...@googlegroups.com
Hi Kirk,
you can do that through canon, the AIO and DIO pins can be set

see emc/task/emccanon.c SET_MOTION_OUTPUT_BIT(int index) and CLEAR_MOTION_OUTPUT_BIT(int index)

> Also, I would prefer to not set the parport pin but connect it in the .ini file to a HAL signal, such as spindleLow, then set the signal in remap.py , similar to the built-in signal for coolant.

you'd want to link the signal to the corresponding motion output pin


but technically all this is no different from using an NGC remap and M6x codes there?

-m

>
> Any comments are welcome. Thank you.
>
>
> --
> Kirk Wallace
> http://www.wallacecompany.com/machine_shop/
> http://www.wallacecompany.com/E45/
>

Kirk Wallace

unread,
Jul 11, 2015, 3:19:56 PM7/11/15
to machi...@googlegroups.com, kwal...@wallacecompany.com


On Saturday, July 11, 2015 at 11:48:34 AM UTC-7, Michael Haberler wrote:
... snip
you can do that through canon, the AIO and DIO pins can be set

see emc/task/emccanon.c  SET_MOTION_OUTPUT_BIT(int index) and CLEAR_MOTION_OUTPUT_BIT(int index)

Ah, so this is just adds the motion module in the chain. This limits the number of pins to the number available in motion, but that may not be too much of an issue. Motion's DIO also has some options as to when the action takes place, which might be safer.
 

> Also, I would prefer to not set the parport pin but connect it in the .ini file to a HAL signal, such as spindleLow, then set the signal in remap.py , similar to the built-in signal for coolant.

you'd want to link the signal to the corresponding motion output pin


but technically all this is no different from using an NGC remap and M6x codes there?

-m

... snip

I think I'll give the emccanon motion DIO method a try.

I do wonder if LinuxCNC's built in signals, such as coolant and brake are too limited and if adding standard devices such as collet closers, part catchers, and such would be a good thing?

Thank you for your replies Alexander and Michael.

Michael Haberler

unread,
Jul 11, 2015, 3:49:55 PM7/11/15
to Kirk Wallace, machi...@googlegroups.com
well those canon methods still work through motion as this is the primary interface into HAL, so the same limitations apply as on the gcode level

working with arbitrary pins is possible in theory on the canon level (new canon + NML command), the issue is more that rs274 has no way of expressing a string name argument

-m

>
> Thank you for your replies Alexander and Michael.
>
>

Kirk Wallace

unread,
Jul 11, 2015, 7:46:25 PM7/11/15
to machi...@googlegroups.com, kwal...@wallacecompany.com
This is what I have so far:

remap.py
~~~~~~~~~~~
#!/usr/bin/python

import emccanon

def m41(self, **words):
    #emccanon.CLEAR_AUX_OUTPUT_BIT(0)  # spindle low clutch
    emccanon.CLEAR_AUX_OUTPUT_BIT(1)  # spindle high clutch
    emccanon.SET_AUX_OUTPUT_BIT(0)    # spindle low clutch
    return

def m42(self, **words):
    emccanon.CLEAR_AUX_OUTPUT_BIT(0)  # spindle low clutch
    #emccanon.CLEAR_AUX_OUTPUT_BIT(1)  # spindle high clutch
    emccanon.SET_AUX_OUTPUT_BIT(1)    # spindle high clutch
    return

~~~~~~~~~~~

hnc.hal
~~~~~~~~~~~
...
net SpindleLow   motion.digital-out-00  parport.0.pin-16-out
net SpindleHigh  motion.digital-out-01  parport.0.pin-09-out
...
~~~~~~~~~~~

This seems to work, but the clutches bind because it takes a while for the current clutch to release while the new clutch engages nearly instantly. When both clutches are engaged the spindle and motor stop due to the clutches binding. I need to find a way to release both clutches, wait for a time, then engage the new clutch. I tried time.sleep() but this doesn't work.

Alexander Rössler

unread,
Jul 12, 2015, 4:12:24 AM7/12/15
to Michael Haberler, Kirk Wallace, machi...@googlegroups.com

Michael Haberler writes:

>> Am 11.07.2015 um 20:25 schrieb Alexander Rössler <ma...@roessler.systems>:
>>
>> Kirk Wallace writes:
>>
>>> I am trying to create an m-code, M41, for my lathe using remapping. This
>>> is with LinuxCNC 2.6, but I'm posting here because there are remapping
>>> experts here (maybe?).
>>>
>>> Currently, I have an M10x.ngc file which is a shell script something like:
>>>
>>> M102
>>> ~~~~~~~~~~~~~
>>> #!/bin/bash
>>> # Sets spindle clutches to Low
>>> halcmd setp parport.0.pin-05-out False
>>> halcmd setp parport.0.pin-06-out True
>>> exit 0
>>> ~~~~~~~~~~~~~
>>>
>>> So it is invoked in g-code with "M102".
>>>
>>> What I would like to do is use M41. To do this, I edited my .ini file:
>> Remapping does only work for MCodes 100-999
>
>
> that is incorrect, see the set of remappable codes in the docs
>
> the range of shell escapes is limited, but that wasnt what Kirk was
> asking
Btw. the docs are incorrect:
https://github.com/machinekit/machinekit-docs/blob/master/src/remap/structure.asciidoc
--
Alexander

Michael Haberler

unread,
Jul 12, 2015, 4:24:10 AM7/12/15
to Alexander Rössler, Kirk Wallace, machi...@googlegroups.com

> Am 12.07.2015 um 10:11 schrieb Alexander Rössler <ma...@roessler.systems>:
>
>
> Michael Haberler writes:
>
>>> Am 11.07.2015 um 20:25 schrieb Alexander Rössler <ma...@roessler.systems>:
>>>
>>> Kirk Wallace writes:
>>>
>>>> I am trying to create an m-code, M41, for my lathe using remapping. This
>>>> is with LinuxCNC 2.6, but I'm posting here because there are remapping
>>>> experts here (maybe?).
>>>>
>>>> Currently, I have an M10x.ngc file which is a shell script something like:
>>>>
>>>> M102
>>>> ~~~~~~~~~~~~~
>>>> #!/bin/bash
>>>> # Sets spindle clutches to Low
>>>> halcmd setp parport.0.pin-05-out False
>>>> halcmd setp parport.0.pin-06-out True
>>>> exit 0
>>>> ~~~~~~~~~~~~~
>>>>
>>>> So it is invoked in g-code with "M102".
>>>>
>>>> What I would like to do is use M41. To do this, I edited my .ini file:
>>> Remapping does only work for MCodes 100-999
>>
>>
>> that is incorrect, see the set of remappable codes in the docs
>>
>> the range of shell escapes is limited, but that wasnt what Kirk was
>> asking
> Btw. the docs are incorrect:
> https://github.com/machinekit/machinekit-docs/blob/master/src/remap/structure.asciidoc

any more detailed bug report ;-?

which type (shell escape or remap), which code(s)?

Kirk Wallace

unread,
Jul 12, 2015, 10:37:10 AM7/12/15
to machi...@googlegroups.com
Thinking a bit more, I think putting the sleep() between clutch commands
just puts one command in the queue, then pauses, then puts the next
command in. The queue then gets processed, which just has two clutch
commands which occur nearly together. What I need is an emccanon command
for G4 dwell that I can place in the queue.

Hmm ...

> def m41(self, **words):
> emccanon.CLEAR_AUX_OUTPUT_BIT(1) # spindle high clutch
> emccanon.DWELL(1) # allow time for the clutch to release
> emccanon.SET_AUX_OUTPUT_BIT(0) # spindle low clutch
> return

Cool. It works in my sim. Now I need to try it on the lathe.

Alexander Rössler

unread,
Jul 13, 2015, 1:39:21 AM7/13/15
to Kirk Wallace, machi...@googlegroups.com
Kirk,

You can also use normal ngc file remaps instead of Python
subroutines. No need to figure out the EMC commands. For example take a
look in this directory:
https://github.com/machinekit/machinekit/tree/master/nc_files/remap-subroutines/fdm
--
Alexander
Reply all
Reply to author
Forward
0 new messages