Custom M code in Python example?

More
03 Nov 2019 06:24 #149471 by cmorley
hal.set_p()
Is used to set pins that can be written to - HAL_IN or HAL_IO
It then checks to see if there is a signal already connected to a pin and if there is it refuses to write to it.

Your program breaks those rules.

h = P
sets your component's pin which doesn't have these restrictions.
But I'm surprised it works more then once.

I suppose each time it makes a component connects the pins sets the pin then closes the component and breaks the connection.
IIRC when a signal is disconnected from a pin it keeps it's last value so that is probably why it works.

I wouldn't do it this way but if it works - it works :)

Chris

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

More
03 Nov 2019 06:25 - 03 Nov 2019 06:26 #149472 by scotta
Ok, a working Python Mcode script that sets an existing HAL signal.
#!/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

Mcode = 'M104'
usage = 'Extruder temperature'
pin = 'SP'
signal = 'ext0-SP'

h = hal.component(Mcode)

print "Usage: " + Mcode + " Pn - where n is the " + usage

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

print 'Setting ' + usage + ' to ' + P

h.newpin(pin, hal.HAL_FLOAT, hal.HAL_IN)
hal.connect(Mcode + '.' + pin, signal)

h[pin] = P

Chris, thanks for your help. the hal.set_p thing is a bit strange...

Regards

Scott
Last edit: 03 Nov 2019 06:26 by scotta.

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

More
03 Nov 2019 06:33 #149473 by cmorley
Glad you got it to work.

set_p works on the HAL system it's the same code as typing setp in HAL and has the same restrictions.

The hal[] = is from inside the component so the direction of the pins are opposite.
(you wouldn't write to the component's IN pin, nor read the OUT pin)

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

Time to create page: 0.069 seconds
Powered by Kunena Forum