User defined M-code calling python script

More
14 Apr 2015 20:38 #57799 by andypugh

when I click back to my axis screen it closes the camera window.


I can't see why that would be. Can you see if LinuxCNC is sending a "terminate" signal?

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

More
14 Apr 2015 20:48 #57800 by dienno
How can I check if linuxcnc is sending a terminate signal?

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

More
14 Apr 2015 20:51 #57801 by andypugh

How can I check if linuxcnc is sending a terminate signal?


Do you have a signal handler in the Python code?

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

More
14 Apr 2015 20:55 #57802 by dienno
You are getting way outside of my skill level now. Sorry. Unfortunately I'm a mechanical engineer.

I'm pretty sure that I don't have a signal handler in the Python code.

I posted the python script call M102 in one of my last posts.

I am at work now so I can't try the spawning process either, but it sounds like that might be a solution that circumvents the terminating issue.

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

More
14 Apr 2015 20:58 #57803 by andypugh

You are getting way outside of my skill level now. Sorry. Unfortunately I'm a mechanical engineer.


Mine too (So am I)

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

More
14 Apr 2015 21:54 #57804 by dgarrett
An example session showing an M file calling a python/tkinter file.
$ #---------------------------------------------------------------
$ # Start in the directory for the axis sim:
$ cd configs/sim/axis 
$ #---------------------------------------------------------------
$ # Copy axis.ini:
$ cp axis.ini tst.ini
$ #---------------------------------------------------------------
# Edit tst.ini for explicit specification for M file location:
$ gedit tst.ini 
#---------------------------------------------------------------
# Show explicit specification for M file location:
$ grep -2 USER_M_PATH tst.ini

[RS274NGC]
USER_M_PATH = .


$ #---------------------------------------------------------------
$ # Show the M102 file:
$ cat ./M102
#!/bin/bash
echo "this is $0"
./a.py &
echo "started a.py in background"
exit 0
$ 
$ #---------------------------------------------------------------
$ # Show the python file (a.py):
$ cat ./a.py
#!/usr/bin/python
def bcmd():
    print 'hellooooooooo'

import Tkinter
rw = Tkinter.Tk()
b  = Tkinter.Button(rw,text="Hello",command=bcmd)
b.pack()
rw.mainloop()
$ 
$ #---------------------------------------------------------------
$ # make sure both files are executable:
$ chmod +x ./M102
$ chmod +x ./a.py
$ 
$ #---------------------------------------------------------------
$ # test simulator tst.ini
$ linuxcnc ./tst.ini &
   ...
   F1,F2, Home All, MDI tab, Run M102:

this is ./M102  
started a.py in background

   ...
   Push the Hello button 
hellooooooooo

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

More
14 Apr 2015 22:02 #57805 by ArcEye
The reason is covered in passing in the docs

www.linuxcnc.org/docs/devel/html/gcode/m...tml#sec:M100-to-M199

Basically it is the difference between serial and parallel.
If Linuxcnc executes a script it waits for a return value before it continues, otherwise havoc would ensue as the machine continued moving, before whatever was commanded has taken effect.

To execute as a standalone app, you need to use the & suffix in bash or use whatever spawnlpe() equivilent there is in python so that return value goes back to Linuxcnc

When the script is executed the PATH env variable will be used (as modified by /scripts/rip-environment if a RIP) so the executable needs to be in a known location.
You might get away with having it in the same dir as the M1xx script, don't know if I have tried it, I normally just symlink into /usr/local/bin for anything outside the env I want to be able to execute

regards

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

More
14 Apr 2015 22:04 #57806 by andypugh

An example session showing an M file calling a python/tkinter file.


Are you saying that it is necessary for the M102 to call bash file which calls the Python file, rather than call the Python file directly?

Is that because the M102 file has to return 0 and exit?

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

More
14 Apr 2015 22:20 - 14 Apr 2015 22:36 #57807 by dgarrett
When an Mnnn file is invoked, it must exit in order for LinuxCNC to continue running a gcode program.
So to start an application using an Mnnn file and keep it running,
it is convenient to use a helper Mnnn script that can start the application in the background
and then exit.

