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

📄 clnnm.nc

📁 主要用于无线传感网络的编写的书籍.对于初学者有着很大的用处
💻 NC
📖 第 1 页 / 共 2 页
字号:
    call Leds.redOn();    return call Timer.start(TIMER_REPEAT,SENSOR_PERIOD);  }    /* Function to be called from Anycast to request for Marzullo intersection */  command result_t CLNNlib.request(CLNNMsg *datat) {    uint16_t trained; // number of times trained so far        atomic {      trained = training;    }    if(trained != MAX_TRAIN)      return FAIL;        atomic {      datat->training = trained;      datat->min      = ptnn->min;      datat->max      = ptnn->max;      datat->f        = ptnn->f;      datat->percent  = ptnn->percent;      datat->mean     = ptnn->mean;      datat->limit    = ptnn->limit;      datat->s2       = ptnn->s2;    }    return SUCCESS;  }   /**  * Used to initialize this component.  */  command result_t StdControl.init() {    /* parameters to assign the weight array */    uint8_t i=0;    uint8_t j=0;    uint16_t step=100;    uint8_t inc=100;    call Leds.init();    call SensorControl.init();    //call Leds.redOff();    //call Leds.redOn();    dbg(DBG_USR1, "CLNN initialized\n");    atomic {      ptnn =(struct clnn*) malloc(sizeof(struct clnn));      reading = 0;      training = 0;      s_sum = 0;      //initilise the weights and clusters      for(i=0; i<NUM_UNITS; i++){		for(j=0; j< NUM_UNITS; j++){	  ptnn->weights[i][j]=step;	  ptnn->clusters[i][0]=NEGDIST;	  ptnn->clusters[i][1]=NEGDIST;	  ptnn->clusters[i][2]=0;	}	step=step+inc;      }      for(i=0; i<NUM_UNITS*3; i++) {	sort[i] = (struct Marzullo*) malloc(sizeof(struct Marzullo));      }      ptnn->min=0;      ptnn->max=0;      ptnn->f=0;      ptnn->percent=0;      ptnn->mean=0;      ptnn->limit=0;      ptnn->s2 = 0;    }    print_weights();    return SUCCESS;  }  /**   * Starts the SensorControl and CommControl components.   * @return Always returns SUCCESS.   */  command result_t StdControl.start() {    call SensorControl.start();    return SUCCESS;  }  /**   * Stops the SensorControl and CommControl components.   * @return Always returns SUCCESS.   */  command result_t StdControl.stop() {    call SensorControl.stop();    call Timer.stop();    return SUCCESS;  }  /* Trains raw sensor data with CLNN clustering */  void clnn_train() {    /* varibles for indexing into arrays */    uint8_t i = 0;    uint8_t j = 0;    uint8_t k = 0;    /* varibles for storing local temp values */    uint32_t temp  = 0;    uint32_t temp2 = 0;    uint32_t sum   = 0;    uint16_t trained;    /* minimum distance beween the winner and input vector */    uint32_t min_dist = NEGDIST;    /* minimum distance beween the lossers and input vector */    uint32_t min_distL = NEGDIST;        /* current training winner */    uint8_t winner = NEGDIST;    /* number of current epochs trained */    uint8_t e=0;        /* number of iterations done */    uint8_t iterations=0;        /* Calculate the Euclidean distance        from the input vector unit to all weights */    for(i=0; i<NUM_UNITS ;i++) {      for(j=0; j<NUM_UNITS; j++) {	temp = ptnn->weights[i][j] - buffer[j];	sum += temp*temp;      }      if(sum <= min_dist || min_dist == -1) {	min_dist = sum;	winner = i;      }      sum = 0;    }        print_raw(); // for debug        atomic {      /* Update the winning cluster interval and the number of won times */      if(ptnn->clusters[winner][2] == 0) {	ptnn->clusters[winner][0] = ptnn->clusters[winner][1] = buffer[0];       }      ptnn->clusters[winner][2]++;    }        for(i=0;i<NUM_UNITS;i++){      atomic {	if(buffer[i] < ptnn->clusters[winner][0]){ 	  ptnn->clusters[winner][0]= buffer[i];	}	if(buffer[i] > ptnn->clusters[winner][1]){ 	  ptnn->clusters[winner][1]= buffer[i];	}      }    }        dbg(DBG_USR1, "(%d,%d,%d)\n", ptnn->clusters[winner][0],	ptnn->clusters[winner][1],ptnn->clusters[winner][2]);    /*      Update the weights of the winner and perform the       leaky learning on other neurons. 	      Do that until one of the following has happend:      1) reach maximum number of EPOCHS      2) the distance between the weight and the input vector       Cannot be improved anymore. This is due to the resolution       of the division operator on modes. I.e no floating points division :(     */        for(e=0; e<MAX_EPOCH; e++){       sum = 0;      for(i=0; i<NUM_UNITS; i++) {	if(i == winner) { 	  for(j=0;j<NUM_UNITS;j++) { 	    atomic {	      temp2 = (buffer[j]- ptnn->weights[winner][j]) >> 3;	      ptnn->weights[winner][j] += temp2;	    }	  }	  /* Calculate the new distance between 	     the input and the weight vector*/	  sum = 0;	  for(k=0;k<NUM_UNITS;k++){	    atomic {	      temp = ptnn->weights[winner][k] - buffer[k];	    }            	    sum += temp*temp;	  }	  	  /* the distance between the weight and the vector 	  cannot be improved anymore. 	  I.e previous distance = currently calculated */	  sum = sqrt(sum);	  if(min_dist == sum) { 	    //call Leds.redOn();	    iterations = e;	    e = MAX_EPOCH;	  }	  else{ 	    min_dist = sum;	  }	}	else {	  for(j =0; j<NUM_UNITS;j++) {	    atomic {	      temp2 = (buffer[j]-ptnn->weights[i][j]) >> 8;	      ptnn->weights[i][j] += temp2;	    }	    sum = 0;	    	    for(k=0;k<NUM_UNITS;k++){	      atomic {		temp = ptnn->weights[j][k] - buffer[k];	      }	      sum += temp*temp;	    }	    sum = sqrt(sum);	    /* the distance between the weight and the input vector 	       can not be improuved anymore. 	       I.e previous distance = currently calculated */	    if(min_distL == sum) {	      //call Leds.redOn();	      continue;	    }	    else{ 	      min_distL = sum;	    }	  }	}      }    }        atomic {      // update the training count      trained = ++training;      call Leds.yellowToggle();      dbg(DBG_USR1, "Training number %d finished with %d interations\n",	  trained, iterations);      print_raw();      print_weights();      print_clusters();    }    // tell ADC to continue to generate raw sensor data    if(trained < MAX_TRAIN)      call Timer.start(TIMER_REPEAT,SENSOR_PERIOD);    // finished training -> do the marzullo intersection    if(trained == MAX_TRAIN) {      //call Leds.greenOn();      call Leds.yellowOn();      doMarzullo();    }    //doMarzullo();  }  /**   * Signalled when data is ready from the ADC. Stuffs the sensor   * reading into the current packet, and call train task when   * BUFFER_SIZE readings have been taken.   * @return Always returns SUCCESS.   */  async event result_t Sensor.dataReady(uint16_t data) {    uint16_t trained;    // For verify    if(verify == 1) {      atomic {	//call Leds.redToggle();	if(data > verify_max || data < verify_min) {	  verify_fault++;	}	else {	  if(verify_fault > 0)	    verify_fault--;	}	num_verify++;	if(num_verify >= MAX_TRAIN*10) {	  call Timer.stop();	  call Leds.redOff();	  call Anycastlib.start_verify(verify_fault);	}      }      return SUCCESS;    }    // -----------------    // For training    atomic {      trained = training;    }        if(trained >= MAX_TRAIN) {      call Timer.stop();      return SUCCESS;    }    atomic {      buffer[reading++] = data;      s_sum += data;      s_sum2 += data*data;      //call Leds.greenToggle();      if(reading == NUM_UNITS) {	dbg(DBG_USR1, "current average: %d\n",s_sum);	reading = 0;	call Timer.stop();	clnn_train();      }    }    return SUCCESS;  }  /**   * Signalled when the clock ticks.   * @return The result of calling Photo.getData().   */  event result_t Timer.fired() {    return call Sensor.getData();  }}

⌨️ 快捷键说明

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