<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://anrg.usc.edu/contiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ashwinit</id>
		<title>Contiki - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://anrg.usc.edu/contiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ashwinit"/>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php/Special:Contributions/Ashwinit"/>
		<updated>2026-05-10T23:15:48Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1672</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1672"/>
				<updated>2015-01-02T00:13:30Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Scheduling algorithms for IEEE 802.15.4e networks - Pedro ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where: 	&amp;lt;sink_id&amp;gt; = Sink identification (starting at 0) and &amp;lt;algorithm&amp;gt; = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;br /&gt;
&lt;br /&gt;
== Heat Diffusion Routing Algorithm Contiki Implementation - Pradipta ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pradipta/heat-diffusion.git&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
This is a contiki implementation of the Heat Diffusion algorithm proposed by Reza Banirazi, Edmond A. Jonckheere, Bhaskar Krishnamachari in &amp;#039;&amp;#039;&amp;#039;“Heat-Diffusion: Pareto optimal dynamic routing for time-varying wireless networks“, International Conference on Computer Communications (INFOCOM), 2014&amp;#039;&amp;#039;&amp;#039;. Heat Diffusion routing is a new multi-hop wireless network routing protocol which is similar to Back-Pressure routing based on concepts of heat diffusion in classical physics&lt;br /&gt;
&lt;br /&gt;
== Backpressure Control Protocol on IPv6 stack of Contiki - Mrunal and Chhavi ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/chhavikapoor/EE652_Final&lt;br /&gt;
&lt;br /&gt;
Use the branch final_project from the repository.&lt;br /&gt;
&lt;br /&gt;
Follow the README-BCP in the final_project repository to simulate the implementation of BCP on IPv6 stack in COOJA.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;: &lt;br /&gt;
&lt;br /&gt;
BCP is an implementation of dynamic backpressure routing in which the routing and forwarding decisions are made on per packet basis that takes into consideration the backpressure weight of each of its neighbors. The ubiquitous use of TCP/IP protocol suite in web applications, peer to peer networking over the internet etc motivated us to implement the Backpressure Collection Protocol on the IPv6 Stack of Contiki. We implemented a functional version of BCP on the IPv6 stack of Contiki OS.&lt;br /&gt;
In our implementation, BCP Sink by default has the node_id = 1.&lt;br /&gt;
&lt;br /&gt;
== Modifications to RPL for Mobility - Pratyush Deshpande, Gopi Marella and Abhilash Hegde ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/pratyush18/contiki-new.git&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
Routing Protocol for Low power and lossy networks (RPL) has been recently adopted  IETF routing protocol standard for low power  wireless sensor networks and Internet of Things applications. Originally RPL is designed for static networks with no support for mobility. But, several IoT applications involve mobile nodes and thus there is a need to modify RPL for supporting mobile node scenarios. In RPL, routing takes place by formationof Destination Oriented Acyclic Graph (DODAG). Several control messages like DODAG Information Objects (DIOs), DODAG Information Solicitation and DODAG Advertisement Objects (DAOs) are exchanged for the DODAG formation. These control messages propagate throughout the network and collaboratively work to form the DODAG. The &lt;br /&gt;
control messages in RPL are controlled by several timers. These control message timers need to be modified for mobile node scenarios. The timers need to be optimized based on mobility of nodes in the network. In this paper we have modified the existing implementation of RPL protocol in Contiki Operating system to improve its performance for scenarios where mobile nodes are involved. We have also developed a test-bed for simulating mobile nodes in COOJA simulator and evaluating performance metrics like Packet delivery ratio, Power consumption and Average latency per packet.&lt;br /&gt;
&lt;br /&gt;
== LOADng for Contiki- Jiahao Liang, Zhikun Liu and Haimo Bai ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/jiahaoliang/EE652_LOADng&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
The LLN On-demand Ad hoc Distance-vector Routing Protocol - Next Generation (LOADng) is a routing protocol, derived from AODV and extended for use in Low power Lossy Networks (LLNs). A reactive protocol, the basic operations of LOAD include generation of Route Requests (RREQs) by a router (originator) for when discovering a route to a destination, forwarding of such RREQs until they reach the destination router, generation of Route Replies (RREPs) upon receipt of a RREQ by the destination, and forwarding of these RREPs towards the originator.&lt;br /&gt;
&lt;br /&gt;
== Location based query and data aggregation using RPL in ContikiOS- Ashwini Telang, Yash Goyal and Subhashini Sundaresan ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/ashwinit/652Final&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
Wireless sensor network has several automation applications in different industrial, urban and commercial settings which demand data collection from a group of nodes, data aggregation can be suitable in these cases to conserve the limited energy reserves on the nodes. In this paper we model routing mechanism based on location hierarchy and compare it with the existing implementation of RPL in Contiki. The tool used to evaluate the performance of the model is Cooja simulator. We have tested for two different topologies with respect to the performance metrics such as ETX, Latency and number of packets forwarded for both multicast and unicast queries. The result shows that the location based query and data aggregation models gives better performance in hierarchical location based topologies in which the sub-branches do not interfere.&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1671</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1671"/>
				<updated>2015-01-01T23:56:47Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Scheduling algorithms for IEEE 802.15.4e networks - Pedro ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where: 	&amp;lt;sink_id&amp;gt; = Sink identification (starting at 0) and &amp;lt;algorithm&amp;gt; = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;br /&gt;
&lt;br /&gt;
== Heat Diffusion Routing Algorithm Contiki Implementation - Pradipta ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pradipta/heat-diffusion.git&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
This is a contiki implementation of the Heat Diffusion algorithm proposed by Reza Banirazi, Edmond A. Jonckheere, Bhaskar Krishnamachari in &amp;#039;&amp;#039;&amp;#039;“Heat-Diffusion: Pareto optimal dynamic routing for time-varying wireless networks“, International Conference on Computer Communications (INFOCOM), 2014&amp;#039;&amp;#039;&amp;#039;. Heat Diffusion routing is a new multi-hop wireless network routing protocol which is similar to Back-Pressure routing based on concepts of heat diffusion in classical physics&lt;br /&gt;
&lt;br /&gt;
== Backpressure Control Protocol on IPv6 stack of Contiki - Mrunal and Chhavi ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/chhavikapoor/EE652_Final&lt;br /&gt;
&lt;br /&gt;
Use the branch final_project from the repository.&lt;br /&gt;
&lt;br /&gt;
Follow the README-BCP in the final_project repository to simulate the implementation of BCP on IPv6 stack in COOJA.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;: &lt;br /&gt;
&lt;br /&gt;
BCP is an implementation of dynamic backpressure routing in which the routing and forwarding decisions are made on per packet basis that takes into consideration the backpressure weight of each of its neighbors. The ubiquitous use of TCP/IP protocol suite in web applications, peer to peer networking over the internet etc motivated us to implement the Backpressure Collection Protocol on the IPv6 Stack of Contiki. We implemented a functional version of BCP on the IPv6 stack of Contiki OS.&lt;br /&gt;
In our implementation, BCP Sink by default has the node_id = 1.&lt;br /&gt;
&lt;br /&gt;
== Modifications to RPL for Mobility - Pratyush Deshpande, Gopi Marella and Abhilash Hegde ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/pratyush18/contiki-new.git&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
Routing Protocol for Low power and lossy networks (RPL) has been recently adopted  IETF routing protocol standard for low power  wireless sensor networks and Internet of Things applications. Originally RPL is designed for static networks with no support for mobility. But, several IoT applications involve mobile nodes and thus there is a need to modify RPL for supporting mobile node scenarios. In RPL, routing takes place by formationof Destination Oriented Acyclic Graph (DODAG). Several control messages like DODAG Information Objects (DIOs), DODAG Information Solicitation and DODAG Advertisement Objects (DAOs) are exchanged for the DODAG formation. These control messages propagate throughout the network and collaboratively work to form the DODAG. The &lt;br /&gt;
control messages in RPL are controlled by several timers. These control message timers need to be modified for mobile node scenarios. The timers need to be optimized based on mobility of nodes in the network. In this paper we have modified the existing implementation of RPL protocol in Contiki Operating system to improve its performance for scenarios where mobile nodes are involved. We have also developed a test-bed for simulating mobile nodes in COOJA simulator and evaluating performance metrics like Packet delivery ratio, Power consumption and Average latency per packet.&lt;br /&gt;
&lt;br /&gt;
== LOADng for Contiki- Jiahao Liang, Zhikun Liu and Haimo Bai ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/jiahaoliang/EE652_LOADng&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
The LLN On-demand Ad hoc Distance-vector Routing Protocol - Next Generation (LOADng) is a routing protocol, derived from AODV and extended for use in Low power Lossy Networks (LLNs). A reactive protocol, the basic operations of LOAD include generation of Route Requests (RREQs) by a router (originator) for when discovering a route to a destination, forwarding of such RREQs until they reach the destination router, generation of Route Replies (RREPs) upon receipt of a RREQ by the destination, and forwarding of these RREPs towards the originator.&lt;br /&gt;
&lt;br /&gt;
== Location based query and data aggregation using RPL in ContikiOS- Ashwini Telang, Yash Goyal and Subhashini Sundaresan ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
Wireless sensor network has several automation applications in different industrial, urban and commercial settings which demand data collection from a group of nodes, data aggregation can be suitable in these cases to conserve the limited energy reserves on the nodes. In this paper we model routing mechanism based on location hierarchy and compare it with the existing implementation of RPL in Contiki. The tool used to evaluate the performance of the model is Cooja simulator. We have tested for two different topologies with respect to the performance metrics such as ETX, Latency and number of packets forwarded for both multicast and unicast queries. The result shows that the location based query and data aggregation models gives better performance in hierarchical location based topologies in which the sub-branches do not interfere.&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1670</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1670"/>
				<updated>2015-01-01T23:55:24Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Scheduling algorithms for IEEE 802.15.4e networks - Pedro ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where: 	&amp;lt;sink_id&amp;gt; = Sink identification (starting at 0) and &amp;lt;algorithm&amp;gt; = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;br /&gt;
