User defined M-code calling python script

More
13 Apr 2015 21:12 #57752 by dienno
This is the first time I've posted so please be gentle, I'm more sensitive than I want to admit.

What I want to do:
I have a user defined M-code that I call through pyvcp that is supposed to turn on a solenoid and launch a python script that opens a separate opencv camera window.

Why do I want to do this?
I have a center finding camera setup on my mill with a pneumatic piston that will flip it down under the spindle to find the center of my part.
I could not get camunits or camview to work on my debian 12.xx install.
I would like to have a pyvcp button that moves the spindle into the correct XYZ location then engage my solenoid/piston to flip the camera into place.
Then I want the same pycvp button to call my python script which opens the camera output and rotates the image accordingly. (note: the opencv camera python script works great stand alone right now)

Current state:
I can call my M101 user defined gcode from my pyvcp panel and it changes the state of my pin, so I know that my custom gcode is working
I have a line in the M101 definition like 'python camera1.py" to call the camera script.
When I run the M101 from pyvcp the pin changes but the camera1.py script does not launch.
If I make a copy of the M101 file and rename it M101.sh and make it executable and run it from the command line it executes my camera1.py script.

Assumption:
I originally assumed that custom m-codes are run as bash scripts so it should function the same when I run it as an m-code or a bash script (guess I'm missing something here).
I am currently assuming that there is some slight difference when the bash script is run from within linuxcnc to call a python script.

Question:
What am I missing?
Is there some specific permission I need to give camera1.py so it can be called from within linuxcnc in an M-code?
Is what I am trying to do possible?

Thanks
Dustin

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

More
13 Apr 2015 21:25 #57755 by ArcEye
Hi

To run a user M code command file these conditions must be met

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

Calling camera1.py will not do anything unless it is executable and the command processor is specified in the header
otherwise use
python camera1.py

Without seeing your script don't know if anything else is at play

regards

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

More
13 Apr 2015 22:38 #57758 by dienno
Thanks for the quick reply ArcEye.

I am at work, so I will have to post the script later.

I looked through all of the user Mcode conditions and I have met them. Also, my mcode does run from pyvcp and it does change the state of my pin it just does not execute the python script also. The script camera1.py is executable and is stored in the same folder as my user defined m-codes.

I think this is what my m-code looks like (file name M101). I'll have to double check when I go home.

#!/bin/bash
# file to turn on parport pin 17 to engage the camera
halcmd setp parport.1.pin-17-out False
python camera1.py (this may also be ./camera1.py, I'm not sure what the final version looked like)
exit 0

When I run the M101 from pyvcp or the manual input, the hal pin changes state but the camera1.py script does not launch.

If I change the file name to M101.sh and comment out the halcmd, and run from the command line it launches the camera1.py script and the camera output pops up on the screen. (see modified code below for file with name M101.sh)
#!/bin/bash
# file to turn on parport pin 17 to engage the camera
# halcmd setp parport.1.pin-17-out False
python camera1.py (this may also be ./camera1.py, I'm not sure what the final version looked like)
exit 0

Why doesn't camera1.py launch when I run M101 from within linuxcnc? Do I need something in the header of the file that references python? I think you mentioned this in your reply but I don't fully understand.

I'm sure I'm doing something dumb.

Thanks for the help

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

More
13 Apr 2015 23:14 #57760 by ArcEye
This M133 script runs a halcmd and then opens JTs ArcBuddy, which is a python app
#!/bin/bash

halcmd setp iocontrol.0.lube_level 1

arcbuddy.py

exit 0

What I meant by the header to specify the processing app is in the first line of arcbuddy.py
#!/usr/bin/python

This is not present in this form in a lot of python scripts, they reference python-env, which may or may not be set

The result is
(ignore the fact that it is Machinekit, I had that loaded at the time)




So you can set a pin and then open a python app if all is correct

regards
Attachments:

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

More
13 Apr 2015 23:48 #57764 by andypugh

What I want to do:
I have a user defined M-code that I call through pyvcp that is supposed to turn on a solenoid and launch a python script that opens a separate opencv camera window.


There are several ways to do this, some more fun than others.

M101 will search for an executable file called "101" and will execute it.
An executable file can be a Python script. So you could simply rename your Python script, start the file with the "magic" incantation
#!/usr/bin/python
Make the file executable (chmod or right-click in the GUI and go to properties) and then M101 should run the Python directly.

Or you could start at the other end...
A GladeVCP panel can have buttons that run arbitrary python code, and init code could create the camera window.
Python scripts can send-G-code to LinuxCNC:
www.linuxcnc.org/docs/html/common/python..._linuxcnc_command_tt (c.mdi)
You can manipulate HAL pins created by the Python code inside the Python code.

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

More
14 Apr 2015 01:11 #57767 by dienno
Thanks for all of the help everyone. I'll give this a shot today or tomorrow.

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

More
14 Apr 2015 07:26 #57770 by dienno
Renaming my python script called camera1.py to M102 worked great. I already had the shebang at the top so it was really easy. Thanks for the help. I'll post my finished scripts for the camera and NGCgui stuff once I make sure it is all debugged. Just in case someone else might have a use for it.

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

More
14 Apr 2015 11:40 #57775 by dienno
I think I jumped the gun a little on claiming full success. My new mcode that calls my camera Python script opens the camera as expected but when I click back to my axis screen it closes the camera window. Hmmm. I'll post my script tomorrow but I'm guessing that I may need to change my approach to what arceye originally suggested.

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

More
14 Apr 2015 13:17 #57778 by ArcEye
You probably need to spawn the process so that it runs independently.

Simple as using

mypyprogram.py &

in the M1xx file

or suitable equivalent if launching from another python program.

regards

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

More
14 Apr 2015 20:30 #57798 by dienno
I'll give that a try. Here is my current mcode (M102) that is actually just the python script that runs the camera (see attached). To do what you are saying, I'm guessing I will have to go back to having my M102 script be a bash script that calls my python script. So I will have to rename my python camera script back to camera1.py and just add camera1.py & to my bash script.

When the bash script calls the python script does it look in the same directory that it resides in, or does it look in the working path specified in the ini file?

Thanks

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

Time to create page: 0.110 seconds
Powered by Kunena Forum