Use the Rasp Pi to log data from bicycles and motorcyles

Programming

The project teams used the Python programming language. From the very beginning, this scripting language has served as a basis for experiments using the Rasp Pi; as a consequence, numerous Python packages are available for controlling GPIO pins and other components.

Listing 1 shows how the potentiometer was read to measure the turning radius. The potentiometer is connected via an ADC to the serial peripheral interface (SPI) bus on the Rasp Pi. By default, the bus is deactivated and should therefore be activated in the Advanced Options settings of raspi-config  [3].

Listing 1

Reading the Potentiometer

#!/usr/bin/python
# Import necessary packages
import spidev
import time
import os
import sys
import RPi.GPIO as GPIO
time.sleep(300)
GPIO.setmode(GPIO.BCM)
# GPIO-Pin 23 = Input
GPIO.setup(23, GPIO.IN)
# Open the SPI Bus
spi = spidev.SpiDev()
spi.open(0,0)
# Define variables
# Delay rate
delay = 0.5
# File path
file = "/boot/daten/messungen/poti.txt"
# Separator
sep = ';'
# Delete file
os.remove(file)
def writeout(data):
  # write to file
  fobj_out = open(file,"a")
  fobj_out.write(str(position))
  fobj_out.write(sep)
  fobj_out.close()
  print position
while True:
  # Read and save sensor data
  data = spi.xfer2([0x00])
  writeout(data)
  time.sleep(delay)
  if GPIO.input(23):
    sys.exit()

The Raspberry Pi GPIO communicates with the individual sensors. Its pins serve as both inputs and outputs. The program queries sensors in an infinite while True loop until recording is stopped. To write the collected data, the teams used a simple data stream that opened the file, wrote the individual variables with a semicolon separator at the end, and then closed the file during each iteration.

A USB-to-TTL adapter cable connected the TX/RX pins of the GPS sensor with a USB port on the Raspberry Pi. The program requested data for measurements in an infinite loop and wrote the measurements to a file with a timestamp. Additional information on combining a Raspberry Pi and a GPS sensor from Adafruit is found on their Ultimate GPS Breakout project page [4].

The Hall sensor method of measuring speed used by the bicycle groups led to two different avenues for achieving the measurement goal. The sensor delivers a positive signal for each rotation of the wheel. This in turn causes an interrupt, which increases a variable by the value of 1.

The program then evaluates the variable every two seconds and converts the data into rotations per minute. These are then written once more to the file. The analysis relies on the fact that the software captures the system time at the beginning and end of the process, thus making it possible to calculate the difference between the two.

The second avenue was to capture the time difference in the system time between two individual measurements and then determine speed using the circumference of the wheel. The number of rotations then provides the clock frequency for the other sensors. This saves memory and does not capture parameters in an idle state. The test ends and measurements stop when the tester presses a button to terminates the Python program with a sys.exit().

The program scans the position of the potentiometer every two seconds in an infinite loop. An ADC processes the potentiometer signals so that the Rasp Pi receives digital data, which is then written to a file. In a subsequent process, a C# program scales the measurement values. This was necessary because the middle position of the potentiometer represents 0 steering movement, so if the potentiometer turns to the right or left, it results in a positive or negative steering value. The program then writes the scaled data with a timestamp to a file. The timestamp makes it possible to synchronize this data with other measurements.

Beginning and End

A variety of methods were considered to start the recording process and other actions, such as capturing the GPS data. It would have been possible to connect the beginning of the measurement period with bootup of the Rasp Pi, provided the corresponding settings had been created in /etc/rc.local. The mini-PC would have then executed the settings when booting, thus starting all the programs that log measurements.

However, it was more convenient to begin the measurement programs by pushing a button. This merely involved writing various commands in a Python script that the Rasp Pi then executed after the button was pressed. The teams chose to end the recording process in the same way. Pressing the button closed a signal circuit.

The individual programs for the sensors queried the button for a positive signal and terminated the program with sys.exit(). The most recent data was placed in the file, which the software then closed properly before the program terminated cleanly. An additional button made sure that the Rasp Pi could be shut down appropriately.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content