I'm trying to make a function which would output a pulse frequency , given a Ts (time.sleep(ts) on my function) The function is for later use on an acceleration ramp for a stepper motor , as the driver works on a pulse train ,right now I made a function which sets an output to low and high with a time.sleep between them , and it works , however viewed on an osciloscope , it is not as exact as I would like
this is my python code
def ts_2_freq( ts ):
#GPIO.output(self.pin_direccion , direccion)
print("Funcionando con Ts={}".format(ts))
for i in xrange(0,1):
GPIO.output(self.pin_pulse,False)
time.sleep(ts)
GPIO.output(self.pin_pulse,True)
time.sleep(ts)
for i in xrange(1000,1,-1):
ts_2_freq(0.005)
which outputs the following pulse train
As you can see the pulse isnt on time every 5ms as expected , so I was hoping a C library would make it , but I need to be able to call it via python , as my whole code is on python (this code later comunicates to a python gui on tkinter)
this code would be used in 2 ways , setting an acceleration ramp (calling it multiple times) , and to give the motor an aproximate velocity based on pulse frequency , this is later corrected by a PID controller any help on a library that generates me this , also other ideas to solve it are welcome . I made some tests modifying the pigpio PWM frequency , but it has specific steps , and I cannot set an intemediate frequency
>>> pi.set_PWM_frequency(26,1000)
1000L
>>> pi.set_PWM_frequency(26,1020)
1000L
>>> pi.set_PWM_frequency(26,1030)
1000L
>>> pi.set_PWM_frequency(26,1050)
1000L
>>> pi.set_PWM_frequency(26,1100)
1000L
>>> pi.set_PWM_frequency(26,1500)
1600L
>>> pi.set_PWM_frequency(26,1200)
1000L
>>> pi.set_PWM_frequency(26,1400)
1600L
>>>