【问题标题】:How to fix an always HIGH Input如何修复始终为高的输入
【发布时间】:2019-06-02 17:37:27
【问题描述】:

我正在尝试让我的 ESP-32 向 Raspberry pi 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

【问题讨论】:

  • 您的代码对我来说似乎很好,但是连接怎么样,您是否连接了 Pi 和 ESP 的 GND?另外,我建议你用电压表测量RaspiPin,看看它是否有变化。
  • 我的 Raspberry 的 GND 由转换器(将电池电压转换为 5 V)和 ESP 的 GND 连接。他们不使用相同的电缆,而是使用相同的端口。
  • 当我打开终端我会看到这个错误信息(我编辑我的帖子)
  • 这个错误是你的问题,因为你的代码永远不会到达第 12 行。看来你的 cam 很忙,你是在运行你的脚本两次吗?或者也许另一个进程正在使用 cam.试试fuser /dev/video0 看看结果。
  • 我不认为我运行脚本两次。我只使用 bash 他自动打开脚本

标签: python arduino raspberry-pi esp32


【解决方案1】:

你的逻辑有一个小错误:

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

相反,您应该检查引脚 11 上是否有任何输入,例如

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

根据是否有任何输入(电流)通过 PIN 11 返回 TrueFalse

GPIO.HIGH 应该将引脚设置为高电平,而不是检查引脚是否正在输入。

【讨论】:

  • GPIO.HIGH, True, 1 是相等的。
  • if(GPIO.input(11) == GPIO.HIGH):python 代码的一部分,但您的建议似乎是用另一种语言编写的……
  • 我可能尝试使用我的 java 语言。但是我编辑了我的帖子,因为我在打开终端时看到了一条错误消息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
  • 2012-11-23
  • 2020-08-08
相关资源
最近更新 更多