Friday, October 22, 2010

Coding Guidelines


I happened to read a book called "Software Development Guidelines", I will list out the points from this book which I liked.

  1. Let each routine designed perform an unique task. Divide the program into routines such that each of them performs a separate task. Let all routines be clearly distinguishable.
  2. Do not make any routine too long. Also do not simply divide the routines without any logical need. If a routine is more than 100 lines one may try to divide it if it can be divided into routines which can perform separate tasks.
  3. Let the routines be loosely coupled. (pass less parameters, make the routine interfaces flexible, make them more visible )
  4. Let all the files related to a module be in one directory.
  5. Let all modules be divided logically. The interfaces should be provided for interaction between modules
  6. All other internal data within module should be private
  7. Declare int, float and all as typedef e.g typedef int int16 (on 16 bit machine) so that one need not go and the variable type everywhere. This makes the program more platforms independent.
  8. Do explicit declarations of all variables before it is used.
  9. Declare all the variables , constants at the beginning of the routine.
  10. Every variable declared should have a comment describing the need, function and limitation of the variable.
  11. Better to declare each variable in a separate line with its description
  12. Always use meaningful names for the variables , this increases the readability of the program like anything (have high quality identifiers )
  13. Do not create two different variables in the program which only differ in their cases (upper case or lower case) because not all compilers are case sensitive. To make program portable use case neutral variables.
  14. Capitalize the first letter of interior words in all multi-word identifiers.
  15. Variable names should be meaningful, concise, and non-ambiguous to an average programmer fluent in the English language.
  16. Try to avoid abbreviated variable names .If used give description of the abbreviated variable names.
  17. Never use a numeric suffix to differentiate two names.
  18. Try to make most identifiers unique in the first few character positions of the identifier. This makes the program easier to read.
  19. If your code contains a chain of if..elseif..elseif.......elseif..... statements, do not use the final else clause to handle a remaining case. Only use the final else to catch an error condition. If you need to test for some value in an if..elseif..elseif.... chain, always test the value in an if or elseif statement.
  20. Always use the most appropriate type of loop (categorized by termination test position). Never force one type of loop to behave like another.
  21. Avoid empty loops. If testing the loop termination condition produces some side effect that is the whole purpose of the loop, move that side effect into the body of the loop. If a loop truly has an empty body, place a comment like "/* nothing */" or "{null statement}" within your code.
  22. Loops with a single exit point are more easily understood
  23. All loops should have one entry point. The program should enter the loop with the instruction at the top of the loop.
  24. Make each loop perform only one function.
  25. Code, as much as possible, should read from top to bottom( do not use lot of goto statements , deeply nested if else with lot of possibility of code jumping to some other location )
  26. An expression should not produce any side effects.
  27. Do not user lesser used operators, porting to different language may be difficult.
  28. Use parentheses where ever necessary. We can do without parentheses for mul, division, addition, and subtraction but for others different languages, different compliers may have different precedence hence better user brackets.
  29. Indentation should be three to four spaces in an indented control structure with four spaces probably being the optimal value.
  30. If you use tabs to indent your code, insert a comment at the very beginning of the program that states the number of positions for each tab stop. E.g., "/* this program is formatted using four character position tab stops. */"
  31. All related statements should be together, there should not be blank lines in between them.
  32. Blank lines should be present in between of declaration and start of code.
  33. Blank should be present in between comment block and code
  34. Whenever there is different logical block blank line should be present.
  35. Source code lines will not exceed 80 characters in length
  36. Always put a blank line between any block statement and the statement(s) it encloses
  37. Write comments that describe blocks of statements rather than individual statements. Comments covering single statements tend to discuss the mechanics of that statement rather than discussing what the program is doing.
  38. Focus paragraph comments on the why rather than the how. Code should explain what the program is doing and why the programmer chose to do it that way rather than explain what each individual statement is doing.
  39. Use comments to prepare the reader for what is to follow. Someone reading the comments should be able to have a good idea of what the following code does without actually looking at the code. Note that this rule also suggests that comments should always precede the code to which they apply.
  40. All comments will be high-quality comments that describe the actions of the surrounding code in a concise manner.
  41. All comments will be up to date. If a programmer makes changes to the code, that programmer is responsible for updating the internal comments and any external documentation affected by those changes.
  42. All inter module communication in C/C++ programs must take place through header files (“.h” files). All extern directives, public class definitions, public type definitions, and public constants must appear in the header file that all interested modules will include.
  43. For functions like malloc which is used at many places we can used a customized safe_malloc for our C application so that the necessary check is also included in the safe_malloc function.
For detailed explanation on guidelines one can refer to the above mentioned book itself , it is available freely in the internet.

Monday, August 9, 2010

Sensor Network for Better, Efficient and Automated (BEA) Farming



Diagram representing overall architecture for BEA farming

Wireless sensor networks have wide variety of applications . I would like to brief on how WSN can be used for better,efficient and automated farming. I and my friends got this idea during a casual discussion. So this articles belongs to them also. [:)].

