Custom M code in Python example?

More
01 Nov 2019 20:50 #149388 by scotta
Hi, I'm using custom M codes for my 3D printer work. Currently they are bash scripts but I'm wondering if anyone has a working example of a Python based M code?

Bash works but it's getting old school now and the syntax is not as intuitive. And you don't have to do work arounds to use floating point maths.

Being able to set Hal pins directly without having to create a component and then do the Hal wiring would be great. An example of one of my M codes.
#!/bin/bash

# M109 in your G code program will run the Linux commands in this
# shell script "batch" file, passing the P and Q variables as command
# line arguments.

# M code to set EXTRUDER 0 temperature and wait until temperature has been reached

echo "Usage: M109 Pn - where n is the temperature"

# give the command line arguments descriptive names
P=$1
#Q=$2

TOLERANCE=0.5

PV=$(halcmd gets ext0-PV)

halcmd sets ext0-SP $P

#bash cannot do floating point comparison

SP=$(echo "($P-$TOLERANCE)"| bc -l)
delta=`echo "scale=3; ($PV < $SP)"| bc -l`


while [ "$delta" != "0" ]
do
  PV=$(halcmd gets ext0-PV)
  delta=`echo "scale=3; ($PV < $SP)"| bc -l`
  sleep 1
done

echo "Temperature reached"

# if a M1nn command exits with nonzero status,
# the gcode program exits.  So always exit 0.
exit 0 ;# 0 for success

Greatly appreciated.

Scott

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

More
02 Nov 2019 00:12 #149399 by cmorley
What version of linuxcnc are you using?

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

More
02 Nov 2019 19:24 #149439 by scotta
Hi Chris,

I'm using LINUXCNC - 2.8.0~pre1 on RPi with custom SPI interface component to an "off the shelf" 3D printer control board running my custom firmware. Currently called spiPRU but that's not a sexy name, maybe Remora firmware..

Is there more options in 2.9?

Thanks

Scott

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

More
02 Nov 2019 21:22 #149444 by cmorley
No there are less in 2.7 (2.8 and master are then same right now)
#!/usr/bin/env python
import sys
import hal

tempcomp = hal.component("_dummy_")
value = hal.get_value("ext0-PV")
print value

variable = sys.argv[1]
print variable
hal.set_p('ext0-PV', '%s'%variable)

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

More
02 Nov 2019 22:10 #149447 by scotta
Hi Chris,

Thanks for the quick response and example code. I note the 2.8 docs have the hal.get_value. I'd been looking at 2,7 docs.

Will try and convert my custom M codes over now. Thanks!!

Scott

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

More
02 Nov 2019 22:30 #149449 by scotta
Hi Chris,

Are there any plans to implement a hal.set_s to be able to set a signal?

For the moment I'll create pins that can be set by the M codes.

Scott

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

More
02 Nov 2019 22:36 #149450 by cmorley
There is no plans by me :) My reasoning is you usually set a signal because you want to have a pin drive another. Once you connect a driving pin you can no longer set the signal.

Though it's probably not that much work to add it - I'm just not very good with c++ code.

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

More
03 Nov 2019 05:35 #149468 by scotta
Hi Chris,

Not sure what I'm doing wrong but regardless if I set the created pin to HAL.OUT, HAL.IN or HAL.IO I get a Runtime error?
#!/usr/bin/env python2

# Note! Change the above python2 to python is not using Arch Linux

# M code to set EXTRUDER 0 temperature
# M104 in your G code program will run the python code in this file, 
# passing the P and Q variables as command line arguments.

import sys
import hal

h = hal.component("M104")

print "Usage: M104 Pn - where n is the Extruder temperature"

P = sys.argv[1]
Q = sys.argv[2]

print P
print Q

h.newpin("PV", hal.HAL_FLOAT, hal.HAL_IO)
hal.connect("M104.PV", "BLtouch-SP")
hal.set_p("M104.PV", "%s"%P)

If I use HAL_OUT I get "pin not writable"
If I use HAL_IN I get "pin connected to signal"

I have a custom.hal file that creates the signal to my destination pin.
# spiPRU command outputs

	net bed-heater-SP => 	spiPRU.SP.0
	net ext0-heater-SP  => 	spiPRU.SP.1
	net ext0-cooling-SP => 	spiPRU.SP.2
	net BLtouch-SP => 	spiPRU.SP.3

Appreciate your help here.

Thanks

Scott

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

More
03 Nov 2019 05:54 #149469 by cmorley
The component gets destroyed each time the script ends.
This includes the HAL pins built by it.

So use the script to manipulate pin values or to read pin values but don't make pins.
If you need component with pins, build it separately and use the script to manipulate it.

Chris

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

More
03 Nov 2019 06:04 #149470 by scotta
The following two methods work. For some reason set_p does not play right?

h.newpin("PV", hal.HAL_FLOAT, hal.HAL_IN)
hal.connect("M104.PV", "BLtouch-SP")
h['PV'] = P

or
h.newpin("PV", hal.HAL_FLOAT, hal.HAL_IN)
msg=Popen('halcmd net BLtouch-SP M104.PV', shell=True, stdout=PIPE).stdout.read()
h['PV'] = P

..?

Scott

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

Time to create page: 0.110 seconds
Powered by Kunena Forum