realtime log position to txt use python script

More
07 Nov 2025 12:20 - 07 Nov 2025 12:29 #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?
Attachments:
Last edit: 07 Nov 2025 12:29 by fery15sty.

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

More
07 Nov 2025 12:43 #338023 by meister
better use python3
#!/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
07 Nov 2025 12:49 #338024 by meister
        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
07 Nov 2025 13:46 - 07 Nov 2025 13:49 #338026 by fery15sty
Replied by fery15sty on topic realtime log position to txt use python script
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: 07 Nov 2025 13:49 by fery15sty.

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

  • tommylight
  • tommylight's Avatar
  • Away
  • Moderator
  • Moderator
More
07 Nov 2025 15:11 #338029 by tommylight
Replied by tommylight on topic realtime log position to txt use python script
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
07 Nov 2025 21:00 #338050 by fery15sty
Replied by fery15sty on topic realtime log position to txt use python script
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.

  • tommylight
  • tommylight's Avatar
  • Away
  • Moderator
  • Moderator
More
07 Nov 2025 21:38 #338055 by tommylight
Replied by tommylight on topic realtime log position to txt use python script
I am pretty sure it did till 2.7 as i used it on every machine, and we had power outages daily back then.
No idea now as we have no power outages so i do not use it anymore.

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

More
18 Nov 2025 07:32 - 18 Nov 2025 07:36 #338604 by fery15sty
Replied by fery15sty on topic realtime log position to txt use python script
I've tried it on LinuxCNC 7.4.15 Debian Wheezy. While LinuxCNC is running, coordinates aren't saved when the power outages.

The last coordinates are only saved when LinuxCNC is closed normally.
Attachments:
Last edit: 18 Nov 2025 07:36 by fery15sty.

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

More
19 Nov 2025 12:06 #338713 by unknown
Just an idea.
The last position that will be recorded will be the last data that that was successfully written to the hardware from the file buffer.
Now the power going out will be for all intents and purposes a random event, there is no guarantee that any particular data will be successfully written to hardware before a power outage.
The only way I can see everything happening gracefully is with a UPS, that once detects a mains outage is able to shutdown gracefully. But the machine itself may not be in the exact position you expect due to inertia and any other unknown factors mechanical or electrical.
So basically data not being written the disk is more of a limitation of an OS when power is removed suddenly.
Also one would hope the file being written to is not stored in tmpfs (ram) then written to disc during the shutdown process of Linuxcnc.

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

Time to create page: 0.094 seconds
Powered by Kunena Forum