DIY PIXHAWK APM等飛控用的聲納


 

 

 

 

代碼:

SR04 + ApmSonar.ino 打包下載

 

注意,使用到了SR04的類庫。

ApmSonar.ino

// sr04 to apm I2c sonar
// by panxu mail: pxzleo@126.com
// 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); 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM