Architectures and Applications for Wireless Sensor Networks (204525) Sensor Node Programming Chaiporn Jaikaeo Department of Computer.

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Advertisements

Microprocessors Typical microprocessor controlled devices: Camera, mobile phone, stereo, mp3 player, electronic toys… High-level microprocessor controlled.
Lab7: Introduction to Arduino
Chung-Ta King National Tsing Hua University
Feb 2007WSN Training: First Steps in nesC Programming1 First Steps in TinyOS and nesC Programming Topics  Timer Application: MyApp  Application directory.
1 Lab 3 Objectives  Case study: “Hello world” program on motes  Write you first program on mote.
Architectures and Applications for Wireless Sensor Networks ( ) Node Programming Models Chaiporn Jaikaeo Department of Computer.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 3: Input/output and co-processors dr.ir. A.C. Verschueren.
 2005 Pearson Education, Inc. All rights reserved Introduction.
7/13/2007AIIT Summer Course - D#1 Wireless Embedded Systems and Networking Lab Day 5: Part 1: TinyOS Programming on Open Source Distribution Jaein Jeong.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Copyright Arshi Khan1 System Programming Instructor Arshi Khan.
Dr. Hoganson CSIS HC11 Demo Program This is our first lab using the 68HC11 microcontroller. We will “talk” to the microcontroller from a PC, run.
Embedded Programming and Robotics
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Part 1 Using the ARM board And start working with C Tutorial 5 and 6
The 6713 DSP Starter Kit (DSK) is a low-cost platform which lets customers evaluate and develop applications for the Texas Instruments C67X DSP family.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Department of Electronic & Electrical Engineering Embedded system Aims: Introduction to: Hardware. Software Ideas for projects ? Robotics/Control/Sensors.
Chapter Introduction to Computers and Programming 1.
By: R Jayampathi Sampath
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Architectures and Applications for Wireless Sensor Networks ( ) Sensor Node Programming II (UART and Radio) Chaiporn Jaikaeo
FINAL MPX DELIVERABLE Due when you schedule your interview and presentation.
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
Architecture Support for OS CSCI 444/544 Operating Systems Fall 2008.
Arduino. What is it? A open-source software suite and single-board microcontroller. Allows easy and affordable prototyping of microcontroller applications.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
(More) Interfacing concepts. Introduction Overview of I/O operations Programmed I/O – Standard I/O – Memory Mapped I/O Device synchronization Readings:
L ILY P AD T RAINING C ENTENNIAL E LEMENTARY 2012 Material by Linz Craig Revision by Sarah Bloms Additional images by Modkit & Adam Meyer.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
Lab 13 Department of Computer Science and Information Engineering National Taiwan University Lab13 – Interrupt + Timer 2014/12/23 1 /16.
Slide 1 Project 1 Task 2 T&N3311 PJ1 Information & Communications Technology HD in Telecommunications and Networking Task 2 Briefing The Design of a Computer.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Architectures and Applications for Wireless Sensor Networks ( ) Sensor Network Programming and MoteLib Simulator Chaiporn Jaikaeo
Network Kernel Architectures and Implementation ( ) Node Programming Chaiporn Jaikaeo Department of Computer Engineering Kasetsart.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Implementation of Embedded OS Lab3 Porting μC/OS-II.
Microcontrollers, Microcomputers, and Microprocessors
بسم الله الرحمن الرحيم MEMORY AND I/O.
Casne.ncl.ac.uk Taking care of the CrumbleBot Please do NOT stress the robot's motors 1.Do NOT push the robot 2.Do NOT hold the.
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club.
Embedded C- Language Lets Learn fundamentals !!. An Embedded system is combination of computer hardware and software, and perhaps additional mechanical.
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin Yi Kyaw
Programming and Debugging with the Dragon and JTAG Many thanks to Dr. James Hawthorne for evaluating the Dragon system and providing the core content for.
Software Architecture of Sensors. Hardware - Sensor Nodes Sensing: sensor --a transducer that converts a physical, chemical, or biological parameter into.
Introduction to Operating Systems Concepts
Having fun with code, using Arduino in a middle school CS classroom
Lets Learn fundamentals !!
INTRODUCTION TO ROBOTICS Part 5: Programming
Programming and Debugging with the Dragon and JTAG
Fundamentals of Computer Engineering
An Arduino Workshop A Microcontroller.
Microprocessor Systems Design I
Arduino and Design of Embedded Applications
Roller Coaster Design Project
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
CSCI1600: Embedded and Real Time Software
Lecture Topics: 11/1 General Operating System Concepts Processes
Node Programming Models
EECE.3170 Microprocessor Systems Design I
Aeroponic Engineering and Vertical Farming
Introduction to Arduino
Presentation transcript:

