Laser Timing Rig Software

In our last blog we talked about our Laser Timing Rig hardware and how it is connected to a Raspberry Pi using the GPIO.
Now it is time to take a look at how we read these signals and generate the lap times:

First we need to check the GPIO pin that the sensor is attached to.
The sensor gives a low value when blocked by a robot, so we are looking for a high to low change on the GPIO pin.

You can use almost any of the GPIO libraries to do this.
We went with pigpio as we used it with our original laser timing rig used for the Pi Wars straight-line speed test.

Once pigpio is setup the basic script looks like this:

import pigpio
import time

### Configuration ###
MIN_LAP = 1.0   # Minimum time difference before registering another crossing
SENSOR = 14     # Set which (BCM) GPIO pins the sensor beam is connected to

# Setup the GPIO
pi = pigpio.pi()
global lastTrip
lastTrip = time.time()

# Callback when the sensor is triggered
def TripSensor(gpio, level, tick):
    global lastTrip
    tripTime = time.time()
    if tripTime > (lastTrip + MIN_LAP):
        # Actual sensor trip
        print 'Lap at %f' % (tripTime)
    else:
        # Duplicate reading
        pass
    lastTrip = tripTime

# Setup the sensor pin
pi.set_mode(SENSOR, pigpio.INPUT)
pi.callback(SENSOR, pigpio.FALLING_EDGE, TripSensor)

# Idle loop, does nothing but keep the script running
while True:
    time.sleep(1.0)

The script works as follows:

  1. Setup the GPIO pin as an input
  2. When the pin has a high to low change (falling edge) call TripSensor
  3. TripSensor checks if the previous call was too recent, if so it is still the robot passing the start line
  4. If the previous call was a while ago it takes a timestamp for the robot reaching the start line

Now we have some start line timestamps, but the full script does a little bit more.
Each time we get a new timestamp this script sends a message down to our camera controlling PC.
This PC is running another script, the one responsible for selecting cameras.


What this script does is to take these timestamps and turn them into lap times.
While waiting for the next one it also counts how long it has been since the latest lap started.
Finally the lap times are drawn over the camera feeds, ready to be passed onto Claire's PC to have the commentary added.

The final result looks like this:

Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre>
  • Syntax highlight code surrounded by the <pre class="brush: lang">...</pre> tags, where lang is one of the following language brushes: bash, cpp, perl, python.
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.