# Enabling Autonomous Agents for Mobile Wireless Sensor Networks
## 5 Implementation
### Battery Simulation of Network Operations  
Battery drainage in mobile WSNs is primarily contributed by two factors: robot movement and packet routing. Robot movement is independent from the network stage as it applies whenever the robot is in movement. The battery consumption (in percentage) due to robot movement is modelled as follows: \(m(d_{m})=100.0\times\frac{d_{m}\times K_{m}}{B_{cp}}\), where \(d_{m}\) represents the distance travelled in any axis, stated in meters, and \(K_{m}\) is a robot-model-dependant constant that represents a certain current motor drain at a constant velocity, in \(mAh/m\). Lastly, \(B_{cp}\) is the battery capacity in \(mAh\). The \(m(d_{m})\) percentage is calculated internally by each robot concurrently with its movement. This calculation is subsequently subtracted from the current battery percentage, denoted as \(b_{c}\). Concerning packet routing during network operations, sensing nodes employ an ad hoc 2.4 GHz-operated protocol that implements a controlled flooding to transmit messages from the sensing nodes towards the data sink. This model, although simple to simulate, is realistic based on actual implementations [20; 21]. According to this routing mechanism, a packet from node i to node j is forwarded (i.e., broadcasted) again by any node receiveing it if, and only if, that node has not already received the same packet and it is closer to the destination than the previous hop. The controlled flooding mechanism is summarized in Listing 1, where \(dist(n1,n2)\) is a function that returns the Euclidean distance between Nodes n1 and n2, \(mark\_seen(n,msg)\) is a function that sets a flag indicating that packet msg has already been retransmitted by node n, and \(seen(n,msg)\) is a function that returns the state of that flag (i.e., if node n has already forwarded message msg). ```
msg={id:msg_id,dest:j,sender:i} forward(i,msg) functionforward(node,msg): ifnode==msg.dest: process(msg) else: forneighborin neighbors(node): ifnotseen(neighbor,msg)anddist(neighbor,msg.dest)<dist(node,msg.dest): mark_seen(neighbor,msg) forward(neighbor,msg)
```  
Listing 1: Pseudocode for the controlled flooding routing protocol. This model assumes that if a node \(n\) is within reach from a node \(i\), there is a 100% percent likelihood of receiving a packet from node \(i\) in node \(n\). This is purely determined by the chosen radio technology and its range. Battery drainage due to network communications for each node \(n\) is proportional to the number of packets node \(n\) is transmitting and forwarding according to the traffic model and the controlled flooding routing mechanism. The battery drainage, \(U_{p}(n)\), for sending a single packet from node \(n\) that can reach all its neighbors within a certain range depends on (but is not proportional to) the transmission power necessary to cover that range. To model this, we define \(U_{p}(n)\) as\(U_{p}(n)=R_{packet}\times(1+R_{incdis}\times Range(n))\), where \(Range(n)\) is the range covered by node \(n\), \(R_{packet}\) represents the baseline battery drainage when transmitting a packet, and \(R_{incdis}(mAh/m)\) models the increase in this drainage depending on the range covered by node n (\(Range(n)\)). \(R_{packet}\) and \(R_{incdis}\) depend on the transceiver model and the chosen radio technology. In the tests presented in this paper, all the nodes are considered to have the same range and therefore the same battery drainage per transmitted packet. Once the battery drainage for sending a single packet from node \(n\) to all its neighbors within the range it covers is determined, it is necessary to compute the amount of packets each node is transmitting and forwarding according to the traffic and routing models.