&lt;br /&gt;
== Heat Diffusion Routing Algorithm Contiki Implementation - Pradipta ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pradipta/heat-diffusion.git&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
This is a contiki implementation of the Heat Diffusion algorithm proposed by Reza Banirazi, Edmond A. Jonckheere, Bhaskar Krishnamachari in &amp;#039;&amp;#039;&amp;#039;“Heat-Diffusion: Pareto optimal dynamic routing for time-varying wireless networks“, International Conference on Computer Communications (INFOCOM), 2014&amp;#039;&amp;#039;&amp;#039;. Heat Diffusion routing is a new multi-hop wireless network routing protocol which is similar to Back-Pressure routing based on concepts of heat diffusion in classical physics&lt;br /&gt;
&lt;br /&gt;
== Backpressure Control Protocol on IPv6 stack of Contiki - Mrunal and Chhavi ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/chhavikapoor/EE652_Final&lt;br /&gt;
&lt;br /&gt;
Use the branch final_project from the repository.&lt;br /&gt;
&lt;br /&gt;
Follow the README-BCP in the final_project repository to simulate the implementation of BCP on IPv6 stack in COOJA.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;: &lt;br /&gt;
&lt;br /&gt;
BCP is an implementation of dynamic backpressure routing in which the routing and forwarding decisions are made on per packet basis that takes into consideration the backpressure weight of each of its neighbors. The ubiquitous use of TCP/IP protocol suite in web applications, peer to peer networking over the internet etc motivated us to implement the Backpressure Collection Protocol on the IPv6 Stack of Contiki. We implemented a functional version of BCP on the IPv6 stack of Contiki OS.&lt;br /&gt;
In our implementation, BCP Sink by default has the node_id = 1.&lt;br /&gt;
&lt;br /&gt;
== Modifications to RPL for Mobility - Pratyush Deshpande, Gopi Marella and Abhilash Hegde ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/pratyush18/contiki-new.git&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
Routing Protocol for Low power and lossy networks (RPL) has been recently adopted  IETF routing protocol standard for low power  wireless sensor networks and Internet of Things applications. Originally RPL is designed for static networks with no support for mobility. But, several IoT applications involve mobile nodes and thus there is a need to modify RPL for supporting mobile node scenarios. In RPL, routing takes place by formationof Destination Oriented Acyclic Graph (DODAG). Several control messages like DODAG Information Objects (DIOs), DODAG Information Solicitation and DODAG Advertisement Objects (DAOs) are exchanged for the DODAG formation. These control messages propagate throughout the network and collaboratively work to form the DODAG. The &lt;br /&gt;
control messages in RPL are controlled by several timers. These control message timers need to be modified for mobile node scenarios. The timers need to be optimized based on mobility of nodes in the network. In this paper we have modified the existing implementation of RPL protocol in Contiki Operating system to improve its performance for scenarios where mobile nodes are involved. We have also developed a test-bed for simulating mobile nodes in COOJA simulator and evaluating performance metrics like Packet delivery ratio, Power consumption and Average latency per packet.&lt;br /&gt;
&lt;br /&gt;
== LOADng for Contiki- Jiahao Liang, Zhikun Liu and Haimo Bai ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/jiahaoliang/EE652_LOADng&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
The LLN On-demand Ad hoc Distance-vector Routing Protocol - Next Generation (LOADng) is a routing protocol, derived from AODV and extended for use in Low power Lossy Networks (LLNs). A reactive protocol, the basic operations of LOAD include generation of Route Requests (RREQs) by a router (originator) for when discovering a route to a destination, forwarding of such RREQs until they reach the destination router, generation of Route Replies (RREPs) upon receipt of a RREQ by the destination, and forwarding of these RREPs towards the originator.&lt;br /&gt;
&lt;br /&gt;
== Location based query and data aggregation using RPL in ContikiOS- Ashwini Telang, Yash Goyal and Subhashini Sundaresan ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1669</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1669"/>
				<updated>2015-01-01T23:54:08Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Scheduling algorithms for IEEE 802.15.4e networks - Pedro ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where: 	&amp;lt;sink_id&amp;gt; = Sink identification (starting at 0) and &amp;lt;algorithm&amp;gt; = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;br /&gt;
&lt;br /&gt;
== Heat Diffusion Routing Algorithm Contiki Implementation - Pradipta ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pradipta/heat-diffusion.git&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
This is a contiki implementation of the Heat Diffusion algorithm proposed by Reza Banirazi, Edmond A. Jonckheere, Bhaskar Krishnamachari in &amp;#039;&amp;#039;&amp;#039;“Heat-Diffusion: Pareto optimal dynamic routing for time-varying wireless networks“, International Conference on Computer Communications (INFOCOM), 2014&amp;#039;&amp;#039;&amp;#039;. Heat Diffusion routing is a new multi-hop wireless network routing protocol which is similar to Back-Pressure routing based on concepts of heat diffusion in classical physics&lt;br /&gt;
&lt;br /&gt;
== Backpressure Control Protocol on IPv6 stack of Contiki - Mrunal and Chhavi ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/chhavikapoor/EE652_Final&lt;br /&gt;
&lt;br /&gt;
Use the branch final_project from the repository.&lt;br /&gt;
&lt;br /&gt;
Follow the README-BCP in the final_project repository to simulate the implementation of BCP on IPv6 stack in COOJA.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;: &lt;br /&gt;
&lt;br /&gt;
BCP is an implementation of dynamic backpressure routing in which the routing and forwarding decisions are made on per packet basis that takes into consideration the backpressure weight of each of its neighbors. The ubiquitous use of TCP/IP protocol suite in web applications, peer to peer networking over the internet etc motivated us to implement the Backpressure Collection Protocol on the IPv6 Stack of Contiki. We implemented a functional version of BCP on the IPv6 stack of Contiki OS.&lt;br /&gt;
In our implementation, BCP Sink by default has the node_id = 1.&lt;br /&gt;
&lt;br /&gt;
== Modifications to RPL for Mobility - Pratyush Deshpande, Gopi Marella and Abhilash Hegde ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/pratyush18/contiki-new.git&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
Routing Protocol for Low power and lossy networks (RPL) has been recently adopted  IETF routing protocol standard for low power  wireless sensor networks and Internet of Things applications. Originally RPL is designed for static networks with no support for mobility. But, several IoT applications involve mobile nodes and thus there is a need to modify RPL for supporting mobile node scenarios. In RPL, routing takes place by formationof Destination Oriented Acyclic Graph (DODAG). Several control messages like DODAG Information Objects (DIOs), DODAG Information Solicitation and DODAG Advertisement Objects (DAOs) are exchanged for the DODAG formation. These control messages propagate throughout the network and collaboratively work to form the DODAG. The &lt;br /&gt;
control messages in RPL are controlled by several timers. These control message timers need to be modified for mobile node scenarios. The timers need to be optimized based on mobility of nodes in the network. In this paper we have modified the existing implementation of RPL protocol in Contiki Operating system to improve its performance for scenarios where mobile nodes are involved. We have also developed a test-bed for simulating mobile nodes in COOJA simulator and evaluating performance metrics like Packet delivery ratio, Power consumption and Average latency per packet.&lt;br /&gt;
&lt;br /&gt;
== LOADng for Contiki- Jiahao Liang, Zhikun Liu and Haimo Bai ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/jiahaoliang/EE652_LOADng&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
The LLN On-demand Ad hoc Distance-vector Routing Protocol - Next Generation (LOADng) is a routing protocol, derived from AODV and extended for use in Low power Lossy Networks (LLNs). A reactive protocol, the basic operations of LOAD include generation of Route Requests (RREQs) by a router (originator) for when discovering a route to a destination, forwarding of such RREQs until they reach the destination router, generation of Route Replies (RREPs) upon receipt of a RREQ by the destination, and forwarding of these RREPs towards the originator.&lt;br /&gt;
&lt;br /&gt;
== Location based query and data aggregation using RPL in ContikiOS- Ashwini Telang, Yash Goyal and Subhashini Sundaresan ==&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1533</id>
		<title>Contiki tutorials</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1533"/>
				<updated>2014-11-09T20:14:55Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Main_Page | Back to Main Page]]&lt;br /&gt;
