倍加福超声波传感器UB4000-F42-U-V15参数:
- General specifications
- Sensing range 200 ... 4000 mm
- Adjustment range 240 ... 4000 mm
- Dead band 0 ... 200 mm
- Standard target plate 100 mm x 100 mm
- Indicators/operating means
- LED green solid green: Power on
- LED yellow solid: object in evaluation range
- LED red normal operation: "fault"
- Electrical specifications
- Operating voltage 17 ... 30 V DC , ripple 10 %SS
- No-load supply current ≤ 60 mA
- Output
- Output type 1 analog output 4 ... 20 mA
- Default setting evaluation limit A1: 240 mm , evaluation limit A2: 4000 mm , wide sound lobe
- Resolution 0.7 mm
由上述参数可知,首先选用的是24的输入电压给传感器供电,而传感器的输出是0~10V的模拟量,由于beaglebone的ADC模块允许最大输入电压为1.8V,且返回的是一个12位数字量,即0~4095(2的12次方),所以为该超声波传感器设计一个降压电路.将该信号调节器的输出连接到8.2 KΩ和1 KΩ的分压电路。为了防止反向电流,我们可以在分压器和板子ADC接口之间提供一个二极管来保护电路。还可以使用电容并联减少噪声.
考虑到实验室电阻的类型和数据的方便运算将R1变成10KΩ,这样输入的电压范围就在0~1V之间,而不是之前的0~1.8V了。
根据实验获得超声波感知障碍物的距离和beaglebone控制器的ADC输出模拟量数值的关系为:
其中: Uadc为模拟量ADC的值,Udis为障碍物的距离(mm)
对于传感器的接线方式如下:
- 棕色+U---->电源正极(17-30V)
- 蓝色-U---->电源负极(GND)
- 白色Teach in----->N/C
- 灰色Sync----->N/C
- 黑色Analog Output
注意:
- 板子的GND是ADC的GND,对应引脚为P9-34
- ADC引脚用的是AIN0,对应引脚为P9-39
- Output经过10KΩ电阻最后还要接回电源负极形成回路
总体接线如图:
最后编写程序读取ADC值和转换的障碍物距离:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
//在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元
#include <time.h>
// 加载设备树
#define SLOTS "/sys/devices/bone_capemgr.9/slots"
#define Ultrasonic "/sys/bus/iio/devices/iio:device0/in_voltage0_raw"
// Ultrasonic variable
char ult_value[10];
int limit;
FILE *stream = NULL;
// clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t,头文件在linux下应该是time.h,而不是ctime
clock_t begin ,end;
int main(int argc, char **argv)
{
int fd;
int i = 0;
// 打开文件,只写模式
fd = open(SLOTS,O_WRONLY);
if(fd < 0)
{
printf("failed to open slots\n");
}
write(fd,"BB-ADC",6);
close(fd);
begin = clock();
// 100000次采样输出所用的时间
while (i<100000)
{
stream = fopen(Ultrasonic,"r+");
// 从一个流中执行格式化输入,fscanf遇到空格和换行时结束,注意空格时也结束。这与fgets有区别,fgets遇到空格不结束。
fscanf(stream,"%s",ult_value);
fclose(stream);
// atoi是把字符串转换成整型数的一个函数
limit = atoi(ult_value);
// 实验测得的障碍物距离和ADC值的关系
double Udis = (limit + 446.2) / 2.22 ;
printf("ult_value= %d ",limit);
printf("Udis_value= %f ",Udis);
i++;
}
end = clock();
unsigned Time = (end - begin)*1.0/CLOCKS_PER_SEC * 1000;
printf("time = %d,i = %d\n",Time,i);
return 0;
}