Sensor network can be used in agriculture to monitor, the moisture content in soil , the fertility of soil, the surrounding weather conditions of the farm. Various sensor nodes can be deployed in the farm which have the capability to monitor the moisture,fertility and temperature. These sensor nodes can in turn give data to a gateway or a farmers mobile. Mobile is one of the very common devices it can act as gateway to collect data from all the senors nodes deployed in the farm. The data can be obtained whenever the farmer goes to his farm along with his mobile or there can be gateway, which aggregates the data from all the senor nodes in the farm, and sends it to the farmers mobile.

Once the data is available at the farmer's mobile he can send this data to experts or his friends or use this data himself to get any advice on farming if necessary. The data obtained on moisture level can be used to water the whole farm appropriately , such that none of the part of the farm will be over watered neither will less watered. Actuators can be used to take appropriate decisions based on sensor readings . There can be server to which all farmers can connect using their mobile and get necessary data. The server can also be used to store the data provided by the farmers. In this way all the agriculture lands can be connected , the data provided by all farmers can be used for various research purposes like to check what fertilizer level and moisture level has resulted in best yield. The data collected from many farmers at a place will help in building a detailed picture of farming at that respective place.

The shared server can also be used to share information such as , crops that can be grown on particular type of soil, best agriculture practices , present price for a particular type of crop etc. These information can be sent to farmers mobile so that farmers are updated with latest happenings.

The sensor nodes can be connected to the gateway or mobile using 802.15.4 (Low rate Wireless Personal Area network ) and the mobile and server can be connected using Internet or using other mobile communication system such as GSM or CDMA. The common sensors that can be used are soil moisture sensor, soil nutrients sensor , temperature sensor and rain sensors.

The diagram below makes the explanation given above more clearer.


The diagram given above is taken from the link given below.






Friday, July 23, 2010

Design Issues in WSN

Wireless sensor networks are made of large number of tiny sensor nodes, which have limited power and less processing capability. The life time of the individual sensor node is not easily predictable and also the network needs to be formed autonomously as it is not possible to manually set up the sensor network for all applications. The sensor network also consists of several different kind of nodes hence heterogeneity needs to be supported. The number of sensor nodes in the network is not constant through out the life time of the network it may vary because of addition of senor nodes or reduction of nodes due to their death . The major factors that need to considered while designing sensor network are listed below.

Fault Tolerance: Possibility of node failure and change of topology of network is quite high in case of WSN. Hence the designer of network should make the network robust and reliable even in case of node failures and topology changes. The network should function smoothly and normally irrespective of node failures and topology changes.

Life Time: WSN are supposed to work for a quite long time with low power consumption. They are supposed to last at-least for 6 months to 1 year. We need to keep in mind that every node in WSN may be powered using just a 3 V battery and this should be sufficient for the entire life time of the node . The design of protocols of WSN should be such that the node consumes as less energy as possible. This will help in making the WSN last longer.

Scalability: The design of WSN should support addition of new nodes any time and also the design should support large number of nodes because some applications in WSN may require quite a huge number of sensor nodes.

Date Aggregation: The sensor nodes in WSN are located close to each other hence the possibility of similar data being generated by the nodes next to each other is quite high. So the data needs to be aggregated and the duplicate data needs to be avoided because the transmission and reception data is the most costly affair in WSN.The data needs to be aggregated at different levels in WSN so that only the necessary data is transmitted and recevied and the redundant data is not communicated.

Cost: The cost of each sensor node is supposed to be 1$ , as WSN can have large number of sensor nodes the total cost of the network can become a quite expensive affair. So the designer of WSN needs to decide on the optimal number of nodes necessary for the application.

Environment: The environment in which the WSN is deployed can be very demanding , so the design of WSN should be such that WSN should be able to survive regardless of the conditions in which WSN s deployed.

Heterogeneity Support: The protocols designed for WSN should support different kinds of sensor nodes and also be able to support variety of applications.

Autonomous Operations: The WSN should be able to organize , reorganize and operate autonomously because sometimes WSN deployed in places where human habitation is not possible.

Limited Memory and Processing Capability : The sensor nodes have very limited memory, power and processing capabilities , so all designs of WSN should not be demanding in terms of processing requirements or memory requirements .

Thursday, May 20, 2010

Applications of Sensor Networks

Wireless Sensor Networks are used to build wide variety of applications . The apps range from agriculture apps to defense apps. All applications involve many sensor nodes, which collect data from the deployed site and this collected data is used for some specific purpose. I will give some examples of applications which are built using sensor networks.

  • Military or Defense Applications
  • Agriculture Applications
  • Environment and Weather Monitoring Applications
  • Habitat Monitoring Applications
  • Applications for Home Automation
  • Applications for Industries
  • Traffic Surveillance and Road Monitoring Applications
  • Applications for Health Monitoring , which is very broad area called as Body Area Network
  • Pollution Monitoring Applications
  • Applications to predict Natural Calamities
  • Applications for Surveillance and Intrusion Prevention
  • Structure Monitoring Applications like bridge monitoring apps



