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
Diff: GoalCounter.cpp
- Revision:
- 1:eb4ee5706587
- Parent:
- 0:d00bd73d08f8
- Child:
- 2:cb1c7db56434
--- a/GoalCounter.cpp Sat Jan 12 18:57:26 2019 +0000 +++ b/GoalCounter.cpp Sun Jan 13 14:23:14 2019 +0000 @@ -1,78 +1,76 @@ #include "GoalCounter.h" -GoalCounter::GoalCounter(PinName pin1, PinName pin2) : _interrupt1(pin1) { - _interrupt1.fall(callback(this, &GoalCounter::tstart1)); - _interrupt1.rise(callback(this, &GoalCounter::tstop1)); - -// _interrupt2.fall(callback(this, &GoalCounter::tstart2)); -// _interrupt2.rise(callback(this, &GoalCounter::tstop2)); - - Timer t1; - Timer t2; +/** GoalCounter class. + * Simple implementation of goal counter for table football using laser diode + * and phototransistor. Output of the phototransistor is connected to the digital + * input pin of MCU. When the laser beam is interrupted by ball passing the the cage + * Timer starts counting and measuring width of the pulse and if the duration of pulse + * is not longer than two seconds, goal is detected and _score variable is incremented. + * + * Pulse duration is also used for the calculation of ball passing speed. However, + * measurement is not precise. + * + * Example: + * @code + * #include "mbed.h" + * #include "GoalCounter.h" + * + * GoalCounter gc(D2); + * TODO + * + * @endcode + * + * @param pin PinName of the pin where is phototransistor connected + + **/ +GoalCounter::GoalCounter(PinName pin, Timer * t) : _t(t) { - _score1 = 0; - _score2 = 0; - - _time1 = 0; - _time2 = 0; + InterruptIn *_interrupt = new InterruptIn(pin); + + _interrupt->fall(callback(this, &GoalCounter::tstart)); + _interrupt->rise(callback(this, &GoalCounter::tstop)); -// _balltimes1 = {0}; -// _balltimes2 = {0}; - - is_goal1 = 0; - is_goal2 = 0; - + _score = 0; + _time = 0; + is_goal = 0; } -void GoalCounter::tstart1() { - t1.start(); +void GoalCounter::tstart() { + _t->start(); } -void GoalCounter::tstop1() { - t1.stop(); - _time1 = t1.read(); - t1.reset(); +void GoalCounter::tstop() { + _t->stop(); + _time = _t->read(); + _t->reset(); - if ( _time1 > 0 && _time1 < 2 && _score1 < 10) { - _balltimes1[++_score1] = _time1; - is_goal1 = 1; + if ( _time > 0 && _time < 2 && _score < 10) { + _score++; + _balltimes[_score] = _time; + is_goal = 1; } } -void GoalCounter::tstart2() { - t2.start(); -} - -void GoalCounter::tstop2() { - t2.stop(); - _time2 = t2.read(); - t2.reset(); - - if ( _time2 > 0 && _time2 < 2 && _score2 < 10) { - _balltimes2[++_score2] = _time2; - is_goal2 = 1; - } -} uint8_t GoalCounter::get_score() { - return _score1; + return _score; } float GoalCounter::get_balltime(uint8_t score) { if (score <= 10 && score > 0) - return _balltimes1[score]; + return _balltimes[score]; else return -1; } float GoalCounter::get_balltime() { - return _balltimes1[_score1]; + return _balltimes[_score]; } float GoalCounter::get_ballspeed(uint8_t score) { if (score <= 10 && score > 0) { - float speed = 0.034f/_balltimes1[score]*3.6f; + float speed = 0.034f/_balltimes[score]*3.6f; return speed; } else @@ -80,7 +78,7 @@ } float GoalCounter::get_ballspeed() { - float speed = 0.034f/_balltimes1[_score1]*3.6f; + float speed = 0.034f/_balltimes[_score]*3.6f; return speed; }