Remapping M100-M199 in Python

More
15 May 2017 22:06 #93243 by DaBit
I was trying to implement a couple of FDM 3D-printer M-codes, such as M104 (set extruder temperature) using remapping to a Python function.

Now, this works for available G-codes and M-codes outside the 100-199 range. If I do this:

(ini file)
..
[RS274NGC]
..
REMAP = M104 modalgroup=10 py=testfunc
..

(./python/remap.py)
..
..
def testfunc(self, **words):
    print "Hello there!"
..
..

Then using M104 in a program results in 'undefined M code'.
If I remap M204 it works fine.

Now, it is not a total disaster since one must translate reprapwhatever flavor G-code to LinuxCNC G-code anyway (or adapt the slicer), but to be honest I expected some sort of hierarchy like 'if the M1xx code is remapped using python/ngc use that, else try to find the user script, else error'.

MachineKit seems to do just that: (click, around line 629) . Is it possible to obtain the same behaviour with LinuxCNC master?

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

More
17 May 2017 21:27 #93316 by andypugh
This is actually very easy, and doesn't need remap.
Just create an executable python file (start the file with #! /usr/bin/python ) which contains your python code. Then call it M104

The M-codes from 100 to 199 simply execute a file of that name, and python files count as executable.

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

More
18 May 2017 15:46 #93356 by DaBit
Replied by DaBit on topic Remapping M100-M199 in Python
Yes, I know, but it is less flexible. Only P and Q parameters, less control over interpreter; cannot choose whether the code is a queuebuster or not for example, and I have to do the remapping of e.g. M84 in ngc or python anyway. And I hate the 15 different files with 10 lines of code each, but that's not the real reason, oh nooooo.. :)

Not sure how to do AIO/DIO (M66 & companions) in script. Something like import emccanon; emccanon.SET_AUX_OUTPUT_BIT(xx) I presume?

Remapping is a nice feature, I only don't like all the exceptions and special cases. Should be a generic thing imho.

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

More
19 May 2017 12:35 - 19 May 2017 12:51 #93380 by bevins
This works for me.

Map your motion.digital-out-xx in hal
Map your motion.digital-in-XX in hal
import linuxcnc
from interpreter import *
from emccanon import MESSAGE, SET_MOTION_OUTPUT_BIT, CLEAR_MOTION_OUTPUT_BIT,SET_AUX_OUTPUT_BIT,CLEAR_AUX_OUTPUT_BIT

def M6_Remap(self, **words):
    #--------------------------------------------------
    # if in preview mode exit without doing anything and all ok
    #----------------------------------------------------------
    if self.task==0:
        return INTERP_OK

    try:

         self.execute("M65 P2") 
         self.execute("M66 P5 L3 Q5")
         self.execute("M65 P3") 


This works extremely well.
Last edit: 19 May 2017 12:51 by bevins.

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

More
07 Feb 2018 17:05 #105534 by sqmathlete
Hi,

I am trying to implement something similar to Bevins' python code to contol M1XX but, I keep getting an "interpreter module does not exist" error.

my .ini is in ~/linuxcnc/configs/ecat-lathe
m-code files M138 ~/linuxcnc/configs/ecat-lathe/m-codes
python files ~/linuxcnc/configs/ecat-lathe/python note: toplevel.py has only one line import remap. The the file remap.py is also in the same folder as toplevel.py and begins with #!/usr/bin/python, then Bevins code.

In the .ini file I added

[RS247_NGC]
REMAP = M138 modal group=10 python=M138

[PYTHON]
PATH_PREPEND= python
# import the following Python module
TOPLEVEL= python/toplevel.py

I don't know if this matters but I created a button in the custom pyvcp side panel that calls the M1XX code. Interestingly, if the M-code is defined as #!/bin/bash and uses the waitfor method on the wiki, the custom m-code will work but requires a lot of ladder logic to cover timeout faults and then there is the issue of connecting multiple faults to halui.abort.

Python is obviously the way to access and execute.command()s such as abort over mutiple fault scenarios. However, I just can't seem to sort out the remap magic. Similar to this thread is forum.linuxcnc.org/20-g-code/33642-custom-m-code-python. Either way some wizardry is required.

Any help would be greatly appreciated,

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

More
26 Feb 2018 20:28 #106557 by andypugh
Sorry for the late reply.

When do you see the error "Interpreter module does not exist". Is it when you start LinuxCNC or when you call the M-code?

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

More
26 Feb 2018 23:06 #106579 by sqmathlete
Hi Andy,

All good! I see the error when linuxcnc starts up.

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

More
27 Feb 2018 00:38 #106589 by andypugh
Interesting.

I think that means that the remap file is being found.
What happens if you comment-out the "from interpreter import *" line in your code?
(And is that what the offending line actually says)

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

More
01 Mar 2018 05:18 #106792 by sqmathlete
It does seem like the remap.py file is being seen. After I commented out " from interpreter import * "
and ran linuxcnc in a terminal I get the following
/linuxcnc-dev/configs/ecat-lathe-1.0/ecat-lathe-1.0.ini:66: executing 'import sys
sys.path.insert(0,"python")'
PythonPlugin: Python  '2.7.3 (default, Nov 24 2017, 16:38:25) 
[GCC 4.7.2]'
is_callable(remap.m141) = TRUE
NML_INTERP_LIST(0x82f94e8)::append(nml_msg_ptr{size=88,type=EMC_TRAJ_SET_G5X}) : list_size=2, line_number=0
NML_INTERP_LIST(0x82f94e8)::append(nml_msg_ptr{size=84,type=EMC_TRAJ_SET_G92}) : list_size=3, line_number=0
NML_INTERP_LIST(0x82f94e8)::append(nml_msg_ptr{size=20,type=EMC_TRAJ_SET_ROTATION}) : list_size=4, line_number=0
is_callable(__init__) = FALSE
Bad character ',' used
Interpreter stack: 	 - int Interp::read_one_item(char*, int*, block_pointer, double*)  - int Interp::read_items(block_pointer, char*, double*)  - int Interp::parse_line(char*, block_pointer, setup_pointer)  - int Interp::_read(const char*) 
emcTaskPlanInit() returned 5
is_callable(__delete__) = FALSE
Trying to fix bindings Left PrevChar {}
Trying to fix bindings Right NextChar {}
Trying to fix bindings Up PrevLine {}
Trying to fix bindings Down NextLine {}
Trying to fix bindings Home LineStart {}
Trying to fix bindings End LineEnd {}
note: MAXV     max: 250.000 units/sec 15000.000 units/min
note: LJOG     max: 250.000 units/sec 15000.000 units/min
note: LJOG default: 50.000 units/sec 3000.000 units/min
waiting for s.joints<0>, s.kinematics_type<0>
waiting for s.joints<0>, s.kinematics_type<0>
waiting for s.joints<0>, s.kinematics_type<0>
waiting for s.joints<0>, s.kinematics_type<0>
waiting for s.joints<0>, s.kinematics_type<0>
waiting for s.joints<0>, s.kinematics_type<0>
waiting for s.joints<0>, s.kinematics_type<0>
waiting for s.joints<0>, s.kinematics_type<0>
waiting for s.joints<0>, s.kinematics_type<0>
A configuration error is preventing LinuxCNC from starting.
More information may be available when running from a terminal.
Shutting down and cleaning up LinuxCNC...

I also tried the other way that you suggested earlier "an executable python file" in my m-codes folder (M140). Linuxcnc will start up fine but if I import either the interpreter or emccannon module I get an error saying that these modules
Issuing EMC_SYSTEM_CMD -- 	 (   +30,+268,    +0,m-codes/M140\032-1.000000\032-1.000000,)
Emit interp-run
Emit interp-run
Traceback (most recent call last):
  File "m-codes/M140", line 4, in <module>
    from interpreter import *
ImportError: No module named interpreter
emcSystemCmd: system command 2785 exited abnormally with value 1

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

More
01 Mar 2018 13:00 #106797 by andypugh
For reasons I am not 100% clear on you can't import emccannon or interpreter into a standalone Python script, they are only available inside code that is part of a remap.

This looks fishy: Bad character ',' used

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

Time to create page: 0.156 seconds
Powered by Kunena Forum