&lt;br /&gt;
== List of Tutorials ==&lt;br /&gt;
&lt;br /&gt;
Completed&lt;br /&gt;
# [[Installation]]&lt;br /&gt;
# [[Hello World]]&lt;br /&gt;
# [[Broadcast Example]]&lt;br /&gt;
# [[Collect View]]&lt;br /&gt;
# [[Contiki build system]]&lt;br /&gt;
# [[Interfacing with Python]]&lt;br /&gt;
# [[Sensor acquisition]] (light, temperature, humidity)&lt;br /&gt;
&lt;br /&gt;
Need review&lt;br /&gt;
# [[Timers]] Tim, Leo&lt;br /&gt;
# [[CFS-Coffee]] Kevin&lt;br /&gt;
&lt;br /&gt;
Starting&lt;br /&gt;
# [[Tutornet]] Pedro, Kwame&lt;br /&gt;
# [[Cooja Simulator]] (Getting started, debugging) Pedro&lt;br /&gt;
# [[Network Stack]] Yash&lt;br /&gt;
# [[CSMA]] Tim, Leo&lt;br /&gt;
# [[RSS measurement]]&lt;br /&gt;
# [[RPL objective function &amp;amp; simulation using DGRM model in cooja  ]] Ashwini Telang&lt;br /&gt;
# [[RPL UDP]] Jiahao Liang&lt;br /&gt;
# [[MAC protocols in ContikiOS]] Pedro&lt;br /&gt;
# [[RPL Border Router]] Chhavi&lt;br /&gt;
# [[REST example running on Cooja and Sky motes]] Mrunal Muni &lt;br /&gt;
# [[Trickle library]] Subhashini Sundaresan&lt;br /&gt;
# [[Packetbuffer Basics]] Pradipta&lt;br /&gt;
# [[Antelope(Database Management System) - Contiki]] Gopi Krishna&lt;br /&gt;
# [[Mobility of Nodes in Cooja]] Pratyush Deshpande&lt;br /&gt;
# [[Contiki Shell]] Abhilash Nagaraj Hegde&lt;br /&gt;
# [[Contiki Coffee File System]] Zhikun Liu&lt;br /&gt;
# [[Contiki Programming Guide]] Haimo Bai&lt;br /&gt;
# [[Analyse of a real 6LoWPAN network using a Contiki-based sniffer module]] Yash Goyal&lt;br /&gt;
&amp;lt;!--[[Processes]] Yash --&amp;gt;&lt;br /&gt;
&amp;lt;!--[[RPL objective function modification and simulation in cooja]]--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--# [[Collect-view Code Details]] Pradipta --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Be sure to include references in your tutorials, especially if you quote material from other sites!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1514</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1514"/>
				<updated>2014-11-09T10:11:09Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopologyone.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the &amp;#039;&amp;#039;&amp;#039;API for RPL Objective function&amp;#039;&amp;#039;&amp;#039; are:&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An example for modifying the objective function can be for &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Load Balancing application&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. The new objective function should be such that it should choose a route that minimizes the ETX and if there are multiple routes with same ETX or having ETX in a predefined range then it should minimize the maximum number of packets forwarded by any one of these routes. This means that any one node is not burdened with the load of forwarding packets while other nodes are under utilized.&amp;lt;br&amp;gt;&lt;br /&gt;
