TinyOS入门共16页文档
入门-tinyos编程思想
入门-tinyos编程思想除非特别说明,所有的程序都是基于tinyos-1.3的,并在VM 模拟Win xp 32位下用cygwin调试运行。
思想:Tinyos使用的是一种特殊的,面向组件的编程思想,这种思想和用惯了面向对象的编程思想的我来说,非常不习惯。
Tinyos和nesC为什么难学?因为他没有正常的教科书,不像C, C++,Java 一样有专门的书或者资料,告诉你需要实现什么功能,要去用到什么类(这里是组件),要去用什么接口,要去用什么函数。
Tinyos完全没有这些东西,什么都是让你自己去摸索和实践。
所以你有太多的不习惯:1. 你不习惯NesC程序的基本组成是Component,每个Component是一个*.nc文件。
这里不像Java,大家都知道java当中的类是有继承关系,有子类超类等等。
你只需要知道一些简单的类,就可以自己扩展学习,即使有没见过的类,也可以找到很详细的介绍和使用方法。
NesC中没有类的概念,自然也就没有子类超类,没有继承,也就是说,如果你知道有这么一个Component,你就可以用这个Component 可以完成一定的工作。
但是如果你不知道这个Component呢,尤其是某些底层的Component 那么嘿嘿~~!!要么你像我一样,去逼自己看懂系统提供那些的Component,要么自己写一个。
可问题你现在连最简单的blink 程序都看不懂,更何况自己写了。
所以每个用NesC的初学者都和瞎子差不多,说白了,你就是什么都不会的。
你的C还有java经验都根本没用。
当然,你会if 还有for语句,这是你在学习tinyos中唯一比买菜大妈强的地方。
2. 你不习惯NesC程序的中Component的使用方法。
NesC程序中采用了一种所谓的wiring的机制来指明component与component之间的关系。
3. 你不习惯NesC程序的中组件的层次结构,也就是接口,方法,属性,函数的关系。
tinyOS操作系统整理
前言 (3)编程小贴士 (3)链接和命名空间 (6)3.1 C (6)3.2 C++ (9)3.3 Java (11)3.4 组件与接口 (11)3.5 Why? (13)接口和模块 (15)4.1 Split Phase (15)4.2带参数的接口 (16)4.3模块的实现 (17)4.4 Tasks(任务) (19)4.5 Concurrency并发 (23)4.6 Allocation (31)Configurations and Wiring (35)5.0.1 The as keyword and other namespace tricks (39)5.1 Pass Through Wiring (41)5.1.1 Multiple Wirings, Fan-in, and Fan-out 多串联,扇入,扇出。
(42)5.2 Combine Functions (44)Parameterized Wiring (46)6.1 Defaults (50)6.2 unique() and uniqueCount() (51)第一章前言这本书介绍了如何在TinyOS 2.0(T2)下编程。
它比之前的指导性的文章更为深入些,而且还涉及到一些额外的主题,例如radio stacks的结构及其实现和一些现存的TinyOS 库。
这本书关注于如何编写nesC代码,以及解释那些nesC和TinyOS设计模式背后的概念和原理。
如果你想很快速的了解如何进行TinyOS编程,你可以先阅读指导(tutorials).如果你对TinyOS的某个子系统很感兴趣,你可以参阅TEPs(TinyOS Enhancement Proposals),它详细阐述了相应的设计,接口和组件。
这些都可以在TinyOS的发布目录下找到。
虽然本书的一些内容仍然适用于TinyOS 1.x的版本,但1.x的版本与T2版本的不同还是会导致不同编程实践。
关于TinyOS编程的详细说明说明书
Programmable transistor potentiometer allows variable Built-in antenna provides 1-4 feet transmission rangeLight Sensor 51-pin-connector Temperature Se nsorAccelerometerSerial Port ConnectionParallel Port ConnectionProcessor/Radio Board51-Pins ConnectorsWhat is TinyOS?w Small, open source, energy efficient operating system designed by UC Berkeley w It was designed specifically for networked sensorsw Event-based systemEach system module is designed to operate bycontinually responding to incoming eventsMore TinyOS stuffw Two-level schedulingEvent handlers deal with hardware interrupts Tasksl Execution mechanism provided for long runningcomputationsl Runs to completion in backgroundl Can be interrupted by low-level system eventsl Task scheduling performed by simple FIFO queueMore TinyOS stuffw ComponentsAllows application developers to combineindependent components into an applicationspecific configurationA specialized language, NESC, was developed toexpress a component graph and the command/event interfaces between componentsThe TinyOS web sitew /tos/index.html DownloadSupportPublicationsHardware DesignsRelated WorkThe TinyOS download & install /tos/download.htmlw Installation packages available for:Windows 2000/XPl Choice of Installshield or Manual installationLinuxw Installation includes other software (Cygwin, JDK, avr tools, etc.)The TinyOS directoryw TinyOS subdirectories:appscontribdoctoolstosSpecification of aTinyOS programw A nesC application consists of one ormore components linked together to form an executablew A component provides and uses interfaces. These interfaces are the only point of access to the component and are bi-directionalw An interface declares a set of functions called commands that the interface provider must implement and another set of functions called events that the interface user must implementSpecification of aTinyOS programw For a component to call the commands in an interface, it must implement the events of that interfacew A single component may use or provide multiple interfaces and multiple instances of the same interfaceImplementation of aTinyOS programw There are two types of components in nesC: modules and configurationsw Modules provide application code, implementing one or more interfacew Configurations are used to assemble other components together, connecting interfaces used by components to interfaces provided by others. This is called wiringImplementation of aTinyOS programw Every nesC application is described by a top-level configuration that wires together the components insidew nesC uses the filename extension ".nc" for all source files --interfaces, modules, and configurationsWhat is NesC?w Network Embedded Systems Cw C-like syntax supporting TinyOSw Reference manual online:/papers/nesc-ref.pdfTinyOS Tutorialsw Provide sample programs with explanation of code w Tutorials included with TinyOS installationw Most up-to-date version of tutorials are online: /tos/tinyos-1.x/doc/tutorial/index.htmlCompiling and uploadingw To compile a TinyOS application, you must first enter the directory of the appw Type: make <platform>w Valid platforms include: mica, mica2, mica2dot w To compile and install: make <platform> install w To assign ID #: make <platform> install.X where X is the node ID (0, 1, 2, …)More compiling and uploading w To install a program that has already been built (no rebuild of target),type: make reinstall[.X] <platform>w To compile a program for use with the TOSSIM simulator, type: make pcw Note: For more information about the TOSSIM simulator, see Lesson 5 in the tutorial:/tos/tinyos-1.x/doc/tutorial/lesson5.htmlExample #1: Blinkw Lesson 1 in the tutorial:/tos/tinyos-1.x/doc/tutorial/lesson1.htmlExample #2: Sensingw Lesson 2 in the tutorial:/tos/tinyos-1.x/doc/tutorial/lesson2.htmlExample #3: Using the radio w Lesson 4 in the tutorial:/tos/tinyos-1.x/doc/tutorial/lesson4.htmlWhat should I do next?1.Download and install TinyOS2.Read the first four tutorialsw Implement the tutorials (schedule TBA)3.Read Chapter 1 of the WSN textbook4.Read “System Architecture Directions forNetworked Sensors”What should I do next? 5.Think about project ideas –use yourimagination//news/article.html?item_id=285/dataman/ActiveTape/Tree/Technologies we haven’t tried yet:Mica2Dot motesTASKNetwork reprogrammingSimulatorsOther helpful linksw Crossbow: Spec sheets and prices for Berkeley motes and othersensor network productsw Search the TinyOS mailing list archives: /tos/search.html。
无线传感器网络操作系统TinyOS
现有的WSN操作系统
• 当前已有多个有代表性的开源的无线 传感器网络操作系统:
Tiny OS 2.1:美国加州大学伯克利分校开发 Mantis OS 0.9.5 (Multimodal Networks of Insitu Sensors) :美国克罗拉多大学开发 SOS 1.7:美国加州大学洛杉矶分校开发
– 命令(command):接口的提供者必须实现它们; – 事件(event):接口的使用者必须实现它们 。
19
组件,接口,命令和事件
• 接口的特点:
Provides未必一定有组件使用,但uses一定要有人提供,否则编 译会提示出错。在动态组件配置语言中uses也可以动态配置。 接口可以连接多个同样的接口,叫做多扇入/扇出。 一个module可以同时提供一组相同的接口,又称参数化接口,表 明该Module可提供多份同类资源,能够同时给多个组件分享。
13
TinyOS的技术特点-3
分阶段作业(Split-Phase Operations)
TinyOS没有提供任何阻塞操作,为了让一个耗时较长的操 作尽快完成,一般来说都是将对这个操作的请求和这个操 作的完成分开来实现,以便获得较高的执行效率。
主动消息通信(Active Message)
每一个消息都维护一个应用层的处理程序。当节点收到消 息后,就会把消息中的数据作为参数,传递给应用层的处 理程序,由其完成消息数据的解析、计算处理或发送响应 消息等工作。
9
TinyOS的设计理念
• 由于WSN的特殊性,研究人员在设计TinyOS系统 时就提出以下几个原则:
– 1)能在有限的资源上运行:要求执行模式允许在单一 的协议栈上运行; – 2)允许高度的并发性:要求执行模式能对事件作出快 速的直接响应; – 3)适应硬件升级:要求组件和执行模式能够应对硬件/ 软件的替换; – 4)支持多样化的应用程序:要求能够根据实际需要, 裁减操作系统的服务; – 5)鲁棒性强:要求通过组件间有限的交互渠道,就能 应对各种复杂情况; – 6)支持一系列平台:要求操作系统的服务具有可移植 性。 10
TinyOS 2.x 入门教程
TinyOS 2.x 编程入门教程Version 1.02010/12/17目录前言 (1)第1章编程环境的建立 (2)1.1安装J AVA 1.6JDK (2)1.2安装CWGWIN(WINDOW ONLY) (2)1.3安装编译器 (3)1.4安装T INY OS工具 (3)1.5设置环境变量 (4)1.6安装G RAPHVIZ (4)1.7检测软件安装情况 (4)1.8USB串口驱动下载及安装 (5)第2章TINYOS简介 (7)2.1NES C语言简介 (7)2.1.1组件(components) (7)2.1.2并发模型 (11)2.2常用MAKE命令简介 (12)2.3应用举例:B LINK ---T INY OS编程的“H ELLO W ORLD”程序 (12)2.3.1Blink配件 (13)2.3.2BlinkC模块 (14)2.3.3事件evenst和命令commands (16)2.3.4编译Blink应用程序 (17)2.3.5安装Blink程序到Telosb节点并运行 (17)2.4下载程序出错故障排除 (18)2.5小结 (20)第3章TELOSB硬件平台常用可编程器件和对应的TINYOS组件介绍 (21)3.1L ED (22)3.1.1Telosb硬件平台上的Led器件展示 (22)3.1.2TinyOS中Led编程组件介绍 (22)3.2传感器 (23)3.2.1Telosb硬件平台上的传感器展示 (23)3.2.2TinyOS中传感器编程组件介绍 (24)3.3串口 (25)3.3.1Telosb硬件平台上的串口展示 (25)3.3.2TinyOS中串口编程组件介绍 (26)3.4R ADIO (26)3.4.1Telosb硬件平台上的Radio相关器件展示 (26)3.4.2TinyOS中Radio编程组件介绍 (27)3.5小结 (27)第4章用事件驱动方式从传感器获取数据 (27)4.1模块实现 (28)4.2ADC (30)第5章TINYOS任务及应用举例 (32)5.1任务的创建和调度 (32)5.2举例:S ENSE T ASK应用程序 (33)5.3小结 (33)第6章TINYOS串口编程 (34)6.1T EST S ERIAL应用程序 (34)6.2串口助手:PC上显示从串口读取的数据 (38)6.3T ELOSB从串口读取数据 (39)6.4O SCILLOSCOPE应用程序——数据感知 (41)6.5B ASE S TATION应用程序 (45)6.6小结 (51)附录1 TINYOS命名约定 (52)附录2 NESC语言特有关键字 (54)前言随着传感器技术、微机电系统、现代网络、无线通信、低功耗等技术的飞速发展,推动了无线传感器网络 (WSN:wireless sensor network) 的产生和发展。
Lesson 5.1 tinyos 教程
Lesson 5.1 SensingThis lesson introduces sensor data acquisition in TinyOS. It demonstrates two sensor applications: a simple application called Sense that periodically takes sensor readings and displays the values on the LEDs. And a more sophisticated application called Oscilloscope where nodes periodically broadcast their sensor readings to a basestation node. Using the Mote-PC serial communication described in the previous lesson the basestation forwards the sensor readings to the PC, where they are visualized with a dedicated graphical user interface.IntroductionSensing is an integral part of sensor network applications. In TinyOS 1.x sensing was syntactically connected with analog-to-digital converters (ADCs): TinyOS 1.x applications such as Oscilloscope or Sense used the ADC and ADCControl interfaces to collect sensor data. When new platforms appeared with sensors that were read out via the serial interface, not only did additional interfaces like ADCError have to be introduced, but it became clear that equating a sensor with an ADC was not always the appropriate thing to do. Usually sensing involves two tasks: configuring a sensor (and/or the hardware module it is attached to, for example the ADC or SPI bus) and reading the sensor data. The first task is tricky, because a sensing application like, for example, Sense is meant to run on any TinyOS platform. How can Sense know the configuration details (like ADC input channel, the required reference voltage, etc.) of an attached sensor? It can't, because the configuration details of sensors will be different from platform to platform. Unless Sense knows about all sensors on all platforms it will be unable to perform the configuration task. However, the second task - reading the sensor data - can be solved so that the Sense application can collect sensor data even though it is agnostic to the platform it is running on. In TinyOS 2.0 platform independent sensing applications such as Oscilloscope, Sense or RadioSenseToLeds do not use configuration interfaces like ADCControlanymore; instead they use the standard data acquisition interfaces Read, ReadStream or ReadNow for collecting sensor data. All configuration details are hidden from the application and this is why you can compile Sense and display sensor data on the telosb or the micaz platform, even though the actual sensors and their connection to the rest of the system may be completely different. This raises questions like the following: Since the Sense application component only uses standard data acquisition interfaces who is in charge ofdefining which sensor it samples? If the Sense application component is not configuring the sensor, then who is responsible for that? How can an applications like Sense display sensor data when they do not know the details about sensorconfiguration? This includes questions like "what is the value range of the sensor data" or "is a temperature reading to be interpreted in degrees Celsius or Fahrenheit"? Let's assume there are several sensors on a platform: what steps have to be taken to let the Sense orOscilloscope application display data from a different sensor? After reading this tutorial you should be able to answer these questions. Using the Sense and Oscilloscope application as an example, the following sections explain how the data acquisition interfaces are used, how the configuration procedure works and, as an example, how Sense can be hooked up to sensors other than the default one on thetelosb platform.The Sense applicationSense is a simple sensing demo application. It periodically samples the default sensor and displays the bottom bits of the readings on the LEDs. The Senseapplication can be found in tinyos-2.x/apps/Sense. Let's first look at the SenseAppC.nc configuration:configuration SenseAppC { } implementation { components SenseC, MainC, LedsC, new TimerMilliC(); components new DemoSensorC() as Sensor;SenseC.Boot -> MainC; SenseC.Leds -> LedsC; SenseC.Timer -> TimerMilliC; SenseC.Read -> Sensor; }The SenseAppC configuration looks similar to the BlinkAppC configuration described in lesson 1 (if you have not done so, read the sections on the Blink application in lesson 1). To understand the wiring let's look at the signature of the SenseC.nc module:module SenseC { uses { interface Boot; interface Leds;interface Timer<TMilli>; interface Read<uint16_t>; } }Like the BlinkC.nc module the SenseC.nc module uses the interfaces Boot, Leds and Timer<TMilli>. Additionally, it uses the Read<uint16_t> interface. The sequence of actions in the SenseC.nc implementation is as follows: SenseC.nc uses the Boot interface to start a periodic timer after the system has been initialized. Every time the timer expires SenseC.nc is signalled a timer event and reads data via the Read<uint16_t> interface. Reading data is a split-phase operation, it is divided in a command Read.read() and an event Read.readDone(). Thus every time the timer expires SenseC.nc calls Read.read() and waits for the Read.readDone() event. When data is signalled in the Read.readDone() event SenseC.nc displays it on the leds: the least significant bit is displayed on LED 0 (0 = off, 1 = on), the second least significant bit is displayed on LED 1 and so on. The Read interface (in tinyos-2.x/tos/interfaces) can be used to read a single piece of sensor data, let's look at it in detail:interface Read<val_t> { /** * Initiates a read of the value. * * @return SUCCESS if a readDone() event will eventually come back. */ command error_t read();/** * Signals the completion of the read(). * * @param result SUCCESS if the read() was successful * @param val the value that has been read*/ event void readDone( error_t result, val_t val ); }If you are not familiar with generic interfaces you will wonder what the meaning of <val_t> (in the first line) is and why the signature of SenseC.nc is using Read<uint16_t>. What you see above is a generic interface definition, because the Read interface takes a type parameter. Generic interfaces are explained in the nesC Language Reference Manual (version 1.2 and above). For now it is enough to know that generic interfaces have at least one type parameter and two components can be wired together only if they provide/use the interface with the same types (note that the readDone event passes a parameter of the <val_t> parameter, which is a placeholder for the actual data type). This means that since SenseC.nc is using the uint16_t variant of theRead interface, it can only be wired to a component that provides the Read<uint16_t> interface and thus SenseC.nc expects to read 16 bit unsigned integer sensor data. If you tried to wire SenseC.nc to a component that provides, for example, a Read<uint8_t> interface you would get an error from the nesC compiler. Recall that the wiring is defined in the SenseAppC.nc configuration. Let's again take a look at which component SenseC.nc is wired to using theRead<uint16_t> interface in the SenseAppC configuration. The interesting lines arecomponents new DemoSensorC() as Sensor;andSenseC.Read -> Sensor;This means that the generic DemoSensorC component provides the Read<uint16_t> interface to SenseC.nc It is important to understand that the SenseC.nc module has no way of telling which sensor it is connected to; in fact it cannot even tell whether it is getting data from a sensor at all, because it can be wired to any component that provides a Read<uint16_t> interface. On a platform without any built-in sensors (like micaz) and no attached sensorboard the DemoSensorC component could simply return constant values. The last sentence hints that theDemoSensorCcomponent is different for every platform: therefore you will not find DemoSensorC.nc in the TinyOS libraries. Instead, a different DemoSensorC.nc has to be written for every platform, i.e. the DemoSensorC.nc implementation for telosb will be different than the DemoSensorC.nc implementation for micaz. This is the answer to the first question asked in the introduction section: the platform dependent DemoSensorC component defines which sensor the Sense or Oscilloscopeapplication is sampling and every platform that wants to run sensing applications such as Oscilloscope, Sense or RadioSenseToLeds has to provide its own version of DemoSensorC. Additionally, sensor boards may come with their own version of DemoSensorC (e.g., the basicsb sensorboard for the mica-family of motes define DemoSensorC.nc to be that board's light sensor).The DemoSensorC componentLet's take a closer look at the DemoSensorC component. Every DemoSensorC component has the following signature:generic configuration DemoSensorC() { provides interface Read<uint16_t>; }In its implementation section, however, DemoSensorC may differ from platform to platform. For example, on the telosb platform DemoSensorC instantiates a component called VoltageC, which reads data from the MCU-internal voltage sensor. Because the micaz doesn't have any built-in sensors its DemoSensorC uses system library component like ConstantSensorC or SineSensorC, which return "fake" sensor data. Thus DemoSensorC is a means of indirecting sensor data acquisition from a platform-specific sensor component (like VoltageC) to platform-independent applications like Sense or Oscilloscope. Usually the configuration of a sensor is done in the component that DemoSensorC instantiates. How can Sense be changed to sample a sensor other than the platform's default sensor? Usually this requires changing only a single line of code inDemoSensorC; for example, if you wanted to replace the VoltageC component on telosb by the constant sensor component ConstantSensorC you could change the following line in DemoSensorC from:components new VoltageC() as DemoSensor;to something likecomponents new ConstantSensorC(uint16_t, 0xbeef) as DemoSensor;What sensors are available depends on the platform. Sensor components are usually located in the respective platform subdirectory (tinyos-2.x/tos/platforms), in the respective sensorboard subdirectory(tinyos-2.x/tos/sensorboards) or, in case of microprocessor-internal sensors, in the respective chips subdirectory (tinyos-2.x/tos/chips). ConstantSensorC and SineSensorC can be found in tinyos-2.x/tos/system.Running the Sense applicationTo compile the Sense application, go to the apps/Sense directory and depending on which hardware you have, type something similar to make telosb install. If you get errors such as the following,SenseAppC.nc:50: component DemoSensorC not found SenseAppC.nc:50: component `DemoSensorC' is not generic SenseAppC.nc:55: no matchyour platform has not yet implemented the DemoSensorC component. For a quick solution you can copy DemoSensorC.nc from tinyos-2.x/tos/platforms/micaz to your platform directory (a good starting point on how to createsensor components is probably TEP 101 and TEP 114). If you have a mica-family mote and a "basic" (mda100) sensor board, you can get a more interesting test by compiling with SENSORBOARD=basicsb make platform installto run Sense using the mda100's light sensor. Once you have installed the application the three least significant bits of the sensor readings are displayed on the node's LEDs (0 = off, 1 = on). It is the least significant bits, because Sense cannot know the precision (value range) of the returned sensor readings and, for example, the three most significant bits in a uint16_t sensor reading sampled through a 12-bit ADC would be meaningless (unless the value was left-shifted). If your DemoSensorC represents a sensor whose readings are fluctuating you may see the LEDs toggle, otherwise Sense is not very impressive. Let's take a look at a more interesting application: Oscilloscope.The Oscilloscope applicationOscilloscope is an application that let's you visualize sensor readings on the PC. Every node that has Oscilloscope installed periodically samples the default sensor via ( DemoSensorC) and broadcasts a message with 10 accumulated readings over the radio. A node running the BaseStation application will forward these messages to the PC using the serial communication. To run Oscilloscope you therefore need at least two nodes: one node attached to your PC running the BaseStation application (BaseStation can be found at tinyos-2.x/apps/BaseStation and was introduced in the previous lesson) and one or more nodes running the Oscilloscope application. Let's take a look at the OscilloscopeAppC.nc configuration:configuration OscilloscopeAppC { } implementation { components OscilloscopeC, MainC, ActiveMessageC, LedsC, new TimerMilliC(), new DemoSensorC() as Sensor, new AMSenderC(AM_OSCILLOSCOPE), new AMReceiverC(AM_OSCILLOSCOPE);OscilloscopeC.Boot -> MainC;OscilloscopeC.RadioControl -> ActiveMessageC; OscilloscopeC.AMSend -> AMSenderC; OscilloscopeC.Receive -> AMReceiverC; OscilloscopeC.Timer -> TimerMilliC; OscilloscopeC.Read -> Sensor; OscilloscopeC.Leds -> LedsC; }The actual implementation of the application is in OscilloscopeC.nc. This is the signature of OscilloscopeC.nc:module OscilloscopeC { uses { interface Boot; interface SplitControl as RadioControl; interface AMSend; interface Receive; interface Timer; interface Read; interface Leds; } }Oscilloscope is a combination of different building blocks introduced in previous parts of the tutorial. Like Sense, Oscilloscope uses DemoSensorC and a timer to periodically sample the default sensor of a platform. When it has gathered 10 sensor readings OscilloscopeC puts them into a message and broadcasts that message via the AMSend interface. OscilloscopeC uses the Receive interface for synchronization purposes (see below) and the SplitControl interface, to switch the radio on. If you want to know more about mote-mote radio communication read lesson 3.Running the Oscilloscope applicationTo install the Oscilloscope application go to tinyos-2.x/apps/Oscilloscope and depending on which hardware you have, type something similar to make telosb install,1. Note the ",1" after the install option, which assigns ID 1 to the node. Assigning IDs to nodes is helpful to differentiate them later on in the GUI, so make sure you assign different IDs to all nodes on which Oscilloscope is installed (e.g. install Oscilloscope on a second node with make telosb install,2 and so on). A node running Oscilloscope will toggle its second LED for every message it has sent and it will toggle its third LED when it has received an Oscilloscope message from another node: incoming messages are used for sequence number synchronization to let nodes catch up when they are switched on later than the others; they are also used for changing the sample rate that defines how often sensor values are read. In case of a problem with the radio connection the first LED will toggle. Install BaseStation on another node and connect it to your PC. As usual, on the BaseStation node you should see the second LED toggle for every message bridged from radio to serial.Running the Java GUITo visualize the sensor readings on your PC first go to tinyos-2.x/apps/Oscilloscope/java and type make. This creates/compiles the necessary message classes and the Oscilloscope Java GUI. Now start a SerialForwarder and make sure it connects to the node on which you have installed the BaseStation application (how this is done is explained in the previous lesson). In case you have problems with the Java compilation or the serial connection work through the previous lesson. Once you have a SerialForwarder running you can start the GUI by typing ./run (in tinyos-2.x/apps/Oscilloscope/java). You should see a window similar to the one below:Each node is represented by a line of different color (you can change the color by clicking on it in the mote table). The x-axis is the packet counter number and the y-axis is the sensor reading. To change the sample rate edit the number in the "sample rate" input box. When you press enter, a message containing the new rate is created and broadcast via the BaseStation node to all nodes in the network. You can clear all received data on the graphical display by clicking on the "clear data" button. The Oscilloscope (or Sense) application displays the raw data as signalled by the Read.readDone() event. How the values are to be interpreted is out of scope of the application, but the GUI let's you adapt the visible portion of the y-axis to a plausible range (at the bottom right).。
精品课件-TinyOS操作系统开发技术及实践-第2章
3
第2章 nesC语言基础
2.2 nesC和C的比较 按照TinyOS官方说法,nesC来源于C,是C语言的一种变种, 所有的C语言中的结构体、函数、甚至指针(建议少使用指针)、数 据类型以及注释格式在nesC中依然是合法的,对于C程序员来说, nesC提出了三个“新概念”: 组件:一组可重用的代码和数据组合,类似于C++中的类。 接口:一组可为其他组件服务的函数和事件集合,是组件之 间交互的界面。 并发执行模型:基于任务和中断的处理机制,定义了组件之 间如何调用,以及中断代码和非中断代码如何交互执行规则。 nesC与C语言的区别主要有程序组成主体、模块之间的调用、命名 空间、编程思想等几方面,下面将对这几个方面进行分析。
第2章 nesC语言基础 第2章 nesC语言基础
2.1 nesC概述 2.2 nesC和C的比较 2.3 nesC程序结构 2.4 接口 2.5 组件 2.6 nesC高级编程 2.7 并发模型 2.8 常用接口和组件 2.9 可视化组件关系图
1
第2章 nesC语言基础
2.1 nesC概述 nesC(Network Embedded System C)是专门为编写TinyOS以 及进行TinyOS编写应用程序而发明的一种语言,用nesC而不是C 语言编写TinyOS(TinyOS最初是由C和汇编编写的)主要基于以下 两点: nesC在语法上支持TinyOS需要的并发执行模型。 nesC编写的源码为编译器的优化提供了可能,最终可以缩小 可执行代码尺寸。 因此,使用nesC生成的程序,更加适合无线传感网络节点。 由于nesC的学习有点难度,建议从C和nesC的区别入手,辅之以 示例,逐渐掌握其编程规则。
*/
25
第2章 nesC语言基础
TinyOS-nesC 编程参考手册
TinyOS/nesC编程参考手册第一部分序言阅读本书的前提:1.你对于C/C++/JAVA有一定的了解,并且理解指针。
2.你已经学过本科级别的操作系统课程,并且理解并发、中断和抢占。
第一章链接与命名空间在TinyOS上编程可能是比较具有挑战的,因为它需要使用一种新的语言,nesC。
但是,nesC 与 C非常相似。
这种实现新系统或协议的学习曲线并不陡峭。
相反,把新的代码与现有的相合并才是困难的开始。
nesC与C最大的不同之处在于其链接模型。
这种挑战和复杂性体现在将一组构件在使用的应用程序中组合起来,而非软件构件的书写上。
为了理解为什么这往往是困难的,接下来我们将重温链接是如何在C/C++/JAVA中工作的,以及这些语言中它是如何影响的代码的良好结构。
然后,我们会测试在nesC中链接是如何工作的,这样,区别就是显而易见的了。
我们的目的并非从系统的角度去看符号表和虚拟函数的链接而是从一个程序员的角度来看。
1.1CC语言有一个独立的全局命名空间给函数和变量使用(为了方便,以下我们把两者都称之为变量)。
一个C的源文件可以声明一个变量以以下的三种方式:声明、定义以及引用。
声明语句表示变量存在,并给出他的属性信息。
一个变量可以被多次声明,举个例子,以下是一份POSIX系统的基础声明:int read(intfd, void* buf, size_t count);声明与实现不同,它只是陈述变量在哪里存在,以及其他代码可以引用它。
调用函数,分配地址,或者载入全部的引用。
比如,以下代码表示变量result为val的引用:result = read(fd, &val, 1);C的编译器一般认为变量在引用前被声明。
当然也有一些例外(一些编译器发出警告,如果你引用一个未声明的函数,并希望他们在稍后声明),但引用的未声明的变量是通常是糟糕的C程序。
定义是最后一种声明的方式。
声明表示变量存在,引用使用变量,而定义实际创建它。
tinyos
基于T iny os 的传感器网络程序实例开发张西红,角阳飞,高彦彦(军械工程学院,河北石家庄050003)摘 要:T iny os 操作系统采用的轻量级线程技术、两层调度方式、事件驱动模式、主动消息通信技术及组件化编程等有效地提高了传感器节点CPU 的使用率,在T iny os 中采用NesC 语言进行应用程序开发,有很多问题值得研究和探讨。
从NesC 语言基于组件化编程的特点入手,讨论基于T iny os 的应用程序的开发过程。
分析了编程语言NesC 的应用程序结构,结合实例给出完整的T iny os 中用T ossim 模拟器模拟应用程序的过程,说明了应用程序开发的步骤及运行方法。
关键词:无线传感器网络;T iny os ;NesC ;程序开发中图分类号:T N711 文献标识码:A 文章编号:1003-3114(2009)01-55-3R esearch on Program Development B ased on Tinyosin Wireless Sensor N etw orksZH ANG X i 2hong ,J I AO Y ang 2fei ,G AO Y an 2yan(Ordnance Engineering C ollege ,Shijiazhuang Hebei 050003,China )Abstract :T iny os operating system has effectively increased the CPU utilization rate of sens or nodes by using the lightweight thread technology ,tw o 2layer schedule m ode ,event 2driven pattern ,active message communication technology ,com ponent programming and s o on.There are still many problems w orth further research and discussion while developing applications based on NesC language in T iny os.S tarting from the characteristic of NesC based on com ponent programming ,the paper discusses the process of program development based on T iny os ,and analyses the structure of the programs based on programming language NesC.C ombined with the piratical exam ple ,the paper finally gives a com plete procedure simulating programs with T issim simulator ,which explains the development steps of the application and the way it w orks.K ey w ords :wireless sens or netw orks ;T iny os ;NesC ;program development基金项目:国家自然科学基金项目(60672143)收稿日期:2008-10-19作者简介:张西红(1964-),男,博士,副教授,硕士生导师。
Lesson1---TinyOS入门
3.1 源文件命名的一些注意
文件名 文件类型
Foo.nc
Foo.h FooC.nc FooP.nc
接口文件
头文件 公共组件(配件或模块) 私有组件(配件或模块)
虽然可以给应用程序中的模块和配件取任意的名称, 但为了简便,建议在编写代码时使用如上表所示的统一的 命名格式。
12
3.2 BlinkAppC.nc
configuration BlinkAppC { // 这里一般由uses 和 provides 从句来说明使用到的和提供的接口,除了顶 层配件,模块和配件多可以使用和提供接口 } Implementation //实现部分 { components MainC, BlinkC, LedsC; //BlinkC是编写的模块 components new TimerMilliC() as Timer0; //as命名别名方便识别 components new TimerMilliC() as Timer1; //同一组件不同实例 components new TimerMilliC() as Timer2; //components指定了这个配件用到的组件components BlinkC -> MainC.Boot; // BlinkC.Boot -> MainC.Boot BlinkC.Timer0 -> Timer0; //BlinkC.Timer0 -> Timer0.Timer0 BlinkC.Timer1 -> Timer1; // ->是连接的意思 BlinkC.Timer2 -> Timer2; // ->是一种包含两个内部规范元素的连接 BlinkC.Leds -> LedsC; //BlinkC.Leds -> LedsC.Leds //也就是把负责实现应用部分的模块BlinkC与系统的组件库连接起来 //记住, BlinkAppC 和 BlinkC 组件是不一样的。更确切的说, BlinkAppC 是 由 Blinkc 组件连同 mainc ,ledsc 和3个 timer定时器一起组成的。 } 13
奋斗开发板TINY入门手册
奋斗版STM32 Tiny开发板入门手册基础知识篇作为STM32的初学者,首先需要了解硬件资源, STM32是意法半导体推出的基于CoreTex-M3内核的32位ARM, 目前主要有5个系列、奋斗版STM32开发板的Tiny选用了STM32F103C8BT作为开发板的MCU。
这个芯片属于STM32F103系列的中容量芯片, QFP48封装, 主要特征:20K片内SRAM 64K片内FLASH。
之所以选用这个芯片作为Tiny开发板的MCU,主要是基于以下几点:1-------体积小 QFP48脚封装,从大型设备到手持设备都可以选用。
2-------性价比高, 该芯片的批量采购价格在10几元左右,渠道稳定,适合工程选用。
很多用户先前都是从51平台转过来的, 对单片机的经验还停留在51带给我们的简单易用上,51单片机的资源简单,且口线定义唯一。
相对高端的51型单片机(如SILICON C8051F系列,AVR等)可以做到一定的口线复用(就是一个口线可以具有多种复用功能),51在处理对计算速度及控制速度相对不高的应用,还是具有很好的性价比及应用性。
近几年,随着应用技术的发展, 产品对嵌入式技术要求越来越高,51单片机已经不能满足复杂嵌入式应用要求, 在ARM技术发展过程中,coretex-M3内核应运而生,它的各项能力使得它成为微控领域之王,STM32是这种内核的产物, 是目前coretex-M3内核平台里最具性价比的平台,可以预见,未来几年里,STM32会很快占据微控平台市场。
硬件篇:功能一:奋斗版STM32 Tiny开发板可以作为2.4G 通信模块NRF24L01的USB 虚拟串口转接器。
通过串口线或者USB线和PC机连接,可以接收及发送帧长度为32字节的NRF24L01 无线数据包, 具有一对六的通道通信能力,默认地址是:收发通道0地址: 0110104334 (16进制)收发通道1地址: 02E3E2E101 (16进制)收发通道2地址: 02E3E2E102 (16进制)收发通道3地址: 02E3E2E103 (16进制)收发通道4地址: 02E3E2E104 (16进制)收发通道5地址: 02E3E2E105 (16进制)开机默认速率2M BPS, 通道0 , 射频信道0, 功率放大0dB, 使能通道0-5的自动应答。
TinyOS
无线传感器网络是当前国际上备受关注的、多学科高度交叉、知识高度集成的前沿热点研究技术,其核心技术Tinyos被誉为是“无线嵌入式系统”。
Tinyos是一个开源的嵌入式操作系统,他由加州大学的伯利克分校开发出来,越来越多的无线传感器网络通信协议正不断被开发并加入Tinyos系统组件中,并应用于技术研究和系统开发中。
无线传感器网络通常利用无线技术在某个区域内实时部署一个专用的无线局域网络。
传输传感器上采集到的数据是无线传感器网络的主要目标。
在无线传感器网络的部署过程,Web服务器是确立网络部署和监控通信传输的关键。
由于嵌入式操作系统具有高可靠性,在恶劣环境或突然断电的情况下,系统仍然能够正常工作,系统中的软件代码一般都固化在只读存储器中。
因此,要求在恶劣环境中仍然能高稳定工作的无线传感器网络可以采用嵌入式操作系统作为其实现平台,这其中包括:嵌入式Linux,Windows CE,Windows XP Embedded,VxWorks,μCOSⅡ,QNX,FreeRTOS,还有定制版的NetBSD和FreeDOS等多种移植平台。
本文主要采用Windows XPEmbedded设计平台。
Tinyos应用操作系统,将GENE-8310作为无线传感器网络中的远程服务器,实现无线网络与有线网络的跨网段传输和远程网络监控。
GENE-8310是研扬科技研制的第三代无风扇解决方案,与以往的3.5 in单板电脑相比GENE-8310在低功耗和高性能方面都有很大的改进,对于客户在低功耗控制下的优秀处理能力等更高需求方面都是最好的选择。
1 嵌入式Tinyos移植嵌入式Tinyos移植方案方框图如图1所示:首先,在开发机PC上,利用Microsoft Windows Embedded Studio工具套件中的组件设计器开发出网关MIB520的驱动组件,并结合第三方工具Dependencywalker,InCtr15开发出应用程序cygwin.bat的组件,之后把构建完成的驱动组件和应用程序组件通过工具套件中的组件数据库管理器导入到组件数据库中,以便开发操作系统时添加。
tinyos中文手册
目 录第一章前言 (3)1T INY OS的安装 (3)2T INY OS支持多种不同设备 (6)3系统及硬件验证 (7)4定制开发环境 (10)附录:M AKE 及M AKEFILE (11)1Makefile文件 (11)2Make命令 (13)3隐含规则 (14)第二章 TINYOS简介 (17)1简介 (17)2应用程序举例:B LINK (18)2.1Blink.nc配置 (18)2.2BlinkM.nc模块 (20)2.3编译Blink应用程序 (24)2.4加载并运行Blink (25)第三章用事件驱动方式从传感器读取数据 (27)1S ENSE M.NC模块 (27)2S ENSE.NC配置 (30)3定时器与参数化接口 (31)4运行S ENSE应用程序 (31)5练习 (32)第四章用于处理应用数据的任务 (34)1任务的创建和调度 (34)2S ENSE T ASK应用程序 (34)3练习 (35)第五章组件组合与无线通信 (36)1C NT T O R FM A ND L EDS应用程序 (36)2I NT T O R FM:发送信息 (39)3I NT T O R FM M:实现网络通信 (41)4G ENERIC C OMM网络堆栈 (42)5使用R FM T O L EDS接收消息 (42)6一些细节问题 (42)7练习 (43)第六章使用TOSSIM模拟TINYOS应用程序 (44)1TOSSIM简介 (44)2建立和运行应用程序 (44)3增加调试语句 (45)4在TOSSIM中使用GDB (46)5T INY V IZ:TOSSIM用户界面 (46)6将来的用途 (51)第七章在PC机上显示数据 (52)1、O SCILLOSCOPE应用程序 (52)2、“监听”工具:显示原始数据包中的数据 (52)3、数据格式分析 (53)4、S ERIAL F ORWARDER程序 (55)5、启动O SCILLOSCOPE图形用户界面GUI (55)6、使用MIG与微粒进行通信 (56)7、通过MIG发送消息 (58)8、练习 (58)第八章:注入和广播数据包 (60)1、注入数据包 (60)2、练习 (60)3、多跳广播 (61)4、练习 (62)第九章:数据收集应用程序 (63)1、S ENSE L IGHT T O L OG应用程序 (63)2、S ENSING接口 (63)3、L OGGER组件、接口、用法和限制 (64)4、收集性能 (65)5、使用S ENSE L IGHT T O L OG收集数据 (65)第十章 TINYDB:一种用于无线传感微粒的声明式查询系统 (66)1简介 (66)2T INY DB (66)3安装T INY DB并运行简单的查询 (66)4T INY DB高级特性 (70)5使用T INY DB的一个简单的J AVA程序 (71)6增加一个属性 (73)第一章前言1 TinyOS的安装TinyOS有两种安装方式,一种是使用安装向导自动安装,另一种是全手动安装。
tinyOS编程教程快速入门
Conclusion
Sensor Hardware Constraints
Lower Power Limited memory Slow CPU Size (Small) Limited hardware parallelisms Communication using radio Low-bandwidth Short range
So why do we need a new OS?
Traditional OS
Huge
! Multi-threaded architecture =>large memory I/O model Kernel and user space separation Typically no energy constraints Ample available resources
Commands/Events/Tasks
Commands Should be non-blocking i.e. take parameters start the processing and return to app; postpone time-consuming work by posting a task Can call commands on other components Events Can call commands, signal other events, post tasks but cannot be signal-ed by commands Pre-empt tasks, not vice-versa Tasks FIFO scheduling Non pre-emptable by other task, pre-emtable by events Used to perform computationally intensive work Can be posted by commands and/or events
新编文档-TinyOS操作系统开发技术及实践(西电版)第4章 平台移植-精品文档
13
第4章 平 台 移 植 4.2.2 系统环境变量
1. MAKERULES变量
根据前述make系统工作流程可知,所有的TinyOS应用程 序目录下都必须有一个Makefile文件。该文件由命令行的 “make 平台名”命令触发make工具解析执行,其内容一般如 代码4-1所示。 【代码4-1】 Makefile
fi
export TOSDIR=$TOSROOT/tos
export MAKERULES=$TOSROOT/support/make/Makerules
echo "TOSDIR=$TOSDIR" echo "MAKERULES=$MAKERULES" echo "TOSMAKE_PATH=$TOSMAKE_PATH" echo "Set cctinyos env SUCCESS!"
18
第4章 平 台 移 植
图4-6 增加命令以自动运行脚本
19
第4章 平 台 移 植 然后,当启动Cygwin时可以看到如图4-7所示信息。
图4-7 cygwin启动时的显示信息
20
第4章 平 台 移 植 4.2.3 启动脚本实例
本小节内容用于实现任务描述4.D.2,在4.D.1的基础上,
建立cctinyos平台的启动脚本文件,并设置为自动启动。 1. 建立启动脚本文件 参考前述mytinyos平台的脚本文件,建立cctinyos平台的 环境变量设置脚本文件,并保存于“opt/cctinyos/”目录下。其 内容如描述4.D.2 cctinyos.env所示。
Makefile文件(以及符合Makefile格式的脚本文件)查找参与编译
的文件并调用nesC编译器,并进一步调用本地编译器和烧写器
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
– docs.tinyos/tinywiki/index.php/TinyOS_Tutorial s
2
安装
• 多种安装方法,参看 docs.tinyos/tinywiki/index.php/Gettin g_started_%282.1%29
TinyOS入门(1)
胡四泉
What?
• An operating system for low power, embedded, wireless devices
– Wireless sensor networks (WSNs) – Sensor-actuator networks – Embedded robotics
• 在本实习中,在WindowsXP上用 VMPlayer运行XubunTOS虚拟机
安装启动步骤
• 安装VMPlayer • 解压xubuntos-2.1-vm.tar.gz到E盘根目录,确认
E:\Xubuntos 2.1目录下包含虚拟机文件Xubuntos 2.1.vmx • 运行VMPlayer • 在VMPlayer中打开Xubuntos 2.1.vmx • 如果系统询问你虚拟机是move还是copy来的,选择 copy; • 如果内存不足,系统建议你reduce memory请accept • 等待Xubuntos启动完成 • 输入用户名xubuntos 密码tinyos • 使用Ctrl+Alt+Enter组合键在虚拟机与WindowsXP间切 换
观察程序结构
• make telosb docs
Thanks! Go Ahead! Good Luck!
谢谢!
xiexie!
• 使用motelist命令查询连接是否成功
上载程序
• 运行make telosb reinstall.8 bsl,/dev/ttyUSB0 上载blink的二进制映像到 telosb的程序空间
观察程序运行效果
• LED闪烁
编译上载合并
• make telosb install.8 bsl,/dev/ttyUSB0
+ tinyos-2.1.0 + apps + docs + support + tos
编译Blink程序
• 进入Blink程序所在的目录:
cd /opt/tinyos-2.1.0/apps/Blink
运行程序编译命令
make telosb
硬件连接
• 把TelosB插入计算机USB口,确保其连接到虚拟 机
开发环境检查
• 作为第一个练习,我们将编译和上载一个非常简单的 TinyOS应用程序——Blink,在此之前先检查开发环境
• 打开一个终端窗口,运行tos-check-env
•然后运行printenv MAKERULES检查tinyos设置,应该显示:
TinyOS目录结构
• 通过桌面上的file mana