WCPS: Wireless Cyber-Physical Simulator

From Cyber-Physical Systems Laboratory
Revision as of 20:18, 7 February 2013 by Boli (talk | contribs)
Jump to navigationJump to search

This page is under construction and incomplete, please contact boli@seas.wustl.edu before using materials below.

End-user's Tutorial on using WCPS: Wireless Cyber-Physical Simulator

Normal User Tutorial on the Wireless Cyber-Physical Simulator(WCPS) WCPS is design for, but not limited to, realistic Wireless Structural Control simulations. The layered infrastructure and efficient integration of state-of-the-art control and wireless networking tools, i.e., Simulink and TOSSIM, have made WCPS an ideal choice for general wireless control simulations with Simulink and TOSSIM.

As an exemplary example of Cyber-Physical Systems that perform close-loop control using real-time sensor data collected through wireless sensor networks, Wireless Structural Control (WSC) systems can play a crucial role in protecting civil infrastructure in the events of earth quakes and other natural disasters. Existing WSC research usually employ wireless sensors installed on small lab structures, which cannot capture realistic delays and data loss in wireless sensor networks deployed on large civil structures and their impacts on structural control. The lack of realistic studies and tools that capture both the cyber (wireless) and physical (structures) aspects of WSC systems represent a hurdle for cyber-physical systems research for civil infrastructure.

WCPS advances the state of the art of WSC and Cyber-physical System through the following contributions. First, it for the first time presents an integrated environment that combines realistic simulations of both wireless sensor networks and structures. WCPS integrates Simulink and TOSSIM, a state-of-the-art sensor network simulator featuring a realistic wireless model seeded by signal traces. Second, release of WCPS in comprised of two realistic case studies each matching a structural model with wireless traces collected from real-world environments. The building study combines a benchmark building model and wireless traces collected from a multi-story building. The bridge study combines the structural model of the Cape Girardeau bridge over the Mississippi River and wireless traces collected from a similar bridge (the Jindo Bridge) in Korea. These case studies shed lights on the challenges of WSC and the limitations of a traditional structural controller under realistic wireless conditions.

The following tutorial introduces in detail how to configure general MATLAB, TinyOS, and PYTHON environments, as well as the WCPS framework. The tutorial herein is an end-user version specifically for end-users that do not do much development but instead trying to do wireless control simulations with Simulink, TOSSIM and WCPS. An advanced tutorial on in-depth TinyOS development (e.g., routing protoocls, MAC layer development) with WCPS can be found [here].

Software Environment Setup

Software Requirements

Install TinyOS

WCPS is implemented and tested on MacOS X (snowleopard), Windows XP, and Windows 7. Current release of WCPS is under TinyOS 2.1.1, which can be installed following the three methods.

  • 1. Follow the TinyOS official tutorial on installation of TinyOS for your specific platform: Link
  • 2. Directly download the pre-tested TinyOS 2.1.1 image from here: [under construction Cygwin] [under construction Mac OS X]

Install Mac Layer Architecture(MLA)

Install MATLAB and Simulink

If you already have MATLAB MATLAB 7.11.0.584 (2010b) or later version, skip this step. Otherwise, follow the tutorial here: install MATLAB

Install Python

If you already have Python 2.7.2 or later version installed, skip this step. Otherwise, follow the manual here: install Python

Environment Setup Testing

Building a Wireless Network in TOSSIM

  • 1. Makefile

"Makefile" takes advantage of the fact that it's not necessary to recompile all the project files that has not been changed. To have the "Makefile" for our project, copy the code below into a txt file and save as "Makefile" without any suffix, or simply download it here: [Makefile]

# target file
COMPONENT=TestNetworkAppC
# dependencies of the target
include $(UPMA_DIR)/Makefile.include
BUILD_EXTRA_DEPS = TestNetworkMsg.py
CFLAGS += -DTOSH_DATA_LENGTH=156
CFLAGS += -I$(TOSDIR)/lib/T2Hack
PFLAGS += -I../../../pure-tdma
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -DFOOTER_SIZE=0 -DTDMA -DUPMA
#TDMA mac layer
UPMA_MAC = pure-tdma
#mig information for python
TestNetworkMsg.py: TestNetwork.h
	mig python -target=$(PLATFORM) $(CFLAGS) -python-classname=TestNetworkMsg TestNetwork.h TestNetworkMsg -o $@
