Counts points that hit a LIDAR sensor, and includes threshold sensing so a point doesn't get counted twice, much in the same spirit as debouncing.

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE X_NUCLEO_53L0A1

The Object Counter counts the number of objects (in our case, ping pong balls) that pass by the sensor. It features a high water mark that ensures only one ball is counted at any given time, and that a ball will only be counted once.

Partner Program

This is intended to be used with Ping Pong Robot, but can be used by itself if you wish

Installation

We use the following components:

All of these can be purchased from SparkFun with the exception of the VL53L0X sensor, wich is from AdaFruit.

To connect these, use the following pin outs:

Class D Amplifier & Speaker

MBEDClass D Audio AmpSpeaker
GndIN-
GndPWR-
5VPWR+
p25IN+
p20S
OUT-Speaker-
OUT+Speaker+

uLCD Screen

MBEDuLCD HeaderuLCD
GndGndGnd
+5V+5V+5V
p13TXRX
p14RXTX
p15RESRES

VL35L0X

MBEDVL35L0X
GndGnd
+3.3VVIN
p26SHDN
p27SDA
p28SCL

Usage

This is intended to be used to track the number of objects that have been collected. Because of this, the software features a "High Water Mark" mentality - after an object has gotten close enough, a flag is tripped - this flag only resets after the distance increases back past the high water mark. In this way, we ensure we count each object once - much in the same spirit as debouncing.

The VL53L0X is a low cost chip that measures distance via "Time of Flight" - basically how long it takes light to travel to the object and back. Because it uses a single laser, it can be quite precise. This makes it ideal for our purposes - tracking a single object as it falls.

Heads Up!

The largest issue is data collection time - while the chip does collect data quickly, if your object is moving at a high speed, it can sometimes miss the object entirely because it didn't "see" the object in the few fractions of a second that the object was close enough!

If you require more precision, there are plenty of higher quality (and higher cost) LIDAR systems - AdaFruit sells many of these.

As a workaround, ensure your object is not moving quickly - this can be accomplished by setting the LIDAR's side on the floor, so its line of sight is directly above the ground. When the object hits the ground, even if it bounces, it should be on the ground enough time for the LIDAR to pick it up. Slightly tilting the ground will make sure your object will roll away after falling.

As with everything else, experience is the best teacher - you might need to tweak the threshold in the code to get optimal results.

Demonstration

The circuit looks like this: /media/uploads/alexhrao/img_9521.jpeg

Revision:
0:fc7ce4e596ff
Child:
1:e82997c13013
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Apr 14 16:02:37 2019 +0000
@@ -0,0 +1,165 @@
+#include "mbed.h"
+#include "rtos.h"
+#include <stdio.h>
+#include "uLCD_4DGL.h"
+#include "XNucleo53L0A1.h"
+
+#define VL53L0_I2C_SDA   p28
+#define VL53L0_I2C_SCL   p27
+
+// Light Thread
+// Only shine light when 
+// Sound Thread
+
+// Game
+volatile int points = 0;
+volatile int time_left = 0;
+int threshold =  0.05f; // ???
+volatile bool is_point = false;
+
+// LIDAR
+static XNucleo53L0A1* lidar = NULL;
+uint32_t dist;
+DigitalOut shdn(p26);
+
+// LCD
+Mutex lcd_lock;
+uLCD_4DGL uLCD(p9, p10, p11); // serial tx, serial rx, reset pin;
+
+// Speaker
+PwmOut speaker(p25);
+
+// LEDs
+PwmOut point_led(p21);
+PwmOut time_led(p22);
+
+// Threads
+Thread sound_thread;
+Thread time_thread;
+Thread points_thread;
+
+// Debugging
+DigitalOut err_led(LED1);
+DigitalOut status_led(LED2);
+Serial pc(USBTX, USBRX);
+
+void time(void) {
+    lcd_lock.lock();
+    uLCD.locate(3, 5);
+    uLCD.text_height(2);
+    uLCD.text_width(2);
+    uLCD.printf("%d", time_left);
+    lcd_lock.unlock();
+    while (1) {
+        // every second, change LED and update screen
+        time_led = !time_led;
+        if (time_left > 0) {
+            lcd_lock.lock();
+            uLCD.locate(3, 5);
+            uLCD.text_height(2);
+            uLCD.text_width(2);
+            uLCD.printf("%d", time_left);
+            lcd_lock.unlock();
+            Thread::wait(1000);
+        } else {
+            break;
+        }
+    }
+    lcd_lock.lock();
+    uLCD.locate(3, 5);
+    uLCD.text_height(2);
+    uLCD.text_width(2);
+    uLCD.printf("0");
+    lcd_lock.unlock();
+}
+
+void sound(void) {
+    int is_new = 1;
+    speaker.period(1.0 / 900.0);
+    while (1) {
+        // if we have a point, sound?
+        if (is_point && is_new) {
+            is_new = 0;
+            // sound that
+            speaker = 1;
+            
+        } else if (!is_point) {
+            is_new = 1;
+            speaker = 0;
+        }
+        Thread::wait(10);
+    }
+}
+
+void print_points(void) {
+    int prev_points = points;
+    while (1) {
+        if (points != prev_points) {
+            lcd_lock.lock();
+            uLCD.locate(1, 2);
+            uLCD.text_width(2);
+            uLCD.text_height(2);
+            uLCD.color(GREEN);
+            uLCD.printf("%d", points);
+            lcd_lock.unlock();
+            prev_points = points;
+        }
+        Thread::wait(100);
+    }
+}
+
+int main() {
+    // Tell the user we're spinning up
+
+    uLCD.cls();
+    uLCD.text_height(2);
+    uLCD.text_width(2);
+    uLCD.locate(1, 2);
+    uLCD.color(RED);
+    uLCD.printf("SPINNING UP");
+    
+    time_left = 60;
+    
+    // Normally, we should worry about deleting this object. However, it will 
+    // survive for the lifetime of the program!
+    DevI2C* device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
+    lidar = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
+    // Reset shdn (?):
+    shdn = 0;
+    wait(0.1);
+    shdn = 1;
+    wait(0.1);
+    
+    int status = lidar->init_board();
+    while (status) {
+        pc.printf("Initializing Board...\r\n");
+        status = lidar->init_board();
+        status_led = !status_led;
+        wait(1);
+    }
+    pc.printf("Finished Initialization\r\n");
+    
+    // Continually measure distance
+    while (1) {
+        uint32_t dist2;
+        status = lidar->sensor_centre->get_distance(&dist2);
+        if (status != VL53L0X_ERROR_NONE) {
+            pc.printf("Status: %d\r\n", status);
+            err_led = 1;
+        }
+        // Determine if we've hit the threshold (4 cases):
+        // 1. NOT in the threshold, current distance is above
+        // 2. NOT in the threshold, current distance is below
+        // 3. IN the threshold, current distance is above
+        // 4. IN the threshold, current distance is below
+        if (!is_point && dist2 <= threshold) {
+            // increment our points
+            ++points;
+            is_point = true;
+            point_led = 1;
+        } else if (is_point && dist2 > threshold) {
+            is_point = false;
+            point_led = 0;
+        }
+    }
+}