- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 AlphaBot2 的树莓派 Zero,它具有 HC-SR04 超声波传感器。使用Python 的实现效果很好。我想用 C 语言实现,因为我需要将它与另一个同样用 C 语言编写的程序绑定(bind),并且出于优化原因。我注意到的第一件事是 Python 代码使用 RPi.GPIO 库,而在 C 中我必须使用 WiringPi 或 bcm2835。所以我决定使用 WiringPi 库。我的程序执行但距离不正确。与我在网上找到的实现不同的一件事是,我使用的是 TRIG 22 和 ECHO 27,因为我的 HC-SR04 连接在 AlphaBot2 上。我没有使用 2 个电阻将其连接到树莓派。当我在传感器前面放置一些障碍物时,即使我将其移动到 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;
}
最佳答案
我找到了使用 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;
}
关于HC-SR04 超声波传感器的 C 程序未显示正确的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52206882/
A是不同元素的序列,B是A的子序列,A-B是A中的所有元素,但不是B中的所有元素距离(A) = 总和|a(i)-a(i+1)|从 i=1 到 n-1找到一个子序列 B 使得 Dist(B)+Dist(
我想通过计算每对中所有(多维)点集之间距离的平均值来量化组相似性。 我可以很容易地手动为每对组手动完成此操作,如下所示: library(dplyr) library(tibble) library(
在 OpenXML 中用于指定大小或 X、Y 坐标的度量单位是什么? (介绍)。 将那些与像素匹配是否有意义,如果是这样,那些如何转换为像素? graphicFrame.Transform = new
我想知道是否有人可以帮助我替换过渡层中的值。 如果我尝试: transitionlayer[transitionlayer >= 0.14] = 0.14 : comparison (5) is
我在 firebase 中有一个列表,其中包括地理位置(经度和纬度),并且我想获得距给定坐标最近的 10 个位置。 我正在从 MySQL 过渡,在那里我将计算 SELECT 中的距离, 并在 ORDE
如何在 Python 中根据 2 个 GPS 坐标计算速度、距离和方向(度)?每个点都有纬度、经度和时间。 我在这篇文章中找到了半正矢距离计算: Calculate distance between
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我只想使用 matplotlib 标记两条曲线之间发生最大偏差的位置。请帮助我。 垂直距离适用于 Kolmogorov–Smirnov test import numpy as np %matplot
我有一个包含数万行重复项的文件。我想根据行号找到重复项之间的平均时间/距离。 例如:(其中第一列是行号) 1 string1 2 string2 3 string2 4 string1 5 strin
用公式speed=distance/time计算时间 但时间总是0我的输入是 distance=10 和 speed=5 我的输出必须 = 2 #include int main() { in
我正在使用 Levenshtein 算法来查找两个字符串之间的相似性。这是我正在制作的程序的一个非常重要的部分,因此它需要有效。问题是该算法没有发现以下示例相似: CONAIR AIRCON 算法给出
对于一个房地产网站,我需要实现一个允许搜索文本和距离的搜索机制。 当 lat 和 lon 记录在单独的列中时,在 MySQL 表上进行距离计算很容易,但房子往往有 LOT true/false 属性。
是否可以在触发前更改 UIPanGestureRecognizer 的距离?目前的实现似乎在触发前有 5-10 像素的距离余量,我想降低它如果可能的话。 原因是我将 UIPanGestureRecog
我试图找到两个网格之间的偏差。例如在 3d 空间中定义的两组点之间的差异,我计划使用一些 3d 可视化工具来可视化距离,例如QT3d 或一些基于开放式 gl 的库。 我有两组网格,基本上是两个 .ST
所以,我有这个函数可以快速返回两个字符串之间的 Levenshtein 距离: Function Levenshtein(ByVal string1 As String, ByVal string2
我正在尝试用字典创建一个光学字符识别系统。 事实上,我还没有实现字典=) 我听说有一些基于 Levenstein 距离的简单指标,这些指标考虑了不同符号之间的不同距离。例如。 'N' 和 'H' 彼此
我在PostGIS数据库(-4326)中使用经纬度/经度SRID。我想以一种有效的方式找到最接近给定点的点。我试图做一个 ORDER BY ST_Distance(point, ST_GeomF
我想从线串的一端开始提取沿线串已知距离处的点的坐标。 例如: library(sf) path % group_by(L1) %>% summarise(do_union =
我已经编写了这些用于聚类基于序列的数据的函数: library(TraMineR) library(cluster) clustering <- function(data){ data <- s
是否可以设置 UILabel 的行之间的距离,因为我有一个 UILabel 包含 3 行,并且换行模式是自动换行? 最佳答案 如果您指的是“前导”,它指的是类型行之间的间隙 - 您无法在 UILabe
我是一名优秀的程序员,十分优秀!