如何固定始终为高的输入

时间:2019-06-02 17:37:27

标签: python arduino raspberry-pi esp32

我正在尝试我的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

1 个答案:

答案 0 :(得分:1)

您的逻辑中有一个小错误:

13:45

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

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

根据是否有任何输入(当前)通过PIN 11,哪个将返回if(GPIO.input(11)){ // the rest f you logic } True

False应该将引脚设置为HIGH,而不是检查引脚是否正在输入。