树莓派锁定

时间:2021-04-09 15:03:42

标签: python raspberry-pi4

我正在运行以下代码。它按计划工作,但大约 25 分钟后,树莓派完全冻结。该代码将在屏幕中间取一个像素,然后获取元组 RGB 值,然后转换为 PWM。然后它通过 GPIO 驱动一条 LED。工作时一切正常,但冻结。我将它用于 HDMI 输入:https://raspberrypiwiki.com/HDMI_TO_CSI-2

我实际上是在使用 teamviewer 远程访问 raspberry pi 来编写代码。我也在使用 Thonny 来编写和调试。

#from picamera import PiCamera
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from time import sleep
from PIL import Image
import picamera
import io
import time, sys
import RPi.GPIO as GPIO
import time 
#used for GPIO numbering
GPIO.setmode(GPIO.BCM) 

#closing the warnings when you are compiling the code
GPIO.setwarnings(False)

RUNNING = True

RED = 17   #Set to appropriate GPIO
GREEN = 22 #Should be set in the 
BLUE = 24  #GPIO.BOARD format
GPIO.cleanup()

#defining the pins as output
GPIO.setup(RED, GPIO.OUT) 
GPIO.setup(GREEN, GPIO.OUT)
GPIO.setup(BLUE, GPIO.OUT)

Freq = 100

#defining the pins that are going to be used with PWM
RED = GPIO.PWM(RED, Freq)
GREEN = GPIO.PWM(GREEN, Freq)
BLUE = GPIO.PWM(BLUE, Freq)
StartR = 1
StartB = 1
StartG = 1


def RGB_to_PWM(Color):
    pwm = (Color/255)*100
    return pwm 

def main():
    while True:
        try:
            global StartR, StartB, StartG
            #we are starting with the loop
            #while RUNNING:
            stream = io.BytesIO()
            with picamera.PiCamera() as camera:
                #camera.start_preview()
                #sleep(2)
                camera.capture(stream, format='jpeg')
                stream.seek(0)
            image = Image.open(stream)
            width, height = image.size
            CurrentRGB = image.convert("RGB")
    #CurrentRGB = image.save("/home/pi/Documents/Programs/Test001.jpg")
            imagex = image.size[0]
            imagey = image.size[1]
            imagex = imagex / 2
            imagey = imagey / 2 
           
            
            rgb_P_Value = image.getpixel((imagex, imagey))
            Rpwm = int(RGB_to_PWM(rgb_P_Value[0]))
            Gpwm = int(RGB_to_PWM(rgb_P_Value[1]))
            Bpwm = int(RGB_to_PWM(rgb_P_Value[2]))
           
#lighting up the pins. 100 means giving 100% to the pin
            RED.start(StartR)
            GREEN.start(StartG)
            BLUE.start(StartB)
            
            if StartR < Rpwm:
                for x in range(StartR,Rpwm,1):
                    RED.ChangeDutyCycle(x)
                    time.sleep(.005)
            else:
                for x in range(StartR,Rpwm-1,-1):
                    RED.ChangeDutyCycle(x)
                    time.sleep(.005)
            
            StartR = Rpwm
            if StartG < Gpwm:
                for x in range(StartG,Gpwm,1):
                    GREEN.ChangeDutyCycle(x)
                    time.sleep(.005)
            else:
                for x in range(StartG,Gpwm-1,-1):
                    GREEN.ChangeDutyCycle(x)
                    time.sleep(.005)
            StartG = Gpwm
            if StartB < Bpwm:
                for x in range(StartB,Bpwm,1):
                    BLUE.ChangeDutyCycle(x)
                    time.sleep(.005)
            else:
                for x in range(StartB,Bpwm-1,-1):
                    BLUE.ChangeDutyCycle(x)
                    time.sleep(.005)
            StartB = Bpwm
            print(StartR)
            print(StartG)
            print(StartB)
        #global StartR
        
        #StartG = Gpwm
        #StartB = Bpwm
           
        except KeyboardInterrupt:
            # the purpose of this part is, when you interrupt the code, it will stop the while loop and turn off the pins, which means your LED won't light anymore
            RUNNING = False
            GPIO.cleanup()
    return
    

main()
   

0 个答案:

没有答案