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

Files at this revision

API Documentation at this revision

Comitter:
nxf46245
Date:
Mon Jan 14 17:48:02 2019 +0000
Parent:
3:ce69ad70270a
Commit message:
Code cleaning, added timeout between two goals

Changed in this revision

GoalCounter.cpp Show annotated file Show diff for this revision Revisions of this file
GoalCounter.h Show annotated file Show diff for this revision Revisions of this file
diff -r ce69ad70270a -r fc48ef79f484 GoalCounter.cpp
--- a/GoalCounter.cpp	Sun Jan 13 16:43:27 2019 +0000
+++ b/GoalCounter.cpp	Mon Jan 14 17:48:02 2019 +0000
@@ -2,28 +2,41 @@
 
 GoalCounter::GoalCounter(PinName pin, Timer * t) : _t(t) {
     
-    InterruptIn *_interrupt = new InterruptIn(pin);
+    InterruptIn *_interrupt = new InterruptIn(pin); // Initialize instace of InterruptIn
+    // It has to be done dynamically because of non-copyable class
      
-    _interrupt->fall(callback(this, &GoalCounter::tstart));
+    _interrupt->fall(callback(this, &GoalCounter::tstart)); // Set callbacks to timer
     _interrupt->rise(callback(this, &GoalCounter::tstop));
     
     _score = 0;
     _time = 0;
-    is_goal = 0; 
+    is_goal = 0;
+    _begin = 0;
+    _end = 0;
+    _last_end = 0;
+    _time_between = 0;
+    
+    _t->start(); 
+}
+
+GoalCounter::~GoalCounter()
+{
+    delete[] _interrupt; // free up dynamically allocated memory
 }
          
 void GoalCounter::tstart() {
-    _t->start();
+    _begin = _t->read(); // read value of the timer
 }
 
 void GoalCounter::tstop() {
-    _t->stop();
-    _time = _t->read();
-    _t->reset();
-    
-    if ( _time > 0 && _time < 2 && _score < 10) {
+    _end = _t->read();
+    _time = _end - _begin;
+    _time_between = _begin - _last_end;
+
+    if ( _time > 0 && _time < 2 && _score < 10  && _time_between > 1) {
        _score++;
        _balltimes[_score] = _time;
+       _last_end = _end;
        is_goal = 1;
     }     
 }
@@ -34,7 +47,7 @@
 }
 
 float GoalCounter::get_balltime(uint8_t score) {
-    if (score <= 10 && score > 0)
+    if (score <= _score && score > 0)
         return _balltimes[score];
     else
         return -1;   
@@ -46,7 +59,7 @@
 }
 
 float GoalCounter::get_ballspeed(uint8_t score) {
-    if (score <= 10 && score > 0) {
+    if (score <= _score && score > 0) {
         float speed = 0.034f/_balltimes[score]*3.6f;
         return speed;
     }
diff -r ce69ad70270a -r fc48ef79f484 GoalCounter.h
--- a/GoalCounter.h	Sun Jan 13 16:43:27 2019 +0000
+++ b/GoalCounter.h	Mon Jan 14 17:48:02 2019 +0000
@@ -36,19 +36,27 @@
 class GoalCounter {
 public:
     /**
-    * Constructor
+    *   Constructor
     *
-    * @param pin Input pin where is the outpout of phototransistor connected
-    * @param timer reference to the Timer instance
+    *   @param pin Input pin where is the outpout of phototransistor connected
+    *   @param timer reference to the Timer instance
     *
     */
-    GoalCounter(PinName pin, Timer * t);  
+    GoalCounter(PinName pin, Timer * t);
     /**
-    *   Starts the timer, this function is called from the interrupt pin callback
+    *   Destructor
+    *
+    *   Destroys instance
+    */
+    ~GoalCounter();   
+    /**
+    *   Reads value of the timer, 
+    *   this function is called from the interrupt pin fall edge callback
     */
     void tstart();
     /**
-    *   Stops the timer, this function is called from the interrupt pin callback
+    *   Reads value from the timer and measures interval between end and beginning
+    *   Called from the interrupt pin rise edge callback
     */
     void tstop();
     /**
@@ -79,7 +87,7 @@
     */
     float get_ballspeed();
     /**
-    *  Variable is 1 when the goal is detected
+    *  Variable is 1 (true) when the goal is detected
     */
     volatile uint8_t is_goal;
 
@@ -87,9 +95,12 @@
     InterruptIn * _interrupt;
     volatile uint8_t _score;
     Timer * _t;
-    float _balltimes[11];
+    float _balltimes[11]; // sorry for the FP arithmetics, i was too lazy to rewrite it
     float _time;
-      
+    float _begin;
+    float _end;
+    float _last_end;
+    float _time_between;      
 };
 
 #endif 
\ No newline at end of file