- Posts: 7368
- Thank you received: 2181
Custom M code in Python example?
- cmorley
- Offline
- Moderator
-
Less
More
6 years 11 months ago #149450
by cmorley
Replied by cmorley on topic Custom M code in Python example?
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.
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.
- scotta
- Offline
- Platinum Member
-
Less
More
- Posts: 959
- Thank you received: 489
6 years 11 months ago #149468
by scotta
Replied by scotta on topic Custom M code in Python example?
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?
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.
Appreciate your help here.
Thanks
Scott
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?
Code:
#!/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.
Code:
# 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.
- cmorley
- Offline
- Moderator
-
Less
More
- Posts: 7368
- Thank you received: 2181
6 years 11 months ago #149469
by cmorley
Replied by cmorley on topic Custom M code in Python example?
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
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.
- scotta
- Offline
- Platinum Member
-
Less
More
- Posts: 959
- Thank you received: 489
6 years 11 months ago #149470
by scotta
Replied by scotta on topic Custom M code in Python example?
The following two methods work. For some reason set_p does not play right?
or
..?
Scott
Code:
h.newpin("PV", hal.HAL_FLOAT, hal.HAL_IN)
hal.connect("M104.PV", "BLtouch-SP")
h['PV'] = P
or
Code:
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.
- cmorley
- Offline
- Moderator
-
Less
More
- Posts: 7368
- Thank you received: 2181
6 years 11 months ago #149471
by cmorley
Replied by cmorley on topic Custom M code in Python example?
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
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.
- scotta
- Offline
- Platinum Member
-
Less
More
- Posts: 959
- Thank you received: 489
6 years 11 months ago - 6 years 11 months ago #149472
by scotta
Replied by scotta on topic Custom M code in Python example?
Ok, a working Python Mcode script that sets an existing HAL signal.
Chris, thanks for your help. the hal.set_p thing is a bit strange...
Regards
Scott
Code:
#!/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: 6 years 11 months ago by scotta.
Please Log in or Create an account to join the conversation.
Time to create page: 0.318 seconds