This library provides simple interface for the table football goal counter based on a IR LED or laser diode and phototransistor.

Dependents:   goal_counter_project

Committer:
nxf46245
Date:
Sun Jan 13 16:27:23 2019 +0000
Revision:
2:cb1c7db56434
Parent:
1:eb4ee5706587
Child:
3:ce69ad70270a
code refactoring;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxf46245 0:d00bd73d08f8 1 #include "GoalCounter.h"
nxf46245 0:d00bd73d08f8 2
nxf46245 1:eb4ee5706587 3 /** GoalCounter class.
nxf46245 1:eb4ee5706587 4 * Simple implementation of goal counter for table football using laser diode
nxf46245 1:eb4ee5706587 5 * and phototransistor. Output of the phototransistor is connected to the digital
nxf46245 1:eb4ee5706587 6 * input pin of MCU. When the laser beam is interrupted by ball passing the the cage
nxf46245 1:eb4ee5706587 7 * Timer starts counting and measuring width of the pulse and if the duration of pulse
nxf46245 1:eb4ee5706587 8 * is not longer than two seconds, goal is detected and _score variable is incremented.
nxf46245 1:eb4ee5706587 9 *
nxf46245 1:eb4ee5706587 10 * Pulse duration is also used for the calculation of ball passing speed. However,
nxf46245 1:eb4ee5706587 11 * measurement is not precise.
nxf46245 1:eb4ee5706587 12 *
nxf46245 1:eb4ee5706587 13 * Example:
nxf46245 1:eb4ee5706587 14 * @code
nxf46245 1:eb4ee5706587 15 * #include "mbed.h"
nxf46245 1:eb4ee5706587 16 * #include "GoalCounter.h"
nxf46245 2:cb1c7db56434 17 *
nxf46245 2:cb1c7db56434 18 * Timer t;
nxf46245 2:cb1c7db56434 19 * GoalCounter gc(D2, &t);
nxf46245 2:cb1c7db56434 20 *
nxf46245 2:cb1c7db56434 21 * int main() {
nxf46245 2:cb1c7db56434 22 * while(1) {
nxf46245 2:cb1c7db56434 23 * if (gc.is_goal)
nxf46245 2:cb1c7db56434 24 * goal(); // call some function
nxf46245 2:cb1c7db56434 25 * }
nxf46245 2:cb1c7db56434 26 *
nxf46245 2:cb1c7db56434 27 * }
nxf46245 1:eb4ee5706587 28 *
nxf46245 1:eb4ee5706587 29 * @endcode
nxf46245 1:eb4ee5706587 30 *
nxf46245 1:eb4ee5706587 31 * @param pin PinName of the pin where is phototransistor connected
nxf46245 2:cb1c7db56434 32 * @param t Timer reference
nxf46245 1:eb4ee5706587 33 **/
nxf46245 1:eb4ee5706587 34 GoalCounter::GoalCounter(PinName pin, Timer * t) : _t(t) {
nxf46245 0:d00bd73d08f8 35
nxf46245 1:eb4ee5706587 36 InterruptIn *_interrupt = new InterruptIn(pin);
nxf46245 1:eb4ee5706587 37
nxf46245 1:eb4ee5706587 38 _interrupt->fall(callback(this, &GoalCounter::tstart));
nxf46245 1:eb4ee5706587 39 _interrupt->rise(callback(this, &GoalCounter::tstop));
nxf46245 0:d00bd73d08f8 40
nxf46245 1:eb4ee5706587 41 _score = 0;
nxf46245 1:eb4ee5706587 42 _time = 0;
nxf46245 1:eb4ee5706587 43 is_goal = 0;
nxf46245 0:d00bd73d08f8 44 }
nxf46245 0:d00bd73d08f8 45
nxf46245 1:eb4ee5706587 46 void GoalCounter::tstart() {
nxf46245 1:eb4ee5706587 47 _t->start();
nxf46245 0:d00bd73d08f8 48 }
nxf46245 0:d00bd73d08f8 49
nxf46245 1:eb4ee5706587 50 void GoalCounter::tstop() {
nxf46245 1:eb4ee5706587 51 _t->stop();
nxf46245 1:eb4ee5706587 52 _time = _t->read();
nxf46245 1:eb4ee5706587 53 _t->reset();
nxf46245 0:d00bd73d08f8 54
nxf46245 1:eb4ee5706587 55 if ( _time > 0 && _time < 2 && _score < 10) {
nxf46245 1:eb4ee5706587 56 _score++;
nxf46245 1:eb4ee5706587 57 _balltimes[_score] = _time;
nxf46245 1:eb4ee5706587 58 is_goal = 1;
nxf46245 0:d00bd73d08f8 59 }
nxf46245 0:d00bd73d08f8 60 }
nxf46245 0:d00bd73d08f8 61
nxf46245 0:d00bd73d08f8 62
nxf46245 0:d00bd73d08f8 63 uint8_t GoalCounter::get_score() {
nxf46245 1:eb4ee5706587 64 return _score;
nxf46245 0:d00bd73d08f8 65 }
nxf46245 0:d00bd73d08f8 66
nxf46245 0:d00bd73d08f8 67 float GoalCounter::get_balltime(uint8_t score) {
nxf46245 0:d00bd73d08f8 68 if (score <= 10 && score > 0)
nxf46245 1:eb4ee5706587 69 return _balltimes[score];
nxf46245 0:d00bd73d08f8 70 else
nxf46245 0:d00bd73d08f8 71 return -1;
nxf46245 0:d00bd73d08f8 72 }
nxf46245 0:d00bd73d08f8 73
nxf46245 0:d00bd73d08f8 74 float GoalCounter::get_balltime() {
nxf46245 1:eb4ee5706587 75 return _balltimes[_score];
nxf46245 0:d00bd73d08f8 76
nxf46245 0:d00bd73d08f8 77 }
nxf46245 0:d00bd73d08f8 78
nxf46245 0:d00bd73d08f8 79 float GoalCounter::get_ballspeed(uint8_t score) {
nxf46245 0:d00bd73d08f8 80 if (score <= 10 && score > 0) {
nxf46245 1:eb4ee5706587 81 float speed = 0.034f/_balltimes[score]*3.6f;
nxf46245 0:d00bd73d08f8 82 return speed;
nxf46245 0:d00bd73d08f8 83 }
nxf46245 0:d00bd73d08f8 84 else
nxf46245 0:d00bd73d08f8 85 return -1;
nxf46245 0:d00bd73d08f8 86 }
nxf46245 0:d00bd73d08f8 87
nxf46245 0:d00bd73d08f8 88 float GoalCounter::get_ballspeed() {
nxf46245 1:eb4ee5706587 89 float speed = 0.034f/_balltimes[_score]*3.6f;
nxf46245 0:d00bd73d08f8 90 return speed;
nxf46245 0:d00bd73d08f8 91 }