WCPS: Wireless Cyber-Physical Simulator

From Cyber-Physical Systems Laboratory
Jump to navigationJump to search

This page is under construction and thus 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.

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

  • 1. 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]

  • 2 Install Mac Layer Architecture(MLA)

Follow the instructions here: Install MLA.

  • 3 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

  • 4 Install Python

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

  • 5 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]


#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]


#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* msgamr; 
  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("%u %u %u \n", rcmr->data2, 2, rcmr->data1);	
  		}	
  		else{
  			//printf("This is sensor %d\n", TOS_NODE_ID);
  		}
  	}
  	if (TOS_NODE_ID % 500 == 3){
  		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;
  } 
}


  • 5. tossim-call.py

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


from TOSSIM import Tossim
from random import *
from TestNetworkMsg import *
import sys

def main():
    sensor_num_mn=sys.argv[1]
    sensor_data_mn=sys.argv[2]
    cm_code=sys.argv[3]
    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']
t = Tossim([])
r = t.radio()
L_topo=[0, 1, 2, 3, 4];

L=list()
lines=sensor_data
#channel 26
f = open("topo_building_all_channel_26-1000.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]))	
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-1000.txt", "r")
	elif(i_int==2):
		noise_flr = open("Noise-floor-channel26-2-1000.txt", "r")
	elif(i_int==3):
		noise_flr = open("Noise-floor-channel26-3-1000.txt", "r")
	elif(i_int==4):
		noise_flr = open("Noise-floor-channel26-4-1000.txt", "r")
	elif(i_int==0):
		noise_flr = open("Noise-floor-channel26-4-1000.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)
msg = TestNetworkMsg()
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(1)
	pkt = t.newPacket();
	pkt.setData(msg.data)
	pkt.setType(msg.get_amType())
	pkt.setSource(node);
	pkt.setDestination(node)
	tm=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);
	ii=ii+1;
t.addChannel('printf', sys.stdout)
t.addChannel("DataFeedback", sys.stdout)
while (t.time() < 10000000000):
	t.runNextEvent()
#print "Completed simulation at" +str(float(t.time())/10000000000)
  • 6. Make and test

Put all the above files into the same folder, prompt a terminal (or a Cygwin window), and: 1. In the terminal, Make micaz sim and hit return 2. In the terminal ./tossim-call.py

And you are ready to go for the wireless network setup.

Simulink modeling

  • 1. Building model
  • 2. Bridge Model

Application layer code:

* code here

Integrated Simulation with WCPS

WSC Examples

Wireless Building Control

* code here

Wireless Bridge Control

* code here

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 us

  • TinyOS and TOSSIM: Bo Li: boli@seas.wustl.edu
  • Simulink Models: Zhuoxiong Sun: SUN152@purdue.edu