Joseph Roberts / LidarLitePwm

Dependents:   LidarLite_test

Fork of MaxbotixDriver by Daniel Casner

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LidarLitePwm.cpp Source File

LidarLitePwm.cpp

00001 #include "LidarLitePwm.h"
00002 
00003 LidarLitePwm::LidarLitePwm(PinName pin) : _interrupt(pin)
00004 {
00005     _pulseStartTime = 0;
00006     _range = 0;
00007     _lidarFilter = new filter(5);
00008     _timer.start();
00009     _interrupt.rise(this, &LidarLitePwm::pulseStart);
00010     _interrupt.fall(this, &LidarLitePwm::pulseStop);
00011 }
00012 
00013 LidarLitePwm::~LidarLitePwm(){}
00014 
00015 int LidarLitePwm::read()
00016 {
00017     if(_range < 30) return 0;
00018     else return _range - 30;
00019 }
00020 
00021 LidarLitePwm::operator int()
00022 {
00023     return read();
00024 }
00025 
00026 void LidarLitePwm::pulseStart()
00027 {
00028     _pulseStartTime = _timer.read_us();
00029 }
00030 
00031 void LidarLitePwm::pulseStop()
00032 {
00033     int endTime = _timer.read_us();
00034     if (endTime < _pulseStartTime) return; // Escape if there's been a roll over
00035     int range = (endTime - _pulseStartTime) / 10; // 10uS per CM
00036     _range = _lidarFilter->process(range);
00037 }