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.
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.
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.
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:
1 Byte | 4 bits | 4 bits | 1 Byte | (Varied Size) | 1 Byte |
HEADER | dataType | snsType | dataNum | data | TAIL |
0x02, constant, indicating the start of a frame | ||||||||||
dataType: | Data Type:
| |||||||||
snsType: | Sensor Type:
| |||||||||
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 |
1 Byte | 4 bits | 4 bits | 4 Byte | 1 Byte |
HEADER | cmdType | snsType | Interval | TAIL |
HEADER: | 0x02, constant, indicating the start of a frame |
cmdType: | Command Type:
|
Interval: | sampling rate. Representing a float of 4 Bytes. |
TAIL: | 0x0A, constant, indicating the end of a frame |
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 |
Controls view
Data view
Plots view
Settings view
Xively upload