Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HC-SR04超声波传感器的C程序未显示正确的距离_C_Raspberry Pi_Arduino Ultra Sonic - Fatal编程技术网

HC-SR04超声波传感器的C程序未显示正确的距离

HC-SR04超声波传感器的C程序未显示正确的距离,c,raspberry-pi,arduino-ultra-sonic,C,Raspberry Pi,Arduino Ultra Sonic,我有一个树莓皮零与AlphaBot2,其中有HC-SR04超声波传感器。使用Python的实现工作得很好。我想在C中实现,因为我需要将它与另一个同样在C中的程序绑定,并且出于优化的原因。我注意到的第一件事是Python代码使用RPi.GPIO库,在C语言中我必须使用wiringPi或bcm2835。所以我决定使用wiringPi库。我的程序执行,但距离不正确。与我在web上发现的实现不同的一点是,我使用的是TRIG 22和ECHO 27,因为我的HC-SR04连接在AlphaBot2上。我没有使

我有一个树莓皮零与AlphaBot2,其中有HC-SR04超声波传感器。使用Python的实现工作得很好。我想在C中实现,因为我需要将它与另一个同样在C中的程序绑定,并且出于优化的原因。我注意到的第一件事是Python代码使用RPi.GPIO库,在C语言中我必须使用wiringPi或bcm2835。所以我决定使用wiringPi库。我的程序执行,但距离不正确。与我在web上发现的实现不同的一点是,我使用的是TRIG 22和ECHO 27,因为我的HC-SR04连接在AlphaBot2上。我没有使用2个电阻器将其连接到raspberry Pi。当我在传感器前面放置一些屏障时,即使我将其移动到30厘米,也只能得到3到5厘米

Distance: 3905724cm
    Distance: 5cm
    Distance: 5cm
    Distance: 5cm
    Distance: 5cm
    Distance: 3cm
    Distance: 3cm
    Distance: 5cm
    Distance: 5cm
这是我的密码:

#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>

#include "ultrasonicClient.h"

#define TRUE (1==1)

// HC-SR04 ultrasonic sensor on AlphaBot2 Pi Zero
#define TRIG 22
#define ECHO 27

static volatile long startTimeUsec;
static volatile long endTimeUsec;
double speedOfSoundMetersPerSecond = 340.29;

void recordPulseLength() {
    startTimeUsec = micros();
    while (digitalRead(ECHO) == HIGH);
    endTimeUsec = micros();
}

void setupUltrasonic() {
    wiringPiSetup();
    pinMode(TRIG, OUTPUT);
    pinMode(ECHO, INPUT);

    // TRIG pin must start LOW
    // Initialize the sensor's trigger pin to low. If we don't pause
    // after setting it to low, sometimes the sensor doesn't work right.
    digitalWrite(TRIG, LOW);
    delay(500); // .5 seconds
}

int getCM() {
    // Send trig pulse
    // Triggering the sensor for 10 microseconds will cause it to send out
    // 8 ultrasonic (40Khz) bursts and listen for the echos.
    digitalWrite(TRIG, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG, LOW);

    int now = micros();
    // Wait for echo start
    // The sensor will raise the echo pin high for the length of time that it took
    // the ultrasonic bursts to travel round trip.
    while (digitalRead(ECHO) == LOW && micros() - now < 30000);
    recordPulseLength();

    long travelTimeUsec = endTimeUsec - startTimeUsec;
    double distanceMeters = 100 * ((travelTimeUsec / 1000000.0) * 340.29) / 2;

    //Wait for echo end
    long startTime = micros();
    while (digitalRead(ECHO) == HIGH);
    long travelTime = micros() - startTime;

    //Get distance in cm
    int distance = travelTime * 34000 / 2;

    return distanceMeters * 100;
}

