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

📄 micasbtest1m.nc

📁 针对wsn网络
💻 NC
📖 第 1 页 / 共 2 页
字号:
		  iTimerThreshold--;		  iTimerThresholdCummulative--;		}	      	      		if(  iTimerThreshold < TIME_UNDER_MIN-2)		  iTimerThreshold = TIME_UNDER_MIN-2;	//dont let the counter underflow				if ( iTimerThresholdCummulative < 0)		  iTimerThresholdCummulative = 0;	//dont let the counter underflow				// Check if ADC is close to saturation		if( xsum>MAG_ADC_ALMOSTMAX 	|| xsum<MAG_ADC_ALMOSTMIN ||		    ysum>MAG_ADC_ALMOSTMAX || ysum<MAG_ADC_ALMOSTMIN)		  iTimeinSaturation++;		else		  iTimeinSaturation = 0;	//clear count if drop out of saturation				/*		  State Machine for Event Detection		*/				switch( EVState) 		  {		  case EV_UNDER:	   					    call Leds.yellowOff();       //LED off  (Yellow LED on MICA)		    call Leds.greenOn();       //LED On  (Green LED on MICA)		  if ( iTimerThreshold > TIME_OVER_MIN) {	   //Are we over the threshold yet?		    iTimerThreshold = 0;	//reset the count - it now reflects elapsed time over		    NextEV = EV_OVER;	//trigger event		  }		  break;		case EV_OVER:						  call Leds.yellowOn();       // led ON  (Yellow LED on MICA)		  call Leds.greenOff();       //LED Off  (Green LED on MICA)		  if(  iTimerThreshold<TIME_UNDER_MIN) { //Are we still over the threshold ?		    iTimerThreshold = 0; //reset the count		    //	 iTimerThresholdCummulative) = 0;		    NextEV = EV_UNDER;	//been under for awhile  		  }		  if(  iTimerThresholdCummulative > TIME_OVER_MAX) { //been on too long		    baselineCount = 0;	//Establish a new BASELINE measure state		    iTimerThreshold = 0;		    iTimerThresholdCummulative = 0;		    NextEV = EV_UNDER;		  }		  break;		  default: NextEV = EV_UNDER ; break;	 //should never get here!		  }//switch EVState		#ifdef new		if(  EVState == EV_UNDER ) {					  call Leds.yellowOff();       //LED off  (Yellow LED on MICA)		  call Leds.greenOn();       //LED On  (Green LED on MICA)		  if( iTimerThreshold > TIME_OVER_MIN) {	   //Are we over the threshold yet?		    iTimerThreshold = 0;	//reset the count		    NextEV = EV_OVER;	//trigger event		  }		}// EV_UNDER#endif //new		//--------------------------process message-----------------------		if( EVState >= EV_OVER)  // it is a real signal   		  { // Event Detected  		    call Leds.yellowOn();       // led ON  (Yellow LED on MICA)		    call Leds.greenOff();       //LED Off  (Green LED on MICA)		    if(  iTimerThresholdCummulative > TIME_OVER_MAX) { //been on too long		      baselineCount = 0;	//Establish a new BASELINE measure state		      iTimerThreshold = 0;		      iTimerThresholdCummulative = 0;		      NextEV = EV_UNDER;		    }		 		  } //event detected		break; //DM_NORMAL			      default:		//should never get here		NextDAQMode = DM_NORMAL;		break;	      }	//switch DAQMode	  }  //else DAQModes      }	//baseline>baseline count    DAQMode = NextDAQMode;		//update state    EVState = NextEV;		//update Event state  }      /**   * Initialize this and all low level components used in this application.   *    * @return returns <code>SUCCESS</code> or <code>FAIL</code>   */  command result_t StdControl.init() {    result_t ret;    samp_on = 1;            //Enable sampling on init    DAQMode = DM_NORMAL;    //DAQ MOde    EVState = EV_UNDER;    cAZBit  = 0;    I2CBusy = I2C_IDLE;     // clear I2C commbus busy flag        iTimeinSaturation = 0;	//reset saturation flag    iTimerThreshold = 0;    iTimerThresholdCummulative = 0;        // initialized bufferdata to look like mag - static info    bufr_data.xsum = 0;    bufr_data.ysum = 500;    avgMagX = 511;    avgMagY = 512;        msgPtr = & buffer1;    oldmsgPtr = & buffer2;    msgIndex = 0;    stepdown = 6;        InitSampling();        ret = call CommControl.init();        //initialize lower components    ret &= call TempControl.init();       //initialize temperature component    ret &= call AccelControl.init();      //initialize accelerometer    ret &= call MagControl.init();        //ret &= call Clock.setRate(64, 0x02); /* every 16 milli seconds */        ret &= call Leds.init();    call Leds.redOff();    call Leds.greenOff();    call Leds.yellowOff();    dbg(DBG_BOOT, ("MAGSZ is initialized.\n"));        return ret;  }  /**   * Start this component.   *    * @return returns <code>SUCCESS</code>   */  command result_t StdControl.start(){    DAQMode = DM_STARTUP;	  //wait for system to settle down    iTimerThreshold = STARTUP_HOLDOFF;    call Timer.start(TIMER_REPEAT, 16); // 16 ms    return SUCCESS;  }  /**   * Stop this component.   *    * @return returns <code>SUCCESS</code>   */  command result_t StdControl.stop() {    return SUCCESS;  }    /**   *  In response to the <code>Clock.fire</code> event, it checks to see if    *  the startup phase has finished or not, is sampling enabled  and issues command    *  to starting sampling the magnetometer (X axis first or Magnetometer A pin).   *     *  @return returns <code>SUCCESS</code>   *   **/  event result_t Timer.fired(){        if ( DAQMode == DM_STARTUP ) {	      //idle until startup of radio etc has finished      iTimerThreshold--;      if (!iTimerThreshold)	DAQMode = DM_AUTOZERO_START;		// do an autozero    }        if( samp_on == 0){      //call Leds.redOff();         //red led off      return SUCCESS;               //break loop if not sampling      }        if( iTimeinSaturation>RE_ZERO_COUNT && (DAQMode==DM_NORMAL) ){       DAQMode = DM_AUTOZERO_START;	  //start autozero operation      iTimeinSaturation = 0;    }        call MagX.getData(); //start ADC for X -maps to Event-2          return SUCCESS;  }  /**   *  In response to the <code>MagX.dataReady</code> event, it stores the sample   *  and issues command to sample the magnetometer's Y axis. (Magnetometer B pin)   *     *  @return returns <code>SUCCESS</code>   *   **/  async event result_t MagX.dataReady(uint16_t data){    bufr_data.adata[ bufr_data.index].x_val = data;    call  MagY.getData(); //get data for MagnetometerB    return SUCCESS;    }  /**   *  In response to the <code>MagY.dataReady</code> event, it stores the sample   *  and issues a task to filter and process the stored magnetometer data.   *   *  It also has a schedule which starts sampling the Temperture and Accelormeter depending   *  on the stepdown counter.   *    *  @return returns <code>SUCCESS</code>   **/  async event result_t MagY.dataReady(uint16_t data){    bufr_data.adata[ bufr_data.index].y_val = data;    post FILTER_DATA();    stepdown--;        if ( stepdown == 4){      call TempADC.getData();    }else if ( stepdown == 2){      call AccelX.getData();    }else if ( stepdown == 0){      call AccelY.getData();      stepdown = 6;    }    return SUCCESS;    }    /**   *  In response to the <code>TempADC.dataReady</code> event, it stores the sample   *  which will be sent out to the UART as a message.   *     *  @return returns <code>SUCCESS</code>   *   **/  async event result_t TempADC.dataReady(uint16_t data){    short * mp = (short *)&( msgPtr->data[0]);    mp[(int) msgIndex++] = data;    return SUCCESS;  }  /**   *  In response to the <code>AccelX.dataReady</code> event, it stores the sample   *  which will be sent out to the UART as a message.   *     *  @return returns <code>SUCCESS</code>   *   **/  async event result_t AccelX.dataReady(uint16_t  data){    uint16_t * mp = (uint16_t *) &( msgPtr->data[0]);    mp[(int) msgIndex++] = data;    return SUCCESS;  }  /**   *  In response to the <code>AccelY.dataReady</code> event, it stores the sample   *  to the message buffer.  If the message buffer is full, sent it out to the UART.   *     *  @return returns <code>SUCCESS</code>   *   **/  async event result_t AccelY.dataReady(uint16_t data){    TOS_MsgPtr tmp;    uint16_t * mp = (uint16_t *) &( msgPtr->data[0]);    mp[(int) msgIndex++] = data;    if ( msgIndex == 15)      {	post SendTask();	//call Send.send(TOS_UART_ADDR, 29,  msgPtr);	msgIndex = 0;	tmp =  oldmsgPtr;	oldmsgPtr =  msgPtr;	msgPtr= tmp;      }    return SUCCESS;  }  /**   *  Response to the <code>Send.sendDone</code> event.  Simply returns.   *     *  @return returns <code>SUCCESS</code>   *   **/  event result_t Send.sendDone(TOS_MsgPtr sent_msgptr, result_t success){    /*      if( oldmsgPtr == sent_msgptr){	//pointing to the same structure as sent?      printf("Message has been sent\n");      }    */    return SUCCESS;  }  /**   *  In response to the <code>MagSetting.gainAdjustXDone</code> event,   *  start adjusting the Y axis.   *   *  @return returns <code>SUCCESS</code> of <code>FAIL</code>   *   **/  event result_t MagSetting.gainAdjustXDone(bool success) {    char ret;    // Offset has been updated, clear busy flag          if(  I2CBusy==I2C_POTX) { //update MAG Y POT      I2CBusy = I2C_POTY;      ret = call MagSetting.gainAdjustY( cMagYOffset);      return ret;    }     return SUCCESS;  }  /**   *  In response to the <code>MagSetting.gainAdjustYDone</code> event,   *  Force a new 4-sample average if we are doing autozeroing.   *   *  @return returns <code>SUCCESS</code>   *   **/  event result_t MagSetting.gainAdjustYDone(bool success) {    I2CBusy = I2C_IDLE;      if(  DAQMode == DM_AUTOZERO)	//Autozeroing so force a new ADC baseline acq      bufr_data.minCount = 0;  //force a new 4-sample average    return SUCCESS;  }  /**   * Module scoped method.     * It sets frame parameters to their initial values.   *   * @return returns <code>SUCCESS</code>   **/  void InitSampling()    {      int i;            avgMagX = 0;      avgMagY = 0;      baselineCount = 0;            //Init buffer contents      bufr_data.index = 0;      bufr_data.minCount = 0;            bufr_data.xsum = 0;      bufr_data.ysum = 0;      bufr_data.trgHoldoffCount = 0;      for(i=0;i<BUFR_WSAMPLES;i++)	{	  bufr_data.adata[i].x_val = 0;	  bufr_data.adata[i].y_val = 0;	}            call Leds.greenOff();   //clear led    }  } // end of implemnetation

⌨️ 快捷键说明

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