One approach to this problem is to define a completely new objective function as described earlier. Another approach is to modify the already present rpl-mhrof.c file. As this OF already uses the minimum ETX part of the problem we just have to implement the load balancing part. The load balancing comes in to picture when a child has multiple routes i.e. parents. Hence the best parent function shouhld be modified accordingly.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Another example could be to choose the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;shortest path with nodes using minimum energy&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. Here the objective function should be such that it first identifies the nodes using minimum energy or within a range and then use minimum hops to reach the destination using these nodes. This can have an application in situation where a node has to quickly send its data without burdening a node which is deficient of energy.&amp;lt;br&amp;gt;&lt;br /&gt;
In this scenario the objective function using energy metric which is already present in the current implementation of rpl-mhrof,c can be used with additional logic for choosing minimum hop count while choosing a parent.&amp;lt;br&amp;gt;&lt;br /&gt;
Other scenarios which necessitate a new or modified objective function can be based on different link metrics such as throughput, link quality level, latency etc.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&amp;lt;br&amp;gt;&lt;br /&gt;
[https://github.com/contiki-os/contiki/tree/release-2-7 https://github.com/contiki-os/contiki/tree/release-2-7] Github branch of ContikiOS-2.7&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1513</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1513"/>
				<updated>2014-11-09T10:08:54Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopologyone.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An example for modifying the objective function can be for &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Load Balancing application&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. The new objective function should be such that it should choose a route that minimizes the ETX and if there are multiple routes with same ETX or having ETX in a predefined range then it should minimize the maximum number of packets forwarded by any one of these routes. This means that any one node is not burdened with the load of forwarding packets while other nodes are under utilized.&amp;lt;br&amp;gt;&lt;br /&gt;
One approach to this problem is to define a completely new objective function as described earlier. Another approach is to modify the already present rpl-mhrof.c file. As this OF already uses the minimum ETX part of the problem we just have to implement the load balancing part. The load balancing comes in to picture when a child has multiple routes i.e. parents. Hence the best parent function shouhld be modified accordingly.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Another example could be to choose the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;shortest path with nodes using minimum energy&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. Here the objective function should be such that it first identifies the nodes using minimum energy or within a range and then use minimum hops to reach the destination using these nodes. This can have an application in situation where a node has to quickly send its data without burdening a node which is deficient of energy.&amp;lt;br&amp;gt;&lt;br /&gt;
In this scenario the objective function using energy metric which is already present in the current implementation of rpl-mhrof,c can be used with additional logic for choosing minimum hop count while choosing a parent.&amp;lt;br&amp;gt;&lt;br /&gt;
Other scenarios which necessitate a new or modified objective function can be based on different link metrics such as throughput, link quality level, latency etc.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&amp;lt;br&amp;gt;&lt;br /&gt;
[https://github.com/contiki-os/contiki/tree/release-2-7 https://github.com/contiki-os/contiki/tree/release-2-7] Github branch of ContikiOS-2.7&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1512</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1512"/>
				<updated>2014-11-09T10:08:22Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopologyone.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An example for modifying the objective function can be for &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Load Balancing application&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. The new objective function should be such that it should choose a route that minimizes the ETX and if there are multiple routes with same ETX or having ETX in a predefined range then it should minimize the maximum number of packets forwarded by any one of these routes. This means that any one node is not burdened with the load of forwarding packets while other nodes are under utilized.&amp;lt;br&amp;gt;&lt;br /&gt;
One approach to this problem is to define a completely new objective function as described earlier. Another approach is to modify the already present rpl-mhrof.c file. As this OF already uses the minimum ETX part of the problem we just have to implement the load balancing part. The load balancing comes in to picture when a child has multiple routes i.e. parents. Hence the best parent function shouhld be modified accordingly.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Another example could be to choose the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;shortest path with nodes using minimum energy&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. Here the objective function should be such that it first identifies the nodes using minimum energy or within a range and then use minimum hops to reach the destination using these nodes. This can have an application in situation where a node has to quickly send its data without burdening a node which is deficient of energy.&amp;lt;br&amp;gt;&lt;br /&gt;
In this scenario the objective function using energy metric which is already present in the current implementation of rpl-mhrof,c can be used with additional logic for choosing minimum hop count while choosing a parent.&amp;lt;br&amp;gt;&lt;br /&gt;
Other scenarios which necessitate a new or modified objective function can be based on different link metrics such as throughput, link quality level, latency etc.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&amp;lt;br&amp;gt;&lt;br /&gt;
[https://github.com/contiki-os/contiki/tree/release-2-7 https://github.com/contiki-os/contiki/tree/release-2-7] Githhub branch of ContikiOS-2.7&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1511</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1511"/>
				<updated>2014-11-09T10:07:54Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopologyone.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An example for modifying the objective function can be for &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Load Balancing application&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. The new objective function should be such that it should choose a route that minimizes the ETX and if there are multiple routes with same ETX or having ETX in a predefined range then it should minimize the maximum number of packets forwarded by any one of these routes. This means that any one node is not burdened with the load of forwarding packets while other nodes are under utilized.&amp;lt;br&amp;gt;&lt;br /&gt;
One approach to this problem is to define a completely new objective function as described earlier. Another approach is to modify the already present rpl-mhrof.c file. As this OF already uses the minimum ETX part of the problem we just have to implement the load balancing part. The load balancing comes in to picture when a child has multiple routes i.e. parents. Hence the best parent function shouhld be modified accordingly.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Another example could be to choose the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;shortest path with nodes using minimum energy&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. Here the objective function should be such that it first identifies the nodes using minimum energy or within a range and then use minimum hops to reach the destination using these nodes. This can have an application in situation where a node has to quickly send its data without burdening a node which is deficient of energy.&amp;lt;br&amp;gt;&lt;br /&gt;
In this scenario the objective function using energy metric which is already present in the current implementation of rpl-mhrof,c can be used with additional logic for choosing minimum hop count while choosing a parent.&amp;lt;br&amp;gt;&lt;br /&gt;
Other scenarios which necessitate a new or modified objective function can be based on different link metrics such as throughput, link quality level, latency etc.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
[https://github.com/contiki-os/contiki/tree/release-2-7] Githhub branch of ContikiOS-2.7&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1510</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1510"/>
				<updated>2014-11-09T10:03:31Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopologyone.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An example for modifying the objective function can be for &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Load Balancing application&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. The new objective function should be such that it should choose a route that minimizes the ETX and if there are multiple routes with same ETX or having ETX in a predefined range then it should minimize the maximum number of packets forwarded by any one of these routes. This means that any one node is not burdened with the load of forwarding packets while other nodes are under utilized.&amp;lt;br&amp;gt;&lt;br /&gt;
One approach to this problem is to define a completely new objective function as described earlier. Another approach is to modify the already present rpl-mhrof.c file. As this OF already uses the minimum ETX part of the problem we just have to implement the load balancing part. The load balancing comes in to picture when a child has multiple routes i.e. parents. Hence the best parent function shouhld be modified accordingly.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Another example could be to choose the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;shortest path with nodes using minimum energy&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. Here the objective function should be such that it first identifies the nodes using minimum energy or within a range and then use minimum hops to reach the destination using these nodes. This can have an application in situation where a node has to quickly send its data without burdening a node which is deficient of energy.&amp;lt;br&amp;gt;&lt;br /&gt;
In this scenario the objective function using energy metric which is already present in the current implementation of rpl-mhrof,c can be used with additional logic for choosing minimum hop count while choosing a parent.&amp;lt;br&amp;gt;&lt;br /&gt;
Other scenarios which necessitate a new or modified objective function can be based on different link metrics such as throughput, link quality level, latency etc.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1509</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1509"/>
				<updated>2014-11-09T10:02:10Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopologyone.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
An example for modifying the objective function can be for Load Balancing application. The new objective function should be such that it should choose a route that minimizes the ETX and if there are multiple routes with same ETX or having ETX in a predefined range then it should minimize the maximum number of packets forwarded by any one of these routes. This means that any one node is not burdened with the load of forwarding packets while other nodes are under utilized.&amp;lt;br&amp;gt;&lt;br /&gt;
One approach to this problem is to define a completely new objective function as described earlier. Another approach is to modify the already present rpl-mhrof.c file. As this OF already uses the minimum ETX part of the problem we just have to implement the load balancing part. The load balancing comes in to picture when a child has multiple routes i.e. parents. Hence the best parent function shouhld be modified accordingly.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Another example could be to choose the shortest path with nodes using minimum energy. Here the objective function should be such that it first identifies the nodes using minimum energy or within a range and then use minimum hops to reach the destination using these nodes. This can have an application in situation where a node has to quickly send its data without burdening a node which is deficient of energy.&amp;lt;br&amp;gt;&lt;br /&gt;
In this scenario the objective function using energy metric which is already present in the current implementation of rpl-mhrof,c can be used with additional logic for choosing minimum hop count while choosing a parent.&amp;lt;br&amp;gt;&lt;br /&gt;
Other scenarios which necessitate a new or modified objective function can be based on different link metrics such as throughput, link quality level, latency etc.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1508</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1508"/>
				<updated>2014-11-09T09:50:50Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopologyone.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&amp;lt;br&amp;gt;&lt;br /&gt;
An example for modifying the objective function can be for Load Balancing application. The new objective function should be such that it should choose a route that minimizes the ETX and if there are multiple routes with same ETX or having ETX in a predefined range then it should minimize the maximum number of packets forwarded by any one of these routes. This means that any one node is not burdened with the load of forwarding packets while other nodes are under utilized.&amp;lt;br&amp;gt;&lt;br /&gt;
One approach to this problem is to define a completely new objective function as described earlier. Another approach is to modify the already present rpl-mhrof.c file. As this OF already uses the minimum ETX part of the problem we just have to implement the load balancing part. The load balancing comes in to picture when a child has multiple routes i.e. parents. Hence the best parent function shouhld be modified accordingly.&amp;lt;br&amp;gt;&lt;br /&gt;
Another example could be to choose the shortest path with nodes using minimum energy. Here the objective function should be such that it first identifies the nodes using minimum energy or within a range and then use minimum hops to reach the destination using these nodes. This can have an application in situation where a node has to quickly send its data without burdening a node which is deficient of energy.&amp;lt;br&amp;gt;&lt;br /&gt;
In this scenario the objective function using energy metric which is already present in the current implementation of rpl-mhrof,c can be used with additional logic for choosing minimum hop count while choosing a parent.&amp;lt;br&amp;gt;&lt;br /&gt;
Other scenarios which necessitate a new or modified objective function can be based on different link metrics such as throughput, link quality level, latency etc.&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1507</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1507"/>
				<updated>2014-11-09T09:49:56Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopologyone.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&amp;lt;br&amp;gt;&lt;br /&gt;
An example for modifying the objective function can be for Load Balancing application. The new objective function should be such that it should choose a route that minimizes the ETX and if there are multiple routes with same ETX or having ETX in a predefined range then it should minimize the maximum number of packets forwarded by any one of these routes. This means that any one node is not burdened with the load of forwarding packets while other nodes are under utilized.&amp;lt;br&amp;gt;&lt;br /&gt;
One approach to this problem is to define a completely new objective function as described earlier. Another approach is to modify the already present rpl-mhrof.c file. As this OF already uses the minimum ETX part of the problem we just have to implement the load balancing part. The load balancing comes in to picture when a child has multiple routes i.e. parents. Hence the best parent function shouhld be modified accordingly.&amp;lt;br&amp;gt;&lt;br /&gt;
Another problem could be to choose the shortest path with nodes using minimum energy. Here the objective function should be such that it first identifies the nodes using minimum energy or within a range and then use minimum hops to reach the destination using these nodes. This can have an application in situation where a node has to quickly send its data without burdening a node which is deficient of energy.&amp;lt;br&amp;gt;&lt;br /&gt;
In this scenario the objective function using energy metric which is already present in the current implementation of rpl-mhrof,c can be used with additional logic for choosing minimum hop count while choosing a parent.&amp;lt;br&amp;gt;&lt;br /&gt;
Other scenarios which necessitate a new or modified objective function can be based on different link metrics such as throughput, link quality level, latency etc.&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:SampleNetTopologyone.png&amp;diff=1506</id>
		<title>File:SampleNetTopologyone.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:SampleNetTopologyone.png&amp;diff=1506"/>
				<updated>2014-11-09T09:42:29Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1505</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1505"/>
				<updated>2014-11-09T09:13:59Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
An objective function basically uses a link metric and has function which subject to a contraint tries to choose the best path for routing.&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1504</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1504"/>
				<updated>2014-11-09T09:07:08Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample network topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1503</id>
		<title>RPL objective function &amp; simulation using DGRM model in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_%26_simulation_using_DGRM_model_in_cooja&amp;diff=1503"/>
				<updated>2014-11-09T09:06:47Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: Created page with &amp;quot; Back to Contiki Tutorials  __TOC__  == Introduction == The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Direct...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1502</id>
		<title>Contiki tutorials</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1502"/>
				<updated>2014-11-09T09:05:38Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* List of Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Main_Page | Back to Main Page]]&lt;br /&gt;
&lt;br /&gt;
== List of Tutorials ==&lt;br /&gt;
&lt;br /&gt;
Completed&lt;br /&gt;
# [[Installation]]&lt;br /&gt;
# [[Hello World]]&lt;br /&gt;
# [[Broadcast Example]]&lt;br /&gt;
# [[Collect View]]&lt;br /&gt;
# [[Contiki build system]]&lt;br /&gt;
# [[Interfacing with Python]]&lt;br /&gt;
# [[Sensor acquisition]] (light, temperature, humidity)&lt;br /&gt;
&lt;br /&gt;
Need review&lt;br /&gt;
# [[Timers]] Tim, Leo&lt;br /&gt;
# [[CFS-Coffee]] Kevin&lt;br /&gt;
&lt;br /&gt;
Starting&lt;br /&gt;
# [[Tutornet]] Pedro, Kwame&lt;br /&gt;
# [[Cooja Simulator]] (Getting started, debugging) Pedro&lt;br /&gt;
# [[Network Stack]] Yash&lt;br /&gt;
# [[CSMA]] Tim, Leo&lt;br /&gt;
# [[RSS measurement]]&lt;br /&gt;
# [[RPL objective function &amp;amp; simulation using DGRM model in cooja  ]] Ashwini Telang&lt;br /&gt;
# [[RPL UDP]] Jiahao Liang&lt;br /&gt;
# [[MAC protocols in ContikiOS]] Pedro&lt;br /&gt;
# [[RPL Border Router]] Chhavi&lt;br /&gt;
# [[REST example running on Cooja and Sky motes]] Mrunal Muni &lt;br /&gt;
# [[Trickle library]] Subhashini Sundaresan&lt;br /&gt;
# [[Packetbuffer Basics]] Pradipta&lt;br /&gt;
# [[Antelope(Database Management System) - Contiki]] Gopi Krishna&lt;br /&gt;
# [[Mobility of Nodes in Cooja]] Pratyush Deshpande&lt;br /&gt;
# [[Contiki Shell]] Abhilash Nagaraj Hegde&lt;br /&gt;
# [[Contiki Coffee File System]] Zhikun Liu&lt;br /&gt;
# [[Contiki Programming Guide]] Haimo Bai&lt;br /&gt;
# [[Analyse of a real 6LoWPAN network using a Contiki-based sniffer module]] Yash Goyal&lt;br /&gt;
&amp;lt;!--[[Processes]] Yash --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--# [[Collect-view Code Details]] Pradipta --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Be sure to include references in your tutorials, especially if you quote material from other sites!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1501</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1501"/>
				<updated>2014-11-09T09:00:52Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
The above figure shows a sample topology which can be used.&amp;lt;br&amp;gt;&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1500</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1500"/>
				<updated>2014-11-09T08:59:31Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To define a completely new Objective Function file(without modifying the existing one) the following functions must be defined in them. Also the makefile should be accordingly modified and care should be taken that your new file should not run into compilation and linking errors. Some of the RPL API functions are:&amp;lt;br&amp;gt;&lt;br /&gt;
* reset(dag): Resets the objective function state for a specific DAG. This function is called when doing a global repair on the DAG.&lt;br /&gt;
* neighbor_link_callback(parent, status, etx): Receives link-layer neighbor information.&lt;br /&gt;
* best_parent(parent1, parent2): Compares two parents and returns the best one, according to the OF.&lt;br /&gt;
* best_dag(dag1, dag2): Compares two DAGs and returns the best one, according to the OF.&lt;br /&gt;
* calculate_rank(parent, base_rank): Calculates a rank value using the parent rank and a base rank.&lt;br /&gt;
* update_metric_container(dag): Updates the metric container for outgoing DIOs in a certain DAG. If the objective function of the DAG does not use metric containers, the function should set the object type to RPL_DAG_MC_NONE.&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1499</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1499"/>
				<updated>2014-11-09T08:26:31Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* best_parent(rpl_parent_t *p1, rpl_parent_t *p2) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParents.png|border|500px|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:BestParents.png&amp;diff=1498</id>
		<title>File:BestParents.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:BestParents.png&amp;diff=1498"/>
				<updated>2014-11-09T08:24:58Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1497</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1497"/>
				<updated>2014-11-09T08:17:18Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|border|300px|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1496</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1496"/>
				<updated>2014-11-09T08:15:42Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalculateRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:CalculateRank.png&amp;diff=1495</id>
		<title>File:CalculateRank.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:CalculateRank.png&amp;diff=1495"/>
				<updated>2014-11-09T08:15:10Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:CalcRank.png&amp;diff=1494</id>
		<title>File:CalcRank.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:CalcRank.png&amp;diff=1494"/>
				<updated>2014-11-09T08:13:33Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: Ashwinit uploaded a new version of &amp;amp;quot;File:CalcRank.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:CalcRank.png&amp;diff=1493</id>
		<title>File:CalcRank.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:CalcRank.png&amp;diff=1493"/>
				<updated>2014-11-09T08:12:51Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: Ashwinit uploaded a new version of &amp;amp;quot;File:CalcRank.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:CalcRank.png&amp;diff=1492</id>
		<title>File:CalcRank.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:CalcRank.png&amp;diff=1492"/>
				<updated>2014-11-09T08:11:59Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: Ashwinit uploaded a new version of &amp;amp;quot;File:CalcRank.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:SampleNetTopology.png&amp;diff=1491</id>
		<title>File:SampleNetTopology.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:SampleNetTopology.png&amp;diff=1491"/>
				<updated>2014-11-09T08:02:20Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: Ashwinit uploaded a new version of &amp;amp;quot;File:SampleNetTopology.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1490</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1490"/>
				<updated>2014-11-09T07:59:04Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|border|500px|center|Sample Network Topology]]&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1489</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1489"/>
				<updated>2014-11-09T07:58:35Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Example scenario for objective function modification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