CLEAN_EXTRA = TestNetworkMsg.py TestNetworkMsg.class TestNetworkMsg.java TestNetworkMsg.pyc
include $(MAKERULES)
migclean:
	rm -rf $(MIGFILES)
  • 2. TestNetwork.h

"TestNetwork.h" defines necessary message structures for the wireless communication. Copy the code below into a txt file and save as "TestNetwork.h", or simply download it here: [TestNetwork.h]

#ifndef TEST_NETWORK_C_H
#define TEST_NETWORK_C_H
enum {
 AM_TESTNETWORKMSG = 0x05,
 AM_TESTNETWORKMSGG = 0x06,
 SAMPLE_RATE_KEY = 0x1,
 CL_TEST = 0xee,
 TEST_NETWORK_QUEUE_SIZE = 8,
};
#endif
  • 3. TestNetworkAppC.nc

"TestNetworkAppC.nc" connects claimed application interfaces to interfaces that are defined in the hardware librare. Copy the code below into a txt file and save as "TestNetworkAppC.nc", or simply download it here: [TestNetworkAppC.nc]


/**
 * Testnework for TDMA simulation in Tossim. 
 * @author Bo Li
 * @version $Revision: 0.1 $ $Date: 2011/03/25 21:53:58 $
 */
#include "TestNetwork.h"

configuration TestNetworkAppC {
}
implementation {
  components TestNetworkC, MainC, ActiveMessageC;
  components new AMSenderC(AM_TESTNETWORKMSG) as Sender;  
  components new AMReceiverC(AM_TESTNETWORKMSG) as Receiver;  
  components new AMReceiverC(AM_TESTNETWORKMSG) as AMReceiver;
  components new TimerMilliC() as InjectionTimer;
  components SerialActiveMessageC as SerialAM;
  
  TestNetworkC.RadioControl -> SerialAM;
  TestNetworkC.SerialReceive -> SerialAM.Receive[AM_TESTNETWORKMSG];
  TestNetworkC.SerialSend -> SerialAM.AMSend[AM_TESTNETWORKMSG];
  
  TestNetworkC.Boot -> MainC;
  TestNetworkC.RadioControl -> ActiveMessageC;
  TestNetworkC.Send -> Sender;  
  TestNetworkC.Receive -> Receiver;
  TestNetworkC.AMReceive -> AMReceiver;
  TestNetworkC.InjectionTimer -> InjectionTimer;
}

  • 4. TestNetworkC.nc

"TestNetworkC.nc" Implements send/receive functionality of a wireless node. Copy the code below into a txt file and save as "TestNetworkC.nc", or simply download it here: [TestNetworkC.nc]


/**
 * Testnework for TDMA simulation in Tossim. 
 * @author Bo Li
 * @version $Revision: 0.1 $ $Date: 2011/03/25 21:53:58 $
 */
#include <Timer.h>
#include "TestNetwork.h"
module TestNetworkC {
  uses interface Boot;
  uses interface SplitControl as RadioControl;
  uses interface StdControl as RoutingControl;
  
  uses interface AMSend as Send;
  uses interface Receive;
  uses interface Receive as AMReceive;
  uses interface Timer<TMilli> as InjectionTimer;
  
