Updating pyVCP halpin from user defined M code Python file

30 Jun 2018 14:53 #113132 by lorenzn
I defined M103 as:
#!/usr/bin/env python

# This file is M103 on the USER_M_PATH
# testing Python interface

import sys
import subprocess
import linuxcnc
import hal

print(sys.argv) #len always 3
    s = linuxcnc.stat() # create a connection to the status channel
    s.poll() # get current values
except linuxcnc.error, detail:
    print "error", detail
g5x = (getattr(s, 'g5x_offset'))
rotation = (getattr(s, 'rotation_xy'))
print("g5x_offset = {}".format(g5x))
print("rotation = {}".format(rotation))

if False:
    # this is incorrect (error):
    hal.set_p("pyvcp.scope-xvalue", "888")

if True:
    # this works but is there a better/recommended method (not using subprocess)?
    subprocess.call("halcmd setp pyvcp.scope-xvalue 123", shell=True)

Can I eliminate subprocess.call() to do this differently?
30 Jun 2018 16:56 #113135 by nkp
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import linuxcnc
c = linuxcnc.command()

c.mdi("M68 E0 Q123")

in HAL:
net vscope  pyvcp.scope-xvalue motion.analog-out-00
The following user(s) said Thank You: lorenzn
30 Jun 2018 17:40 #113138 by lorenzn
Thank you. I had to move c.mode call into a try block to get it to work.
except KeyboardInterrupt:

I see the number of I/O can be increased too. docs:
" If the number of analog I/O needed is more than the default of 4 you can add up to 16 analog I/O by using the num_aio option when loading motmod."
02 Jul 2018 15:45 #113238 by andypugh

lorenzn wrote: Can I eliminate subprocess.call() to do this differently?

The LinuxCNC testsuite seems to use subprocess, though wrapped in a function;


So I think you can probably assume that subprocess is a good way to do it.

I think calling an M-code from G-code that then runs an MDI command to set a HAL pin that is netted to the pin that needs to be changed is needlessly complicated.

Though if you want to change a HAL pin from G-code then the GM68 (etc) codes are the simplest and most reliable way. But do it directly, not via an M-code.
The following user(s) said Thank You: lorenzn
Time to create page: 0.108 seconds
Powered by Kunena Forum