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
- Committer:
- nxf46245
- Date:
- 2019-01-13
- Revision:
- 2:cb1c7db56434
- Parent:
- 1:eb4ee5706587
- Child:
- 3:ce69ad70270a
File content as of revision 2:cb1c7db56434:
#include "GoalCounter.h" /** 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" * * Timer t; * GoalCounter gc(D2, &t); * * int main() { * while(1) { * if (gc.is_goal) * goal(); // call some function * } * * } * * @endcode * * @param pin PinName of the pin where is phototransistor connected * @param t Timer reference **/ GoalCounter::GoalCounter(PinName pin, Timer * t) : _t(t) { InterruptIn *_interrupt = new InterruptIn(pin); _interrupt->fall(callback(this, &GoalCounter::tstart)); _interrupt->rise(callback(this, &GoalCounter::tstop)); _score = 0; _time = 0; is_goal = 0; } void GoalCounter::tstart() { _t->start(); } void GoalCounter::tstop() { _t->stop(); _time = _t->read(); _t->reset(); if ( _time > 0 && _time < 2 && _score < 10) { _score++; _balltimes[_score] = _time; is_goal = 1; } } uint8_t GoalCounter::get_score() { return _score; } float GoalCounter::get_balltime(uint8_t score) { if (score <= 10 && score > 0) return _balltimes[score]; else return -1; } float GoalCounter::get_balltime() { return _balltimes[_score]; } float GoalCounter::get_ballspeed(uint8_t score) { if (score <= 10 && score > 0) { float speed = 0.034f/_balltimes[score]*3.6f; return speed; } else return -1; } float GoalCounter::get_ballspeed() { float speed = 0.034f/_balltimes[_score]*3.6f; return speed; }