  uses interface Receive as SerialReceive;
  uses interface AMSend as SerialSend;
}
implementation {
  message_t packet;
  TestNetworkMsg* rcm;
  TestNetworkMsg* rcmr;
  TestNetworkMsg* msgamr; 
  TestNetworkMsg tnmsg_in_transit; 
  uint8_t msglen;
  enum {
    RECEIVER=0,
  };
  event void Boot.booted() {
    call RadioControl.start();
  }
  event void RadioControl.startDone(error_t err) {
    if (err != SUCCESS) {
      call RadioControl.start();
    }
    else {
    }
  }
  void failedSend() {
    dbg("App", "%s: Send failed.\n", __FUNCTION__);
  }
  event void Send.sendDone(message_t* m, error_t err) {
    if (err != SUCCESS) {
    }
    dbg("TestNetworkC", "Send completed.\n");
  }
  uint8_t prevSeq = 0;
  uint8_t firstMsg = 0;
  event message_t* 
  Receive.receive(message_t* msg, void* payload, uint8_t len) {
  	rcmr = (TestNetworkMsg*)payload; 
  	if (TOS_NODE_ID % 500 == 0){
  		if (rcmr->data1==1)
  		{	
  			//printf("This is sensor %d\n", TOS_NODE_ID);
	  		printf("%u %u %u \n", rcmr->data2, 2, rcmr->data1);	
  		}	
  		else{
  			//printf("This is sensor %d\n", TOS_NODE_ID);
  			printf("%u %u %u \n", rcmr->data2, 1, rcmr->data1);
  		}
  	}
  	if (TOS_NODE_ID % 500 == 3){
	  	//printf("This is sensor %d\n", TOS_NODE_ID);
  		call Send.send(0, rcm, sizeof(TestNetworkMsg));
  	}
  	return msg;
 }
 event message_t* AMReceive.receive(message_t* bufPtr, void* payload, uint8_t len) {	
 	rcm = (TestNetworkMsg*)payload;
    msgamr = (TestNetworkMsg*)call Send.getPayload(&packet, sizeof(TestNetworkMsg));
    if (msgamr == NULL) {
    	return;
    }
    memcpy(msgamr, rcm, sizeof(TestNetworkMsg));
    if (len != sizeof(TestNetworkMsg)) {
    	return bufPtr;
    }
  	if (TOS_NODE_ID!=0){
	  	if (TOS_NODE_ID  % 500 == 1){
  		 	if (call Send.send(3, &packet, sizeof(TestNetworkMsg)) != SUCCESS) {
  	 			failedSend();
  	 		}else {
      		}
    	}else if (TOS_NODE_ID  % 500 !=0 && TOS_NODE_ID  % 500 != 1){
    		if (call Send.send(0, &packet, sizeof(TestNetworkMsg)) != SUCCESS) {
      			failedSend();
      		}else {
      		}
    	}
  	}  	
  	 return bufPtr;
  }
  event void InjectionTimer.fired(){}
  event void RadioControl.stopDone(error_t err) {}
  void sendMessage() {}
  event void SerialSend.sendDone(message_t* m, error_t err) {}
  event message_t* SerialReceive.receive(message_t* msg, void* payload, uint8_t len) {
  	return msg;
  } 
}

Simulink Modeling in WCPS

General simulink modeling

Structural models in WCPS

Example

Application layer code:

* navigation
** mainpage|mainpage
** Special:Recentchanges|Recent changes

Integrated Simulation with WCPS

WSC Examples with WCPS

Wireless Building Control

Application layer code:

* navigation
** mainpage|mainpage
** Special:Recentchanges|Recent changes

Wireless Bridge Control

Application layer code:

* navigation
** mainpage|mainpage
** Special:Recentchanges|Recent changes
  • 5. tossim_config-tdma-fast.py

"tossim_config-tdma-fast.py" configures TOSSIM network and does packet injection into the Tossim network. Copy the code below into a txt file and save as "TestNetworkC.nc", or simply download it here: [TestNetworkC.nc]


from TOSSIM import *
#from tinyos.tossim.TossimApp import *
from TossimApp import *
from random import *
from TestNetworkMsg import *
import sys

def main():
    sensor_num_mn=sys.argv[1]
    sensor_data_mn=sys.argv[2]
    topo_jc=sys.argv[3]
    topo_hc=sys.argv[4]
    cm_code=sys.argv[5]
    return {'y0':sensor_num_mn,'y1':sensor_data_mn,'y2':cm_code}

sensor_num=int(main()['y0'])
sensor_data=main()['y1']
cmmd_code=main()['y2']

n = NescApp("TestNetwork", "app.xml")
#t = Tossim(n.variables.variables())
t = Tossim([])
r = t.radio()

L_topo=[0, 1, 2, 3, 4];
#L_topo=[0, 7, 8, 9, 10];

L=list()  
lines=sensor_data
comma_eliminated_lines=lines.split(",")
for line in comma_eliminated_lines:
	if (line!=""):
		if (line!=","):
			L.append(line)
len_L=len(L)

# this is topology for channel 26
f = open("topo_building_all_channel_26-1000.txt", "r")
# this is topology for channel 18 
#f = open("topo_building_all_channel_18.txt", "r");

lines = f.readlines()

for line in lines:
	s = line.split()
	if (len(s) > 0):
		r.add(int(s[0]), int(s[1]), float(s[2]))
