Joseph Roberts / MaxbotixDriver

Fork of MaxbotixDriver by Daniel Casner

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sonar.cpp Source File

sonar.cpp

00001 #include "sonar.h"
00002 
00003 Sonar::Sonar(PinName input, Timer& t) :
00004     interrupt(input),
00005     time(t),
00006     pulseStartTime(0),
00007     range(0) {
00008     interrupt.rise(this, &Sonar::pulseStart);
00009     interrupt.fall(this, &Sonar::pulseStop);
00010 }
00011 
00012 int Sonar::read() {
00013     return range;
00014 }
00015 
00016 Sonar::operator int() {
00017     return read();
00018 }
00019 
00020 void Sonar::pulseStart() {
00021     pulseStartTime = time.read_us();
00022 }
00023 
00024 void Sonar::pulseStop() {
00025     int endTime = time.read_us();
00026     if (endTime < pulseStartTime) return; // Escape if there's been a roll over
00027     range = (endTime - pulseStartTime) / 58; // 58uS per CM
00028 }