Difference between revisions of "Sensing"

From TinyOS Wiki
Jump to: navigation, search
(New page: <div class="title">Lesson 5: Sensing</div><div class="subtitle">Last Modified: 16 June 2006</div> This lesson introduces sensor data acquisition in TinyOS. It demonstrates two sensor appl...)
 
Line 1: Line 1:
<div class="title">Lesson 5: Sensing</div><div class="subtitle">Last Modified: 16 June 2006</div>
+
This lesson introduces sensor data acquisition in TinyOS. It demonstrates two sensor applications: a simple application called [[Sensing#The Sense application|Sense]] that periodically takes sensor readings and displays the values on the LEDs. And a more sophisticated application called [[Sensing#The Oscilloscope application|Oscilloscope]] where nodes periodically broadcast their sensor readings to a basestation node. Using the Mote-PC serial communication described in the [Mote-PC serial communication and SerialForwarder|previous lesson] the basestation forwards the sensor readings to the PC, where they are visualized with a dedicated graphical user interface.
 
 
This lesson introduces sensor data acquisition in TinyOS. It demonstrates two sensor applications: a simple application called [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson5.html#sense Sense] that periodically takes sensor readings and displays the values on the LEDs. And a more sophisticated application called [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson5.html#oscilloscope Oscilloscope] where nodes periodically broadcast their sensor readings to a basestation node. Using the Mote-PC serial communication described in the [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson4.html previous lesson] the basestation forwards the sensor readings to the PC, where they are visualized with a dedicated graphical user interface.
 
  
 
=Introduction=
 
=Introduction=
Line 22: Line 20:
 
=The Sense application=
 
=The Sense application=
  
<code>Sense</code> is a simple sensing demo application. It periodically samples the default sensor and displays the bottom bits of the readings on the LEDs. The <code>Sense</code> application can be found in <code>tinyos-2.x/apps/Sense</code>. Let's first look at the <code>[http://www.tinyos.net/tinyos-2.x/apps/Sense/SenseAppC.nc SenseAppC.nc]</code> configuration:
+
<code>Sense</code> is a simple sensing demo application. It periodically samples the default sensor and displays the bottom bits of the readings on the LEDs. The <code>Sense</code> application can be found in <code>tinyos-2.x/apps/Sense</code>. Let's first look at the <code>[http://www.tinyos.net/tinyos-2.x/apps/Sense/SenseAppC.nc SenseAppC.nc]</code> configuration:  
 
 
 
 
  configuration SenseAppC
 
  configuration SenseAppC
 
  {
 
  {
Line 37: Line 33:
 
   SenseC.Read -> Sensor;
 
   SenseC.Read -> Sensor;
 
  }
 
  }
 
+
The <code>SenseAppC</code> configuration looks similar to the <code>BlinkAppC</code> configuration described in [[Getting Started with TinyOS| 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 <code>[http://www.tinyos.net/tinyos-2.x/apps/Sense/SenseC.nc SenseC.nc]</code> module:  
The <code>SenseAppC</code> configuration looks similar to the <code>BlinkAppC</code> configuration described in [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson1.html 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 <code>[http://www.tinyos.net/tinyos-2.x/apps/Sense/SenseC.nc SenseC.nc]</code> module:
 
 
 
 
 
  module SenseC
 
  module SenseC
 
  {
 
  {
Line 50: Line 43:
 
   }
 
   }
 
  }
 
  }
 
 
Like the <code>BlinkC.nc</code> module the <code>SenseC.nc</code> module uses the interfaces <code>Boot</code>, <code>Leds</code> and <code>Timer<TMilli></code>. Additionally, it uses the <code>Read<uint16_t></code> interface. The sequence of actions in the <code>SenseC.nc</code> implementation is as follows: <code>SenseC.nc</code> uses the <code>Boot</code> interface to start a periodic timer after the system has been initialized. Every time the timer expires <code>SenseC.nc</code> is signalled a timer event and reads data via the <code>Read<uint16_t></code> interface. Reading data is a split-phase operation, it is divided in a command <code>Read.read()</code> and an event <code>Read.readDone()</code>. Thus every time the timer expires <code>SenseC.nc</code> calls <code>Read.read()</code> and waits for the <code>Read.readDone()</code> event. When data is signalled in the <code>Read.readDone()</code> event <code>SenseC.nc</code> 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.
 
Like the <code>BlinkC.nc</code> module the <code>SenseC.nc</code> module uses the interfaces <code>Boot</code>, <code>Leds</code> and <code>Timer<TMilli></code>. Additionally, it uses the <code>Read<uint16_t></code> interface. The sequence of actions in the <code>SenseC.nc</code> implementation is as follows: <code>SenseC.nc</code> uses the <code>Boot</code> interface to start a periodic timer after the system has been initialized. Every time the timer expires <code>SenseC.nc</code> is signalled a timer event and reads data via the <code>Read<uint16_t></code> interface. Reading data is a split-phase operation, it is divided in a command <code>Read.read()</code> and an event <code>Read.readDone()</code>. Thus every time the timer expires <code>SenseC.nc</code> calls <code>Read.read()</code> and waits for the <code>Read.readDone()</code> event. When data is signalled in the <code>Read.readDone()</code> event <code>SenseC.nc</code> 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 <code>[http://www.tinyos.net/tinyos-2.x/tos/interfaces/Read.nc Read]</code> interface (in <code>tinyos-2.x/tos/interfaces</code>) can be used to read a single piece of sensor data, let's look at it in detail:
+
The <code>[http://www.tinyos.net/tinyos-2.x/tos/interfaces/Read.nc Read]</code> interface (in <code>tinyos-2.x/tos/interfaces</code>) can be used to read a single piece of sensor data, let's look at it in detail:  
 
 
 
 
  interface Read<val_t> {
 
  interface Read<val_t> {
 
   /**
 
   /**
Line 72: Line 62:
 
   event void readDone( error_t result, val_t val );
 
   event void readDone( error_t result, val_t val );
 
  }
 
  }
 
 
If you are not familiar with generic interfaces you will wonder what the meaning of <code><val_t></code> (in the first line) is and why the signature of <code>SenseC.nc</code> is using <code>Read<uint16_t></code>. What you see above is a ''generic interface definition'', because the <code>Read</code> 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 <code>readDone</code> event passes a parameter of the <code><val_t></code> parameter, which is a placeholder for the actual data type). This means that since <code>SenseC.nc</code> is using the <code>uint16_t</code> variant of the <code>Read</code> interface, it can only be wired to a component that provides the <code>Read<uint16_t></code> interface and thus <code>SenseC.nc</code> expects to read 16 bit unsigned integer sensor data. If you tried to wire <code>SenseC.nc</code> to a component that provides, for example, a <code>Read<uint8_t></code> interface you would get an error from the nesC compiler.
 
If you are not familiar with generic interfaces you will wonder what the meaning of <code><val_t></code> (in the first line) is and why the signature of <code>SenseC.nc</code> is using <code>Read<uint16_t></code>. What you see above is a ''generic interface definition'', because the <code>Read</code> 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 <code>readDone</code> event passes a parameter of the <code><val_t></code> parameter, which is a placeholder for the actual data type). This means that since <code>SenseC.nc</code> is using the <code>uint16_t</code> variant of the <code>Read</code> interface, it can only be wired to a component that provides the <code>Read<uint16_t></code> interface and thus <code>SenseC.nc</code> expects to read 16 bit unsigned integer sensor data. If you tried to wire <code>SenseC.nc</code> to a component that provides, for example, a <code>Read<uint8_t></code> interface you would get an error from the nesC compiler.
  
Recall that the wiring is defined in the <code>SenseAppC.nc</code> configuration. Let's again take a look at which component <code>SenseC.nc</code> is wired to using the <code>Read<uint16_t></code> interface in the <code>SenseAppC</code> configuration. The interesting lines are
+
Recall that the wiring is defined in the <code>SenseAppC.nc</code> configuration. Let's again take a look at which component <code>SenseC.nc</code> is wired to using the <code>Read<uint16_t></code> interface in the <code>SenseAppC</code> configuration. The interesting lines are  
 
 
 
 
   components new DemoSensorC() as Sensor;
 
   components new DemoSensorC() as Sensor;
 
+
and  
and
 
 
 
 
 
   SenseC.Read -> Sensor;
 
   SenseC.Read -> Sensor;
 
 
This means that the ''generic'' <code>DemoSensorC</code> component provides the <code>Read<uint16_t></code> interface to <code>SenseC.nc</code>
 
This means that the ''generic'' <code>DemoSensorC</code> component provides the <code>Read<uint16_t></code> interface to <code>SenseC.nc</code>
  
It is important to understand that the <code>SenseC.nc</code> 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 <code>Read<uint16_t></code> interface. On a platform without any built-in sensors (like ''micaz'') and no attached sensorboard the <code>DemoSensorC</code> component could simply return constant values. The last sentence hints that the <code>DemoSensorC</code> component is different for every platform: therefore you will not find <code>DemoSensorC.nc</code> in the TinyOS libraries. Instead, a different <code>DemoSensorC.nc</code> has to be written for every platform, i.e. the <code>DemoSensorC.nc</code> implementation for telosb will be different than the <code>DemoSensorC.nc</code> implementation for micaz. This is the answer to the first question asked in the [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson5.html#introduction introduction] section: the ''platform dependent'' <code>DemoSensorC</code> component defines which sensor the <code>Sense</code> or <code>Oscilloscope</code> application is sampling and every platform that wants to run sensing applications such as <code>Oscilloscope</code>, <code>Sense</code> or <code>RadioSenseToLeds</code> has to provide its own version of <code>DemoSensorC</code>. Additionally, sensor boards may come with their own version of <code>DemoSensorC</code> (e.g., the <code>basicsb</code> sensorboard for the mica-family of motes define <code>DemoSensorC.nc</code> to be that board's light sensor).
+
It is important to understand that the <code>SenseC.nc</code> 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 <code>Read<uint16_t></code> interface. On a platform without any built-in sensors (like ''micaz'') and no attached sensorboard the <code>DemoSensorC</code> component could simply return constant values. The last sentence hints that the <code>DemoSensorC</code> component is different for every platform: therefore you will not find <code>DemoSensorC.nc</code> in the TinyOS libraries. Instead, a different <code>DemoSensorC.nc</code> has to be written for every platform, i.e. the <code>DemoSensorC.nc</code> implementation for telosb will be different than the <code>DemoSensorC.nc</code> implementation for micaz. This is the answer to the first question asked in the [[Sensing#Introduction introduction]] section: the ''platform dependent'' <code>DemoSensorC</code> component defines which sensor the <code>Sense</code> or <code>Oscilloscope</code> application is sampling and every platform that wants to run sensing applications such as <code>Oscilloscope</code>, <code>Sense</code> or <code>RadioSenseToLeds</code> has to provide its own version of <code>DemoSensorC</code>. Additionally, sensor boards may come with their own version of <code>DemoSensorC</code> (e.g., the <code>basicsb</code> sensorboard for the mica-family of motes define <code>DemoSensorC.nc</code> to be that board's light sensor).
  
 
==The DemoSensorC component==
 
==The DemoSensorC component==
  
Let's take a closer look at the <code>DemoSensorC</code> component. Every <code>DemoSensorC</code> component has the following signature:
+
Let's take a closer look at the <code>DemoSensorC</code> component. Every <code>DemoSensorC</code> component has the following signature:  
 
 
 
 
  generic configuration DemoSensorC()
 
  generic configuration DemoSensorC()
 
  {
 
  {
 
   provides interface Read<uint16_t>;
 
   provides interface Read<uint16_t>;
 
  }
 
  }
 
 
In its implementation section, however, <code>DemoSensorC</code> may differ from platform to platform. For example, on the ''telosb'' platform <code>DemoSensorC</code> instantiates a component called <code>VoltageC</code>, which reads data from the MCU-internal voltage sensor. Because the ''micaz'' doesn't have any built-in sensors its <code>DemoSensorC</code> uses system library component like <code>ConstantSensorC</code> or <code>SineSensorC</code>, which return "fake" sensor data. Thus <code>DemoSensorC</code> is a means of indirecting sensor data acquisition from a platform-specific sensor component (like <code>VoltageC</code>) to platform-independent applications like <code>Sense</code> or <code>Oscilloscope</code>. Usually the configuration of a sensor is done in the component that <code>DemoSensorC</code> instantiates.
 
In its implementation section, however, <code>DemoSensorC</code> may differ from platform to platform. For example, on the ''telosb'' platform <code>DemoSensorC</code> instantiates a component called <code>VoltageC</code>, which reads data from the MCU-internal voltage sensor. Because the ''micaz'' doesn't have any built-in sensors its <code>DemoSensorC</code> uses system library component like <code>ConstantSensorC</code> or <code>SineSensorC</code>, which return "fake" sensor data. Thus <code>DemoSensorC</code> is a means of indirecting sensor data acquisition from a platform-specific sensor component (like <code>VoltageC</code>) to platform-independent applications like <code>Sense</code> or <code>Oscilloscope</code>. Usually the configuration of a sensor is done in the component that <code>DemoSensorC</code> instantiates.
  
How can <code>Sense</code> be changed to sample a sensor other than the platform's default sensor? Usually this requires changing only a single line of code in <code>DemoSensorC</code><nowiki>; for example, if you wanted to replace the </nowiki><code>VoltageC</code> component on ''telosb'' by the constant sensor component <code>ConstantSensorC</code> you could change the follwoing line in <code>DemoSensorC</code> from:
+
How can <code>Sense</code> be changed to sample a sensor other than the platform's default sensor? Usually this requires changing only a single line of code in <code>DemoSensorC</code><nowiki>; for example, if you wanted to replace the </nowiki><code>VoltageC</code> component on ''telosb'' by the constant sensor component <code>ConstantSensorC</code> you could change the follwoing line in <code>DemoSensorC</code> from:  
 
 
 
 
  components new VoltageC() as DemoSensor;
 
  components new VoltageC() as DemoSensor;
 
+
to something like  
to something like
 
 
 
 
 
  components new ConstantSensorC(uint16_t, 0xbeef) as DemoSensor;
 
  components 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 (<code>tinyos-2.x/tos/platforms</code>), in the respective sensorboard subdirectory (<code>tinyos-2.x/tos/sensorboards</code>) or, in case of microprocessor-internal sensors, in the respective chips subdirectory (<code>tinyos-2.x/tos/chips</code>). <code>ConstantSensorC</code> and <code>SineSensorC</code> can be found in <code>tinyos-2.x/tos/system</code>.
 
What sensors are available depends on the platform. Sensor components are usually located in the respective platform subdirectory (<code>tinyos-2.x/tos/platforms</code>), in the respective sensorboard subdirectory (<code>tinyos-2.x/tos/sensorboards</code>) or, in case of microprocessor-internal sensors, in the respective chips subdirectory (<code>tinyos-2.x/tos/chips</code>). <code>ConstantSensorC</code> and <code>SineSensorC</code> can be found in <code>tinyos-2.x/tos/system</code>.
  
 
==Running the Sense application==
 
==Running the Sense application==
  
To compile the <code>Sense</code> application, go to the <code>apps/Sense</code> directory and depending on which hardware you have, type something similar to <code>make telosb install</code>. If you get errors such as the following,
+
To compile the <code>Sense</code> application, go to the <code>apps/Sense</code> directory and depending on which hardware you have, type something similar to <code>make telosb install</code>. If you get errors such as the following,  
 
 
 
 
  SenseAppC.nc:50: component DemoSensorC not found
 
  SenseAppC.nc:50: component DemoSensorC not found
 
  SenseAppC.nc:50: component `DemoSensorC' is not generic
 
  SenseAppC.nc:50: component `DemoSensorC' is not generic
 
  SenseAppC.nc:55: no match
 
  SenseAppC.nc:55: no match
 
 
your platform has not yet implemented the <code>DemoSensorC</code> component. For a quick solution you can copy <code>DemoSensorC.nc</code> from <code>tinyos-2.x/tos/platforms/micaz</code> to your platform directory (a good starting point on how to create sensor components is probably [http://www.tinyos.net/tinyos-2.x/doc/html/tep101.html TEP 101] and [http://www.tinyos.net/tinyos-2.x/doc/html/tep114.html TEP 114]).
 
your platform has not yet implemented the <code>DemoSensorC</code> component. For a quick solution you can copy <code>DemoSensorC.nc</code> from <code>tinyos-2.x/tos/platforms/micaz</code> to your platform directory (a good starting point on how to create sensor components is probably [http://www.tinyos.net/tinyos-2.x/doc/html/tep101.html TEP 101] and [http://www.tinyos.net/tinyos-2.x/doc/html/tep114.html 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
+
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'' install
 
  SENSORBOARD=basicsb make ''platform'' install
 
 
to run <code>Sense</code> using the mda100's light sensor.
 
to run <code>Sense</code> using the mda100's light sensor.
  
Line 135: Line 103:
 
=The Oscilloscope application=
 
=The Oscilloscope application=
  
<code>Oscilloscope</code> is an application that let's you visualize sensor readings on the PC. Every node that has <code>Oscilloscope</code> installed periodically samples the default sensor ([http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson5.html#demosensorc via <code>DemoSensorC</code>]) and broadcasts a message with 10 accumulated readings over the radio. A node running the <code>BaseStation</code> application will forward these messages to the PC using the serial communication. To run <code>Oscilloscope</code> you therefore need at least two nodes: one node attached to your PC running the <code>BaseStation</code> application (<code>BaseStation</code> can be found at <code>tinyos-2.x/apps/BaseStation</code> and was introduced in the [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson4.html previous lesson]) and one or more nodes running the <code>Oscilloscope</code> application.
+
<code>Oscilloscope</code> is an application that let's you visualize sensor readings on the PC. Every node that has <code>Oscilloscope</code> installed periodically samples the default sensor via ([[Sensing#The DemoSensorC component| <code>DemoSensorC</code>]]) and broadcasts a message with 10 accumulated readings over the radio. A node running the <code>BaseStation</code> application will forward these messages to the PC using the serial communication. To run <code>Oscilloscope</code> you therefore need at least two nodes: one node attached to your PC running the <code>BaseStation</code> application (<code>BaseStation</code> can be found at <code>tinyos-2.x/apps/BaseStation</code> and was introduced in the [[Mote-PC serial communication and SerialForwarder| previous lesson]]) and one or more nodes running the <code>Oscilloscope</code> application.
  
Let's take a look at the <code>[http://www.tinyos.net/tinyos-2.x/apps/Oscilloscope/OscilloscopeAppC.nc OscilloscopeAppC.nc]</code> configuration:
+
Let's take a look at the <code>[http://www.tinyos.net/tinyos-2.x/apps/Oscilloscope/OscilloscopeAppC.nc OscilloscopeAppC.nc]</code> configuration:  
 
 
 
 
  configuration OscilloscopeAppC
 
  configuration OscilloscopeAppC
 
  {
 
  {
Line 157: Line 123:
 
   OscilloscopeC.Leds -> LedsC;
 
   OscilloscopeC.Leds -> LedsC;
 
  }
 
  }
 
+
The actual implementation of the application is in <code>[http://www.tinyos.net/tinyos-2.x/apps/Oscilloscope/OscilloscopeC.nc OscilloscopeC.nc]</code>. This is the signature of <code>OscilloscopeC.nc</code>:  
The actual implementation of the application is in <code>[http://www.tinyos.net/tinyos-2.x/apps/Oscilloscope/OscilloscopeC.nc OscilloscopeC.nc]</code>. This is the signature of <code>OscilloscopeC.nc</code><nowiki>: </nowiki>
 
 
 
 
 
  module OscilloscopeC
 
  module OscilloscopeC
 
  {
 
  {
Line 168: Line 131:
 
     interface AMSend;
 
     interface AMSend;
 
     interface Receive;
 
     interface Receive;
     interface Timer<nowiki>;
+
     interface Timer;
     interface Read</nowiki><nowiki>;
+
     interface Read;
 
     interface Leds;
 
     interface Leds;
 
   }
 
   }
 
  }
 
  }
</nowiki>
+
<code>Oscilloscope</code> is a combination of different building blocks introduced in previous parts of the tutorial. Like [Sensing$The Sense Application|<code>Sense</code>], <code>Oscilloscope</code> uses <code>DemoSensorC</code> and a timer to periodically sample the default sensor of a platform. When it has gathered 10 sensor readings <code>OscilloscopeC</code> puts them into a message and broadcasts that message via the <code>AMSend</code> interface. <code>OscilloscopeC</code> uses the <code>Receive</code> interface for synchronization purposes (see below) and the <code>SplitControl</code> interface, to switch the radio on. If you want to know more about mote-mote radio communication read [Mote-mote radio communication|lesson 3].
 
 
<code>Oscilloscope</code> is a combination of different building blocks introduced in previous parts of the tutorial. Like [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson5.html#sense <code>Sense</code>], <code>Oscilloscope</code> uses <code>DemoSensorC</code> and a timer to periodically sample the default sensor of a platform. When it has gathered 10 sensor readings <code>OscilloscopeC</code> puts them into a message and broadcasts that message via the <code>AMSend</code> interface. <code>OscilloscopeC</code> uses the <code>Receive</code> interface for synchronization purposes (see below) and the <code>SplitControl</code> interface, to switch the radio on. If you want to know more about mote-mote radio communication read [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson3.html lesson 3].
 
  
 
==Running the Oscilloscope application==
 
==Running the Oscilloscope application==
Line 185: Line 146:
 
==Running the Java GUI==
 
==Running the Java GUI==
  
To visualize the sensor readings on your PC first go to <code>tinyos-2.x/apps/Oscilloscope/java</code> and type <code>make</code>. This creates/compiles the necessary message classes and the <code>Oscilloscope</code> Java GUI. Now start a SerialForwarder and make sure it connects to the node on which you have installed the <code>BaseStation</code> application (how this is done is explained in the [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson4.html previous lesson]). In case you have problems with the Java compilation or the serial connection work through the [http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson4.html previous lesson].
+
To visualize the sensor readings on your PC first go to <code>tinyos-2.x/apps/Oscilloscope/java</code> and type <code>make</code>. This creates/compiles the necessary message classes and the <code>Oscilloscope</code> Java GUI. Now start a SerialForwarder and make sure it connects to the node on which you have installed the <code>BaseStation</code> application (how this is done is explained in the [[Mote-PC serial communication and SerialForwarder| previous lesson]]). In case you have problems with the Java compilation or the serial connection work through the [[Mote-PC serial communication and SerialForwarder| previous lesson]].
  
 
Once you have a SerialForwarder running you can start the GUI by typing <code>./run</code> (in <code>tinyos-2.x/apps/Oscilloscope/java</code>). You should see a window similar to the one below:
 
Once you have a SerialForwarder running you can start the GUI by typing <code>./run</code> (in <code>tinyos-2.x/apps/Oscilloscope/java</code>). You should see a window similar to the one below:
Line 202: Line 163:
 
* [http://csl.stanford.edu/~pal/pubs/tinyos-programming-1-0.pdf TinyOS Programming Guide]
 
* [http://csl.stanford.edu/~pal/pubs/tinyos-programming-1-0.pdf TinyOS Programming Guide]
  
<br />
+
<hr>
 
 
----
 
 
<center>
 
<center>
  
< '''[http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson4.html Previous Lesson]''' | '''[http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/index.html Top]''' | '''[http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson6.html Next Lesson ] >'''
+
< '''[[Mote-PC serial communication and SerialForwarder|Previous Lesson]]''' | '''[Sensing#Introduction|Top]''' | '''[[Boot Sequence|Next Lesson]] >'''
  
 
</center>
 
</center>

Revision as of 16:51, 27 December 2007

This 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 [Mote-PC serial communication and SerialForwarder|previous lesson] the basestation forwards the sensor readings to the PC, where they are visualized with a dedicated graphical user interface.

Introduction

Sensing 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 ADCControl anymore; 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 of defining 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 sensor configuration? 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 or Oscilloscope 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 the telosb platform.

The Sense application

Sense is a simple sensing demo application. It periodically samples the default sensor and displays the bottom bits of the readings on the LEDs. The Sense application 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 the Read 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 the Read<uint16_t> interface in the SenseAppC configuration. The interesting lines are

  components new DemoSensorC() as Sensor;

and

  SenseC.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 the DemoSensorC component 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 Sensing#Introduction introduction section: the platform dependent DemoSensorC component defines which sensor the Sense or Oscilloscope application 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 component

Let'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 in DemoSensorC; for example, if you wanted to replace the VoltageC component on telosb by the constant sensor component ConstantSensorC you could change the follwoing line in DemoSensorC from:

components new VoltageC() as DemoSensor;

to something like

components 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 application

To 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 match

your 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 create sensor 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 install

to 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 application

Oscilloscope 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 [Sensing$The Sense Application|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 [Mote-mote radio communication|lesson 3].

Running the Oscilloscope application

To 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 GUI

To 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:

Oscilloscope.jpg

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).

Related Documentation


< Previous Lesson | [Sensing#Introduction|Top] | Next Lesson >