Need help to speed up user defined M code

More
01 Feb 2011 23:32 #6944 by dubostjp
Project Description:
I use a CNC machine equip with EMC2 to scan a tooth matrix (two time 20X50mm ) at about 20 micron step (I use 0.9 degree steeper motor). The tooth distance scan measurement is done via a laser measurement unit (spot at 3 micron) via a RS232 COM port. The matrix scan (20X20 micron grid) is done via a .ngc G code program that call a custom C code to read and store the laser measurement value. The laser C code is called via a user defined M code call (M101) that initiate a bash script.

Problem Description:
This system is very slow. The laser can only read about 15 points per second. According to my investigation, even if my laser C code is very fast, this system spent almost all his time into the [get into the M101 call] and [get out of M101 code]. When I run the system with an empty M101 code (a bash file that just return), I got about the same scan time.

Questions:
Do you know how I can improve the speed of this system?

Do you know if it has a way to remove the M101 call and patch the EMC2 code to include the C laser code directly into the EMC2 code (after re-compiling the EMC2 code). Which process that I should follow if this solution is valuable.

Thank you

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

More
02 Feb 2011 00:08 - 02 Feb 2011 00:15 #6946 by andypugh
How does the bash script access the data? What form is it in? You might be able to write a HAL module to do the same thing.

<edit>
OK, so reading your message, the data is on RS232. It might be possible to read RS232 data into HAL, I am pretty certain you can read it into Classic Ladder.
PySerial can be used, I think.
Last edit: 02 Feb 2011 00:15 by andypugh.

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

More
02 Feb 2011 01:23 #6948 by dubostjp
For each scanned point, I call the M110 bash script defined below. The ar200 is a custom C code that send a RS232 command to the laser and the bash re-direct the laser response (RS232 ASCII code) into a tmp file. The .ngc code start with a M101 call that initiate the laser.

As notified into my question, the scanned time is very slow even with an empty M110 bash file.


$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#!/bin/bash
# M110 of G code; read a distance code
# Shell script file, passing the P and Q variables as command line arguments.
# Command line arguments descriptive names
PP=$1 # X axe position
QQ=$2 # Y axe position

# write the position X Y
echo -n "$PP $QQ " >> /home/beldent/emc2/nc_files/tmp$PPID.txt

# Write read distance to temp file
/home/beldent/emc2/nc_files/ar200 >> /home/beldent/emc2/nc_files/tmp$PPID.txt \
2> /home/beldent/emc2/nc_files/M110.stat

exit 0

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

More
02 Feb 2011 01:44 #6949 by dubostjp
dubostjp wrote:

For each scanned point, I call the M110 bash script defined below. The ar200 is a custom C code that send a RS232 command to the laser and the bash re-direct the laser response (RS232 ASCII code) into a tmp file. The .ngc code start with a M101 call that initiate the laser.

As notified into my question, the scanned time is very slow even with an empty M110 bash file.


$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#!/bin/bash
# M110 of G code; read a distance code
# Shell script file, passing the P and Q variables as command line arguments.
# Command line arguments descriptive names
PP=$1 # X axe position
QQ=$2 # Y axe position

# write the position X Y
echo -n "$PP $QQ " >> /home/beldent/emc2/nc_files/tmp$PPID.txt

# Write read distance to temp file
/home/beldent/emc2/nc_files/ar200 >> /home/beldent/emc2/nc_files/tmp$PPID.txt \
2> /home/beldent/emc2/nc_files/M110.stat

exit 0


Addendum

The ar200 C code open a /dev/tty RS232 port, call a send data function (3 byte) to request the laser to read the distance, wait the answer via a receive data function (with timeout), then, it prints on the standart outout the response (about 10 bytes). The baud rate is fixed at 56K baud.

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

More
02 Feb 2011 10:43 #6956 by andypugh
dubostjp wrote:

The ar200 C code open a /dev/tty RS232 port, call a send data function (3 byte) to request the laser to read the distance, wait the answer via a receive data function (with timeout), then, it prints on the standart outout the response (about 10 bytes). The baud rate is fixed at 56K baud.


Do you know how long the code takes to execute?

I am wondering if you could put that code in a custom HAL component (see linuxcnc.org/docs/html/hal_comp.html) which returns without doing anything normally, but reads/writes the values to the text file when an input bit is high (presumably from one of the G-code digital outputs (M62?)

The thing about realtime functions is that they run every thread period, and have to complete within a fraction of the thread period. This might mean that you need to slow down the slow thread to make this possible.

It probably makes a lot more sense to do it as a userspace function, communicating status on (software) digital IO pins. But I don't know much about Userspace components.
The existing userspace HAL components are here
git.linuxcnc.org/gitweb?p=emc2.git;a=tre...2bc25df682b2;hb=HEAD
And the Modbus one might be a very good template to follow.
However, it is probably much easier to use the comp preprocessor to create the special HAL stuff (mainly interface pins). Comp also allows you to add new components without recompiling the whole of EMC2.

I imagine that you would have, in the comp file

pin in bit sample_now "trigger a sample";
pin out bit finished "finished";
pin in float x-pos "pin to be linked to axis.0.pos-cmd";
pin in float y-pos "pin to be linged to axis.1.pos-cmd";
option userspace true;

;;

<Your original C-code in a wait-loop>

And then in the G-code you would

M62 P0 ;set sample-now pin
M66 P1 L1; wait for sample-done pin rising edge
M63 P0; reset sample-now pin

This is all off the top of my head, any or all of these steps might be unfeasible.

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

Time to create page: 0.096 seconds
Powered by Kunena Forum