python - 如何让树莓派通过USB连接响应Arduino代码

标签 python linux arduino raspberry-pi

我试过通过 USB 线将 Arduino 连接到树莓派。 Arduino 板连接到超声波传感器,并根据它是否在一定距离内发现障碍物(非常简单的代码)发送 0 或 1 的串行消息。问题是这样的:我试图让 Raspberry Pi 读取 Arduino 代码并同时播放 mp3 文件,但由于某些原因似乎不起作用!我不确定问题是否出在编码上,或者 Pi 可能无法响应从 Arduino 发送到串行监视器的消息(如果是这种情况,那将是非常可悲的)。任何帮助将不胜感激

这是 Arduino 代码(我使用的是 UNO 开发板):

    /*
HC-SR04 Ping distance sensor:

VCC to Arduino

Vin GND to Arduino GND

Echo to Arduino pin 12

Trig to Arduino pin 11 */

#include <NewPing.h> //downloaded from the internet & unzipped in libraries folder in Arduino Directory

#define TRIGGER_PIN 11 // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor.

int maximumRange = 70; // Maximum range needed

int minimumRange = 35; // Minimum range needed

long duration, distance; // Duration used to calculate distance

void setup() {

Serial.begin (9600);

pinMode(TRIGGER_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);

}

void loop() {

/* The following trigPin/echoPin cycle is used to determine the distance of the nearest object through reflecting soundwaves off of it */

digitalWrite(TRIGGER_PIN, LOW);

delayMicroseconds(2);

digitalWrite(TRIGGER_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(TRIGGER_PIN, LOW);

duration = pulseIn(ECHO_PIN, HIGH);

distance = (duration/2) / 29.1; //formula to convert the value measured by the ultrasonic sensor into centimeters

if (distance >= maximumRange || distance <= minimumRange)

{

Serial.println("0"); //means the path is clear

}

else {

Serial.println("1"); //means there is an obstacle in front of the ultrasonic sensor !

}

delay(50); //Delay 50ms before next reading.

}

这是我在 Pi 中使用的 python 代码(我有 Raspberry Pi 2): 注意:我已经评论了不起作用的部分,因为我尝试了下面显示的许多不同的代码组合

import serial
import RPi.GPIO as GPIO
import sys
import os
from subprocess import Popen
from subprocess import call
import time
import multiprocessing

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

arduinoSerialData = serial.Serial('/dev/ttyACM0', 9600)

while True:



time.sleep(0.01)
if(arduinoSerialData.inWaiting()>0):
myData = arduinoSerialData.readline()
print(myData)

if myData == '1': #THIS IS WHERE THE PROBLEMS START
#os.system('omxplayer sound.mp3') #tried this didn't work
#os.system('python player.py') #which is basically a python program with the previous line in it, also not working!
# I even tried enclosing that part (after if myData == '1') in a while loop and also didn't work !

最佳答案

首先,您的 IF 条件看起来不正确。我不明白 distance <= minimumRange表示路径畅通。

接下来,您正在向串行端口写入一行;一行可以是 0\r\n1\r\n .然后您正在阅读 Arduino 的一行,返回上述两种可能性之一。然后,您将阅读的行与 1 进行比较.都不是 0\r\n也不1\r\n等于1 ,所以条件永远不会为真也就不足为奇了。您可以通过多种方式解决此问题:

  • 更改 Serial.println()Serial.print()
  • 更改 arduinoSerialData.readline()arduinoSerialData.readline().rstrip()
  • 将条件更改为 if 1 in myData:

另一件事要记住,read()返回 bytes Python 3 中的对象不是 Python 2 中的字符串。因此,任何涉及文字的比较都应确保包含必要的 b''。信封。比如,如果您从读取数据中去除 CRLF,您的条件应该改为 if myData == b'1': .

关于python - 如何让树莓派通过USB连接响应Arduino代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40186390/

相关文章:

python - 如何在 Python 2.7 中复制类及其列表成员而不复制引用?

python - 与内置指纹传感器 Python 接口(interface)

python - 如何在 Python Logging 中设置源主机地址?

linux - 如何在 Linux 上卸载 perl

php - linux下执行php文件的读取权限

c++ - Arduino 库中的编译错误

c++ - 使用 C++ 从 Arduino 读取串行数据

javascript - Flask Python中未呈现不同的模板

linux - 在最后一个正斜杠后重命名目录的最后一部分

arduino - 在同一个 Arduino 脚本中使用 Serial.print 和 digitalWrite