If the Mnnn program exits with return code 0, an including gcode program will continue.
If the Mnnn program exits with return code not zero, an including gcode program will stop.

The helper Mnnn script can be in any form of executable so long as it is found by
the search rules for Mnnn files. An application started by a Mnnn helper script will be found
using the PATH environment unless an explicit path is used. In the example above, a.py
resides in the directory of the configuration file and it was started with an explicit pathname (./a.py)


Recent LinuxCNC versions support an ini file stanza to start independent, persistent applications:
[APPLICATIONS]
APP=appname
ref: www.linuxcnc.org/docs/2.7/html/config/in...APPLICATIONS-section
Last edit: 14 Apr 2015 22:36 by dgarrett.

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

More
15 Apr 2015 09:12 #57810 by dienno
ok, I went back to the set up where I have an mcode (M100) which is a bash script that changes the camera piston state and calls the python script.

It turns out that the reason it was not launching the python script was because I needed an absolute path to the python file ( I guess it could also be relative if I new where it was searching from but this seemed easier. The & in the code to try spawning did not work.

Either way it works and functions how I would like it to.

Here is the final code that I used

Custom mcode bash script (File name M100)
#!/bin/bash
# file to engage camera piston
halcmd setp parport.1.pin-17-out False
~/linuxcnc/configs/DVDCNC1/mcodes/camera1.py
exit 0

and the python script that runs the output from the live camera.
#!/usr/bin/python
"""
requires: python-opencv 

"""
import cv2

#required for rotation transform
import numpy as np


camera_number = 0 #0, 1, x..

white = 255,255,255
black = 0,0,0
red = 0,0,255
green = 0,255,0
blue = 255,0,0

#constants
rot_angle = 90 #Angle to rotate image by
color_line = blue #lines and circles color
scale = 1
x_offset=0	#offset between camera and spindle
y_offset=20	#offset between camera and spindle

#captures video from 0
cap = cv2.VideoCapture(camera_number)
ret, frame = cap.read()		#capture the first frame to determine size
img1 = frame		  	#rename frame before transformations
# find the orginal height and width of the image (output is a tuple)
rows, cols, depth = img1.shape
#print rows, cols, depth
# Get rotation matrix getRotationmatrix2D(center,angle,scale)
M = cv2.getRotationMatrix2D((cols/2,rows/2),rot_angle,scale)


while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    #ret = cap.set(3,640)
    #ret = cap.set(4,480)

    # calculate rotated image warpAffine(input image,output image,trans matrix,output size)
    img_rotated = cv2.warpAffine(frame,M,(cols,rows))

    # add lines and circles to the captured frame
    cv2.line(img_rotated,(0,(240-y_offset)),(640,(240-y_offset)),(color_line),1)
    cv2.line(img_rotated,((320+x_offset),0),((320+x_offset),480),(color_line),1)

    cv2.circle(img_rotated,((320+x_offset),(240-y_offset)), 20, (color_line), 1)
    cv2.circle(img_rotated,((320+x_offset),(240-y_offset)), 50, (color_line), 1)
    cv2.circle(img_rotated,((320+x_offset),(240-y_offset)), 90, (color_line), 1)
    cv2.circle(img_rotated,((320+x_offset),(240-y_offset)), 130, (color_line), 1)
    cv2.circle(img_rotated,((320+x_offset),(240-y_offset)), 170, (color_line), 1)
    cv2.rectangle(img_rotated,(0,450),(80,480),(0,0,0),-1)
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img_rotated,'[q]uit',(10,470), font, 0.7,(255,255,255),1)
    #cv2.putText(img_rotated,'[q]uit    [a]circle+ [z]circle-    [c]olor',(10,470), font, 0.7,(255,255,255),1)

    # shows final image imshow(window_name,target_image)
    cv2.imshow('DVD CNC Camera', img_rotated)

    # wait for input keys
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Thanks again

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

Time to create page: 0.101 seconds
Powered by Kunena Forum