在TelosB(即TinyOS)中运行TestNetworkC.nc时产生错误的输出

时间:2018-09-30 15:50:19

标签: tinyos nesc

我正在尝试通过使用TelosB测量吞吐量,数据包传输率和网络生存时间来分析网络。我正在使用TinyOS-2.1.1进行仿真和编程。作为基本代码,我使用的是操作系统随附的TestNetworkC.nc

我已经做了一些修改以获取数据,但是我得到的输出却有问题。输出显示,只有1个节点(根正在发送和接收),其余节点保持空闲。

#include <Timer.h>
#include "TestNetwork.h"
#include "CtpDebugMsg.h"

module TestNetworkC {
  uses interface Boot;
  uses interface SplitControl as RadioControl;
  uses interface SplitControl as SerialControl;
  uses interface StdControl as RoutingControl;
  uses interface StdControl as DisseminationControl;
  uses interface DisseminationValue<uint32_t> as DisseminationPeriod;
  uses interface Send;
  uses interface Leds;
  uses interface Read<uint16_t> as ReadSensor;
  uses interface Timer<TMilli>;
  uses interface Timer<TMilli> as Timer1;
  uses interface RootControl;
  uses interface Receive;
  uses interface AMSend as UARTSend;
  uses interface CollectionPacket;
  uses interface CtpInfo;
  uses interface CtpCongestion;
  uses interface Random;
  uses interface Queue<message_t*>;
  uses interface Pool<message_t>;
  uses interface CollectionDebug;
  uses interface AMPacket;
  uses interface Packet as RadioPacket;
}
implementation {
  task void uartEchoTask();
  message_t packet;
  message_t uartpacket;
  message_t* recvPtr = &uartpacket;
  uint8_t msglen;
  uint8_t msgsuccess;
  uint8_t msgfail;
  uint8_t totmsg;
  uint8_t sendpkt;
  uint8_t pkts;
  uint8_t recpkt;
  uint8_t battery = 100;
  bool sendBusy = FALSE;
  bool uartbusy = FALSE;
  bool firstTimer = TRUE;
  bool measurebat = TRUE;
  uint16_t seqno;
  enum {
    SEND_INTERVAL = 8192
  };

  event void ReadSensor.readDone(error_t err, uint16_t val) { }  

  event void Boot.booted() {
    dbg("App", "Booted at %s.\n", sim_time_string());
    call SerialControl.start();
    if(measurebat)
    call Timer1.startPeriodic( 5000 );
  }
  event void Timer1.fired()
  {
    //dbg("App", "Battery at @ %s is %d.\n", sim_time_string(), battery);
    dbg("App", "Sent: %d\nRec: %d\nSuc: %d\nFail:%d\nFailed: %d", sendpkt, recpkt, msgsuccess, msgfail, pkts);

  }
  event void SerialControl.startDone(error_t err) {
    call RadioControl.start();
    dbg("App", "Radio on %s.\n", sim_time_string());

  }
  event void RadioControl.startDone(error_t err) {
    if (err != SUCCESS) {
      call RadioControl.start();
    }
    else {
      call DisseminationControl.start();
      call RoutingControl.start();
      if (TOS_NODE_ID % 500 == 0) {
        dbg("App", "Root Node %d.\n", TOS_NODE_ID);
    call RootControl.setRoot();
      }
      seqno = 0;
        call Timer.startOneShot(call Random.rand16() & 0x1ff);
    }
  }

  event void RadioControl.stopDone(error_t err) {
    dbg("App", "Radio Stopped at %s.\n", sim_time_string());
  }

  event void SerialControl.stopDone(error_t err) {} 

  void failedSend() {
    dbg("App", "%s: Send failed.\n", __FUNCTION__);

    call CollectionDebug.logEvent(NET_C_DBG_1);
  }


  void sendMessage() {
    TestNetworkMsg* msg = (TestNetworkMsg*)call Send.getPayload(&packet, sizeof(TestNetworkMsg));
    uint16_t metric;
    am_addr_t parent = 0;
    sendpkt++;
    call CtpInfo.getParent(&parent);
    call CtpInfo.getEtx(&metric);

    msg->source = TOS_NODE_ID;
    msg->seqno = seqno;
    msg->data = 0xCAFE;
    msg->parent = parent;
    msg->hopcount = 0;
    msg->metric = metric;
    msg->powerCount = battery;
    battery = battery - 2;

    if (call Send.send(&packet, sizeof(TestNetworkMsg)) != SUCCESS) {
      failedSend();
      call Leds.led0On();
      pkts++;
      dbg("TestNetworkC", "%s: Transmission failed.\n", __FUNCTION__);
    }
    else {
      sendBusy = TRUE;
      seqno++; 

      dbg("TestNetworkC", "%s: Transmission succeeded.\n", __FUNCTION__);
    }
    if(msg->powerCount <= 0){
      //call RadioControl.stop();
    }
  }


  event void Timer.fired() {
    uint32_t nextInt;
    dbg("TestNetworkC", "TestNetworkC: Timer fired.\n");
    nextInt = call Random.rand32() % SEND_INTERVAL;
    nextInt += SEND_INTERVAL >> 1;
    call Timer.startOneShot(nextInt);
    if (!sendBusy)
    sendMessage();
  }

  event void Send.sendDone(message_t* m, error_t err) {
    if (err != SUCCESS) {
      msgfail++;
      call Leds.led0On();
    }
    sendBusy = FALSE;
    msgsuccess++;
    dbg("TestNetworkC", "Send completed.\n");
  }

  event void DisseminationPeriod.changed() {
    const uint32_t* newVal = call DisseminationPeriod.get();
    call Timer.stop();
    call Timer.startPeriodic(*newVal);
  }


  uint8_t prevSeq = 0;
  uint8_t firstMsg = 0;

  event message_t* 
  Receive.receive(message_t* msg, void* payload, uint8_t len) {
    dbg("TestNetworkCrec", "Received packet at %s from node %hhu with seq no %hhu.\n", sim_time_string(), call CollectionPacket.getOrigin(msg), call CollectionPacket.getSequenceNumber(msg));
    call Leds.led1Toggle();
    recpkt++;

    if (call CollectionPacket.getOrigin(msg) == 1) {
      if (firstMsg == 1) {
    if (call CollectionPacket.getSequenceNumber(msg) - prevSeq > 1) {
      call Leds.led2On();
    }
      } else {
    firstMsg = 1;
      }
      prevSeq = call CollectionPacket.getSequenceNumber(msg);
    }

    if (!call Pool.empty() && call Queue.size() < call Queue.maxSize()) {
      message_t* tmp = call Pool.get();
      call Queue.enqueue(msg);
      if (!uartbusy) {
        post uartEchoTask();
      }
      return tmp;
    }
    return msg;
 }

 task void uartEchoTask() {
    dbg("Traffic", "Sending packet to UART.\n");
   if (call Queue.empty()) {
     return;
   }
   else if (!uartbusy) {
     message_t* msg = call Queue.dequeue();
     dbg("Traffic", "Sending packet to UART.\n");
     if (call UARTSend.send(0xffff, msg, call RadioPacket.payloadLength(msg)) == SUCCESS) {
       uartbusy = TRUE;
     }
     else {
      call CollectionDebug.logEventMsg(NET_C_DBG_2,
                       call CollectionPacket.getSequenceNumber(msg),
                       call CollectionPacket.getOrigin(msg),
                       call AMPacket.destination(msg));
     }
   }
 }

  event void UARTSend.sendDone(message_t *msg, error_t error) {
    dbg("Traffic", "UART send done.\n");
    uartbusy = FALSE;
    call Pool.put(msg);
    if (!call Queue.empty()) {
      post uartEchoTask();
    } 
    else {
      //        call CtpCongestion.setClientCongested(FALSE);
    }
  }

  /* Default implementations for CollectionDebug calls.
   * These allow CollectionDebug not to be wired to anything if debugging
   * is not desired. */

    default command error_t CollectionDebug.logEvent(uint8_t type) {
        return SUCCESS;
    }
    default command error_t CollectionDebug.logEventSimple(uint8_t type, uint16_t arg) {
        return SUCCESS;
    }
    default command error_t CollectionDebug.logEventDbg(uint8_t type, uint16_t arg1, uint16_t arg2, uint16_t arg3) {
        return SUCCESS;
    }
    default command error_t CollectionDebug.logEventMsg(uint8_t type, uint16_t msg, am_addr_t origin, am_addr_t node) {
        return SUCCESS;
    }
    default command error_t CollectionDebug.logEventRoute(uint8_t type, am_addr_t parent, uint8_t hopcount, uint16_t metric) {
        return SUCCESS;
    }

}