int runUltrasonicClient() {
    int count = 0;
    setupUltrasonic();

    while (count < 60) {
        printf("Distance: %dcm\n", getCM());
        count++;
        delay(500); // 0.5 second
    }
    return 0;
}
#包括
#包括
#包括
#包括“ultrasonicClient.h”
#定义真(1==1)
//HC-SR04超声波传感器位于AlphaBot2 Pi零点
#定义TRIG 22
#定义回声27
静态挥发性长startTimeUsec;
静态挥发性长余辉;
声速倍增每秒=340.29;
void recordPulseLength(){
startTimeUsec=micros();
而(数字读取(回波)=高);
endTimeUsec=micros();
}
空隙率(){
wiringPiSetup();
pinMode(触发、输出);
pinMode(回波,输入);
//触发销必须低启动
//将传感器的触发引脚初始化为低。如果我们不暂停
//将其设置为低后,有时传感器工作不正常。
数字写入(触发器,低电平);
延迟(500);/.5秒
}
int getCM(){
//发送触发脉冲
//触发传感器10微秒将使其发出
//8个超声波(40Khz)脉冲并收听回声。
数字写入(TRIG,高);
延迟微秒(10);
数字写入(触发器,低电平);
int now=micros();
//等待回波开始
//传感器将在所需的时间内将回波引脚升高到较高的位置
//超声波脉冲往返传播。
而(数字读取(回波)=低和微秒()-现在<30000);
recordPulseLength();
long travelTimeUsec=endTimeUsec-startTimeUsec;
双测距仪=100*((travelTimeUsec/1000000.0)*340.29)/2;
//等待回音结束
长起始时间=微秒();
而(数字读取(回波)=高);
长行程时间=微秒()-开始时间;
//获取以厘米为单位的距离
int距离=旅行时间*34000/2;
返回距离表*100;
}
int runUltrasonicClient(){
整数计数=0;
设置超声波();
而(计数<60){
printf(“距离:%dcm\n”,getCM());
计数++;
延迟(500);//0.5秒
}
返回0;
}

我找到了一个使用bcm2835执行的代码

static uint64_t cyclePulse(int trigger, int echo) {
    if (!bcm2835_init())
        return 1;

    // Set RPi pin echo to be an input pin
    bcm2835_gpio_fsel(echo, BCM2835_GPIO_FSEL_INPT);
    // Set RPi pin P1-11 to be an output pin
    bcm2835_gpio_fsel(trigger, BCM2835_GPIO_FSEL_OUTP);

    // Declare the unsigned int timer variables to measure pulses
    uint64_t width, begin, start, end;
    int max = 80, check;

    begin = bcm2835_st_read();

    // Emit pulse for 10 microseconds
    bcm2835_gpio_write(trigger, HIGH); // Set trigger state HIGH
    bcm2835_delayMicroseconds(10);  // Wait 10 microseconds
    bcm2835_gpio_write(trigger, LOW);  // Set trigger state LOW

    // Infinite loop until a pulse is received
    while (bcm2835_gpio_lev(echo) == LOW && check < max) {
        start = bcm2835_st_read();
        check = (int) begin - start;
    }

    // Loop and delay for one microsecond until falling edge detected
    while (bcm2835_gpio_lev(echo) == HIGH) {
        bcm2835_delayMicroseconds(1);
    }
    // Record the ending time of the pulse to get the pulse width
    end = bcm2835_st_read();

    // Get the final with of the pulse
    width = end - start;

    //Close the bcm2835 bridge
    bcm2835_close();

    // Return the total width of the returned pulse
    return width;
}
静态uint64\u t循环脉冲(int触发器、int回波){
如果(!bcm2835_init())
返回1;
//将RPi引脚echo设置为输入引脚
bcm2835_gpio_fsel(回波,bcm2835_gpio_fsel_输入);
//将RPi引脚P1-11设置为输出引脚
bcm2835_gpio_fsel(触发器,bcm2835_gpio_fsel_输出);
//声明无符号整数计时器变量以测量脉冲
uint64\t宽度、开始、开始、结束;
int max=80,检查;
begin=bcm2835_st_read();
//发射脉冲10微秒
bcm2835_gpio_写入(触发器,高);//将触发器状态设置为高
bcm2835_延迟微秒(10);//等待10微秒
bcm2835_gpio_写入(触发器,低);//将触发器状态设置为低
//无限循环直到接收到脉冲
而(bcm2835_gpio_lev(echo)=低和检查<最大值){
开始=bcm2835_st_read();
检查=(int)开始-开始;
}
//循环并延迟一微秒,直到检测到下降沿
而(bcm2835_gpio_lev(回波)=高){
bcm2835_延迟微秒(1);
}
//记录脉冲的结束时间以获得脉冲宽度
end=bcm2835_st_read();
//用脉搏来做最后的测试
宽度=结束-开始;
//关闭bcm2835桥
bcm2835_close();
//返回返回脉冲的总宽度
返回宽度;
}