微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有python的Raspberry pi编码器未绘制显示RPM -OPEN的图形

如何解决具有python的Raspberry pi编码器未绘制显示RPM -OPEN的图形

我已经尝试了很多次,但无法获得可能是这个问题的答案 我已将带有霍尔效应编码器的raspBerry pi 4附加为编码器,仅使用X1解码器进行编码,PPR为160,并且还包含动态模型,但无法正确执行以在开环上运行。我还通过简单的低通滤波器对信号进行过滤,它可以与PID一起正常工作,但无法在简单的开环系统上获得结果。

   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)
            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]
    #         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',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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。