|
本帖最后由 MACE 于 2017-12-5 15:34 编辑
买了两个sharp红外测距传感器GP2Y0A710K0F(100-550cm)和gp2y0a02yk_e(20-150cm),买回来看到论坛中的帖子,距离的转换曲线有点复杂,就扔边上了。前天没事干就晚上搜了搜,居然找到了一个库函数,支持4种夏普的红外测距传感器。作者介绍说,他说肯据官方文档中的曲线做的拟合,精度我也不会测,接上线下载程序,觉得还是不错的,对于检测人的有无,及距离还是够用的。库中采样次数25次,中值滤波取中间值。传感器的电源和地之间串接440uF电容,用来稳定电源电压。
支持4种sharpIR传感器的读取,在示例程序中做相应的修改:
GP2Y0A02YK0F --> "20150"
GP2Y0A21YK --> "1080"
GP2Y0A710K0F --> "100500"
GP2YA41SK0F --> "430"
[mw_shl_code=bash,true]//SHARP IR 红外测距模块测试GP2Y0A710K0F(100-550cm)
#include <SharpIR.h>
#define ir A0 //模拟输入引脚
#define model 100500 //传感器型号
// ir: the pin where your sensor is attached
// model: an int that determines your sensor: 1080 for GP2Y0A21Y
// 20150 for GP2Y0A02Y
// 100500 for GP2Y0A710K0F
// 430 for GP2YA41SK0F
// (working distance range according to the datasheets)
SharpIR SharpIR(ir, model);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
delay(500);
unsigned long pepe1=millis(); // takes the time before the loop on the library begins
int dis=SharpIR.distance(); // this returns the distance to the object you're measuring
Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);
unsigned long pepe2=millis()-pepe1; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(pepe2);
}[/mw_shl_code]
库的官方下载地址:https://github.com/guillaume-rico/SharpIR
|
|