[[File:SampleNetTopology.png|frame|center|Sample Network Topology]]&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:SampleNetTopology.png&amp;diff=1487</id>
		<title>File:SampleNetTopology.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:SampleNetTopology.png&amp;diff=1487"/>
				<updated>2014-11-09T07:57:37Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: Ashwinit uploaded a new version of &amp;amp;quot;File:SampleNetTopology.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=File:SampleNetTopology.png&amp;diff=1486</id>
		<title>File:SampleNetTopology.png</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=File:SampleNetTopology.png&amp;diff=1486"/>
				<updated>2014-11-09T07:56:17Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1468</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1468"/>
				<updated>2014-11-09T07:27:53Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Related Files and Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
== Example scenario for objective function modification ==&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1465</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1465"/>
				<updated>2014-11-09T07:26:44Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* You Will learn */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Example scenarios and modification of RPL Objective Function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1458</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1458"/>
				<updated>2014-11-09T07:24:49Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General Issues ==&lt;br /&gt;
While running cooja be sure to run as sudo ant run since running only ant run might throw up some errors since it might not be able to access some files.&amp;lt;br&amp;gt;&lt;br /&gt;
If the network is huge or ETX values are large, it might take a while for the packets from the leaf node to reach the root sink node. Hence you might have to run the simulation for a long time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1456</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1456"/>
				<updated>2014-11-09T07:23:13Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Cooja Simulation using DGRM model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The DGRM model is used since it is easier to change the link reception rate. Also it is easier to form a link between two desired nodes excluding the others.&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1446</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1446"/>
				<updated>2014-11-09T07:12:00Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&amp;lt;br&amp;gt;&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1445</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1445"/>
				<updated>2014-11-09T07:11:39Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6552 Objective Function Zero for the Routing Protocol for Low-Power and Lossy Networks (RPL)&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
