⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 surgem.nc

📁 nesC写的heed算法
💻 NC
字号:
// $Id: SurgeM.nc,v 1.7.2.3 2003/08/18 22:09:37 cssharp Exp $

/*									tab:4
 * "Copyright (c) 2000-2003 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */

includes Surge;
includes SurgeCmd;

/*
 *  Data gather application
 */

module SurgeM {
  provides {
    interface StdControl;
  }
  uses {
    interface ADC;
    interface Timer;
    interface Leds;
    interface StdControl as Sounder;
    interface Send;
    interface Receive as Bcast; 
    interface RouteControl;
	/*** HEED: clustering and control ******/
	interface EnergyControl;
	interface Intercept as InterceptSurgeMsg;
  }
}

implementation {

  enum {
    TIMER_GETADC_COUNT = 1,            // Timer ticks for ADC 
    TIMER_CHIRP_COUNT = 10,            // Timer on/off chirp count
  };

  bool sleeping;			// application command state
  bool focused;
  bool rebroadcast_adc_packet;

  TOS_Msg gMsgBuffer;
  norace uint16_t gSensorData;		// protected by gfSendBusy flag
  bool gfSendBusy;

  int timer_rate;
  int timer_ticks;

  /*** HEED: clustering and control ******/
  #define COLLECT_COUNT 1			
  uint16_t numCollectedPoints, numSentPoints, motesAlive, sendMotesAlive;
  int CH_timer_ticks, reportNumber;
  /***************************************/

  /***********************************************************************
   * Initialization 
   ***********************************************************************/
      
  static void initialize() {
    timer_rate = INITIAL_TIMER_RATE;
    atomic gfSendBusy = FALSE;
    sleeping = FALSE;
    rebroadcast_adc_packet = FALSE;
    focused = FALSE;

	/*** HEED: clustering and control ******/
	numCollectedPoints = numSentPoints = 0;
	motesAlive = sendMotesAlive = 0;
	CH_timer_ticks = 0;
	reportNumber = 0;
	/****************************************/
  }

  task void SendData() {
    SurgeMsg *pReading;
    uint16_t Len;

    dbg(DBG_USR1, "SurgeM: Sending sensor reading\n");
    
    if (pReading = (SurgeMsg *)call Send.getBuffer(&gMsgBuffer,&Len)) {
      pReading->type = SURGE_TYPE_SENSORREADING;
      pReading->parentaddr = call RouteControl.getParent();
	  //pReading->reading = gSensorData;
	  pReading->reading = reportNumber;		// use gSensorData for real data collection
	  
	  /***** HEED code **********/
	  pReading->remPower = call EnergyControl.getRemainingPoints();		// remaining power
	  pReading->overhead = call EnergyControl.getOverhead();			// overhead so far
	  pReading->nPoints = numSentPoints;								// # collected readings
	  pReading->motesAlive = sendMotesAlive;							// which motes are alive
	  /**************************/
      
      if ((call Send.send(&gMsgBuffer,sizeof(SurgeMsg))) != SUCCESS)
		atomic gfSendBusy = FALSE;
    }
  }

  command result_t StdControl.init() {
    initialize();
    return SUCCESS;
  }

  command result_t StdControl.start() {
    return call Timer.start(TIMER_REPEAT, timer_rate);
    return SUCCESS;
  }

  command result_t StdControl.stop() {
    return call Timer.stop();
  }

  /***********************************************************************
   * Commands and events
   ***********************************************************************/
  
  event result_t Timer.fired() {

	// make sure there is still some power left
	if (!(call EnergyControl.isAlive())) {
		call StdControl.stop();
		return SUCCESS;
	}

#ifdef CLUSTERING_ON
	// Case 1. Clustering is being applied ...
	// generate your locally sensed data
	if (TOS_LOCAL_ADDRESS != 0) {
		numCollectedPoints++;
		motesAlive |= (1 << (TOS_LOCAL_ADDRESS-1));
	}
	CH_timer_ticks++;
	// If no parent is available, continue holding the data.
	if (call RouteControl.getParent() == 0xFFFF) {
		return SUCCESS;
	}
	// If cluster head, send aggregated data
	if (call RouteControl.isClusterHead()) {	
		if (CH_timer_ticks % COLLECT_COUNT == 0) {
			atomic {
				numSentPoints = numCollectedPoints;
				sendMotesAlive = motesAlive;
				motesAlive = 0;
				if (TOS_LOCAL_ADDRESS != 0) 
					numCollectedPoints = 0;
				reportNumber++;
				if (!gfSendBusy) {
					gfSendBusy = TRUE;
					post SendData();
				}
			}
		}
	}
	// If not cluster head, send your reading
	else {
		atomic {
			numSentPoints = numCollectedPoints;
			sendMotesAlive = motesAlive;
			motesAlive = 0;
			if (TOS_LOCAL_ADDRESS != 0) 
				numCollectedPoints = 0;
			reportNumber++;		
			if (!gfSendBusy) {
				gfSendBusy = TRUE;
				post SendData();
			}
		}
	}
#else 
	// Case 2. Clustering is not applied
	// Base station send aggregated data
	// Regular motes simply forward their readings.
	atomic {
		if (TOS_LOCAL_ADDRESS == 0)  {
			//numSentPoints++;
			atomic {
				numSentPoints += numCollectedPoints;
				numCollectedPoints = 0;
			}
		}
		else
			numSentPoints = 1;
		reportNumber++;	
		if (!gfSendBusy) {	
			gfSendBusy = TRUE;
			post SendData();
		}
	}
	call Leds.redToggle();
#endif
/*    dbg(DBG_USR1, "SurgeM: Timer fired\n");
    timer_ticks++;
    if (timer_ticks % TIMER_GETADC_COUNT == 0) {
      call ADC.getData();
    }
    // If we're the focused node, chirp
    if (focused && timer_ticks % TIMER_CHIRP_COUNT == 0) {
      call Sounder.start();
    }
    // If we're the focused node, chirp
    if (focused && timer_ticks % TIMER_CHIRP_COUNT == 1) {
      call Sounder.stop();
    }*/
    return SUCCESS;
  }
 
  async event result_t ADC.dataReady(uint16_t data) {
    //SurgeMsg *pReading;
    //uint16_t Len;
    dbg(DBG_USR1, "SurgeM: Got ADC reading: 0x%x\n", data);
    atomic {
      if (!gfSendBusy) {
	gfSendBusy = TRUE;	
	gSensorData = data;
	post SendData();
      }
    }
    return SUCCESS;
  }

  event result_t Send.sendDone(TOS_MsgPtr pMsg, result_t success) {
    dbg(DBG_USR2, "SurgeM: output complete 0x%x\n", success);
    //call Leds.greenToggle();
    atomic gfSendBusy = FALSE;
    return SUCCESS;
  }


  /* Command interpreter for broadcasts
   *
   */

  event TOS_MsgPtr Bcast.receive(TOS_MsgPtr pMsg, void* payload, uint16_t payloadLen) {
    SurgeCmdMsg *pCmdMsg = (SurgeCmdMsg *)payload;

    dbg(DBG_USR2, "SurgeM: Bcast  type 0x%02x\n", pCmdMsg->type);

    if (pCmdMsg->type == SURGE_TYPE_SETRATE) {       // Set timer rate
      timer_rate = pCmdMsg->args.newrate;
      dbg(DBG_USR2, "SurgeM: set rate %d\n", timer_rate);
      call Timer.stop();
      call Timer.start(TIMER_REPEAT, timer_rate);

    } else if (pCmdMsg->type == SURGE_TYPE_SLEEP) {
      // Go to sleep - ignore everything until a SURGE_TYPE_WAKEUP
      dbg(DBG_USR2, "SurgeM: sleep\n");
      sleeping = TRUE;
      call Timer.stop();
      call Leds.greenOff();
      call Leds.yellowOff();

    } else if (pCmdMsg->type == SURGE_TYPE_WAKEUP) {
      dbg(DBG_USR2, "SurgeM: wakeup\n");

      // Wake up from sleep state
      if (sleeping) {
	initialize();
        call Timer.start(TIMER_REPEAT, timer_rate);
	sleeping = FALSE;
      }

    } else if (pCmdMsg->type == SURGE_TYPE_FOCUS) {
      dbg(DBG_USR2, "SurgeM: focus %d\n", pCmdMsg->args.focusaddr);
      // Cause just one node to chirp and increase its sample rate;
      // all other nodes stop sending samples (for demo)
      if (pCmdMsg->args.focusaddr == TOS_LOCAL_ADDRESS) {
	// OK, we're focusing on me
	focused = TRUE;
	call Sounder.init();
	call Timer.stop();
	call Timer.start(TIMER_REPEAT, FOCUS_TIMER_RATE);
      } else {
	// Focusing on someone else
	call Timer.stop();
	call Timer.start(TIMER_REPEAT, FOCUS_NOTME_TIMER_RATE);
      }

    } else if (pCmdMsg->type == SURGE_TYPE_UNFOCUS) {
      // Return to normal after focus command
      dbg(DBG_USR2, "SurgeM: unfocus\n");
      focused = FALSE;
      call Sounder.stop();
      call Timer.stop();
      call Timer.start(TIMER_REPEAT, timer_rate);
    }
    return pMsg;
  }

/********************************************
 *		Intercept a Surge Message
 *			Collect and aggregate the data
 *			if you are a cluster head
 ********************************************/

  event result_t InterceptSurgeMsg.intercept(TOS_MsgPtr msg, void* payload, uint16_t payloadLen) {
    SurgeMsg *sMsg = (SurgeMsg *)payload;

#ifdef CLUSTERING_ON
	if (call RouteControl.isClusterHead()) {
		if (sMsg->type == SURGE_TYPE_SENSORREADING) {
			numCollectedPoints += sMsg->nPoints;
			motesAlive |= sMsg->motesAlive;
		}
	}
#else
	if (sMsg->type == SURGE_TYPE_SENSORREADING && TOS_LOCAL_ADDRESS == 0) {
		numCollectedPoints++;
	}
#endif
	call Leds.yellowToggle();
	return SUCCESS;
  }

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -