我与树莓派的通信

import RPi.GPIO as GPIO
import time
import sys

GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.OUT)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(4,GPIO.OUT)
pwm=GPIO.PWM(2,80)
pwm.start(20)
GPIO.output(3,True)
GPIO.output(4,False)
file=open("/sys/class/thermal/thermal_zone0/temp")
Real_T=float(file.read())/1000
file.close()
Ideal_T=sys.argv[2]
Delta=[0.6]
Kp=sys.argv[1]
Ti=10000
Td=0
Output_PWM=20
# times=0
def All_of_List(a):
    s=0
    for i in range(len(a)):
        s=s+a[i]
    return s

def Real_Temp():
    file=open("/sys/class/thermal/thermal_zone0/temp")
    global Real_T
    Real_T=float(file.read())/1000
    file.close()

def Init(): 
    Real_Temp()
    global Ideal_T
    Ideal_T=33
    global Delta
    Delta=[0]
    global Kp
    Kp=8
    global Ti
    Ti=1
    global Output_PWM
    Output_PWM=20

def update_Output(Time_Gap):
    global Ideal_T
    global Delta
    global Kp
    global Ti
    global Td
    global Output_PWM
    global Real_T
    P=Kp
    I=Time_Gap/Ti
    D=Td/Time_Gap
    Increase=0
    Real_Temp()
    Delta.append(Real_T-Ideal_T)
    # if Delta[-1]>-0.5 and Delta[-1]<0.5:
    Increase=P*(Delta[-1] + I * All_of_List(Delta) + D * (Delta[-1]-Delta[-2]))
    Output_PWM=Output_PWM+Increase
    if Output_PWM>100:
        Output_PWM=100
    elif Output_PWM<20:
        Output_PWM=20

Usage_time=[]
# times=0

def Find_Kp():
    times=0
    while True:
        times=times+1
        # if(times>100):
        # times=0
        # Init()
        Time_Gap=1
        time.sleep(Time_Gap)
        if Delta[-1]<0.2 and Delta[-1]>-0.2 and times>100:
            Usage_time.append(times)
            print times
            break
        update_Output(Time_Gap)
        global Output_PWM
        pwm.ChangeDutyCycle(Output_PWM)
        print "%d:%0.3f "%(times,Real_T)
    pwm.ChangeDutyCycle(5)
    delay=0
    while Real_T<32:
        Real_Temp()
        time.sleep(1)
        delay=delay+1
        print delay
        print Real_T 
    # pwm.ChangeDutyCycle(0)

Find_Kp()
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.OUT)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(4,GPIO.OUT)
pwm=GPIO.PWM(2,80)
pwm.start(20)
GPIO.output(3,True)
GPIO.output(4,False)
file=open("/sys/class/thermal/thermal_zone0/temp")
Real_T=float(file.read())/1000
file.close()
Ideal_T=26
Delta=[0.6]
Kp=8
Ti=10000
Td=0
Output_PWM=20
# times=0

def All_of_List(a[]):
    s=0
    for i in range(len(a)):
        s=s+a[i]
    return s

def Real_Temp():
    file=open("/sys/class/thermal/thermal_zone0/temp")
    global Real_T
    Real_T=float(file.read())/1000
    file.close()

def Init():   
    Real_Temp()
    global Ideal_T
    Ideal_T=33
    global Delta
    Delta=[0]
    global Kp
    Kp=8
    global Ti
    Ti=1
    global Output_PWM
    Output_PWM=20

def update_Output(Time_Gap):
    global Ideal_T
    global Delta
    global Kp
    global Ti
    global Td
    global Output_PWM
    global Real_T
    P=Kp
    I=Time_Gap/Ti
    D=Td/Time_Gap
    Increase=0
    Real_Temp()
    Delta.append(Real_T-Ideal_T)
    # if Delta[-1]>-0.5 and  Delta[-1]<0.5:
    Increase=P*(Delta[-1] + I * All_of_List(Delta) + D * (Delta[-1]-Delta[-2]))
    Output_PWM=Output_PWM+Increase
    if Output_PWM>100:
        Output_PWM=100
    elif Output_PWM<20:
        Output_PWM=20


Usage_time=[]
# times=0

