Brief Description

The objective of this homework is to explore organizing embedded software using mbed as the platform in context of a simple system. Sensor data flows in the direction of: mbed → PC → Xively.

Embedded Target Design

The mbed code is organized in a single main.cpp file which invokes libraries to read the sensor data. The Accelerometer, Magnetometer, and Touch Sensor libraries were acquired through the mbed website and the Analog Input and Output libraries were included in the mbed SDK.

The software consists of preemptable sensor loop running at a fixed rate, a serial data receive interrupt handler, and a direct-memory access serial transmitter. On each iteration of the sensor loop a time counter is checked to determine which sensor are due for reading. Each sensor function polls data from the sensor, packs the data into an output frame, and then write the frame to the output queue.

Note: the sensor reading was originally performed in a timer interrupt routine, however, it moved to a background task to minimize interference with the reception of serial commands.

Data Logger Design

The data logger is implemented as a Graphical User Interface using Python and the GTK+ Toolkit. The source code is organized around the following classes:

Data Collection

The mbed FRDM-KL46Z is responsible for data collection and sending it to the host via Serial-over-USB. Sensors data is collected at a periodic rate and is transmitted to the PC using the DATA_FRAME format shown below. The Serial data transmit is handled by the KL46Z Direct-Memory Access (DMA) controller so that data transmission does not block the CPU. When sensor data is ready, it is packed and appended to a transmit buffer. At the end of the periodic loop the DMA controller is enabled and data transmission begins

Commands are sent to the mbed using the COMMAND_FRAME format and are processed using a serial interrupt handler. The handler parses the incoming messages and performs command processing when a valid COMMAND_FRAME is received.

Serial Message Formats

As is mentioned above, the mbed and communicate via UART, thus working based on serial protocol. Since the mbed can be perceived as a slave to the PC, we let the mbed collect and send DATA_FRAME, while PC is responsible for generating COMMAND_FRAME. The frame structure is as follows:

Data Frame

1 Byte4 bits 4 bits 1 Byte (Varied Size)1 Byte
HEADERdataTypesnsTypedataNumdata TAIL

Field Descriptions

HEADER:
0x02, constant, indicating the start of a frame
dataType:Data Type:
0- int81 - int162 - int32
3- uint84 - uint165 - uint32
6- float7 - double
snsType: Sensor Type:
  • 0 - LIGHT_SENSOR
  • 1 - ACC_SENSOR
  • 2 - MAG_SENSOR
  • 3 - TOUCH_SENSOR
  • 4 - ADC_SENSOR
dataNum: Data number. Number of data pack in the frame
data: Sensor data. How it is packed is based on dataType and dataNum
TAIL: 0x0A, constant, indicating the end of a frame

Command Frame

1 Byte4 bits 4 bits 4 Byte 1 Byte
HEADERcmdTypesnsTypeIntervalTAIL

Field Descriptions

HEADER: 0x02, constant, indicating the start of a frame
cmdType: Command Type:
  • 0 - START
  • 1 - STOP
  • 2 - SET_INT
Interval:sampling rate. Representing a float of 4 Bytes.
TAIL: 0x0A, constant, indicating the end of a frame

Sampling Rate

The following sampling rates were obtained for each sensor, and a combined sampling rate when all sensors are sampling simultaneously. Data collection was performed using a 230400 serial baud rate.

Sensor Sampling Rate
Accelerometer 560 Hz
Magnetometer 530 Hz
Touch Sensor 1930 Hz
Light Sensor 2414 Hz
Analog to Digital 820 Hz
All Combined 150 Hz

Online Submission

Code for mbed
http://mbed.org/users/andy753421/code/Sensors/
Code for PC
https://sites.google.com/a/g.ucla.edu/ibeaconnav/homework1/python.zip
Video Link
(we will do a demo during office hours)
Screenshots

Controls view

Data view

Plots view

Settings view

Xively upload