突发奇想,D类音频功放是用两个H桥来驱动两线圈,电磁驱动原理跟步进电机驱动是一样的。那么能否用D类音频功放直接驱动步进电机呢?
只要在左右声道分别输出相位差是90度的正弦波应该就可以驱动电机了。于是说干就干,立马淘了TPA3116D2功放板来实验。
音频功放确实能够驱动步进电机。但经测试发现驱动特性比较一般,抖动挺大,信号频率到100Hz左右电机就转不动。
视频可移步B站:https://www.bilibili.com/video/BV1MW4y1Q7kP/
用于生成驱动信号的wav文件的python如下,感兴趣的同学也可以尝试下。
#!/usr/bin/env python
import numpy as np
import scipy.signal as signal
import wave
from array import array
freq0 = 20 # 信号起始频率(Hz)
freq1 = 20 # 信号终止频率(Hz)
seconds = 20 # 音频时长(秒)
sample_rate = 4800 # 采样率
t = np.arange(0, seconds, 1.0/sample_rate)
vleft = signal.chirp(t, f0=freq0, f1=freq1, t1=seconds, method='linear')
vright = signal.chirp(t, f0=freq0, f1=freq1, t1=seconds, method='linear', phi=90)
# import matplotlib.pyplot as plt
# plt.plot(t, vleft)
# plt.plot(t, vright)
# plt.show()
# exit()
left_scale = vleft * 32000
right_scale = vright * 32000
left_data = left_scale.astype(np.int16)
right_data = right_scale.astype(np.int16)
frame_data = array('h')
for i in range(len(left_data)):
frame_data.append(left_data[i])
frame_data.append(right_data[i])
fname = "{}~{}Hz-{}s.wav".format(freq0, freq1, seconds)
wf = wave.open(fname, "wb")
wf.setnchannels(2) # 2声道
wf.setsampwidth(2) # 采样宽(字节)
wf.setframerate(sample_rate)
wf.setcomptype('NONE','not compressed') # 设置采样格式:无压缩
wf.writeframes(frame_data.tobytes())
wf.close()
print("generated: {}".format(fname))
最近编辑记录 海石生风 (2023-06-16 12:04:50)
离线
优化双声道数据的合并
#!/usr/bin/env python
import numpy as np
import scipy.signal as signal
import wave
freq0 = 10 # 信号起始频率(Hz)
freq1 = 50 # 信号终止频率(Hz)
seconds = 20 # 音频时长(秒)
sample_rate = 4800 # 采样率
t = np.arange(0, seconds, 1.0/sample_rate)
vleft = signal.chirp(t, f0=freq0, f1=freq1, t1=seconds, method='linear')
vright = signal.chirp(t, f0=freq0, f1=freq1, t1=seconds, method='linear', phi=90)
# import matplotlib.pyplot as plt
# plt.plot(t, vleft)
# plt.plot(t, vright)
# plt.show()
# exit()
left_scale = vleft * 32000
right_scale = vright * 32000
left_data = left_scale.astype(np.int16)
right_data = right_scale.astype(np.int16)
cat_data = np.array([left_data, right_data])
frame_data = np.transpose(cat_data)
fname = "{}~{}Hz-{}s.wav".format(freq0, freq1, seconds)
wf = wave.open(fname, "wb")
wf.setnchannels(2) # 2声道
wf.setsampwidth(2) # 采样宽(字节)
wf.setframerate(sample_rate)
wf.setcomptype('NONE','not compressed') # 设置采样格式:无压缩
wf.writeframes(frame_data.tobytes())
wf.close()
print("generated: {}".format(fname))
最近编辑记录 海石生风 (2023-06-16 13:32:57)
离线
不应该啊,正常应该可以驱动的吧?拿示波器看看波形
离线
音频功放驱动实现步进电机的加减速及正反转控制,视频请移步B站:https://www.bilibili.com/video/BV1nj411D754/
生成wav波形文件的python脚本如下:
#!/usr/bin/env python
import numpy as np
import scipy.signal as signal
import wave
freq_min = 10
freq_max = 50
shifting_seconds = 0.5 # 加速或减速时长
con_seconds = 1.5 # 匀速时长
SAMPLE_RATE = 48000 # 采样率
def genwave(f0, f1, seconds, phase_diff=90):
t = np.arange(0, seconds, 1.0/SAMPLE_RATE)
vleft = signal.chirp(t, f0=f0, f1=f1, t1=seconds, method='linear')
vright = signal.chirp(t, f0=f0, f1=f1, t1=seconds, method='linear', phi=phase_diff)
left_scale = vleft * 32000
right_scale = vright * 32000
left_data = left_scale.astype(np.int16)
right_data = right_scale.astype(np.int16)
cat_data = np.array([left_data, right_data])
frame_data = np.transpose(cat_data)
return frame_data
def genaction(f_min, f_max, shifting_t, con_t, is_forward=True):
phase = 90 if is_forward else -90
acc = genwave(f_min, f_max, shifting_t, phase)
con = genwave(f_max, f_max, con_t, phase)
dec = genwave(f_max, f_min, shifting_t, phase)
action = np.concatenate((acc, con, dec))
return action
def genround(f_min, f_max, shifting_t, con_t):
forward = genaction(f_min, f_max, shifting_t, con_t, True)
backward = genaction(f_min, f_max, shifting_t, con_t, False)
return np.concatenate((forward, backward))
# frames = genaction(freq_min, freq_max, shifting_seconds, con_seconds, False)
frames = genround(freq_min, freq_max, shifting_seconds, con_seconds)
seconds = shifting_seconds*2 + con_seconds
fname = "{}~{}Hz-{}s.wav".format(freq_min, freq_max, seconds)
wf = wave.open(fname, "wb")
wf.setnchannels(2) # 2声道
wf.setsampwidth(2) # 采样宽(字节)
wf.setframerate(SAMPLE_RATE)
wf.setcomptype('NONE','not compressed') # 设置采样格式:无压缩
wf.writeframes(frames.tobytes())
wf.close()
print("generated: {}".format(fname))
离线