From fa3bab2d3459482a9d63ff021e5d3d3906960579 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 30 Mar 2013 18:55:15 +0100 Subject: digital/ucoolib/ucoolib/dev/usdist: add US distance sensor support --- digital/ucoolib/ucoolib/dev/usdist/Module | 1 + digital/ucoolib/ucoolib/dev/usdist/usdist.cc | 99 ++++++++++++++++++++++++++++ digital/ucoolib/ucoolib/dev/usdist/usdist.hh | 79 ++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 digital/ucoolib/ucoolib/dev/usdist/Module create mode 100644 digital/ucoolib/ucoolib/dev/usdist/usdist.cc create mode 100644 digital/ucoolib/ucoolib/dev/usdist/usdist.hh diff --git a/digital/ucoolib/ucoolib/dev/usdist/Module b/digital/ucoolib/ucoolib/dev/usdist/Module new file mode 100644 index 00000000..bedbac40 --- /dev/null +++ b/digital/ucoolib/ucoolib/dev/usdist/Module @@ -0,0 +1 @@ +dev_usdist_SOURCES = usdist.cc diff --git a/digital/ucoolib/ucoolib/dev/usdist/usdist.cc b/digital/ucoolib/ucoolib/dev/usdist/usdist.cc new file mode 100644 index 00000000..759e68cb --- /dev/null +++ b/digital/ucoolib/ucoolib/dev/usdist/usdist.cc @@ -0,0 +1,99 @@ +// ucoolib - Microcontroller object oriented library. {{{ +// +// Copyright (C) 2013 Nicolas Schodet +// +// APBTeam: +// Web: http://apbteam.org/ +// Email: team AT apbteam DOT org +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "usdist.hh" + +namespace ucoo { + + +UsDist::UsDist (UsDistControl &ctrl, Adc &adc, Io &io, + int distance_min, int distance_max, int distance_too_far) + : adc_ (adc), io_ (io), next_ (0), + distance_min_ (distance_min), distance_max_ (distance_max), + distance_too_far_ (distance_too_far), distance_ (-1) +{ + next_ = ctrl.sensors_; + ctrl.sensors_ = this; +} + +void +UsDist::enable () +{ + io_.set (); +} + +void +UsDist::disable () +{ + io_.reset (); +} + +int +UsDist::read () +{ + int v = adc_.read (); + // The sensor returns a value between 4 and 20 mA proportional to the + // distance between calibrated values. Ignore faulty sensors. + const int max = adc_.get_resolution (); + const int min = max / 5; + if (v <= min / 4) + distance_ = -1; + else if (v <= min) + distance_ = distance_min_; + else + distance_ = distance_min_ + + ((v - min) * (distance_max_ - distance_min_) / (max - min)); + if (distance_too_far_ != -1 && distance_ > distance_too_far_) + distance_ = -1; + return distance_; +} + +UsDistControl::UsDistControl (int period) + : period_ (period), sensors_ (0), current_ (0), wait_ (0) +{ +} + +bool +UsDistControl::update () +{ + bool new_measure = false; + if (wait_) + wait_--; + else + { + if (current_) + { + current_->disable (); + current_->read (); + new_measure = true; + current_ = current_->next_; + } + if (!current_) + current_ = sensors_; + current_->enable (); + wait_ = period_ - 1; + } + return new_measure; +} + +} // namespace ucoo diff --git a/digital/ucoolib/ucoolib/dev/usdist/usdist.hh b/digital/ucoolib/ucoolib/dev/usdist/usdist.hh new file mode 100644 index 00000000..6f86d24a --- /dev/null +++ b/digital/ucoolib/ucoolib/dev/usdist/usdist.hh @@ -0,0 +1,79 @@ +#ifndef ucoolib_dev_usdist_usdist_hh +#define ucoolib_dev_usdist_usdist_hh +// ucoolib - Microcontroller object oriented library. {{{ +// +// Copyright (C) 2013 Nicolas Schodet +// +// APBTeam: +// Web: http://apbteam.org/ +// Email: team AT apbteam DOT org +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "ucoolib/intf/adc.hh" +#include "ucoolib/intf/io.hh" + +namespace ucoo { + +class UsDistControl; + +/// Single US distance sensor. +class UsDist +{ + public: + /// Constructor. + UsDist (UsDistControl &ctrl, Adc &adc, Io &io, + int distance_min, int distance_max, int distance_too_far = -1); + /// Enable measure. + void enable (); + /// Disable measure. + void disable (); + /// Read current value. + int read (); + /// Return last measured distance. + int get () const { return distance_; } + private: + Adc &adc_; + Io &io_; + UsDist *next_; + int distance_min_, distance_max_, distance_too_far_; + int distance_; + friend class UsDistControl; +}; + +/// Handle a set of US distance sensors. +class UsDistControl +{ + public: + /// Constructor. + UsDistControl (int period); + /// Make measurements, return true if a new measure is available. + bool update (); + private: + /// Number of updates between measures. + int period_; + /// Pointer to linked list of sensors. + UsDist *sensors_; + /// Current measuring sensor. + UsDist *current_; + /// Number of updates before the next action. + int wait_; + friend class UsDist; +}; + +} // namespace ucoo + +#endif // ucoolib_dev_usdist_usdist_hh -- cgit v1.2.3