def Find_Kp():
    global Ideal_T
    global Usage_time
    global Delta
    global Kp
    global Ti
    global Td
    global Output_PWM
    global Real_T
    times=0
    while True:
        times=times+1
        # if(times>100):
        #     times=0
        #     Init()
        Time_Gap=1
        time.sleep(Time_Gap)
        if(Delta[-1]<0.2 and Delta[-1]>-0.2)
            Usage_time.append(times)
            break
        update_Output(Time_Gap)
        global Output_PWM
        pwm.ChangeDutyCycle(Output_PWM)
        print "%d:%0.3f "%(times,Real_T)
    time.sleep(30)


x=10
while x>0:
    Find_Kp()
    x=x-1
for i in range(len(Usage_time)):
    print Usage_time[i]

=========

我与树莓派的通信_第1张图片
import RPi.GPIO as GPIO  
import time  
   
durt=[1,1,1,1,1,1,1,1,1,1,1,1,1+0.5,0.5,1+1,1,1,1,1,1,1,1,1,1,1,1,1,1+0.5,0.5,1+1,1,1,1,1,1,0.5,0.5,1,1,1,0.5,0.5,1,1,1,1,1,1,1,1,1,1,1,1,1,0.5,0.5,1,1,1,1,1+0.5,0.5,1+1]
tune=[330,330,393,441,
  441,393,330,330,
  294,294,330,330,
  330,330,330,
  330,330,393,441,
  441,393,330,330,
  294,294,330,330,
  330,294,294,
  330,330,330,294,
  330,330,393,330,294,
  330,330,393,330,330,
  294,330,221,-1,
  330,330,393,441,
  441,393,330,393,330,
  294,294,330,330,
  330,294,294]

PIN_NO = 7   
   
GPIO.setmode(GPIO.BOARD)  
GPIO.setup(PIN_NO, GPIO.OUT)  
 
def beep(seconds):  
        GPIO.output(PIN_NO, GPIO.HIGH)  
        time.sleep(seconds)  
        GPIO.output(PIN_NO, GPIO.LOW)  
   
while True:
        for i in range(len(tune)):  
            beep(tune[i]/1000000)  
            time.sleep(durt[i]/2000)  
### dianji ###
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.OUT)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(4,GPIO.OUT)

pwm=GPIO.PWM(2,80)

pwm.start(20)

GPIO.output(3,True)
GPIO.output(4,False)
s=4
while True:
pwm.ChangeDutyCycle(s*5)
s=s+1
if s==21:
s=4
time.sleep(1)
#   pwm.ChangeDutyCycle(20)
time.sleep(2)
####  PID.py####
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.OUT)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(4,GPIO.OUT)
pwm=GPIO.PWM(2,80)
pwm.start(20)
GPIO.output(3,True)
GPIO.output(4,False)
file=open("/sys/class/thermal/thermal_zone0/temp")
Real_T=float(file.read())/1000
file.close()
Ideal_T=24
Delta=[0]
Kp=8
Ti=10000
Td=0
Output_PWM=20
# times=0

def All_of_List(a[]):
    s=0
    for i in range(len(a)):
        s=s+a[i]
    return s

def Real_Temp():
    file=open("/sys/class/thermal/thermal_zone0/temp")
    global Real_T
    Real_T=float(file.read())/1000
    file.close()

def Init():   
    Real_Temp()
    global Ideal_T
    Ideal_T=33
    global Delta
    Delta=[0]
    global Kp
    Kp=8
    global Ti
    Ti=1
    global Output_PWM
    Output_PWM=20

def update_Output(Time_Gap):
    global Ideal_T
    global Delta
    global Kp
    global Ti
    global Td
    global Output_PWM
    global Real_T
    P=Kp
    I=Time_Gap/Ti
    D=Td/Time_Gap
    Increase=0
    Real_Temp()
    Delta.append(Real_T-Ideal_T)
    # if Delta[-1]>-0.5 and  Delta[-1]<0.5:
    Increase=P*(Delta[-1] + I * All_of_List(Delta) + D * (Delta[-1]-Delta[-2]))
    Output_PWM=Output_PWM+Increase
    if Output_PWM>100:
        Output_PWM=100
    elif Output_PWM<20:
        Output_PWM=20



times=0
while True:
    times=times+1
    if(times>100):
        times=0
        Init()
    Time_Gap=1
    time.sleep(Time_Gap)
    if(Delta[-1]<0.5)
        break
    update_Output(Time_Gap)
    global Output_PWM
    pwm.ChangeDutyCycle(Output_PWM)

你可能感兴趣的:(我与树莓派的通信)