Architectures and Applications for Wireless Sensor Networks (204525) Sensor Node Programming Chaiporn Jaikaeo Department of Computer Engineering Kasetsart University

2 Outline Microcontroller programming Microcontroller programming  Software development cycle IWING's MoteLib IWING's MoteLib

3 IWING-MRF Mote Radio transceiver 8-bit AVR Microcontroller USB Connector (for reprogramming and power) Analog/Digital sensor connectors External battery connector UART connectors Morakot Saravanee, Chaiporn Jaikaeo, Intelligent Wireless Network Group (IWING), KU

4 Microcontroller flash memory BSL Typical Development Process For microcontrollers with bootstrap loader (BSL) installed For microcontrollers with bootstrap loader (BSL) installed Source code (C/Asm) Cross Compiler/Assembler Machine code Serial/USB

5 Build Simple App Let's build a simple application Let's build a simple application How to know whether our program is running? How to know whether our program is running?  Make mote output something  What can be used as output?

6 IWING-MRF Schematic Available on course's homepage Available on course's homepage

7 IWING-MRF – Blinking LED Task: turn a LED on and off repeatedly Task: turn a LED on and off repeatedly Idea Idea  Configure Port D's Pin 5 (PD5) for output  Repeatedly set the pin logic level to 0 and 1  Add some delay before toggling pin level

8 IWING-MRF C Code – blink.c How to add delay? How to add delay? Can the code be made shorter? Can the code be made shorter? #include main() { DDRD |= (1 << 5); // Make PD5 output while (1) { // Set pin logic to low PORTD &= ~(1 << 5); // Add some delay // Set pin logic to high PORTD |= (1 << 5); }

9 Compiling Make an ELF binary by running cross compiler Make an ELF binary by running cross compiler Note: blink.elf is not a Windows or Linux executable! Translate the object file into ihex format Translate the object file into ihex format $ avr-gcc -mmcu=atmega328p –o blink.elf blink.c $ avr-objcopy -j.text -j.data –O ihex blink.elf blink.hex

10 Flashing Code Into Mote Plug mote into a USB port Plug mote into a USB port Activate boot-loader Activate boot-loader  Press and release RESET while holding USER/B.L. Make sure it is recognized by your PC Make sure it is recognized by your PC Invoke chip programmer Invoke chip programmer $ avrdude -p atmega328p -c usbasp -U flash:w:blink.hex $ lsusb Bus 003 Device 049: ID 16c0:05dc Bus 001 Device 003: ID 046d:c03d Logitech, Inc.

11 IWING-MRF's Boot Loader

12 Creating Makefile To compile To compile To download program to flash (will compile if needed) To download program to flash (will compile if needed) make make flash Tab character

13 Programming Exercise Modify blink.c so that Modify blink.c so that  Under normal condition, LEDs blink in the sequence Red  Yellow  Green  When the is pressed (and held), only Green will blink  When the user button is pressed (and held), only Green will blink

14 IWING's MoteLib Software Hardware Morakot Saravanee, Patra Poome, Chaiporn Jaikaeo, Intelligent Wireless Network Group (IWING), KU

15 Hardware Abstraction IWING-MRF Hardware IWING-MRF API Implementation

16 Mote and Network Emulator Virtual Mote

17 Programming Model MoteLib provides event-based programming environment MoteLib provides event-based programming environment Idle loop Radio event handlerSensor event handlerTimer event handler Boot event handler Handled by MoteLib Handled by developer

18 Example Turn red LED on and off repeatedly every 500 ms Turn red LED on and off repeatedly every 500 ms #include Timer t; void fired() { ledToggle(0); } void boot() { timerCreate(&t); timerStart(&t, TIMER_PERIODIC, 500, fired); }

19 Example: Creating Makefile # Platform to build the code for PLATFORM = iwing-mrf # Required target without extension TARGET = blink # Include MoteLib's main make rules include $(MOTELIB_DIR)/Makerules

