Creating pulses through parallel port

More
18 Feb 2025 06:28 #321896 by MohammedSobh
Creating pulses through parallel port was created by MohammedSobh
Hi everyone 
Iam traying to control nema17 with tb6600 motor driver through my parallel port using python this is my code:
 
import hal
import time
import subprocess


class CNCController:
    def _init_(self):
        self.comp = hal.component("cnc_control") # Create HAL component
        self.comp.newpin("x-step", hal.HAL_BIT, hal.HAL_OUT)
        self.comp.newpin("x-dir", hal.HAL_BIT, hal.HAL_OUT)
        self.comp.ready()
         # Load HAL real-time thread # Start the HAL thread
        subprocess.run(["halcmd", "loadrt", "threads", "name=cnc_thread", "period=1000000"])
        subprocess.run(["halcmd", "addf", "cnc_control.step_x", "cnc_thread"])
        subprocess.run(["halcmd", "start"]) # Connect step pin to parallel port
        subprocess.run(["halcmd", "setp", "stepgen.0.stepspace", "0"])
        subprocess.run(["halcmd", "setp", "stepgen.0.steplen", "0"])
        subprocess.run(["halcmd", "setp", "stepgen.0.maxvel", "37.5"])
        subprocess.run(["halcmd", "net", "x-step", "cnc_control.x-step", "=>", "parport.0.pin-01-out"])
        subprocess.run(["halcmd", "net", "x-dir", "cnc_control.x-dir", "=>", "parport.0.pin-14-out"])
        print("HAL Component Ready!")
    def load_hal_file(self,file_path):
        with open(file_path, 'r') as file:
            for line_num, line in enumerate(file, 1):
                line = line.strip()
                if not line or line.startswith("#"): # Ignore empty lines or comments
                    continue
                print(f"Loading line {line_num}: {line}")
                try: # Run the halcmd command
                     result = subprocess.run(["halcmd", "source", file_path], capture_output=True, text=True) # Check if there's an error output
                     if result.returncode != 0:
                         print(f"Error in line {line_num}: {result.stderr}")
                except Exception as e:
                    print(f"Exception while processing line {line_num}: {e}")
    def step_x(self):
        """Toggle step pin using real-time HAL. """ the problem is here even if there is no time the motor move slow 
        self.comp["x-step"] = True
        time.sleep(0.000001)
        self.comp["x-step"] = False
        time.sleep(0.000001)

if _name_ == "_main_":
    cnc = CNCController()
    cnc.comp["x-dir"] = False
    while True:
        cnc.step_x()
The problem is the motor is moving so slow even if i change the time.sleep between pulse on and off

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

More
18 Feb 2025 14:27 #321918 by langdons
Replied by langdons on topic Creating pulses through parallel port
Use `from time import sleep`


That way, you can just type `sleep()` instead of `time.sleep()`

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

More
18 Feb 2025 14:29 #321919 by langdons
Replied by langdons on topic Creating pulses through parallel port
Don't use time.sleep()


Use a scheduled, clock-synchronised method.

Something similar to Java's Timer or

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

More
19 Feb 2025 00:36 #321954 by unknown
Replied by unknown on topic Creating pulses through parallel port
From my understanding.
Your stepper speed will be limited by the speed of the thread. Hence the reason for a Parallel port setup has a base thread that runs a far higher frequency.

The thread is running at 1khz, it will take 2 periods of the servo thread to set the Step pin Hi then Lo. eg 500Hz.

Divide 500hz by the Number of steps (200) and you will have, theoretically, 2.5rpm. If using micro steps this will be even lower.
The following user(s) said Thank You: langdons

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

More
19 Feb 2025 13:58 #321987 by langdons
Replied by langdons on topic Creating pulses through parallel port
I'm sure the Java Timer API could achieve this, each Timer runs on its own thread.

I don't know much Python anymore though.

I'm a Java guy.

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

More
19 Feb 2025 14:01 #321989 by langdons
Replied by langdons on topic Creating pulses through parallel port
It's probably easier to just use a 555 timer.

You can use a logic gate or binary counter as a buffer, if needed.

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

More
20 Feb 2025 17:18 #322090 by MohammedSobh
Replied by MohammedSobh on topic Creating pulses through parallel port
thank you for your reply but this does not work for me as even if I removed the time.sleep the motor moving with the same speed

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

More
20 Feb 2025 17:21 #322093 by MohammedSobh
Replied by MohammedSobh on topic Creating pulses through parallel port
the motor move with it's speed when use linuxcnc app but this app can't do what I want from the machine if there is any way to program my python code to work as linuxcnc app

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

More
20 Feb 2025 17:23 #322094 by MohammedSobh
Replied by MohammedSobh on topic Creating pulses through parallel port
the motor move with it's speed when use linuxcnc app but this app can't do what I want from the machine if there is any way to program my python code to work as linuxcnc app

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

More
20 Feb 2025 23:51 #322130 by unknown
Replied by unknown on topic Creating pulses through parallel port
Like I said it's the thread period that is limiting your speed.

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

Time to create page: 0.064 seconds
Powered by Kunena Forum