如何修复总是高Inpu

2024-04-26 09:30:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试我的ESP-32发送一个高输出到树莓皮3 B+。我已经尝试使用不同的引脚,但结果总是一样的。他总是得到很高的投入。其目标是,覆盆子应该采取的照片时,esp 32发送高输出。 在您询问ESP如何知道何时应该发送输出之前。嗯,当他发现一个物体的时候。你知道吗

在这里您可以看到我的python代码

import time
import os                                                                                       # import the time
from picamera import PiCamera                                                                   # import image from the camera into the Raspberry
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)                                                                          # define pins

GPIO.setup(11, GPIO.IN)                                                                         # setup pin 18 as input

if __name__=='__main__':
        #Variable
        cam = Picamera()                                                                                                                                        # makes the variable cam
    while True:

        if(GPIO.input(11) == GPIO.HIGH):



            #Define how newimagen is made of (Variable)                                         # make the variable fname which give us the date and Time 
            os.chdir('/home/pi/Desktop/image/')                                                 # Define where we are working now 
            images = [i for i in os.listdir(os.getcwd()) if i.lower().startswith('image')]      # define what images is : 
                                                                                                # os.listdir returns a list containing of the entries in the directory os.getcwd...
                                                                                                # os.getcwd returns the current working directory of a process

            if images:
                newest = max(images, key=os.path.getmtime)                                      # max() is a method that returns the largest item of sth
                                                                                                # os.path.getmtime return the time of last modification
            else:
                newest = 'image0.jpg'


            number = int(''.join([i for i in newest if i.isdigit()]))                           #number is the number of the newest image : image1 --> number = 1. This method look up if there is an image and which number it has
            newimagen = 'image'+str(number+1)+'.jpg'                                            # newimagen is the variable that decide which image it will be(image1 or image2...)

            #Camera Code changing 
            cam.resolution = (2592,1944)                                                        # method to change the resoltuion

            #Main Code      
            cam.capture('/home/pi/Desktop/image/'+newimagen)                                    # method that take a photo and then save it on the local desktop as the name of newimagen

这是我的Arduino密码

// Define SensorS pins
#define trigPin 15
#define echoPin 2

//Define SensorXL pins
#define trigPinXL 14
#define echoPinXL 13

//Define Raspberry Pin
#define RaspiPin 26

//Define Motor pins
#define motorIn3 16 //Input 3
#define motorIn1 17 //Input 1
#define motorIn4 18 //Input 4
#define motorIn2 19 //Input 2

// Defines variables
long duration;
int distance;

// Define ActivateDistance
const int activateDistance = 40;
const int activateDistance2 =40;

void setup()
{
    // Sets the trigPin as an Output
    pinMode(trigPin, OUTPUT);
    pinMode(trigPinXL, OUTPUT);
    // Sets the echoPin as an Input
    pinMode(echoPin, INPUT);
    pinMode(echoPinXL, INPUT);
    // sets the Motorpins  as outputs:
    pinMode(motorIn1, OUTPUT);
    pinMode(motorIn2, OUTPUT);
    pinMode(motorIn3, OUTPUT);
    pinMode(motorIn4, OUTPUT);
    //Sets Raspberry Pin as output
    pinMode(RaspiPin, OUTPUT);
    // Starts the serial communication
    Serial.begin(9600); 
}

void stop()
{
    // stop motor without duration
    Serial.println("STOP");
    digitalWrite(motorIn1, LOW);
    digitalWrite(motorIn2, LOW);
    digitalWrite(motorIn3, LOW);
    digitalWrite(motorIn4, LOW);
}

void left(int duration)
{
    //Motor goes to left
    Serial.println("LEFT");
    digitalWrite(motorIn1, LOW);
    digitalWrite(motorIn2, HIGH);
    digitalWrite(motorIn3, HIGH);
    digitalWrite(motorIn4, LOW);
    delay(duration);
    stop();
}

void right(int duration)
{
    //Motor goes to left
    Serial.println("RIGHT");
    digitalWrite(motorIn1, HIGH);
    digitalWrite(motorIn2, LOW);
    digitalWrite(motorIn3, LOW);
    digitalWrite(motorIn4, HIGH);
    delay(duration);
    stop();
}

void forward(int duration)
{
    //Motor goes forward
    Serial.println("FORWARD");
    digitalWrite(motorIn2, HIGH);
    digitalWrite(motorIn4, HIGH);
    digitalWrite(motorIn3, LOW);
    digitalWrite(motorIn1, LOW);
    delay(duration);
    stop();
}

long get_distance(void)
{
    //get distance from sensor
    // Clears the trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance = duration * 0.034 / 2;
    return distance;
}

long get_distanceXL(void)
{
    //get distance from sensor
    // Clears the trigPin
    digitalWrite(trigPinXL, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPinXL, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPinXL, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPinXL, HIGH);
    // Calculating the distance
    distance = duration * 0.034 / 2;
    return distance;
}
int turn = 0;
void loop()
{
    // check sensor
    if (get_distance() <= activateDistance)
    {
        Serial.println("Found an Obstacle!!!");
        // go right for 1 second
        right(1000);

        while(turn<4)
        {
          //turn on the Raspberry Pin
          digitalWrite(RaspiPin, HIGH);
          if(get_distanceXL()>activateDistance2)
          {
            //go left for 1 second
            left(1000);
            forward(1500);
            turn = turn + 1;
          }
          else
            //go forward for 1 second
            forward(1000);
        }
        //turn off the Raspberry Pin
        digitalWrite(RaspiPin, LOW);
    }

    else
        // go forward for 1 second
        forward(1000);
}

我打开终端看到了这个问题。我不确定这是否有助于解决我的问题:

mmal: mmal_vc_port_enable: failed to enable port vc.null_sink:in:0(OPQV): ENOSPC
mmal: mmal_port_enable: failed to enable connected port (vc.null_sink:in:0(OPQV))0x1d13d20 (ENOSPC)
mmal: mmal_connection_enable: output port couldn't be enabled
Traceback (most recent call last):
  File "/home/pi/Desktop/camera.py", line 12, in <module>
    cam = PiCamera()                                                                        # makes the variable cam
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 433, in __init__
    self._init_preview()
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 513, in _init_preview
    self, self._camera.outputs[self.CAMERA_PREVIEW_PORT])
  File "/usr/lib/python2.7/dist-packages/picamera/renderers.py", line 558, in __init__
    self.renderer.inputs[0].connect(source).enable()
  File "/usr/lib/python2.7/dist-packages/picamera/mmalobj.py", line 2212, in enable
    prefix="Failed to enable connection")
  File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 184, in mmal_check
    raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources

Tags: theinimagegpioifosenablelow
1条回答
网友
1楼 · 发布于 2024-04-26 09:30:24

你的逻辑有一个小错误:

  if(GPIO.input(11) == GPIO.HIGH)

相反,您应该检查您的pin11上是否有任何输入,如

if(GPIO.input(11)){
// the rest f you logic
}

它将返回TrueFalse,基于是否有任何输入(电流)通过引脚11。你知道吗

GPIO.HIGH应该将一个pin设置为HIGH,而不是检查该pin是否得到输入

相关问题 更多 >