if True:
	boot_count=0;
	for i in L_topo:
		i_int=int(i)
		m = t.getNode(i_int);
		if (i_int==1):
			noise_flr = open("Noise-floor-channel26-1.txt", "r")
			#noise_flr = open("Noise-floor-channel18-1.txt", "r")
		elif(i_int==2):
			noise_flr = open("Noise-floor-channel26-2.txt", "r")
			#noise_flr = open("Noise-floor-channel18-2.txt", "r")
		elif(i_int==3):
			noise_flr = open("Noise-floor-channel26-3.txt", "r")
			#noise_flr = open("Noise-floor-channel18-3.txt", "r")
		elif(i_int==4):
			noise_flr = open("Noise-floor-channel26-4.txt", "r")
			#noise_flr = open("Noise-floor-channel18-4.txt", "r")
		elif(i_int==0):
			noise_flr = open("Noise-floor-channel26-4.txt", "r")
			#noise_flr = open("Noise-floor-channel18-4.txt", "r")
		lines_noise = noise_flr.readlines()
		for line_noise in lines_noise:
			strrr = line_noise.strip()
			if (strrr != ""):
				val = int(strrr)
				m = t.getNode(i_int);
				m.addNoiseTraceReading(val)
		m.createNoiseModel();
		m.turnOn()
		m.bootAtTime(0)
		#print "Booting ", i_int, " at time ", str(0)
		boot_count=boot_count+1;

msg = TestNetworkMsg()
msg_count=0;


injection_round=2;

for ir in range(1, injection_round):
	ii=1;
	for i in range(4, 0, -1):
		#for k in range(1,round_remainder/sensor_num+1):
		#	mycode = "msg.set_data"+str(k)+"("+str(L[(i-1)*(round_remainder/sensor_num)+k-1])+")"
		#	print mycode
		#	exec mycode
		node=i
		msg.set_data1(node)
		msg.set_data2(ir)
		pkt = t.newPacket();
		pkt.setData(msg.data)
		pkt.setType(msg.get_amType())
		pkt.setSource(node);
		pkt.setDestination(node)
		tm=ir*2000000000+(ii)*100000000
		pkt.deliver(node, t.time()+tm)
		#print "At injectTime: "+str(float((float)(t.time()+tm)/10000000000.0))+" we inject packet to "+str(node);
		msg_count=msg_count+1;
		ii=ii+1;

t.addChannel('printf', sys.stdout)
#print "Starting simulation."
#t.addChannel("AM", sys.stdout)
#t.addChannel("TreeRouting", sys.stdout)
#t.addChannel("TestNetworkC", sys.stdout)
#t.addChannel("Route", sys.stdout)
#t.addChannel("PointerBug", sys.stdout)
#t.addChannel("QueueC", sys.stdout)
#t.addChannel("Gain", sys.stdout)
#t.addChannel("Forwarder", sys.stdout)
#t.addChannel("TestNetworkC", sys.stdout)
t.addChannel("DataFeedback", sys.stdout)
#t.addChannel("Error", sys.stdout)
#t.addChannel("App", sys.stdout)
#t.addChannel("Traffic", sys.stdout)
#t.addChannel("Acks", sys.stdout)
#t.addChannel("SNRLoss", sys.stdout)
#ticksPerSecond=100,0000,0000

while (t.time() < 100000000000*5):
	t.runNextEvent()
#print "Completed simulation at" +str(float(t.time())/10000000000)


References

  • B. Li, Z. Sun, K. Mechitov, G. Hackmann, C. Lu, S. Dyke, G. Agha and B. Spencer, "Realistic Case Studies of Wireless Structural Control," ACM/IEEE International Conference on Cyber-Physical Systems (ICCPS'13), April 2013.
  • Z. Sun, B. Li, S.J. Dyke and C. Lu, "Evaluation of Performances of Structural Control Benchmark Problem with Time Delays from Wireless Sensor Network," Joint Conference of the Engineering Mechanics Institute and ASCE Joint Specialty Conference on Probabilistic Mechanics and Structural Reliability (EMI/PMC'12), June 2012.
  • H. Lee, A. Cerpa, and P. Levis. Improving wireless simulation through noise modeling. In IPSN, 2007.
  • P. Levis, N. Lee, M. Welsh, and D. Culler. Tossim: Accurate and scalable simulation of entire tinyos applications. In Sensys, 2003.

Contact the Authors:

Bo Li: boli@seas.wustl.edu