Creating pulses through parallel port
- MohammedSobh
- Offline
- New Member
-
Less
More
- Posts: 4
- Thank you received: 0
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:The problem is the motor is moving so slow even if i change the time.sleep between pulse on and off
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()Please Log in or Create an account to join the conversation.
- unknown
- Offline
- Platinum Member
-
Less
More
- Posts: 888
- Thank you received: 325
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.
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.
Please Log in or Create an account to join the conversation.
- MohammedSobh
- Offline
- New Member
-
Less
More
- Posts: 4
- Thank you received: 0
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.
- MohammedSobh
- Offline
- New Member
-
Less
More
- Posts: 4
- Thank you received: 0
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.
- MohammedSobh
- Offline
- New Member
-
Less
More
- Posts: 4
- Thank you received: 0
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.
- unknown
- Offline
- Platinum Member
-
Less
More
- Posts: 888
- Thank you received: 325
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.
- unknown
- Offline
- Platinum Member
-
Less
More
- Posts: 888
- Thank you received: 325
21 Feb 2025 01:19 #322146
by unknown
Replied by unknown on topic Creating pulses through parallel port
What also helps if you give an overview of your complete project, rather than just a little bit at a time.
If you just want to run a single stepper Linuxcnc may be overkill, operating the parallel port via a simple C program may be easier, but that will mean obtaining permissions from the kernel to do these, and running in user space may not give consistent timings for the stepper.
The other option would be to have a hardware base approach and use python or what ever to control when the stepper runs & stops.
Even a simple Arduino sketch controlled by via a serial connection would be easy. Depending on the library used you could have control over decel & accel.
If you just want to run a single stepper Linuxcnc may be overkill, operating the parallel port via a simple C program may be easier, but that will mean obtaining permissions from the kernel to do these, and running in user space may not give consistent timings for the stepper.
The other option would be to have a hardware base approach and use python or what ever to control when the stepper runs & stops.
Even a simple Arduino sketch controlled by via a serial connection would be easy. Depending on the library used you could have control over decel & accel.
The following user(s) said Thank You: tommylight
Please Log in or Create an account to join the conversation.
Time to create page: 0.075 seconds