[http://home.deib.polimi.it/cesana/teaching/IoT/papers/routing/RPL.pdf] IPSO Alliance RPL April 2011&lt;br /&gt;
[http://dunkels.com/adam/ko11contikirpl.pdf] ContikiRPL&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1438</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1438"/>
				<updated>2014-11-09T07:00:15Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
RFC 6550 RPL: IPv6 Routing Protocol for Low-Power and Lossy Networks&amp;lt;br&amp;gt;&lt;br /&gt;
RFC 6719 MRHOF: The Minimum Rank with Hysteresis Objective Function &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1420</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1420"/>
				<updated>2014-11-09T06:44:46Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Source Code and used Directories */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1418</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1418"/>
				<updated>2014-11-09T06:44:28Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Source Code and used Directories */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and used Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&amp;lt;br&amp;gt;&lt;br /&gt;
~/contiki-2.7/tools/cooja&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1416</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1416"/>
				<updated>2014-11-09T06:44:01Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Source Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code and used Directories ==&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-conf.h&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-of0.c&lt;br /&gt;
~/contiki-2.7/core/net/rpl/rpl-mrhof.c&lt;br /&gt;
~/contiki-2.7/tools/cooja&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1409</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1409"/>
				<updated>2014-11-09T06:39:27Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1402</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1402"/>
				<updated>2014-11-09T06:38:06Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Related Files and Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&amp;lt;!--=== rpl.c ===--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1400</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1400"/>
				<updated>2014-11-09T06:36:31Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Related Files and Functions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- REMOVE --&amp;gt;&lt;br /&gt;
=== rpl.c ===&lt;br /&gt;
This file has contiki implementation of RPL-IPv6 Routing Protocol for Low-Power and Lossy Networks (IETF RFC 6550).&lt;br /&gt;
&amp;lt;source lang = c&amp;gt;&lt;br /&gt;
void &lt;br /&gt;
rpl_link_neighbor_callback(const rimeaddr_t *addr, int status, int numtx)&lt;br /&gt;
 {&lt;br /&gt;
  uip_ipaddr_t ipaddr;&lt;br /&gt;
  rpl_parent_t *parent;&lt;br /&gt;
  rpl_instance_t *instance;&lt;br /&gt;
  rpl_instance_t *end;&lt;br /&gt;
&lt;br /&gt;
  uip_ip6addr(&amp;amp;ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);&lt;br /&gt;
  uip_ds6_set_addr_iid(&amp;amp;ipaddr, (uip_lladdr_t *)addr);&lt;br /&gt;
&lt;br /&gt;
  for(instance = &amp;amp;instance_table[0], end = instance + RPL_MAX_INSTANCES; instance &amp;lt; end; ++instance) {&lt;br /&gt;
    if(instance-&amp;gt;used == 1 ) {&lt;br /&gt;
      parent = rpl_find_parent_any_dag(instance, &amp;amp;ipaddr);&lt;br /&gt;
      if(parent != NULL) {&lt;br /&gt;
        /* Trigger DAG rank recalculation. */&lt;br /&gt;
        PRINTF(&amp;quot;RPL: rpl_link_neighbor_callback triggering update\n&amp;quot;);&lt;br /&gt;
        parent-&amp;gt;updated = 1;&lt;br /&gt;
        if(instance-&amp;gt;of-&amp;gt;neighbor_link_callback != NULL) {&lt;br /&gt;
          instance-&amp;gt;of-&amp;gt;neighbor_link_callback(parent, status, numtx);&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== rpl-dag.c ===--&amp;gt;&lt;br /&gt;
&amp;lt;!--This file has logic for Directed Acyclic Graphs in RPL.--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1396</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1396"/>
				<updated>2014-11-09T06:35:02Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* Cooja Simulation using DGRM model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-dag.c, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- REMOVE --&amp;gt;&lt;br /&gt;
=== rpl.c ===&lt;br /&gt;
This file has contiki implementation of RPL-IPv6 Routing Protocol for Low-Power and Lossy Networks (IETF RFC 6550).&lt;br /&gt;
&amp;lt;source lang = c&amp;gt;&lt;br /&gt;
void &lt;br /&gt;
rpl_link_neighbor_callback(const rimeaddr_t *addr, int status, int numtx)&lt;br /&gt;
 {&lt;br /&gt;
  uip_ipaddr_t ipaddr;&lt;br /&gt;
  rpl_parent_t *parent;&lt;br /&gt;
  rpl_instance_t *instance;&lt;br /&gt;
  rpl_instance_t *end;&lt;br /&gt;
&lt;br /&gt;
  uip_ip6addr(&amp;amp;ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);&lt;br /&gt;
  uip_ds6_set_addr_iid(&amp;amp;ipaddr, (uip_lladdr_t *)addr);&lt;br /&gt;
&lt;br /&gt;
  for(instance = &amp;amp;instance_table[0], end = instance + RPL_MAX_INSTANCES; instance &amp;lt; end; ++instance) {&lt;br /&gt;
    if(instance-&amp;gt;used == 1 ) {&lt;br /&gt;
      parent = rpl_find_parent_any_dag(instance, &amp;amp;ipaddr);&lt;br /&gt;
      if(parent != NULL) {&lt;br /&gt;
        /* Trigger DAG rank recalculation. */&lt;br /&gt;
        PRINTF(&amp;quot;RPL: rpl_link_neighbor_callback triggering update\n&amp;quot;);&lt;br /&gt;
        parent-&amp;gt;updated = 1;&lt;br /&gt;
        if(instance-&amp;gt;of-&amp;gt;neighbor_link_callback != NULL) {&lt;br /&gt;
          instance-&amp;gt;of-&amp;gt;neighbor_link_callback(parent, status, numtx);&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-dag.c ===&lt;br /&gt;
This file has logic for Directed Acyclic Graphs in RPL.&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu3.png|border|500px|right]]--&amp;gt;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu3.png|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;!-- [[File:Simu4.png|border|250px|right]]--&amp;gt;&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu4.png|border|300px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1390</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1390"/>
				<updated>2014-11-09T06:31:49Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* neighbor_link_callback(rpl_parent_t *p, int status, int numtx) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-dag.c, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- REMOVE --&amp;gt;&lt;br /&gt;
=== rpl.c ===&lt;br /&gt;
This file has contiki implementation of RPL-IPv6 Routing Protocol for Low-Power and Lossy Networks (IETF RFC 6550).&lt;br /&gt;
&amp;lt;source lang = c&amp;gt;&lt;br /&gt;
void &lt;br /&gt;
rpl_link_neighbor_callback(const rimeaddr_t *addr, int status, int numtx)&lt;br /&gt;
 {&lt;br /&gt;
  uip_ipaddr_t ipaddr;&lt;br /&gt;
  rpl_parent_t *parent;&lt;br /&gt;
  rpl_instance_t *instance;&lt;br /&gt;
  rpl_instance_t *end;&lt;br /&gt;
&lt;br /&gt;
  uip_ip6addr(&amp;amp;ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);&lt;br /&gt;
  uip_ds6_set_addr_iid(&amp;amp;ipaddr, (uip_lladdr_t *)addr);&lt;br /&gt;
&lt;br /&gt;
  for(instance = &amp;amp;instance_table[0], end = instance + RPL_MAX_INSTANCES; instance &amp;lt; end; ++instance) {&lt;br /&gt;
    if(instance-&amp;gt;used == 1 ) {&lt;br /&gt;
      parent = rpl_find_parent_any_dag(instance, &amp;amp;ipaddr);&lt;br /&gt;
      if(parent != NULL) {&lt;br /&gt;
        /* Trigger DAG rank recalculation. */&lt;br /&gt;
        PRINTF(&amp;quot;RPL: rpl_link_neighbor_callback triggering update\n&amp;quot;);&lt;br /&gt;
        parent-&amp;gt;updated = 1;&lt;br /&gt;
        if(instance-&amp;gt;of-&amp;gt;neighbor_link_callback != NULL) {&lt;br /&gt;
          instance-&amp;gt;of-&amp;gt;neighbor_link_callback(parent, status, numtx);&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-dag.c ===&lt;br /&gt;
This file has logic for Directed Acyclic Graphs in RPL.&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function. The new_etx is calculated based on the formula using both the recorded and packet ETX values. The link_metric is updated with the new ETX value.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
[[File:Simu3.png|border|500px|right]]&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
[[File:Simu4.png|border|250px|right]]&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1383</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1383"/>
				<updated>2014-11-09T06:26:16Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* best_parent(rpl_parent_t *p1, rpl_parent_t *p2) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-dag.c, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- REMOVE --&amp;gt;&lt;br /&gt;
=== rpl.c ===&lt;br /&gt;
This file has contiki implementation of RPL-IPv6 Routing Protocol for Low-Power and Lossy Networks (IETF RFC 6550).&lt;br /&gt;
&amp;lt;source lang = c&amp;gt;&lt;br /&gt;
void &lt;br /&gt;
rpl_link_neighbor_callback(const rimeaddr_t *addr, int status, int numtx)&lt;br /&gt;
 {&lt;br /&gt;
  uip_ipaddr_t ipaddr;&lt;br /&gt;
  rpl_parent_t *parent;&lt;br /&gt;
  rpl_instance_t *instance;&lt;br /&gt;
  rpl_instance_t *end;&lt;br /&gt;
&lt;br /&gt;
  uip_ip6addr(&amp;amp;ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);&lt;br /&gt;
  uip_ds6_set_addr_iid(&amp;amp;ipaddr, (uip_lladdr_t *)addr);&lt;br /&gt;
&lt;br /&gt;
  for(instance = &amp;amp;instance_table[0], end = instance + RPL_MAX_INSTANCES; instance &amp;lt; end; ++instance) {&lt;br /&gt;
    if(instance-&amp;gt;used == 1 ) {&lt;br /&gt;
      parent = rpl_find_parent_any_dag(instance, &amp;amp;ipaddr);&lt;br /&gt;
      if(parent != NULL) {&lt;br /&gt;
        /* Trigger DAG rank recalculation. */&lt;br /&gt;
        PRINTF(&amp;quot;RPL: rpl_link_neighbor_callback triggering update\n&amp;quot;);&lt;br /&gt;
        parent-&amp;gt;updated = 1;&lt;br /&gt;
        if(instance-&amp;gt;of-&amp;gt;neighbor_link_callback != NULL) {&lt;br /&gt;
          instance-&amp;gt;of-&amp;gt;neighbor_link_callback(parent, status, numtx);&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-dag.c ===&lt;br /&gt;
This file has logic for Directed Acyclic Graphs in RPL.&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).Here the PARENT_SWITCH_THRESHOLD_DIV is defined to be 2.&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
[[File:Simu3.png|border|500px|right]]&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
[[File:Simu4.png|border|250px|right]]&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1382</id>
		<title>RPL objective function modification and simulation in cooja</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=RPL_objective_function_modification_and_simulation_in_cooja&amp;diff=1382"/>
				<updated>2014-11-09T06:24:27Z</updated>
		
		<summary type="html">&lt;p&gt;Ashwinit: /* best_parent(rpl_parent_t *p1, rpl_parent_t *p2) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The Routing Protocol for Low-Power and Lossy Networks (RPL) builds a Destination Oriented Directed Acyclic Graph (DODAG) using the Objective Function (OF). The Objective Function uses routing metrics to form the DODAG based on some algorithm or calculation formula. Basically the objective functions optimize or constrain the routing metrics that are used to form the routes and hence help in choosing the best route. There could be many objective functions in operation on the same node and mesh network because deployments vary greatly with different objectives and a single mesh network may need to carry traffic with very different requirements of path quality.&lt;br /&gt;
&lt;br /&gt;
The Contiki implementation of RPL, there are 2 objective functions, but by default it uses the one that minimizes the ETX values. But the same cannot be the best routing strategy in all routing scenarios. Hence there arises a need to modify the objective function accordingly to accommodate any additional constraints or to achieve a different objective.&lt;br /&gt;
&lt;br /&gt;
== You Will learn ==&lt;br /&gt;
The basic assumption of this tutorial is that you know the working of the Routing Protocol for Low-Power and Lossy Networks (RPL). Here the Contiki OS 2.7v implementation of RPL is used and explained.&amp;lt;br&amp;gt; The following are explained in this tutorial:&lt;br /&gt;
* Different RPL related functions and their working&lt;br /&gt;
* Modifying an existing RPL objective function&lt;br /&gt;
* Simulation in Cooja using DGRM model&lt;br /&gt;
&lt;br /&gt;
== Related Files and Functions ==&lt;br /&gt;
&amp;lt;!-- File name, Location path, important variables, important functions --&amp;gt;&lt;br /&gt;
&amp;lt;!-- link between the files which function is called by whom --&amp;gt;&lt;br /&gt;
&amp;lt;!-- example problem statement and topology --&amp;gt;&lt;br /&gt;
&amp;lt;!-- how modification is made i.e. in which part of code and how it affects i.e. logic --&amp;gt;&lt;br /&gt;
Some of the important files that have the gist of RPL are rpl-config.h, rpl-dag.c, rpl-of0.c, rpl-mhrof.c. However only some of the important functions in these files are mentioned here.&lt;br /&gt;
&lt;br /&gt;
=== rpl-config.h ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--=== Function call hierarchy during each function description === --&amp;gt;&lt;br /&gt;
==== RPL_CONF_STATS ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_STATS */&lt;br /&gt;
#ifndef RPL_CONF_STATS&lt;br /&gt;
#define RPL_CONF_STATS 0&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here RPL Configuration statistics are disabled. To enable we need it to set to 1.&lt;br /&gt;
&lt;br /&gt;
==== RPL_DAG_MC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_DAG_MC */&lt;br /&gt;
#ifdef RPL_CONF_DAG_MC&lt;br /&gt;
#define RPL_DAG_MC RPL_CONF_DAG_MC&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_DAG_MC RPL_DAG_MC_NONE&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here RPL metric container of the type using ETX and ENERGY are supported. However if you develop an objective function and an associated metric container can be supported.&lt;br /&gt;
&lt;br /&gt;
==== RPL_OF ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
/* RPL_CONF_OF */&lt;br /&gt;
#ifdef RPL_CONF_OF&lt;br /&gt;
#define RPL_OF RPL_CONF_OF&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_OF rpl_mrhof&lt;br /&gt;
#endif &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;The RPL_CONF_OF parameter configures the RPL objective function.&amp;#039;&amp;#039;&amp;#039; ETX is the default objective function here. This should be defined to be the name of an rpl_of object linked into the system image, e.g., rpl_of0. Here you can also define it to be your own developed Objective function. &lt;br /&gt;
&lt;br /&gt;
==== RPL_LEAF_ONLY ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifdef RPL_CONF_LEAF_ONLY&lt;br /&gt;
#define RPL_LEAF_ONLY RPL_CONF_LEAF_ONLY&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_LEAF_ONLY 0&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The node should stay as a leaf node or not is decided by this value.(as mentioned in draft-ietf-roll-rpl-19#section-8.5)&lt;br /&gt;
&lt;br /&gt;
==== RPL_INIT_LINK_METRIC ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
#ifndef RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        5&lt;br /&gt;
#else&lt;br /&gt;
#define RPL_INIT_LINK_METRIC        RPL_CONF_INIT_LINK_METRIC&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the initial metric attributed to the link when ETX is unknown. It can be changed to any desirable value. Further as we will see we will set this value to 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- REMOVE --&amp;gt;&lt;br /&gt;
=== rpl.c ===&lt;br /&gt;
This file has contiki implementation of RPL-IPv6 Routing Protocol for Low-Power and Lossy Networks (IETF RFC 6550).&lt;br /&gt;
&amp;lt;source lang = c&amp;gt;&lt;br /&gt;
void &lt;br /&gt;
rpl_link_neighbor_callback(const rimeaddr_t *addr, int status, int numtx)&lt;br /&gt;
 {&lt;br /&gt;
  uip_ipaddr_t ipaddr;&lt;br /&gt;
  rpl_parent_t *parent;&lt;br /&gt;
  rpl_instance_t *instance;&lt;br /&gt;
  rpl_instance_t *end;&lt;br /&gt;
&lt;br /&gt;
  uip_ip6addr(&amp;amp;ipaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);&lt;br /&gt;
  uip_ds6_set_addr_iid(&amp;amp;ipaddr, (uip_lladdr_t *)addr);&lt;br /&gt;
&lt;br /&gt;
  for(instance = &amp;amp;instance_table[0], end = instance + RPL_MAX_INSTANCES; instance &amp;lt; end; ++instance) {&lt;br /&gt;
    if(instance-&amp;gt;used == 1 ) {&lt;br /&gt;
      parent = rpl_find_parent_any_dag(instance, &amp;amp;ipaddr);&lt;br /&gt;
      if(parent != NULL) {&lt;br /&gt;
        /* Trigger DAG rank recalculation. */&lt;br /&gt;
        PRINTF(&amp;quot;RPL: rpl_link_neighbor_callback triggering update\n&amp;quot;);&lt;br /&gt;
        parent-&amp;gt;updated = 1;&lt;br /&gt;
        if(instance-&amp;gt;of-&amp;gt;neighbor_link_callback != NULL) {&lt;br /&gt;
          instance-&amp;gt;of-&amp;gt;neighbor_link_callback(parent, status, numtx);&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== rpl-dag.c ===&lt;br /&gt;
This file has logic for Directed Acyclic Graphs in RPL.&lt;br /&gt;
&lt;br /&gt;
=== rpl-of0.c ===&lt;br /&gt;
An implementation of RPL&amp;#039;s objective function 0 is in this file.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)====&lt;br /&gt;
[[File:CalcRank.png|frame|right|If parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
 {&lt;br /&gt;
  rpl_rank_t increment;&lt;br /&gt;
  if(base_rank == 0) {&lt;br /&gt;
    if(p == NULL) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    base_rank = p-&amp;gt;rank;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  increment = p != NULL ?&lt;br /&gt;
                p-&amp;gt;dag-&amp;gt;instance-&amp;gt;min_hoprankinc :&lt;br /&gt;
                DEFAULT_RANK_INCREMENT;&lt;br /&gt;
&lt;br /&gt;
  if((rpl_rank_t)(base_rank + increment) &amp;lt; base_rank) {&lt;br /&gt;
    PRINTF(&amp;quot;RPL: OF0 rank %d incremented to infinite rank due to wrapping\n&amp;quot;,&lt;br /&gt;
        base_rank);&lt;br /&gt;
    return INFINITE_RANK;&lt;br /&gt;
  }&lt;br /&gt;
  return base_rank + increment;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Here rank of a node is calculated. The rank of the node is based on its parents rank and base rank. If the base rank is zero and the node has no parent then the rank of the node is infinite. If the base rank is zero and parent is present then the base rank becomes equal to the parent rank. If the base rank is not zero, then depending if the parent is present or not the increment becomes min_hoprankinc of the parent in the DAG instance or DEFAULT_RANK_INCREMENT. In short if Parent is NULL then base_rank is incremented by a default increment else it uses information about parent to increment the base_rank. In the last part, if the calculated new rank is less than the base rank then the new rank becomes infinity due to wrapping around. The new calculated rank is equal to the base rank added with the increment.&lt;br /&gt;
&lt;br /&gt;
==== best_dag(rpl_dag_t *d1, rpl_dag_t *d2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_dag_t *&lt;br /&gt;
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)&lt;br /&gt;
{&lt;br /&gt;
  if(d1-&amp;gt;grounded) {&lt;br /&gt;
    if (!d2-&amp;gt;grounded) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  } else if(d2-&amp;gt;grounded) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d1-&amp;gt;preference &amp;lt; d2-&amp;gt;preference) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    if(d1-&amp;gt;preference &amp;gt; d2-&amp;gt;preference) {&lt;br /&gt;
      return d1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(d2-&amp;gt;rank &amp;lt; d1-&amp;gt;rank) {&lt;br /&gt;
    return d2;&lt;br /&gt;
  } else {&lt;br /&gt;
    return d1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two DAGs and returns the best of the Two DAGs (passed as input d1 and d2)according to the Objective function. There are 3 criteria to find the best DAG here. The first one is to find if the DAG is grounded. Second is the preference metric of each DAG. The third one is the rank of each DAG.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
[[File:BestParent.png|frame|right|Child c1 chooses Parent p1 according to OF.]]&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t r1, r2;&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  &lt;br /&gt;
  PRINTF(&amp;quot;RPL: Comparing parent &amp;quot;);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p1));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d) with parent &amp;quot;,&lt;br /&gt;
        p1-&amp;gt;link_metric, p1-&amp;gt;rank);&lt;br /&gt;
  PRINT6ADDR(rpl_get_parent_ipaddr(p2));&lt;br /&gt;
  PRINTF(&amp;quot; (confidence %d, rank %d)\n&amp;quot;,&lt;br /&gt;
        p2-&amp;gt;link_metric, p2-&amp;gt;rank);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  r1 = DAG_RANK(p1-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p1-&amp;gt;link_metric;&lt;br /&gt;
  r2 = DAG_RANK(p2-&amp;gt;rank, p1-&amp;gt;dag-&amp;gt;instance) * RPL_MIN_HOPRANKINC  +&lt;br /&gt;
         p2-&amp;gt;link_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = (rpl_dag_t *)p1-&amp;gt;dag; /* Both parents must be in the same DAG. */&lt;br /&gt;
  if(r1 &amp;lt; r2 + MIN_DIFFERENCE &amp;amp;&amp;amp;&lt;br /&gt;
     r1 &amp;gt; r2 - MIN_DIFFERENCE) {&lt;br /&gt;
    return dag-&amp;gt;preferred_parent;&lt;br /&gt;
  } else if(r1 &amp;lt; r2) {&lt;br /&gt;
    return p1;&lt;br /&gt;
  } else {&lt;br /&gt;
    return p2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function compares two parents and returns the best one according to the Objective function. Here the parents are compared based on their rank and the ETX value of that parent. This is one of the important functions, since the route formation is based on this after choosing the best DAG.&lt;br /&gt;
&lt;br /&gt;
=== rpl-mhrof.c ===&lt;br /&gt;
The Minimum Rank with Hysteresis Objective Function (MRHOF) is implemented in this file. The objective function uses ETX as routing metric and it also has stubs for energy metric.&lt;br /&gt;
&lt;br /&gt;
==== calculate_path_metric(rpl_parent_t *p) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_path_metric_t&lt;br /&gt;
calculate_path_metric(rpl_parent_t *p)&lt;br /&gt;
{&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
#if RPL_DAG_MC == RPL_DAG_MC_NONE&lt;br /&gt;
  return p-&amp;gt;rank + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ETX&lt;br /&gt;
  return p-&amp;gt;mc.obj.etx + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY&lt;br /&gt;
  return p-&amp;gt;mc.obj.energy.energy_est + (uint16_t)p-&amp;gt;link_metric;&lt;br /&gt;
#else&lt;br /&gt;
#error &amp;quot;Unsupported RPL_DAG_MC configured. See rpl.h.&amp;quot;&lt;br /&gt;
#endif /* RPL_DAG_MC */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here path metric is calculated according to the OF. If no parent is present then a default metric which is calculated based on Maximum Path Cost is used. If no OF is mentioned then path metric is sum of rank( and link metric. For OF based on ETX, it is sum of ETX measured (p-&amp;gt;mc.obj.etx) and link metric. Similarly if the OF is based on Energy then energy value (p-&amp;gt;mc.obj.energy.energy_est) is added to the link metric. This function is called by [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29_2|best_parent()]] to compare the path metrics of the two parents.&lt;br /&gt;
&lt;br /&gt;
==== neighbor_link_callback(rpl_parent_t *p, int status, int numtx) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static void&lt;br /&gt;
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)&lt;br /&gt;
{&lt;br /&gt;
  uint16_t recorded_etx = p-&amp;gt;link_metric;&lt;br /&gt;
  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  uint16_t new_etx;&lt;br /&gt;
&lt;br /&gt;
  /* Do not penalize the ETX when collisions or transmission errors occur. */&lt;br /&gt;
  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {&lt;br /&gt;
    if(status == MAC_TX_NOACK) {&lt;br /&gt;
      packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +&lt;br /&gt;
               (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;&lt;br /&gt;
&lt;br /&gt;
    PRINTF(&amp;quot;RPL: ETX changed from %u to %u (packet ETX = %u)\n&amp;quot;,&lt;br /&gt;
        (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(new_etx  / RPL_DAG_MC_ETX_DIVISOR),&lt;br /&gt;
        (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));&lt;br /&gt;
    p-&amp;gt;link_metric = new_etx;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function receives link-layer neighbor information. The parameter status is set either to 0 or 1. The numetx parameter specifies the current ETX(estimated transmissions) for the neighbor. The recorded ETX is the link_metric and packet ETX is the numetx parameter passed to the function.&lt;br /&gt;
&lt;br /&gt;
==== calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_rank_t&lt;br /&gt;
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)&lt;br /&gt;
{&lt;br /&gt;
  rpl_rank_t new_rank;&lt;br /&gt;
  rpl_rank_t rank_increase;&lt;br /&gt;
&lt;br /&gt;
  if(p == NULL) {&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      return INFINITE_RANK;&lt;br /&gt;
    }&lt;br /&gt;
    rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;&lt;br /&gt;
  } else {&lt;br /&gt;
    rank_increase = p-&amp;gt;link_metric;&lt;br /&gt;
    if(base_rank == 0) {&lt;br /&gt;
      base_rank = p-&amp;gt;rank;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  if(INFINITE_RANK - base_rank &amp;lt; rank_increase) {&lt;br /&gt;
    /* Reached the maximum rank. */&lt;br /&gt;
    new_rank = INFINITE_RANK;&lt;br /&gt;
  } else {&lt;br /&gt;
   /* Calculate the rank based on the new rank information from DIO or&lt;br /&gt;
      stored otherwise. */&lt;br /&gt;
    new_rank = base_rank + rank_increase;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return new_rank;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to the [[#calculate_rank.28rpl_parent_t_.2Ap.2C_rpl_rank_t_base_rank.29|calculate_rank()]] function explained earlier but with a slight difference. In this OF, the rank_increase is the increment which is equal to [[#RPL_INIT_LINK_METRIC|RPL_INIT_LINK_METRIC]] if parent = NULL. Else it is equal to the link_metric. The new rank is the increment added to the base rank.&lt;br /&gt;
&lt;br /&gt;
==== best_parent(rpl_parent_t *p1, rpl_parent_t *p2) ====&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
static rpl_parent_t *&lt;br /&gt;
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)&lt;br /&gt;
{&lt;br /&gt;
  rpl_dag_t *dag;&lt;br /&gt;
  rpl_path_metric_t min_diff;&lt;br /&gt;
  rpl_path_metric_t p1_metric;&lt;br /&gt;
  rpl_path_metric_t p2_metric;&lt;br /&gt;
&lt;br /&gt;
  dag = p1-&amp;gt;dag; /* Both parents are in the same DAG. */&lt;br /&gt;
&lt;br /&gt;
  min_diff = RPL_DAG_MC_ETX_DIVISOR /&lt;br /&gt;
             PARENT_SWITCH_THRESHOLD_DIV;&lt;br /&gt;
&lt;br /&gt;
  p1_metric = calculate_path_metric(p1);&lt;br /&gt;
  p2_metric = calculate_path_metric(p2);&lt;br /&gt;
&lt;br /&gt;
  /* Maintain stability of the preferred parent in case of similar ranks. */&lt;br /&gt;
  if(p1 == dag-&amp;gt;preferred_parent || p2 == dag-&amp;gt;preferred_parent) {&lt;br /&gt;
    if(p1_metric &amp;lt; p2_metric + min_diff &amp;amp;&amp;amp;&lt;br /&gt;
       p1_metric &amp;gt; p2_metric - min_diff) {&lt;br /&gt;
      PRINTF(&amp;quot;RPL: MRHOF hysteresis: %u &amp;lt;= %u &amp;lt;= %u\n&amp;quot;,&lt;br /&gt;
             p2_metric - min_diff,&lt;br /&gt;
             p1_metric,&lt;br /&gt;
             p2_metric + min_diff);&lt;br /&gt;
      return dag-&amp;gt;preferred_parent;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  return p1_metric &amp;lt; p2_metric ? p1 : p2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar tto the [[#best_parent.28rpl_parent_t_.2Ap1.2C_rpl_parent_t_.2Ap2.29|best_parent()]] function earlier which compares the two parents and returns the best one. Here [[#calculate_path_metric.28rpl_parent_t_.2Ap.29 |path metric]] calculated for each parent is the basis for comparison. First it is checked if any of the parent is set as the preferred parent in the DAG formed and if so then it is chosen as the best parent (based on the MRHOF hysteresis RFC 6719). Otherwise, the two calculated metrics are compared and the parent with the lower metric is the best parent. In this OF, a switch is made to that minimum Rank path only if it is shorter (in terms of path cost) than the current path by at least a given threshold.  This second mechanism is called &amp;quot;hysteresis&amp;quot; (RFC 6719).&amp;lt;br&amp;gt;&lt;br /&gt;
This parent selection occurs in the following cases &lt;br /&gt;
* during the initial formation of the network or&lt;br /&gt;
* when path cost to the neighboring nodes changes or &lt;br /&gt;
* a new node appears in the neighborhood of the node&lt;br /&gt;
&lt;br /&gt;
=== Example scenario for objective function modification ===&lt;br /&gt;
&lt;br /&gt;
== Cooja Simulation using DGRM model ==&lt;br /&gt;
The following are the steps to form a new simulation:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; You can refer to [[Cooja Simulator]] for an introduction to Cooja.&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Cooja&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Go to your Contiki folder(contiki-2.7) and then go to /tools/cooja directory&amp;lt;br&amp;gt; Run the command sudo ant run to open up a cooja GUI.&amp;lt;br&amp;gt;&lt;br /&gt;
 $ cd contiki-2.7/tools/cooja&lt;br /&gt;
 $ sudo ant run&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Start a New Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Choose a &amp;#039;&amp;#039;New Simulation&amp;#039;&amp;#039; from the File drop down menu (or alternatively can do Ctrl+N).&amp;lt;br&amp;gt;The following screen will pop up.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:NewSimuScn1.jpg|border|500px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
Give a name to your simulation in the &amp;#039;&amp;#039;Simulation Name&amp;#039;&amp;#039; field. &lt;br /&gt;
Choose the &amp;#039;&amp;#039;&amp;#039;Directed Graph Radio Medium(DGRM)&amp;#039;&amp;#039;&amp;#039; from the drop down menu for the &amp;#039;&amp;#039;Radio Medium&amp;#039;&amp;#039; under &amp;#039;&amp;#039;&amp;#039;Advanced Settings&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Click on the &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button. &amp;lt;br&amp;gt;&lt;br /&gt;
The new simulation created will open up multiple windows as shown.&amp;lt;br&amp;gt;&lt;br /&gt;
[[File:Simu2.png|border|600px|center]]&amp;lt;br&amp;gt;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sink Mote&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
[[File:Simu3.png|border|500px|right]]&lt;br /&gt;
Add a mote of the type Sink. Here udp-sink code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. Click on &amp;#039;&amp;#039;Compile&amp;#039;&amp;#039; button. A &amp;#039;&amp;#039;Create&amp;#039;&amp;#039; button will appear on successful compilation which adds number of motes as desired in the network. Here only one sink is used. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Sender Motes&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Add a mote of the type Sink. Here udp-sender code from the rpl-collect example (&amp;#039;&amp;#039;/examples/ipv6/rpl-collect/&amp;#039;&amp;#039;) is being used. However you can upload any code you want to implement according to your application. &amp;lt;br&amp;gt;&lt;br /&gt;
Compile the code and create many &amp;lt;!--how many?????--&amp;gt; motes of this type according to the topology.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Placement of the mote does not matter here. You can place your motes anywhere in the graph. Since this is different than the distance model, we make explicit links between the motes for communication, hence distance between them makes no difference.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Add Communication Links&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
[[File:Simu4.png|border|250px|right]]&lt;br /&gt;
Add two communication links between each set of nodes so that the communication can be bidirectional.&amp;lt;br&amp;gt; Click on Tools-&amp;gt;DGRM Links... This will open a DGRM Configurator Dialog box. Click on Add. Select source and destination nodes and again click on add. This will add a unidirectional link from the source to destination node. For a bidirectional link you need to add one more link with source and destination nodes switched. You can add multiple links in this way. After adding the links close the dialog box.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can change other parameters of the link such as &amp;#039;&amp;#039;RX ratio, RSSI, LQI&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Delay&amp;#039;&amp;#039; according to your application. These parameters affect the individual link quality eg. &amp;#039;&amp;#039;RX ratio&amp;#039;&amp;#039; affect the ETX values. So to test your application under various Link Quality conditions these parameters can be changed. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also you can Delete an existing one using the remove option. The Import option helps in importing any data file which already has these link connections and parameters specified in it. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Run Simulation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Run the simulation by using the &amp;#039;&amp;#039;Start&amp;#039;&amp;#039; option in the &amp;#039;&amp;#039;Simulation Control&amp;#039;&amp;#039; window. This will initiate the motes and allocate all with a new Rime address and other initialization processes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Watch Output&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The motes output and debug messages can be seen in the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; window. You can filter the output based on the node &amp;#039;&amp;#039;ID:node_id&amp;#039;&amp;#039; to watch a particular node. You can also watch particular debug messages by filtering them. The other useful functions of the &amp;#039;&amp;#039;Motes Output&amp;#039;&amp;#039; are &amp;#039;&amp;#039;File, Edit&amp;#039;&amp;#039; and &amp;#039;&amp;#039;View&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;File&amp;#039;&amp;#039; option helps in saving the output to a file. The &amp;#039;&amp;#039;Edit&amp;#039;&amp;#039; has the option of copying the output - either full or a particular selected messages. You can also clear the messages using the &amp;#039;&amp;#039;Clear all messages&amp;#039;&amp;#039; option.&amp;lt;br&amp;gt;&lt;br /&gt;
You can use these messages saved in file to make observations and plot graphs according to the objective of your experiment.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;!-- Use one or two figures showing graph formation --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
Edited by - Ashwini Telang &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Ashwinit</name></author>	</entry>

	</feed>