我用来模拟的Python代码是

:
from TOSSIM import *
from tinyos.tossim.TossimApp import *
from random import *
import sys

#n = NescApp("TestNetwork", "app.xml")
#t = Tossim(n.variables.variables())
t = Tossim([])
r = t.radio()

f = open("sparse-grid.txt", "r")
lines = f.readlines()
for line in lines:
  s = line.split()
  if (len(s) > 0):
    if s[0] == "gain":
      r.add(int(s[1]), int(s[2]), float(s[3]))

noise = open("meyer-short.txt", "r")
lines = noise.readlines()
for line in lines:
  str = line.strip()
  if (str != ""):
    val = int(str)
    for i in range(0, 10):
      m = t.getNode(i);
      m.addNoiseTraceReading(val)



for i in range(0, 10):
  m = t.getNode(i);
  m.createNoiseModel();
  time = randint(t.ticksPerSecond(),  15* t.ticksPerSecond())
  m.bootAtTime(time)
  print "Booting ", i, " at time ", time

print "Starting simulation."

#t.addChannel("AM", sys.stdout)
#t.addChannel("TreeRouting", sys.stdout)
#t.addChannel("TestNetworkC", sys.stdout)
#t.addChannel("Route", sys.stdout)
#t.addChannel("PointerBug", sys.stdout)
#t.addChannel("QueueC", sys.stdout)
#t.addChannel("Gain", sys.stdout)
#t.addChannel("Forwarder", sys.stdout)
#t.addChannel("TestNetworkC", sys.stdout)
#t.addChannel("TestNetworkCrec", sys.stdout)
t.addChannel("App", sys.stdout)
#t.addChannel("Traffic", sys.stdout)
#t.addChannel("Acks", sys.stdout)
#t.addChannel("TreeRoutingCtl", sys.stdout)
#t.addChannel("LI", sys.stdout)
#t.addChannel("rept", sys.stdout)

while (t.time() < 1000 * t.ticksPerSecond()):
  t.runNextEvent()

print "Completed simulation."

我得到的输出是

  

调试(1):发送:1记录:0成功:0失败:0失败:0

对于除根以外的所有节点,即节点0:

  

调试(0):已发送:115记录:115成功:115失败:0失败:0

理想情况下,应该有失败的消息,这里不会发生。

问题在于,为什么只有发送和接收的根节点。如何在此设置中测量吞吐量,PDR和网络寿命?

0 个答案:

没有答案