I want create phase delay (as below figure) between each pulse generated from pigpio library, I using usleep to delay before generate pulse as in code, but all pusle generted is not phase difference (as the second figure). Please show me the way to done as the first figure.thanks
import sys, struct
from socket import*
import numpy
import numpy as np
import pickle
import time, pigpio
delay_array1=[10,20,18,15]
##create 40khz pulse
usleep = lambda x: time.sleep(x/1000000.0)
FREQ=40000
PWM1=2
PWM2=3
PWM3=4
PWM4=21
GPIO=[PWM1,PWM2,PWM3,PWM4]
_channels = len(GPIO)#_channels=26
print(_channels)
_dc=[0]*_channels #_dc=[0,..,0]; 26 phan tu 0
print (_dc)
_micros=1000000/FREQ
old_wid = None
def set_dc(channel, dc):
global old_wid
if dc < 0:
dc = 0
elif dc > _micros:
dc = _micros
_dc[channel] = dc
for c in range(_channels):#c run 0..25
d = _dc[c]
g = GPIO[c]
if d == 0:
pi.wave_add_generic([pigpio.pulse(0, 1<<g, _micros)])
elif d == _micros:
pi.wave_add_generic([pigpio.pulse(1<<g, 0, _micros)])
else:
pi.wave_add_generic(
[pigpio.pulse(0, 1<<g, d), pigpio.pulse(1<<g, 0, _micros-d)])
new_wid = pi.wave_create()
print('id:',new_wid)
if old_wid is not None:
pi.wave_send_using_mode(new_wid, pigpio.WAVE_MODE_REPEAT_SYNC)
#Spin until the new wave has started.
while pi.wave_tx_at() != new_wid: # returns the id of the waveform currently being transmitted
pass
#It is then safe to delete the old wave
pi.wave_delete(old_wid)
else:
#transmits the waveform with id wave_id, repeats until wave_tx_stop is called
#or another call to wave_send_* is made
pi.wave_send_repeat(new_wid)
old_wid = new_wid
pi = pigpio.pi()
if not pi.connected:
exit(0)
## calls a user supplied function (a callback) whenever the specified GPIO edge is detected
#def cbf(gpio, level, tick):
# print (gpio, level, tick)
#cb1 = pi.callback(13, pigpio.EITHER_EDGE, cbf)
#data1= cb1.tally()
#print (data2)
# set wave GPIO to output mode
for g in GPIO:
pi.set_mode(g, pigpio.OUTPUT)
usleep(delay_array1[0])
set_dc(0,_micros/2)
usleep(delay_array1[1])
set_dc(1,_micros/2)
usleep(delay_array1[2])
set_dc(2,_micros/2)
usleep(delay_array1[3])
set_dc(3,_micros/2)
time.sleep(60)
pulses=pi.wave_get_pulses()
print(pulses)
pi.wave_tx_stop()
if old_wid is not None:
pi.wave_delete(old_wid)
pi.wave_clear()
cb1.reset_tally()
cb1.cancel()
pi.stop()