带python的Raspberry pi编码器未绘制显示RPM的图形-打开
我已经试过很多次了,但都没能弄明白这里面可能有什么问题 我已经附加了raspberry pi 4和霍尔效应编码器,编码器仅使用X1解码,PPR为160,还包括动态模型,但无法正确执行它在开环上运行。我还使用低通滤波器对信号进行滤波,它与PID一起工作良好,但无法在简单的开环系统上得到结果带python的Raspberry pi编码器未绘制显示RPM的图形-打开,python,matplotlib,raspberry-pi,Python,Matplotlib,Raspberry Pi,我已经试过很多次了,但都没能弄明白这里面可能有什么问题 我已经附加了raspberry pi 4和霍尔效应编码器,编码器仅使用X1解码,PPR为160,还包括动态模型,但无法正确执行它在开环上运行。我还使用低通滤波器对信号进行滤波,它与PID一起工作良好,但无法在简单的开环系统上得到结果 import RPi.GPIO as GPIO import numpy as np import time import matplotlib.pyplot as plt
import RPi.GPIO as GPIO
import numpy as np
import time
import matplotlib.pyplot as plt
from time import sleep
import random
import sensormotion as sm
from motor_model import motor
from scipy.integrate import odeint
#Defining GPIO PINS
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Sensor Input Aout
GPIO.setup(18, GPIO.OUT) #PWM PIN
GPIO.setup(27, GPIO.OUT) #Left Direction
GPIO.setup(22, GPIO.OUT) #Right Direction
#Initial Setup Setup
simulationTime = 10 #Total Simulation time for the Motor
samplingTime = 0.01 #Sampling time for each tick count
setPoint = 500 #Desire RPM
count = 0 #counting the ticks of the motor
refperiod = 5 #PWM period time taken by PWM to complete one cycle.
pwmResolution = 4096 # 4096 steps Because of raspberry pi 4 has 12bit resolution has factor 0.00024
simulationTime = int(1000 / 24 * simulationTime) #converting Raspberry pi from resolution factor 0.00024
refperiod = int(1000 / 24 * refperiod)
encoderCount = 55 #assuming Pulse Per Revolution here
scale = 1 / samplingTime #frequency of the tick count
pwmOut = GPIO.PWM(18, 1 / samplingTime) # GPIO.PWM ([pin],frequency]) here 100Hz when simulation time is 0.01
try:
while True:
##########Couting the encoder up pulses
def counterup(channel):
global count
if GPIO.input(channel) == 1:
count += 1
else:
count += 0
print(count)
#Enocder ticks count to RPM conversion
def speedConvert():
speedDC = count * 60 * scale / encoderCount
print(speedDC)
return speedDC
#### MotorDirection Setting
def motorDirection(direction):
# Counter Clockwise Direction
if direction == 'ccw':
GPIO.output(27, 1)
GPIO.output(22, 0)
# Clockwise Direction
elif direction == 'cw':
GPIO.output(27, 0)
GPIO.output(22, 1)
else: # Stop the Motor
GPIO.output(27, 0)
GPIO.output(22, 0)
motorDirection ('cw')
#intializing arrays for saving values
y = np.array([])
t = np.linspace(0,20)
for x in range (0,20):
count = 0
GPIO.add_event_detect(17, GPIO.RISING, callback = counterup)
sensorRead = speedConvert()
pwmOut.start(50)
y = np.append(y,sensorRead)
sleep(samplingTime) #Updating Error after each sampling time that is 0.01 or 1 ms
#Intialzing count here for the update
#If all the above logic fails motor will not run
else :
pwmOut.start(0)
####################Singal Filtering#############################
# #-------------------------LOW PASS FILTER
# # t = np.multiply(t, 24 / 1000)
# sampling_rate = 1000
# #Building a butterworth filter with specified parameters.
# # Frequency, sampling rate, filter type, filter order
# b, a = sm.signal.build_filter(5.7, sampling_rate, 'low', filter_order=1)
# y_f = sm.signal.filter_signal(b, a, y) # Filtering Encoder Data
#
##################Dc Motor Model Plot# #################################################
# Parameter for Dc motor
# J = 0.01 # [kg.m^2] Moment of inertia of motor
# b = 0.1 # [N.m.s] Damping ratio of mechanical system
# K = 0.01 # constant Ke= Kt = K
# R = 1 # [ohm] Electrical resistance
# L = 0.5 # [H] Electrical inductance
#
# y0 = [ 0,0,0]
# ym= odeint(motor, y0, t )
######################Comparative results of Low pass and Orignal Signal#################
print(y)
plt.figure(figsize=(6, 5))
# plt.plot(t, ym[:, 2], color='b', linewidth = 2,label = 'Simulated Result')
plt.plot(t, y, 'k' , linewidth = 0.3 ,label='Encoder Output')
# plt.plot(t, y_f, color='g', linewidth = 2,label = 'Low Pass Filter')
plt.title("Comparative Results of Reference Signal, Simulated Output and Encoder Output with Low Pass Filter")
plt.xlabel('Time [s]')
plt.ylabel('Speed (RPM [rad/s]')
plt.legend(loc='upper right')
plt.grid()
plt.show()
#after Completting the loop
motorDirection('stop')
except KeyboardInterrupt:
print("program interrupted")
finally:
GPIO.cleanup()