Hardware-in-the-Loop Simulation

Hardware-in-the-Loop Simulation

EE199 Directed Research Progress Report
Annie I-An Chen
Faculty Advisor: Paulo Tabuada
Graduate Advisor: Manuel Mazo Jr.
UCLA Department of Electrical Engineering
2009/1 - 2009/7

  1. Introduction
  2. System Overview
  3. TinyOS Applications for the Mote
  4. C Applications and MEX-files on the Computer
  5. Simulation

1. Introduction

The objective of this project is to explore how the performance of control systems is affected by closing the control loops over wireless networks.

This portion of the project, developed throughout six months of undergraduate research, focuses on the implementation of hardware-in-the-loop simulation, in which the plant is modeled and simulated in MATLAB Simulink while the communication is implemented over a real network consisting of several Mica motes.


2. System Overview

The following Simulink model depicts a control system with a feedback loop closed over Mica motes with wireless communication:

Simulink model of an exemplary system

The sensor and actuator blocks call MEX functions to control Mica motes. Corresponding TinyOS programs are installed on the respective motes.

Tha author uses the following hardware/software:

  • Operating System: Mac OS X
  • Programming Board: Crossbow MIB520
  • Mote: Crossbow MPR2400 (MICAz)
  • MATLAB version: R2008a

3. TinyOS Applications for the Mote

Background Knowledge

TinyOS Installation
Installing TinyOS 2.0.2 : Official guideline
Installing TinyOS on Windows Vista (pdf) : The author's troubleshooting notes for installing TinyOS with Cygwin on Windows Vista. However, due to further compatiblity issues with lcc, MATLAB's linker on Windows for MEx-files, the author recommends using Linux-based machines for this project.
TinyOS Programming
TinyOS Programming by Philip Levis (pdf) : Explains fundamental concepts of TinyOS and nesC programming.
TinyOS Tutorials : Step-by-step tutorials that give instructions on building and using specific applications. 1.3 Mote-mote radio communication and 1.4 Mote-PC serial communication and SerialForwarder are particularly helpful for this project.
TinyOS Data Communication Protocol
message_t : Describes the TinyOS 2.x message buffer.
Serial Communication : Explains the TinyOS 2.x serial communication system for mote-to-PC data exchange.
Packet Protocols : Documents the interfaces used by packet protocol components in TinyOS 2.x.

TinyOS Applications

This application can be found under /tinyos-2.x/apps/. It forwards packets between the UART and the radio. We are particularly interested in its ability to receive packets from the computer via the serial port and pass them to the radio.
ActuatorC.nc, ActuatorP.nc, Makefile
This application stores the packet received most recently via the radio. It sends that packet to the serial port upon receiving a packet with a payload of length 1 (4 bytes) containing the number 126 from the serial port.

4. C Applications and MEX-files on the Computer

Background Knowledge

C Application Compilation
(1) In a terminal window, change the directory to where the source file is:
e.g. $ cd /tinyos-2.x/support/sdk/c
(2) Compile the source file. The author uses gcc as the compiler.
e.g. $ gcc -o sf sf.c sfsource.c
MEX-file Programming
MEX-files Guide : Official documentation
MEX-file Compilation
(1) Choose the compiler by typing the following in MATLAB command window:
>> mex help
The author uses gcc as the compiler.
(2) Compile the source code by typing the following in MATLAB command window:
>> mex [sourcefile].c [library].c
where [sourcefile] is the name of the source code and [library] is the library it includes. If it includes more than one library, separate each with a single space.
MEX-file Execution
(1) Change the directory to where the MEX file is.
e.g. >> cd /tinyos-2.x/support/sdk/c
(2) Call the MEX-file as a function.
e.g. >> queryMEX('localhost', 9002)

C Applications and MEX-files

There are several useful C applications in /tinyos-2.x/support/sdk/c/. The three applications pertaining to this project are: serialsend.c, sf.c, sflisten.c
A simple way to find out the name of the serial port to which the mote is connected is to list out the contents of the dev directory:
$ ls /dev
serialsendMEX.c : MEX-file version of serialsend.c used in MATLAB. Sends out one packet via serial communication to the connected mote.
e.g. >> serialsendMEX('/dev/cu.usbserial-XBQ9U3L0B', 57600, [2 3 4])
sends out the double array [2 3 4] to the mote via serial port /dev/cu.usbserial-XBQ9U3L0B at baud rate 57600Hz.
sf.c : Serial forwarder provided in /tinyos-2.x/support/sdk/c/. Compile and run this C application to set up a serial forwarder in the command terminal.
e.g. $ sf 9002 /dev/cu.usbserial-XBQ9TF5AB 57600
sets up port 9002 of the serial forwarder to correspond to serial port /dev/cu.usbserial-XBQ9TF5AB.
qrMEX.c : Modified MEX-file version of sfsend.c and sflisten.c combined, but only sends out the query packet that contains the number 126, and then listens for one packet returned by the mote, both via the serial forwarder.
e.g. >> Z = qrMEX('localhost', 9002)
returns the packet payload received from the mote as a double matrix named Z.
The following MEX-files and C applications were written to aid the developement of the final MEX-files used in the project:
queryMEX.c : Sends the query packet (packet with a single number 126 as the payload) to a mote via the serial forwarder.
receiveMEX.c : Modified MEX-file version of sflisten.c. Listens for and returns one packet from a mote via the serial forwarder.
querypacket.c : C version of queryMEX.c.
receivepacket.c : C version of receiveMEX.c.

5. Simulation

The applications above are the keys to constructing blocks representing the sensor and the actuator in MATLAB Simulink. The sensor and actuator blocks are both Embedded MATLAB Function blocks that call the MEX-files functions serialsendMEX and qrMEX, respectively. Their functions were tested together in a simple system and proved to be working accordingly.

sensor.mdl : Simulink block of the sensor.
actuator.mdl : Simulink block of the actuator.
whole.mdl : Simple system that tests both blocks, as shown below:



TinyOS 2.0.2 related files
All source files mentioned above
Sample Simulink models

Last Updated: 2009/9/2. Questions or comments? Please email Annie I-An Chen