20 Example: Build and Flash App Build your application Build your application Program the mote with Program the mote with make make flash

21 MoteLib API Residing in $(MOTELIB_DIR)/include Residing in $(MOTELIB_DIR)/include  motelib/system.h  motelib/led.h  motelib/timer.h  motelib/button.h  motelib/sensor.h  motelib/actor.h  motelib/radio.h  motelib/uart.h Complete API documentation can be found here Complete API documentation can be found here 

22 System API ( motelib/system.h ) Provides boot() function signature Provides boot() function signature Provides various function declarations for node ID and network ID inquiry Provides various function declarations for node ID and network ID inquiry Should be included in every MoteLib application Should be included in every MoteLib application

23 LED API ( motelib/led.h ) Turn LED#2 on Turn LED#2 on Turn LED#1 off Turn LED#1 off Toggle LED#0 Toggle LED#0 Use LEDs to display binary value Use LEDs to display binary value ledSet(2,1); ledSet(1,0); ledToggle(0); ledSetValue(5);

24 Timer API ( motelib/timer.h ) Define and initialize a timer Define and initialize a timer Start the timer with 1-second timeout; trigger only once; call function fired when triggered Start the timer with 1-second timeout; trigger only once; call function fired when triggered Start the timer with 1-second timeout; trigger periodically Start the timer with 1-second timeout; trigger periodically Timer t; timerCreate(&t); timerStart(&t, TIMER_ONESHOT, 1000, fired); timerStart(&t, TIMER_PERIODIC, 1000, fired);