Friday, May 7, 2010

Addressing in WSN

Several approaches have been proposed for addressing in WSN(Wireless Sensor Network) . I will brief about some approaches which I have read.

"Lease based addressing for event driven wireless sensor networks" proposes to use a network level unique address to use a node temporarily. Three phases are proposed by this paper, they are boot up phase , address request phase and address release phase. Initially when the node boots up, it assigns itself a link local address and waits for the broadcast message from sink. Once the node receives the broadcast message from sink it send address request message to sink. The sink maintains a table of address assigned and table of address unassigned. If the sink has any unassigned address then the sink assigns an unique address to the requesting node. The node utilizes this address for required duration and then releases the address , the node uses release address message for this purpose.

"Distributed address assignment" specified in ZigBee standard provides a method to distribute addresses available at the coordinator among all nodes. It gives a mathematical formula, which is utilized to distibute address among the nodes. The coordinator distributes the available addresses to its immediate children, every router child is given equal number of addresses and non router child is given a single address. Every router node in turn assigns available addresses to its children (differentiating between router child and non router child). This method may not be very relevant if the network does not grow uniformly .

"A novel approach for dynamic address assignment in wireless sensor networks" proposes to use only a part of available addresses for initial distribution . This initial distribution is done as specified in ZigBee. Later if any other node requires more addresses then it requests for more addresses from its parent, which is allocated if the parent has required number of addresses else the request is relayed till the initial server. Th parent also may ask some of its children to return unused addresses if the parent is in need of addresses. This method is applicable even if the network grows non uniformly.

"Dynamic Address Allocation for Management and Control in WSN" proposes a novel method. The address server sends a broadcasts message once the network starts up and in turn all nodes select an address dynamically(randomly selected) and send the selected address to the server. The server checks if the address is already in use and if so the server asks the respective node (which ever is newer) to select another address using redo message. If the address is unique then the node is confirmed about the address using assign message. This process continues until the node gets an unique address.

"An energy efficient node address naming scheme for WSN" provides procedures for cluster based sensor networks. Along with the above specified methods there are several other approaches for addressing in WSN, one can refer to the references in the above papers for information on other approaches.

Friday, March 5, 2010

Save Energy - The Buzz Word in Sensor Network

WSN(Wireless Sensor Network)applications use sensor nodes which are mostly powered using a 3 V battery.Sensor networks are supposed to last at least for six months ,hence the available energy needs to be used very prudently. This is similar to usage of available financial sources during recession,every penny needs to be spent after much thought. Lot of research has been done and is being done in the field of optimal usage of energy in sensor networks.

I will be briefing about some of these energy saving methods in WSN. All different procedures and methods given here are from papers or patents which I have come across.

Dynamic Power Management(DPM) specifies five modes of operations for each sensor node.Each mode consumes different amount of power. The mode of operation of sensor node varies from being fully active to deep sleep state. Maximum energy is consumed in fully active state and least is consumed in deep sleep state. A method called as Dynamic Voltage Scaling (DVS) is also proposed , the operating frequency and operating voltage is varied according to the sensor node's mode. The components in sensor node such as transceiver , processor, sensors are dynamically switched off or put to sleep in different modes. By these dynamic changes in the sensor nodes energy conservation achieved in the sensor nodes. The table below summarizes the different sates of the sensor node.

State Processor Memory Sensor Radio
S0 Active Active On Tx, Rx
S1 Idle Sleep On Rx
S2 Sleep Sleep On Rx
S3 Sleep Sleep On Off
S4 Sleep Sleep Off Off

After DPM lets see how a protocol called "LEACH" to uses energy optimally. LEACH is one of the well known protocol in WSN. In this protocol data aggregation is done at Cluster heads before further forwarding data , this in turn reduces the amount of data being transferred and saves lot of energy, which would be otherwise consumed for transmission and reception of the un-aggregated data. In WSN data transmission or data reception is the most energy consuming activity compared any other activity so data aggregation at various levels helps in conservation of energy. LEACH also saves energy by making all nodes(other than cluster head) go to sleep when they do not having anything to communicate. All nodes are allocated different time slots to communicate by the cluster head hence the nodes are awake only during their time slot. The figure below shows Cluster Heads, normal nodes and Sink in LEACH protocol.



In paper titled "Power Efficient Organization of Wireless Sensor Network" authors try to use only the optimal number of sensor nodes from all available sensor nodes to cover a given area. By this unnecessary sensor nodes are made to sleep and unnecessary redundant data is avoided and also one can rotate between sleeping and active sensor nodes.

In paper tilted " Power efficient Topologies for Wireless Sensor Networks" authors propose a method to find a sensor network topology which consumes least power to cover given area, in other words the sensor nodes are placed such that they cover the given area by utilizing minimum number of sensor nodes.

There are some more protocols such as S-MAC, STEM, SMACS which try use to available energy in best possible way , if interested one can find them in Google. As I told earlier the efficient usage of energy in one of the most important and one of most researched topics in Wireless Sensor Network, hence one can find many articles, papers and patents on the same.