Advanced Lube pump timing and memory for oil mist spindles

More
05 Mar 2025 14:07 - 07 Mar 2025 20:03 #323267 by Sternfox
Hi Guys,

I have written a script for all of you that use oil mist spindles. 
This py script will time the on and off usage of your spindle and activate the lube pump for a specific amount of time. Then when you close down linuxcnc, a JSON file will be updated with the exact time that has elapsed. making sure you do not over lube your spindle. It will remember how long you last ran your spindle and only start the pump once the allotted lube cycle time has elapsed.

This has been developed for use with a HAAS mill using the Bijur Lubrication system.

I hope it helps someone. 

File Attachment:

File Name: lube.py
File Size:3 KB


Hal components needed 
loadrt oneshot count=1

#central lubrication
loadusr -W /home/cnc/linuxcnc/configs/haas_probebasic/lube.py
net lube-run lube.run => hm2_7i95.0.ssr.00.out-02 # your output for the lube pump
net spindle-cw   lube.spindle_status <= 
.PY is attached but i will paste it here with some explanations
#!/usr/bin/env python3
import linuxcnc, hal, time, os, json
# File to store cycle state
STATE_FILE = "/home/cnc/linuxcnc/configs/haas_probebasic/lube_pump_state.json"  #location of the json file that will automatically be generated
# Initialize HAL component
lube = hal.component("lube")
lube.newpin("run", hal.HAL_BIT, hal.HAL_OUT)
lube.newpin("delay", hal.HAL_BIT, hal.HAL_OUT)
lube.newpin("spindle_status", hal.HAL_BIT, hal.HAL_IN)
lube.newpin("reset", hal.HAL_BIT, hal.HAL_IN)
lube.ready()
# Pump timing settings
PUMP_RUN_DURATION = 10  # Seconds HOW LONG THE PUMP WILL RUN
REST_DURATION = 1800  # 30 minutes (in seconds) HOW LONG THE PUMP CYCLE TIME 
# Initialize variables
pump_running = False
resting = False
elapsed_rest_time = 0
remaining_rest_time = REST_DURATION
start_time = None
# Load previous state if exists
if os.path.exists(STATE_FILE):
    try:
        with open(STATE_FILE, "r") as f:
            state = json.load(f)
            elapsed_rest_time = state.get("elapsed_rest_time", 0)
            resting = state.get("resting", False)
            # Ensure remaining rest time continues exactly where it left off
            remaining_rest_time = max(0, REST_DURATION - elapsed_rest_time)
            if resting:
                print(f"Machine restarted. Pump remains in rest mode for {remaining_rest_time:.2f} seconds.")
            else:
                remaining_rest_time = REST_DURATION  # Reset if not previously resting
    except Exception as e:
        print(f"Error loading state: {e}")
print("Lube system initialized.")
try:
    while True:
        time.sleep(0.1)  # Reduce CPU usage
        # Check spindle status
        if lube["spindle_status"]:
            if not pump_running and not resting:
                pump_running = True
                lube["run"] = 1
                start_time = time.time()
                print("Pump running.")
            # Stop pump after run duration
            if pump_running and (time.time() - start_time >= PUMP_RUN_DURATION):
                pump_running = False
                resting = True
                lube["run"] = 0
                lube["delay"] = 1  # Indicate pump resting
                start_time = time.time()
                print("Pump resting.")
        # Handle rest period
        if resting:
            if lube["spindle_status"]:
                elapsed_rest_time += time.time() - start_time
                start_time = time.time()
                if elapsed_rest_time >= REST_DURATION:
                    resting = False
                    lube["delay"] = 0  # Rest complete
                    elapsed_rest_time = 0
                    print("Rest complete. Pump ready to run again.")
            else:
                start_time = time.time()  # Pause timer if spindle stops
        # Save state periodically
        with open(STATE_FILE, "w") as f:
            json.dump(
                {
                    "elapsed_rest_time": elapsed_rest_time,
                    "resting": resting,
                },
                f,
            )
        # Handle reset signal
        if lube["reset"]:
            print("Reset signal received.")
            resting = False
            elapsed_rest_time = 0
            lube["reset"] = 0  # Reset the reset signal
except KeyboardInterrupt:
    print("Shutting down lube system.")
    lube["run"] = 0
    raise SystemExit

 
Attachments:
Last edit: 07 Mar 2025 20:03 by andypugh. Reason: Added code tags
The following user(s) said Thank You: tommylight, HalaszAttila, pommen, besriworld, Mr. Mass, Hastrh2o

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

More
07 Mar 2025 20:03 #323461 by andypugh
You might want to consider adding this to the "Contributed Components" on wiki.linuxcnc.org
The following user(s) said Thank You: Sternfox

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

More
02 Apr 2025 05:18 #325492 by HalaszAttila
As I saw, all the download links on the "Contributed Components" page are not working.

 
Attachments:

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

More
02 Apr 2025 08:47 - 02 Apr 2025 08:49 #325500 by Aciera
There is a problem with https certificates try the http site:
wiki.linuxcnc.org/cgi-bin/wiki.pl?ContributedComponents

Note though that this page has not been updated for years, like most pages on the wiki.
Last edit: 02 Apr 2025 08:49 by Aciera.

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

More
02 Apr 2025 21:47 #325555 by andypugh
This isn't a great solution, but I don't have the access to fix the Dreamhost certificate.

But, you can copy the download link, then paste it into the address bar of a new window, delete the "s" in "https://..." and then it should download the file.

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

Time to create page: 0.139 seconds
Powered by Kunena Forum