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
GoalCounter.cpp@1:eb4ee5706587, 2019-01-13 (annotated)
- Committer:
- nxf46245
- Date:
- Sun Jan 13 14:23:14 2019 +0000
- Revision:
- 1:eb4ee5706587
- Parent:
- 0:d00bd73d08f8
- Child:
- 2:cb1c7db56434
Fix NonCopyable issue
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:eb4ee5706587 | 17 | * |
nxf46245 | 1:eb4ee5706587 | 18 | * GoalCounter gc(D2); |
nxf46245 | 1:eb4ee5706587 | 19 | * TODO |
nxf46245 | 1:eb4ee5706587 | 20 | * |
nxf46245 | 1:eb4ee5706587 | 21 | * @endcode |
nxf46245 | 1:eb4ee5706587 | 22 | * |
nxf46245 | 1:eb4ee5706587 | 23 | * @param pin PinName of the pin where is phototransistor connected |
nxf46245 | 1:eb4ee5706587 | 24 | |
nxf46245 | 1:eb4ee5706587 | 25 | **/ |
nxf46245 | 1:eb4ee5706587 | 26 | GoalCounter::GoalCounter(PinName pin, Timer * t) : _t(t) { |
nxf46245 | 0:d00bd73d08f8 | 27 | |
nxf46245 | 1:eb4ee5706587 | 28 | InterruptIn *_interrupt = new InterruptIn(pin); |
nxf46245 | 1:eb4ee5706587 | 29 | |
nxf46245 | 1:eb4ee5706587 | 30 | _interrupt->fall(callback(this, &GoalCounter::tstart)); |
nxf46245 | 1:eb4ee5706587 | 31 | _interrupt->rise(callback(this, &GoalCounter::tstop)); |
nxf46245 | 0:d00bd73d08f8 | 32 | |
nxf46245 | 1:eb4ee5706587 | 33 | _score = 0; |
nxf46245 | 1:eb4ee5706587 | 34 | _time = 0; |
nxf46245 | 1:eb4ee5706587 | 35 | is_goal = 0; |
nxf46245 | 0:d00bd73d08f8 | 36 | } |
nxf46245 | 0:d00bd73d08f8 | 37 | |
nxf46245 | 1:eb4ee5706587 | 38 | void GoalCounter::tstart() { |
nxf46245 | 1:eb4ee5706587 | 39 | _t->start(); |
nxf46245 | 0:d00bd73d08f8 | 40 | } |
nxf46245 | 0:d00bd73d08f8 | 41 | |
nxf46245 | 1:eb4ee5706587 | 42 | void GoalCounter::tstop() { |
nxf46245 | 1:eb4ee5706587 | 43 | _t->stop(); |
nxf46245 | 1:eb4ee5706587 | 44 | _time = _t->read(); |
nxf46245 | 1:eb4ee5706587 | 45 | _t->reset(); |
nxf46245 | 0:d00bd73d08f8 | 46 | |
nxf46245 | 1:eb4ee5706587 | 47 | if ( _time > 0 && _time < 2 && _score < 10) { |
nxf46245 | 1:eb4ee5706587 | 48 | _score++; |
nxf46245 | 1:eb4ee5706587 | 49 | _balltimes[_score] = _time; |
nxf46245 | 1:eb4ee5706587 | 50 | is_goal = 1; |
nxf46245 | 0:d00bd73d08f8 | 51 | } |
nxf46245 | 0:d00bd73d08f8 | 52 | } |
nxf46245 | 0:d00bd73d08f8 | 53 | |
nxf46245 | 0:d00bd73d08f8 | 54 | |
nxf46245 | 0:d00bd73d08f8 | 55 | uint8_t GoalCounter::get_score() { |
nxf46245 | 1:eb4ee5706587 | 56 | return _score; |
nxf46245 | 0:d00bd73d08f8 | 57 | } |
nxf46245 | 0:d00bd73d08f8 | 58 | |
nxf46245 | 0:d00bd73d08f8 | 59 | float GoalCounter::get_balltime(uint8_t score) { |
nxf46245 | 0:d00bd73d08f8 | 60 | if (score <= 10 && score > 0) |
nxf46245 | 1:eb4ee5706587 | 61 | return _balltimes[score]; |
nxf46245 | 0:d00bd73d08f8 | 62 | else |
nxf46245 | 0:d00bd73d08f8 | 63 | return -1; |
nxf46245 | 0:d00bd73d08f8 | 64 | } |
nxf46245 | 0:d00bd73d08f8 | 65 | |
nxf46245 | 0:d00bd73d08f8 | 66 | float GoalCounter::get_balltime() { |
nxf46245 | 1:eb4ee5706587 | 67 | return _balltimes[_score]; |
nxf46245 | 0:d00bd73d08f8 | 68 | |
nxf46245 | 0:d00bd73d08f8 | 69 | } |
nxf46245 | 0:d00bd73d08f8 | 70 | |
nxf46245 | 0:d00bd73d08f8 | 71 | float GoalCounter::get_ballspeed(uint8_t score) { |
nxf46245 | 0:d00bd73d08f8 | 72 | if (score <= 10 && score > 0) { |
nxf46245 | 1:eb4ee5706587 | 73 | float speed = 0.034f/_balltimes[score]*3.6f; |
nxf46245 | 0:d00bd73d08f8 | 74 | return speed; |
nxf46245 | 0:d00bd73d08f8 | 75 | } |
nxf46245 | 0:d00bd73d08f8 | 76 | else |
nxf46245 | 0:d00bd73d08f8 | 77 | return -1; |
nxf46245 | 0:d00bd73d08f8 | 78 | } |
nxf46245 | 0:d00bd73d08f8 | 79 | |
nxf46245 | 0:d00bd73d08f8 | 80 | float GoalCounter::get_ballspeed() { |
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 | |
nxf46245 | 0:d00bd73d08f8 | 85 | |
nxf46245 | 0:d00bd73d08f8 | 86 | |
nxf46245 | 0:d00bd73d08f8 | 87 | |
nxf46245 | 0:d00bd73d08f8 | 88 | |
nxf46245 | 0:d00bd73d08f8 | 89 |