Download presentation
Presentation is loading. Please wait.
1
Shanghai Jiao Tong University-October 2016
TinyOS 2.1 Yanan Xu Shanghai Jiao Tong University-October 2016 1 1
2
What is TinyOS, NesC, Telosb
TinyOS is an operating system for low power, embedded, wireless device Provide multiple components for user Only install necessary components to chip NesC is a programming language used in TinyOS Define components Wire components Telosb is a typical device we used Provide radio, leds, sensors etc on chip 2 2 2
3
Telosb device Reset Button USB Connector Internal Antenna LEDs 3 3 3
4
Programming Environment
Install VirtualBox Start VirtualBox File -> import Virtual Applicance -> choose the system image Start
5
Programming Environment
This is a virtual machine with Ubuntu OS User name: wcu Pass word: nosecurity Tiny OS programming environment has been set up in this machine
6
How to copy files Connect one USB disk to your computer
Devices-> USB->USB disk
7
How to copy files Connect one USB disk to your computer
Devices-> USB->USB disk USB disk appears
8
Tiny OS Places->computer->Filesystem->opt->tinyos- 2.x
examples document Interfaces and libs
9
Example 1: Apps/Blink Convention used in the TinyOS source tree
Blink is composed of two components: a module, called "BlinkC.nc", and a configuration, called "BlinkAppC.nc". Convention used in the TinyOS source tree File Name File Type Foo.nc Interface Foo.h Header File FooC.nc Public Module FooP.nc Private Module FooAppC.nc Configuration
10
Run Blink Run Blink demo Connect a mote to your computer
VirtualBox: Devices->USB-> Telos
11
Run Blink Run Blink demo Connect a mote to your computer
VirtualBox: Devices->USB-> Telos Open a terminal, Change the shell working directory to /opt/tinyos-2.x/apps/Blink/ Check mote : motelist Install program: make telosb install /dev/ttyUSB0
12
BlinkC.nc: a module BlinkC.nc BlinkAppC.nc
13
BlinkC.nc: a module
14
BlinkC.nc: a module Commands Events
We can call commands provided by existing modules But we need to write codes for the Events function Events Events are triggered by some events, e.g. package arrives, timeout
15
BlinkC.nc: a module What do we need for letting a led light blink several times? Program entrance Led light Timer
16
Component : Module Modules are components that have variables and executable code Modules use interfaces Modules provide interfaces (not shown in the example) Use interface rename Excutable code
17
BlinkAppC.nc: a configuration
Configurations are components that wire (->) other components together Some components system defined, some user defined A.B->C.B means: component A uses interface B, component C provides interface B Component A can call command of interface B, but must implement event of B Interface B implement its commands, provide event (like interrupt) for component who use it A.B->C.B is equivalent to: A.B->C, A->C.B, C<-A.B, C.B<-A B.B is a terrible but correct format. Remember former B is component, latter B is interface
18
Component : configuration
User defined component Configuration wires BlinkC with other components (TimerMilliC, MainC, LedsC) BlinkC.Timer0->Timer0.Timer0 is correct but confusing Terrible rename A.B->C.B BlinkC Leds provides uses Timer0 Boot MainC TimerMilliC (Timer0)m LedsCm
19
Singletons and Generics
Singleton components are unique: they exist in a global namespace Generics are instantiated: each instantiation is a new, independent copy configuration BlinkC { … } implementation { components new TimerC(); components BlinkC; BlinkC.Timer -> TimerC; } 19 19 19
20
Interfaces details Collections of related functions
Define how components connect Interfaces are bi-directional: for A->B Commands are from A to B (implemented by B) Events are from B to A (implemented by A) Can have parameters (types) interface Timer<tag> { command void startOneShot(uint32_t period); command void startPeriodic(uint32_t period); event void fired(); } 20 20 20
21
Implementation of BlinkC.nc
Use Boot Implement event booted Use Timer Call command startPeriodic Implement event fired Use Leds Call command led0Toggle()
22
Example 2: Mote-mote radio communication
create a simple application that increments a counter, displays the counter's three least significant bits on the three LEDs, sends a message with the counter value over the radio.
23
Interfaces Boot: start the device Timer: for timing SplitControl: start the radio AMSend: send a packet Receive: receive a packet Package: store the data Leds: turn on the LEDs or turn off the LEDs
24
Mote-mote radio communication
We implement this application with two steps Implement Blink application A timer changes count variable Turn on or turn off the LEDs according to the count variable Add Radio communication part A mote send packets, and put the value of count in the packets A mote receive packets, get the count value, and turn on or turn off the LEDs according to the count
25
Step 1: Reimplementing Blink
Four files
26
Reimplementing Blink File: BlinkToRadioC.nc #include <Timer.h>
#include "BlinkToRadio.h" module BlinkToRadioC { uses interface Boot; uses interface Leds; uses interface Timer<TMilli> as Timer0; }
27
Reimplementing Blink File: BlinkToRadioC.nc implementation {
uint16_t counter = 0; event void Boot.booted() { call Timer0.startPeriodic(TIMER_PERIOD_MILLI); } event void Timer0.fired() { counter++; call Leds.set(counter);
28
Reimplementing Blink File: BlinkToRadioAppC.nc
#include <Timer.h> #include "BlinkToRadio.h" configuration BlinkToRadioAppC { } implementation { components MainC; components LedsC; components BlinkToRadioC as App; components new TimerMilliC() as Timer0; App.Boot -> MainC; App.Leds -> LedsC; App.Timer0 -> Timer0;
29
Reimplementing Blink File: BlinkToRadio.h #ifndef BLINKTORADIO_H
#define BLINKTORADIO_H enum { TIMER_PERIOD_MILLI = 250 }; #endif
30
Reimplementing Blink File: Makefile COMPONENT=BlinkToRadioAppC
Make telosb install /dev/ttyUSB0 Result: telosb will set the LEDs to the counter COMPONENT=BlinkToRadioAppC include $(MAKERULES)
31
Step 2: add radio communication
Procedure of Sending a message Start device Control.start() Check if start success Define a packet, get the pointer of its PayLoad Packet.getPayload(&packet, user_message_size) Fill the payload Send the packet AMSend.send(destination, &packet, size) Procedure of receiving a message Receive packets with Receive.receive event Get the payload of the packet Set the LEDs to the counter number just received with Leds.set
32
Add Radio communication part
File: BlinkToRadio.h Comment out and add ... enum { AM_BLINKTORADIO = 6, TIMER_PERIOD_MILLI = 250 };
33
Add Radio communication part
File: BlinkToRadioC.nc add module BlinkToRadioC { ... uses interface Packet; uses interface AMSend; uses interface SplitControl as AMControl; uses interface Receive; }
34
Add Radio communication part
File: BlinkToRadioC.nc add implementation { bool busy = FALSE; message_t pkt; ... }
35
Add Radio communication part
File: BlinkToRadioC.nc Comment out and add event void Boot.booted() { //call Timer0.startPeriodic(TIMER_PERIOD_MILLI); call AMControl.start(); }
36
Add Radio communication part
File: BlinkToRadioC.nc Comment out and add event void AMControl.startDone(error_t err) { if (err == SUCCESS) { call Timer0.startPeriodic(TIMER_PERIOD_MILLI); } else { call AMControl.start(); event void AMControl.stopDone(error_t err) {}
37
Add Radio communication part
File: BlinkToRadioC.nc Comment out and add event void Timer0.fired() { counter++; //call Leds.set(counter); if (!busy) { BlinkToRadioMsg* btrpkt = (BlinkToRadioMsg*)(call Packet.getPayload(&pkt, sizeof (BlinkToRadioMsg))); btrpkt->nodeid = TOS_NODE_ID; btrpkt->counter = counter; if (call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(BlinkToRadioMsg)) == SUCCESS) { busy = TRUE; }
38
Add Radio communication part
File: BlinkToRadioC.nc Comment out and add event void AMSend.sendDone(message_t* msg, error_t error) { if (&pkt == msg) { busy = FALSE; }
39
Add Radio communication part
File: BlinkToRadioC.nc Comment out and add event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len) { if (len == sizeof(BlinkToRadioMsg)) { BlinkToRadioMsg* btrpkt = (BlinkToRadioMsg*)payload; call Leds.set(btrpkt->counter); } return msg;
40
Add Radio communication part
File: BlinkToRadioAppC.nc Comment out and add implementation { ... components ActiveMessageC; components new AMSenderC(AM_BLINKTORADIO); components new AMReceiverC(AM_BLINKTORADIO); }
41
Add Radio communication part
File: BlinkToRadioAppC.nc Comment out and add implementation { ... App.Packet -> AMSenderC; App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Receive -> AMReceiverC; }
42
Test the application! $ motelist $ make telosb install, 1 /dev/ttyUSB0
$ make telosb reinstall, 2 /dev/ttyUSB0
43
Assignment 2 Mote-mote radio communication
One mote as master node, others as slave node Show how many motes communicate with the master node Use the LEDs to show the number of motes
44
Grading scheme Sensor nodes can communicate with each other. 20%
When a node connect to the master node, LEDs should be set to old number plus one. 40% When a node is shut down, LEDs should be set to old number minus one. 40% Code quality. 10%
45
Submission Face to face demo Codes Report Deadline November 18, Friday
46
Reference wiki/index.php/TinyOS_Tutorials /papers/ai-tinyos.pdf
47
THE END
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.