Updating pyVCP halpin from user defined M code Python file

More
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
        
try:
    s = linuxcnc.stat() # create a connection to the status channel
    s.poll() # get current values
except linuxcnc.error, detail:
    print "error", detail
    sys.exit(1)
	
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?

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

More
30 Jun 2018 16:56 #113135 by nkp
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import linuxcnc
c = linuxcnc.command()

c.mode(linuxcnc.MODE_MDI)
c.wait_complete()
 
c.mdi("M68 E0 Q123")





in HAL:
net vscope  pyvcp.scope-xvalue motion.analog-out-00
The following user(s) said Thank You: lorenzn

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

More
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.
try:
    c.mode(linuxcnc.MODE_MDI)
except KeyboardInterrupt:
    pass

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."

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

More
02 Jul 2018 15:45 #113238 by andypugh

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


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

github.com/LinuxCNC/linuxcnc/blob/af15a4...nlink/hallink.py#L20

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

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

More
04 Mar 2019 14:34 #127701 by pl7i92
you might consider to use this option here
by Mcode

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

Time to create page: 0.074 seconds
Powered by Kunena Forum