25 Timer API (cont'd) Defining callback Defining callback void fired(Timer *t) { // do something }

26 Button API ( motelib/button.h ) Set handler to monitor button event Set handler to monitor button event  Usually called in boot() Handler example Handler example buttonSetHandler(handler); void handler(ButtonStatus s) { if (s == BUTTON_PRESSED) // do something if (s == BUTTON_RELEASED) // do something }

27 Programming Practice button-count.c button-count.c  Counts how many times the USER button has been pressed  Then shows the number (only 3 LSBs) on the LEDs  Count to 7 and wrap around to 0

28 Sensor API ( motelib/sensor.h ) Read digital input from input#0 Read digital input from input#0 Request analog reading (asynchronous) from input#3 Request analog reading (asynchronous) from input#3 uint16_t x = sensorReadDigital(SENSOR_0); sensorRequestAnalog(SENSOR_3, dataReady); : void dataReady(uint16_t value) { // value stores sensor reading }

29 Actor API ( motelib/actor.h ) Activate output #2 (set logic to High) Activate output #2 (set logic to High) Deactivate output #3 (set logic to Low) Deactivate output #3 (set logic to Low) actorSetState(ACTOR_2,1); actorSetState(ACTOR_3,0);

30 Measures light and temperature Measures light and temperature Sensor Board PC4 (SENSOR_4) PC2 (SENSOR_2) PC0 (SENSOR_0)

31 Sensor Board Schematic

32 Sensor Reading Procedure Step 1: Turn on sensor power (PC0) Step 1: Turn on sensor power (PC0) Step 2: Request analog reading Step 2: Request analog reading Step 3: Wait until value is available Step 3: Wait until value is available Step 4: Turn off sensor power Step 4: Turn off sensor power

33 Example: sense-light.c Every 100 ms, measure light and display the value on LEDs Every 100 ms, measure light and display the value on LEDs  Light value is in range 0 – 1023  Need to scale down to 0 – 7

34 Example #include Timer t; void readDone(uint16_t value); void readLight(Timer *t); void boot() { timerCreate(&t); timerStart(&t, TIMER_PERIODIC, 100, readLight); } void readLight(Timer *t) { actorSetState(ACTOR_0, 1); sensorRequestAnalog(SENSOR_1, readDone); } void readDone(uint16_t value) { ledSetValue(value/128); actorSetState(ACTOR_0, 0); }

35 Programming Practice Modify sense-light.c so that light is sampled 4 times in each measurement Modify sense-light.c so that light is sampled 4 times in each measurement  Average value is displayed on LEDs

36 Creating a Reading Task Event-based code can be difficult to read and maintain Event-based code can be difficult to read and maintain Idea Idea  Make a reading task that runs forever  Other tasks can also be added to run concurrently Start timer Wait until timer expired Create timer Turn on sensors Request reading Wait until data ready Complete 4 samples? Compute and display average Turn off sensors

37 Synchronous Operations MoteLib provides various checks to support synchronous operation MoteLib provides various checks to support synchronous operation E.g., E.g.,  timerExpired(t)  Determines whether timer t has already expired  Only works for one-shot timer  sensorAnalogWaiting(s)  Returns true if the system still waits for sensor s  sensorAnalogResult(s)  Returns the most recent value of sensor s

38 First Attempt #include Timer t; void readLightTask(); void boot() { readLightTask(); } void readLightTask() { uint8_t i; uint16_t sum = 0; timerCreate(&t); while (1) { timerStart(&t, TIMER_ONESHOT, 100, NULL); while (!timerExpired(&t)) ; actorSetState(ACTOR_0, 1); for (i = 0; i < 4; i++) { sensorRequestAnalog(SENSOR_1, NULL); while (sensorAnalogWaiting(SENSOR_1)) ; sum += sensorAnalogResult(SENSOR_1); } ledSetValue(sum/4/128); actorSetState(ACTOR_0, 0); } Will this work?

39 Emulating Concurrency Previous example wouldn't work because of the blocking while-loop Previous example wouldn't work because of the blocking while-loop  Other parts of the system will be unresponsive Must return to MoteLib inside of the while- loops Must return to MoteLib inside of the while- loops During MoteLib's idle loop, keep jumping into the while-loops During MoteLib's idle loop, keep jumping into the while-loops

40 Programming Model MoteLib's Idle loop Task 2 Event handler2 Task 1 Event handler1 Handled by MoteLib Handled by developer call return call return continue yield yield continue

41 Implementation Details How to ask MoteLib to call our tasks? How to ask MoteLib to call our tasks?  MoteLib provides setLoopRoutine(), allowing a function to be called every idle loop How to have a task yield and correctly come back to where it left? How to have a task yield and correctly come back to where it left? void myroutine() { // something to be executed continuously } void boot() { : setLoopRoutine(myroutine); }

42 Duff's Device Invented to optimize data transfer by means of loop unwinding Invented to optimize data transfer by means of loop unwinding Switch cases are used like GOTO labels Switch cases are used like GOTO labels do { *to = *from++; } while(--count > 0); register n = (count + 7) / 8; switch(count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while(--n > 0); }

43 Protothreads Invented by Adam Dunkels and Oliver Schmidt Invented by Adam Dunkels and Oliver Schmidt  Used in the Contiki OS Provides light-weight mechanism for concurrent programming using standard C macros and switch-case statements Provides light-weight mechanism for concurrent programming using standard C macros and switch-case statements Heavily inspired by Duff's Device and Simon Tatham's Coroutines in C Heavily inspired by Duff's Device and Simon Tatham's Coroutines in CCoroutines in CCoroutines in C See See 

44 Revised sense-light.c ////////////////////////////// PT_THREAD(readLightTask(struct pt *pt)) { static uint8_t i; static uint16_t sum = 0; PT_BEGIN(pt); timerCreate(&t); while (1) { timerStart(&t, TIMER_ONESHOT, 100, NULL); PT_WAIT_UNTIL(pt, timerExpired(&t)); actorSetState(ACTOR_0, 1); for (i = 0; i < 4; i++) { sensorRequestAnalog(SENSOR_1, NULL); PT_WAIT_WHILE(pt, sensorAnalogWaiting(SENSOR_1)); sum += sensorAnalogResult(SENSOR_1); } ledSetValue(sum/4/128); actorSetState(ACTOR_0, 0); } PT_END(pt); } #include struct pt readLight_pt; PT_THREAD(readLightTask(struct pt *pt)); Timer t; ////////////////////////////// void scheduleTasks() { readLightTask(&readLight_pt); } ////////////////////////////// void boot() { setLoopRoutine(scheduleTasks); }

45 Protothreads Limitations Local variables must be manually preserved Local variables must be manually preserved  Local variables are created on stack  They are destroyed when function returns  So they should be stored in an explicit state object  Or declared static, if reentrancy is not required switch-case statements are not allowed switch-case statements are not allowed Cannot take advantage of multi-processing Cannot take advantage of multi-processing