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.h@4:fc48ef79f484, 2019-01-14 (annotated)
- Committer:
- nxf46245
- Date:
- Mon Jan 14 17:48:02 2019 +0000
- Revision:
- 4:fc48ef79f484
- Parent:
- 3:ce69ad70270a
Code cleaning, added timeout between two goals
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nxf46245 | 1:eb4ee5706587 | 1 | #ifndef GOALCOUNTER_H |
nxf46245 | 1:eb4ee5706587 | 2 | #define GOALCOUNTER_H |
nxf46245 | 1:eb4ee5706587 | 3 | |
nxf46245 | 0:d00bd73d08f8 | 4 | #include "mbed.h" |
nxf46245 | 0:d00bd73d08f8 | 5 | |
nxf46245 | 3:ce69ad70270a | 6 | /** GoalCounter class. |
nxf46245 | 3:ce69ad70270a | 7 | * Simple implementation of goal counter for table football using laser diode |
nxf46245 | 3:ce69ad70270a | 8 | * and phototransistor. Output of the phototransistor is connected to the digital |
nxf46245 | 3:ce69ad70270a | 9 | * input pin of MCU. When the laser beam is interrupted by ball passing the the cage |
nxf46245 | 3:ce69ad70270a | 10 | * Timer starts counting and measuring width of the pulse and if the duration of pulse |
nxf46245 | 3:ce69ad70270a | 11 | * is not longer than two seconds, goal is detected and _score variable is incremented. |
nxf46245 | 3:ce69ad70270a | 12 | * |
nxf46245 | 3:ce69ad70270a | 13 | * Pulse duration is also used for the calculation of ball passing speed. However, |
nxf46245 | 3:ce69ad70270a | 14 | * measurement is not precise. |
nxf46245 | 3:ce69ad70270a | 15 | * |
nxf46245 | 3:ce69ad70270a | 16 | * Example: |
nxf46245 | 3:ce69ad70270a | 17 | * @code |
nxf46245 | 3:ce69ad70270a | 18 | * #include "mbed.h" |
nxf46245 | 3:ce69ad70270a | 19 | * #include "GoalCounter.h" |
nxf46245 | 3:ce69ad70270a | 20 | * |
nxf46245 | 3:ce69ad70270a | 21 | * Timer t; |
nxf46245 | 3:ce69ad70270a | 22 | * GoalCounter gc(D2, &t); |
nxf46245 | 3:ce69ad70270a | 23 | * |
nxf46245 | 3:ce69ad70270a | 24 | * int main() { |
nxf46245 | 3:ce69ad70270a | 25 | * while(1) { |
nxf46245 | 3:ce69ad70270a | 26 | * if (gc.is_goal) |
nxf46245 | 3:ce69ad70270a | 27 | * goal(); // call some function |
nxf46245 | 3:ce69ad70270a | 28 | * } |
nxf46245 | 3:ce69ad70270a | 29 | * |
nxf46245 | 3:ce69ad70270a | 30 | * } |
nxf46245 | 3:ce69ad70270a | 31 | * |
nxf46245 | 3:ce69ad70270a | 32 | * @endcode |
nxf46245 | 3:ce69ad70270a | 33 | * |
nxf46245 | 3:ce69ad70270a | 34 | **/ |
nxf46245 | 3:ce69ad70270a | 35 | |
nxf46245 | 0:d00bd73d08f8 | 36 | class GoalCounter { |
nxf46245 | 0:d00bd73d08f8 | 37 | public: |
nxf46245 | 3:ce69ad70270a | 38 | /** |
nxf46245 | 4:fc48ef79f484 | 39 | * Constructor |
nxf46245 | 3:ce69ad70270a | 40 | * |
nxf46245 | 4:fc48ef79f484 | 41 | * @param pin Input pin where is the outpout of phototransistor connected |
nxf46245 | 4:fc48ef79f484 | 42 | * @param timer reference to the Timer instance |
nxf46245 | 3:ce69ad70270a | 43 | * |
nxf46245 | 3:ce69ad70270a | 44 | */ |
nxf46245 | 4:fc48ef79f484 | 45 | GoalCounter(PinName pin, Timer * t); |
nxf46245 | 3:ce69ad70270a | 46 | /** |
nxf46245 | 4:fc48ef79f484 | 47 | * Destructor |
nxf46245 | 4:fc48ef79f484 | 48 | * |
nxf46245 | 4:fc48ef79f484 | 49 | * Destroys instance |
nxf46245 | 4:fc48ef79f484 | 50 | */ |
nxf46245 | 4:fc48ef79f484 | 51 | ~GoalCounter(); |
nxf46245 | 4:fc48ef79f484 | 52 | /** |
nxf46245 | 4:fc48ef79f484 | 53 | * Reads value of the timer, |
nxf46245 | 4:fc48ef79f484 | 54 | * this function is called from the interrupt pin fall edge callback |
nxf46245 | 3:ce69ad70270a | 55 | */ |
nxf46245 | 1:eb4ee5706587 | 56 | void tstart(); |
nxf46245 | 3:ce69ad70270a | 57 | /** |
nxf46245 | 4:fc48ef79f484 | 58 | * Reads value from the timer and measures interval between end and beginning |
nxf46245 | 4:fc48ef79f484 | 59 | * Called from the interrupt pin rise edge callback |
nxf46245 | 3:ce69ad70270a | 60 | */ |
nxf46245 | 1:eb4ee5706587 | 61 | void tstop(); |
nxf46245 | 3:ce69ad70270a | 62 | /** |
nxf46245 | 3:ce69ad70270a | 63 | * Returns score |
nxf46245 | 3:ce69ad70270a | 64 | * @return score score of the game |
nxf46245 | 3:ce69ad70270a | 65 | */ |
nxf46245 | 0:d00bd73d08f8 | 66 | uint8_t get_score(); |
nxf46245 | 3:ce69ad70270a | 67 | /** |
nxf46245 | 3:ce69ad70270a | 68 | * Returns time of a ball pass for a given score |
nxf46245 | 3:ce69ad70270a | 69 | * @param score of the game |
nxf46245 | 3:ce69ad70270a | 70 | * @return time of a ball pass in seconds |
nxf46245 | 3:ce69ad70270a | 71 | */ |
nxf46245 | 0:d00bd73d08f8 | 72 | float get_balltime(uint8_t score); |
nxf46245 | 3:ce69ad70270a | 73 | /** |
nxf46245 | 3:ce69ad70270a | 74 | * Returns time of a ball pass for the current score |
nxf46245 | 3:ce69ad70270a | 75 | * @return time of a ball pass in seconds |
nxf46245 | 3:ce69ad70270a | 76 | */ |
nxf46245 | 0:d00bd73d08f8 | 77 | float get_balltime(); |
nxf46245 | 3:ce69ad70270a | 78 | /** |
nxf46245 | 3:ce69ad70270a | 79 | * Returns speed of a ball pass for a given score |
nxf46245 | 3:ce69ad70270a | 80 | * @param score of the game |
nxf46245 | 3:ce69ad70270a | 81 | * @return speed of a ball pass in kph |
nxf46245 | 3:ce69ad70270a | 82 | */ |
nxf46245 | 0:d00bd73d08f8 | 83 | float get_ballspeed(uint8_t score); |
nxf46245 | 3:ce69ad70270a | 84 | /** |
nxf46245 | 3:ce69ad70270a | 85 | * Returns speed of a ball pass for the current score |
nxf46245 | 3:ce69ad70270a | 86 | * @return speed of a ball pass in kph |
nxf46245 | 3:ce69ad70270a | 87 | */ |
nxf46245 | 0:d00bd73d08f8 | 88 | float get_ballspeed(); |
nxf46245 | 3:ce69ad70270a | 89 | /** |
nxf46245 | 4:fc48ef79f484 | 90 | * Variable is 1 (true) when the goal is detected |
nxf46245 | 3:ce69ad70270a | 91 | */ |
nxf46245 | 1:eb4ee5706587 | 92 | volatile uint8_t is_goal; |
nxf46245 | 0:d00bd73d08f8 | 93 | |
nxf46245 | 0:d00bd73d08f8 | 94 | private: |
nxf46245 | 1:eb4ee5706587 | 95 | InterruptIn * _interrupt; |
nxf46245 | 1:eb4ee5706587 | 96 | volatile uint8_t _score; |
nxf46245 | 1:eb4ee5706587 | 97 | Timer * _t; |
nxf46245 | 4:fc48ef79f484 | 98 | float _balltimes[11]; // sorry for the FP arithmetics, i was too lazy to rewrite it |
nxf46245 | 1:eb4ee5706587 | 99 | float _time; |
nxf46245 | 4:fc48ef79f484 | 100 | float _begin; |
nxf46245 | 4:fc48ef79f484 | 101 | float _end; |
nxf46245 | 4:fc48ef79f484 | 102 | float _last_end; |
nxf46245 | 4:fc48ef79f484 | 103 | float _time_between; |
nxf46245 | 0:d00bd73d08f8 | 104 | }; |
nxf46245 | 0:d00bd73d08f8 | 105 | |
nxf46245 | 1:eb4ee5706587 | 106 | #endif |