unable to toggle machine power by python interface

More
10 years 1 month ago #79807 by giantstone
HI, I'm using python interface trying to automatically run linuxcnc,
when I used
if task_state !=2 :
c.state(linuxcnc.STATE_ON) #
c.wait_complete() # wait until mode switch executed
print 'state changed to', getattr(s, 'task_state' )
the machine's task state made no change. I have to manually click on the toggle machine power.

here is my script:

import linuxcnc
import numpy as np

if __name__ == '__main__':

try:

e = linuxcnc.error_channel()
s = linuxcnc.stat() # create a connection to the status channel
c = linuxcnc.command()
c.wait_complete() # wait until mode switch executed

c.reset_interpreter()
c.teleop_enable(1)
c.wait_complete() # wait until mode switch executed

task_state = getattr(s, 'task_state' )
task_mode = getattr(s, 'task_mode' )
homed = getattr(s, 'homed' )
current_line = getattr(s, 'motion_line' )
## motion_mode = getattr(s, 'motion_mode' )
motion_type = getattr(s, 'motion_type' )
paused = getattr(s, 'paused' )
print 'task_state',task_state,'task_mode',task_mode, 'homed',homed
print 'current_line',current_line,'paused',paused


if task_state !=2 :
c.state(linuxcnc.STATE_ON) #
c.wait_complete() # wait until mode switch executed
print 'state changed to', getattr(s, 'task_state' )

## if np.sum(homed)<4: # X Y Z A
## if task_mode !=1: #(MODE_MDI, MODE_MANUAL, MODE_AUTO).
## c.mode(linuxcnc.MODE_MANUAL ) #linuxcnc.MODE_AUTO
## c.wait_complete() # wait until mode switch executed
## c.home(0)
## c.home(1)
## c.home(2)
## c.home(3)
## c.wait_complete() # wait until mode switch executed
## print 'homed', getattr(s, 'homed' )


if task_mode !=2: #(MODE_MDI, MODE_MANUAL, MODE_AUTO).
c.mode(linuxcnc.MODE_AUTO) #linuxcnc.MODE_AUTO
c.wait_complete() # wait until mode switch executed
print 'mode changed to', getattr(s, 'task_mode' )

print 'working'
c.abort()
c.reset_interpreter()
c.program_open("/home/sam3/linuxcnc/NGC_file/fur_cut1.ngc")
c.auto(linuxcnc.AUTO_RUN, 0)

flag = 0
while 1:
error = e.poll()
print_error(error)
s.poll() # get current values
## print getattr(s, 'dtg' )
motion_type = getattr(s, 'motion_type' )
task_mode = getattr(s, 'task_mode' )
print 'motion_type',motion_type,'task_mode',task_mode

if motion_type == 2 and flag==0 :#0 = idle 1: Traverse 2: Linear feed 3: Arc feed 4: Tool change 5: Probing 6: Rotary axis indexing)
c.auto(linuxcnc.AUTO_PAUSE) #run, step, pause or resume a program
c.wait_complete() # wait until mode switch executed

c.auto(linuxcnc.AUTO_RESUME) #run, step, pause or resume a program
flag=1

except linuxcnc.error, detail:
print "error", detail
sys.exit(1)

finally:
c.abort()


I also tried to home axes, but it didn't work neither. ( see the commented section above).

could someone help me to address these issues?

Please Log in to join the conversation.

More
10 years 1 month ago #79811 by cmorley
You must poll before checking task state.

I started linuxcnc, then opened a python terminal:
Code:
>>> import linuxcnc >>> c = linuxcnc.command() >>> c.state(linuxcnc.STATE_ESTOP_RESET) >>> c.state(linuxcnc.STATE_ON) >>> s = linuxcnc.stat() >>> s.poll() >>> task_state = getattr(s, 'task_state' ) >>> print task_state 4

worked fine.
Also would recommend not using code like this:
if task_state !=4 :
instead use:
if task_state !=linuxcnc.STATE_ON:

It's safer, in the future the enumeration may change but the command name should not.
Chris M

Please Log in to join the conversation.

More
10 years 1 month ago #79813 by giantstone
Hi, Chris, Thanks for your reply,
I solved the issue now. here is my updated scripts:
Code:
import linuxcnc import numpy as np if __name__ == '__main__': try: e = linuxcnc.error_channel() s = linuxcnc.stat() # create a connection to the status channel s.poll() # get current values c = linuxcnc.command() c.wait_complete() # wait until mode switch executed c.reset_interpreter() c.teleop_enable(1) c.wait_complete() # wait until mode switch executed task_state = getattr(s, 'task_state' ) task_mode = getattr(s, 'task_mode' ) homed = getattr(s, 'homed' ) current_line = getattr(s, 'motion_line' ) ## motion_mode = getattr(s, 'motion_mode' ) motion_type = getattr(s, 'motion_type' ) paused = getattr(s, 'paused' ) print 'task_state',task_state,'task_mode',task_mode, 'homed',homed print 'current_line',current_line,'paused',paused if task_state != linuxcnc.STATE_ON : c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) # c.wait_complete() # wait until mode switch executed s.poll() # get current values task_state = getattr(s, 'task_state' ) print 'state changed to', task_state if np.sum(homed)<4: if task_mode !=linuxcnc.MODE_MANUAL : #(MODE_MDI, MODE_MANUAL, MODE_AUTO). c.mode(linuxcnc.MODE_MANUAL ) #linuxcnc.MODE_AUTO c.wait_complete() # wait until mode switch executed c.home(0) c.wait_complete() # wait until mode switch executed c.home(1) c.wait_complete() # wait until mode switch executed c.home(2) c.wait_complete() # wait until mode switch executed c.home(3) c.wait_complete() # wait until mode switch executed s.poll() # get current values homed = getattr(s, 'homed' ) print 'homed', homed if task_mode !=linuxcnc.MODE_AUTO: #(MODE_MDI, MODE_MANUAL, MODE_AUTO). c.mode(linuxcnc.MODE_AUTO) #linuxcnc.MODE_AUTO c.wait_complete() # wait until mode switch executed s.poll() # get current values task_mode = getattr(s, 'task_mode' ) print 'mode changed to', task_mode print 'working' c.abort() c.reset_interpreter() c.program_open("/home/sam3/linuxcnc/NGC_file/fur_cut1.ngc") c.auto(linuxcnc.AUTO_RUN, 0) flag = 0 while 1: error = e.poll() print_error(error) s.poll() # get current values ## print getattr(s, 'dtg' ) motion_type = getattr(s, 'motion_type' ) task_mode = getattr(s, 'task_mode' ) print 'motion_type',motion_type,'task_mode',task_mode if motion_type == 2 and flag==0 :#0 = idle 1: Traverse 2: Linear feed 3: Arc feed 4: Tool change 5: Probing 6: Rotary axis indexing) c.auto(linuxcnc.AUTO_PAUSE) #run, step, pause or resume a program c.wait_complete() # wait until mode switch executed c.auto(linuxcnc.AUTO_RESUME) #run, step, pause or resume a program flag=1 except linuxcnc.error, detail: print "error", detail sys.exit(1) finally: c.abort()

Please Log in to join the conversation.

More
10 years 1 month ago #79969 by BigJohnT
It's much clearer to read if you just do a straight compare of the status. There is no need to use getattr in this example.
Code:
task_state = getattr(s, 'task_state' ) if task_state != linuxcnc.STATE_ON : if s.task_state != linuxcnc.STATE_ON:

JT

Please Log in to join the conversation.

Time to create page: 0.088 seconds
Powered by Kunena Forum