realtime log position to txt use python script

More
11 months 3 weeks ago - 11 months 3 weeks ago #338021 by fery15sty
Can someone help me create a Python script to record positions in the event of a power loss or LinuxCNC error?

I plan to create an external real-time position logger with an update interval of 0.001 seconds. I use this feature for a wirecut machine. I'm using a Python script and have tried activating log_position_to_txt.py with the M-code command, but the position logging process results in a code overflow, causing the .txt file to grow uncontrollably.

Here's the code for log_position_to_txt.py:

##############################
#!/usr/bin/env python2.7
import linuxcnc
import time

stat = linuxcnc.stat()

with open("position-log.txt", "w") as outfile:

while True:

stat.poll()
x,y,z,a,b,c,u,v,w = stat.actual_position

outfile.write("X ")
outfile.write("{:.4f}\n".format(x))
outfile.write("Y ")
outfile.write("{:.4f}\n".format(y))

time.sleep(0.001)
####################################


Can someone help me fix my Python script so that... position recording does not result in code accumulation?
Last edit: 11 months 3 weeks ago by fery15sty.

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

More
11 months 3 weeks ago #338023 by meister
better use python3
Code:
#!/usr/bin/env python3 # # import linuxcnc import time stat = linuxcnc.stat() with open("position-log.txt", "w") as outfile:     while True:         stat.poll()         row = []         for n, axis in enumerate("XYZABCUVW"):             stat.actual_position[n]             row.append(f"{axis} {stat.actual_position[n]}")             #print(f"{axis} {stat.actual_position[n]}")             #outfile.write(f"{axis} {stat.actual_position[n]}\n")         print(" ".join(row))         outfile.write(" ".join(row))         outfile.write("\n")         time.sleep(0.1)

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

More
11 months 3 weeks ago #338024 by meister
Code:
outfile.seek(0) # seek to start of file (keeps only one line in the file) outfile.write(" ".join(row)) outfile.flush() # immediately write to disk / performance warning !!!

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

More
11 months 3 weeks ago - 11 months 3 weeks ago #338026 by fery15sty
Thanks for your help. But position-log.txt doesn't show up to record.

I'm using LinuxCNC 2.8.4 Debian Buster.
Last edit: 11 months 3 weeks ago by fery15sty.

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

More
11 months 3 weeks ago #338029 by tommylight
LinuxCNC already has that feature, it is called POSITION_FILE and should be added to the ini file in the [TRAJ] section, something like this
POSITION_FILE = last_position.txt

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

More
11 months 3 weeks ago #338050 by fery15sty
I've tried POSITION_FILE, but it can only save the position when linuxcnc is closed normally. When my PC is suddenly turned off, such as due to a power outage or linuxcnc error, POSITION_FILE doesn't save the last position of the machine movement.

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

Time to create page: 0.072 seconds
Powered by Kunena Forum