• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
✿ﻬஐcSﻬ✿Tranquilty✿ﻬஐcSﻬ✿
想念,思念 漂浮在整个网络里面
博客园    首页    新随笔    联系   管理    订阅  订阅

DIY PIXHAWK APM等飞控用的声纳

 

 

 

 

代码:

SR04 + ApmSonar.ino 打包下载

 

注意,使用到了SR04的类库。

ApmSonar.ino

// sr04 to apm I2c sonar
// by panxu mail: [email protected]
// Created 2014
#include <Wire.h>
#include "SR04.h"
#define TRIG_PIN 2
#define ECHO_PIN 3
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
uint16_t reading_cm;
uint16_t tempread;
void setup()
{
  reading_cm=0; 
  Wire.begin(0x70);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  tempread=sr04.DistanceAvg(25,1);
  if (tempread<500)  
       reading_cm=tempread;  
}
void requestEvent()
{
  byte sendhi;
  byte sendli;
  byte sendbyte[2];
  uint16_t tempreading_cm;
  tempreading_cm=reading_cm;
  sendhi=tempreading_cm>>8;
  sendli=tempreading_cm&0xff;
  sendbyte[0]=sendhi;
  sendbyte[1]=sendli;
  Wire.write(sendbyte,2); 
}

 

posted @ 2016-12-27 16:44  Tranquilty  阅读(2597)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3