ICOMP-UNC / Pepsicity

Advance conveyor belt system.
MIT License
0 stars 0 forks source link

Implement Timer and Ultrasonic Sensor #6

Closed FrancoPerotti closed 6 days ago

FrancoPerotti commented 2 weeks ago

Object Counting

To count the objects passing over the conveyor belt, we will use an ultrasonic sensor (HY-SRF05) and the TIMER0 module of the LPC1769.

The ultrasonic sensor will be used to measure the distance (time) to the object (if any) in front of it. On the other hand, TIMER0 will be used to periodically start the ultrasonic sensor measurements and to measure the sensor's times.

Working of the Ultrasonic Sensor (HY-SRF05)

The HY-SRF05 ultrasonic sensor is a device that measures the distance to an object using high-frequency sound waves. It works by emitting a sound pulse and measuring the time it takes for the pulse to return after bouncing off an object.

Here are the steps of its operation:

  1. Start Measurement: Set the TRIGGER input pin to high for at least 10 µs. This signals the sensor to start a measurement.

  2. Emit Ultrasonic Pulse: Upon receiving the activation signal, the sensor emits a 40 kHz ultrasonic pulse through the transmitting transducer. This pulse travels through the air toward the object in front of the sensor.

  3. Activate ECHO Pin: Immediately after sending the ultrasonic pulse, the ECHO output pin is activated (set to high).

  4. Reflection and Pulse Reception: When the ultrasonic pulse hits an object, it bounces back and returns to the sensor. Upon receiving the bounce, the ECHO pin is set back to low.

  5. Distance Calculation: The time the ECHO pin stays high (from the moment the pulse was emitted until it returns) is measured. Using this duration, the distance is calculated with the formula:

    $$Distance = \frac{Time \times v_{sound}}{2}$$

    The speed of sound in air is approximately 343 m/s at room temperature (this can vary with environmental conditions).

image

Application of the Ultrasonic Sensor for Object Counting

Since we are not using the ultrasonic sensor to measure distances, but to detect objects, we are not interested in using the previous formula. Instead, we will use the time during which the ECHO pin remains active, which is simply the time it takes for the pulses to travel to and from the object.

$$t_{ECHO}:\ time\ during\ which\ the\ ECHO\ is\ active$$

Physically, the ultrasonic sensor will be positioned to point at a surface that limits the range of its measurements. This surface could be the conveyor belt itself (if positioned pointing downwards), or it could be a vertical surface beside the belt (in which case the sensor would be positioned on the opposite side).

Given the physical setup of the sensor, we know that the pulses it emits will always return, either by bouncing off an object on the belt or by hitting the limiting surface. The presence of this surface establishes a maximum time for the pulses to travel (round-trip time).

$$t_{RMAX}: maximum\ time\ the\ pulses\ take\ to\ travel\ round-trip\ from\ the\ sensor$$

If an object is between the sensor and the limiting surface, the time it takes for the pulses to travel will be less than the maximum time. However, a safety margin is required to avoid false positives and provide a greater margin for decision-making.

$$d:\ distance\ between\ the\ sensor\ and\ the\ belt$$

$$m:\ safety\ margin$$

$$t_{MAX}:\ maximum\ time\ at\ which\ it\ is\ considered\ there\ is\ no\ object$$

$$t{MAX}=\frac{d - m}{v{sound}}$$

This gives us the following decision criteria:

Application of TIMER0 for Object Counting

TIMER0 will be used for three tasks:

  1. Start the ultrasonic sensor measurement by setting the TRIGGER pin for 10 µs.
  2. Calculate the time the ECHO pin stays high ($t_{ECHO}$).
  3. Restart the timer after a period that allows us to obtain an appropriate time between measurements ($t_{MED}$) for periodicity.

To achieve this, we will use both the MATCH and CAPTURE channels of TIMER0:

Counting Different Objects

We must take into account that the width of the objects on the conveyor, the space between them, and their speed (conveyor belt speed) can be variable. Due to this, we cannot establish a fixed time between measurements. The best approach is to set a measurement time small enough to adapt to any situation.

By using a small measurement time, we must be aware that a single object will, by definition, be measured multiple times, so we need to implement a mechanism to avoid counting the same object multiple times.

To solve this, we can use flags to track whether an object is being detected or not.

This also raises a question: When do we increment the object count?

In our case, we've chosen the second option because when the object is no longer detected, it is most likely completely past the detection area.

Measurement Steps

The following steps describe the high-level process of a measurement:

  1. Start TIMER0.

  2. After a time $t_{START}$, a MATCH interrupt occurs and within its ISR, we set the TRIGGER pin.

  3. After another time $t_{START} + 10 \mu s$, another MATCH interrupt occurs, and within its ISR, we clear the TRIGGER pin.

  4. The ultrasonic sensor sends the pulses, and the ECHO pin is automatically set high.

  5. The ECHO pin being set high triggers a CAPTURE interrupt, and in its ISR, we store the timer value at that moment as $t_{1}$.

  6. After bouncing back, the pulse returns to the sensor, and the ECHO pin is cleared, triggering another CAPTURE interrupt.

  7. In the ISR of this interrupt, we store the timer value as $t_{2}$ and perform the following calculation:

    $$t{ECHO} = t{2} - t_{1}$$

  8. After calculating $t{ECHO}$, we compare it with $t{MAX}$.

    • If $t{ECHO} < t{MAX} \rightarrow$ there is an object.
      • Set a flag indicating that an object is being detected.
    • If $t{ECHO} > t{MAX} \rightarrow$ there is no object.
      • If an object was being detected (flag raised) → increment the count.
      • If no object was being detected (flag lowered) → do nothing.
  9. After a time $t{RESET} = t{MED} - t_{START}$, another MATCH interrupt occurs, restarting the timer and beginning the next measurement process.

Flowchart

image

FrancoPerotti commented 1 week ago

Due to pin access limitations on the board, it was decided to replace the use of TIMER0 with TIMER2 as described at #14.