Skip to content

Raspberry Pi

rv456 edited this page Apr 16, 2018 · 10 revisions

This page will give an overview of the software written for the Raspberry Pi within the BowlsEye system. However, full documentation and source code is available here.

Bluetooth


The Bluetooth communication between the Pi and the GUI on the remote computer has been implemented using the Bluez C library. As there is two-way communication between the Pi and the computer, both a server and a client are needed. To implement this with minimal disruption to the main distance measurement routine, a Bluetooth 'listening' thread is used. This allows the user parameters sent from the GUI to be received with minimal latency, and follows a provider consumer design pattern. The user parameters a sent in a 'one shot' from the GUI, meaning that the received data is an array of 4, comma separated, strings (scan start trigger, sample rate, step size, maximum distance). These strings are converted to integers and stored in the global user parameters buffer/array. To then use these parameters, the structures that access them contain a pointer member, which points to the address of the desired parameter in the user parameter array. These structures can then access the user parameter values by de-referencing this pointer member. This allows for an efficient parameter updating as the modules which use them only access them when they require, and will naturally find the most recently updated values.

To send the calculated distance measurement and corresponding angle to the GUI, a client is used. The two values are converted to a single string, which is interpreted within the GUI code.

ADC


The most time critical aspect of the BowlsEye system is the sampling of the reflected ultrasonic wave from the bowls. The chosen ADC has a maximum sample rate of 1Msps, however this clock must be provided by SPI SCK, in our case generated by the Raspberry Pi. Due to the fact that each sample transfer is 16-bits long (even though the ADC has a 12-bit resolution, the data transfers are 16-bit, see datasheet, to reach the maximum sampling rate the Pi must provide an 8MHz - 16MHz clock to achieve the range of sampling capabilities of the ADC. Using the pigpio library allows the Pi to achieve this clock rate, however it was found that performing single samples at a time resulted in highly variable sample timing - I.E. inconsistent CS behaviour, which triggers the conversion. Thus, due to the real time constraint on the system, it was decided to instead use a single transfer for the full maximum distance resolution buffer size. This results in the CS being low throughout the conversion, and the accurate clock from the SPI SCK line providing a reliable sample rate.

Transducer Excitation


As explained the the Ultrasound page of this wiki, the timing and length of the transducer excitation is highly important if the BowlsEye system is to achieve an accurate measurement. This was also one of the main challenges of the project as generating a short, 40kHz square wave as the excitation signal proved troublesome when combating with an operating system, as is the case with the Raspberry Pi. After experimenting with timer callbacks, threads and bit banging, we again found the solution to this issue using the aforementioned pigpio library. To achieve the short, 8 pulse 40kHz excitation signal, the gpioWave methods within the library are utilised. This allowed us to dictate the on duration, off duration and number of pulses within the excitation signal. The resultant excitation signal is shown below.

Alternative Text

Stepper Motor


The stepper motor used in the BowlsEye system is a 4 wire bipolar type. That is, it features internal coils, which must be activated in different combinations to turn the motor a single step. See below for a good illustration of the inner workings of a stepper motor (source).
StepperMotor

The [StepperMotor_t](link to source) was designed to handle the communication between the Pi and L298 H bridge motor driver, which in turn enables the coils within the stepper motor.

Clone this wiki locally