void CubeCreature::tic(){ timer++; detect_at(); if(core!=0)core->tic(); orders->tic();//sent order to state move_state_tic();//control creature avoid_collision();//avoid friend unit colli move_timer_tic(); timer_tic(); if(timer>1000000)timer=0; }
// constructor CE_Network_Loading::CE_Network_Loading(int argc, char **argv, ExtensibleCognitiveRadio *_ECR) { ECR = _ECR; // bandwidths are initialized to be evenly allocated active_bw_cfg_req = false; net_saturated = false; bw_cfg = 1; tx_bw_cfg_req = 1; t_settle = timer_create(); timer_tic(t_settle); // define subcarrier allocation 0 for (int i = 0; i < num_sc; i++) { if ((i+1 > num_sc/2 - guard_sc_0) && (i < num_sc/2 + guard_sc_0)) sc_alloc_0[i] = OFDMFRAME_SCTYPE_NULL; else if (abs((int)((float)num_sc/2.0 - (float)i - 0.5))%sc_pilot_freq == 0) sc_alloc_0[i] = OFDMFRAME_SCTYPE_PILOT; else sc_alloc_0[i] = OFDMFRAME_SCTYPE_DATA; } // define subcarrier allocation 1 for (int i = 0; i < num_sc; i++) { if ((i+1 > num_sc/2 - guard_sc_1) && (i < num_sc/2 + guard_sc_1)) sc_alloc_1[i] = OFDMFRAME_SCTYPE_NULL; else if (abs((int)((float)num_sc/2.0 - (float)i - 0.5))%sc_pilot_freq == 0) sc_alloc_1[i] = OFDMFRAME_SCTYPE_PILOT; else sc_alloc_1[i] = OFDMFRAME_SCTYPE_DATA; } // define subcarrier allocation 2 for (int i = 0; i < num_sc; i++) { if ((i+1 > num_sc/2 - guard_sc_2) && (i < num_sc/2 + guard_sc_2)) sc_alloc_2[i] = OFDMFRAME_SCTYPE_NULL; else if (abs((int)((float)num_sc/2.0 - (float)i - 0.5))%sc_pilot_freq == 0) sc_alloc_2[i] = OFDMFRAME_SCTYPE_PILOT; else sc_alloc_2[i] = OFDMFRAME_SCTYPE_DATA; } }
void Job::tic(){ timer_tic(); }
/*---------------------------------------------------------------------------*/ void clock_isr (void) { timer_tic() ; sendEOI() ; } /* End of Function clock_isr() */
// main program int main (int argc, char **argv) { // command-line options int verbose = 1; int ppm_error = 0; int gain = 0; unsigned int nfft = 64; float offset = -65.0f; float scale = 5.0f; float fft_rate = 10.0f; float rx_resamp_rate; float bandwidth = 800e3f; unsigned int logsize = 4096; char filename[256] = "rtl_asgram.dat"; int r, n_read; uint32_t frequency = 100000000; uint32_t samp_rate = DEFAULT_SAMPLE_RATE; uint32_t out_block_size = DEFAULT_BUF_LENGTH; uint8_t *buffer; int dev_index = 0; int dev_given = 0; struct sigaction sigact; normalizer_t *norm; // int d; while ((d = getopt(argc,argv,"hf:b:B:G:n:p:s:o:r:L:F:")) != EOF) { switch (d) { case 'h': usage(); return 0; case 'f': frequency = atof(optarg); break; case 'b': bandwidth = atof(optarg); break; case 'B': out_block_size = (uint32_t)atof(optarg); break; case 'G': gain = (int)(atof(optarg) * 10); break; case 'n': nfft = atoi(optarg); break; case 'o': offset = atof(optarg); break; case 'p': ppm_error = atoi(optarg); break; case 's': samp_rate = (uint32_t)atofs(optarg); break; case 'r': fft_rate = atof(optarg); break; case 'L': logsize = atoi(optarg); break; case 'F': strncpy(filename,optarg,255); break; case 'd': dev_index = verbose_device_search(optarg); dev_given = 1; break; default: usage(); return 1; } } // validate parameters if (fft_rate <= 0.0f || fft_rate > 100.0f) { fprintf(stderr,"error: %s, fft rate must be in (0, 100) Hz\n", argv[0]); exit(1); } if (!dev_given) { dev_index = verbose_device_search("0"); } if (dev_index < 0) { exit(1); } r = rtlsdr_open(&dev, (uint32_t)dev_index); if (r < 0) { fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); exit(1); } sigact.sa_handler = sighandler; sigemptyset(&sigact.sa_mask); sigact.sa_flags = 0; sigaction(SIGINT, &sigact, NULL); sigaction(SIGTERM, &sigact, NULL); sigaction(SIGQUIT, &sigact, NULL); sigaction(SIGPIPE, &sigact, NULL); /* Set the sample rate */ verbose_set_sample_rate(dev, samp_rate); /* Set the frequency */ verbose_set_frequency(dev, frequency); if (0 == gain) { /* Enable automatic gain */ verbose_auto_gain(dev); } else { /* Enable manual gain */ gain = nearest_gain(dev, gain); verbose_gain_set(dev, gain); } verbose_ppm_set(dev, ppm_error); rx_resamp_rate = bandwidth/samp_rate; printf("frequency : %10.4f [MHz]\n", frequency*1e-6f); printf("bandwidth : %10.4f [kHz]\n", bandwidth*1e-3f); printf("sample rate : %10.4f kHz = %10.4f kHz * %8.6f\n", samp_rate * 1e-3f, bandwidth * 1e-3f, 1.0f / rx_resamp_rate); printf("verbosity : %s\n", (verbose?"enabled":"disabled")); unsigned int i; // add arbitrary resampling component msresamp_crcf resamp = msresamp_crcf_create(rx_resamp_rate, 60.0f); assert(resamp); // create buffer for sample logging windowcf log = windowcf_create(logsize); // create ASCII spectrogram object float maxval; float maxfreq; char ascii[nfft+1]; ascii[nfft] = '\0'; // append null character to end of string asgram q = asgram_create(nfft); asgram_set_scale(q, offset, scale); // assemble footer unsigned int footer_len = nfft + 16; char footer[footer_len+1]; for (i=0; i<footer_len; i++) footer[i] = ' '; footer[1] = '['; footer[nfft/2 + 3] = '+'; footer[nfft + 4] = ']'; sprintf(&footer[nfft+6], "%8.3f MHz", frequency*1e-6f); unsigned int msdelay = 1000 / fft_rate; // create/initialize Hamming window float w[nfft]; for (i=0; i<nfft; i++) w[i] = hamming(i,nfft); //allocate recv buffer buffer = malloc(out_block_size * sizeof(uint8_t)); assert(buffer); // create buffer for arbitrary resamper output int b_len = ((int)(out_block_size * rx_resamp_rate) + 64) >> 1; complex float buffer_resamp[b_len]; debug("resamp_buffer_len: %d", b_len); // timer to control asgram output timer t1 = timer_create(); timer_tic(t1); norm = normalizer_create(); verbose_reset_buffer(dev); while (!do_exit) { // grab data from device r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read); if (r < 0) { fprintf(stderr, "WARNING: sync read failed.\n"); break; } if ((bytes_to_read > 0) && (bytes_to_read < (uint32_t)n_read)) { n_read = bytes_to_read; do_exit = 1; } // push data through arbitrary resampler and give to frame synchronizer // TODO : apply bandwidth-dependent gain for (i=0; i<n_read/2; i++) { // grab sample from usrp buffer complex float rtlsdr_sample = normalizer_normalize(norm, *((uint16_t*)buffer+i)); // push through resampler (one at a time) unsigned int nw; msresamp_crcf_execute(resamp, &rtlsdr_sample, 1, buffer_resamp, &nw); // push resulting samples into asgram object asgram_push(q, buffer_resamp, nw); // write samples to log windowcf_write(log, buffer_resamp, nw); } if ((uint32_t)n_read < out_block_size) { fprintf(stderr, "Short read, samples lost, exiting!\n"); break; } if (bytes_to_read > 0) bytes_to_read -= n_read; if (timer_toc(t1) > msdelay*1e-3f) { // reset timer timer_tic(t1); // run the spectrogram asgram_execute(q, ascii, &maxval, &maxfreq); // print the spectrogram printf(" > %s < pk%5.1fdB [%5.2f]\n", ascii, maxval, maxfreq); printf("%s\r", footer); fflush(stdout); } } // try to write samples to file FILE * fid = fopen(filename,"w"); if (fid != NULL) { // write header fprintf(fid, "# %s : auto-generated file\n", filename); fprintf(fid, "#\n"); fprintf(fid, "# num_samples : %u\n", logsize); fprintf(fid, "# frequency : %12.8f MHz\n", frequency*1e-6f); fprintf(fid, "# bandwidth : %12.8f kHz\n", bandwidth*1e-3f); // save results to file complex float * rc; // read pointer windowcf_read(log, &rc); for (i=0; i<logsize; i++) fprintf(fid, "%12.4e %12.4e\n", crealf(rc[i]), cimagf(rc[i])); // close it up fclose(fid); printf("results written to '%s'\n", filename); } else { fprintf(stderr,"error: %s, could not open '%s' for writing\n", argv[0], filename); } // destroy objects normalizer_destroy(&norm); msresamp_crcf_destroy(resamp); windowcf_destroy(log); asgram_destroy(q); timer_destroy(t1); rtlsdr_close(dev); free (buffer); return 0; }
// execute function void CE_Network_Loading::execute() { // Update the status of network saturation based on thresholds // with hysteresis int queued_bytes = ECR->get_tx_queued_bytes(); //printf("Queued Bytes: %i\n", queued_bytes); if ((!net_saturated) && (queued_bytes > sat_thresh_upper)){ dprintf("Transmitter saturated\n"); net_saturated = true; } else if (net_saturated && (queued_bytes < sat_thresh_lower)){ net_saturated = false; dprintf("Transmitter no longer saturated\n"); } // Request more bandwidth if all of the following conditions are met: // The network load is saturating our transmission, the bandwidth being // used can be increased, and we are not already requesting increased // bandwidth. if (net_saturated && (bw_cfg < 2) && (!active_bw_cfg_req) && (timer_toc(t_settle)>settle_period)){ active_bw_cfg_req = true; tx_bw_cfg_req = bw_cfg + 1; timer_tic(t_settle); dprintf("Requesting increased bandwidth to cfg %i\n", tx_bw_cfg_req); memcpy(tx_control_info, (void*)&tx_bw_cfg_req, sizeof(tx_bw_cfg_req)); ECR->set_tx_control_info(tx_control_info); } // Remove request for increased bandwidth if the network is no longer // saturated if ((!net_saturated) && active_bw_cfg_req) { active_bw_cfg_req = false; tx_bw_cfg_req = bw_cfg; dprintf("Removing request for increased bandwidth\n"); memcpy(tx_control_info, (void*)&tx_bw_cfg_req, sizeof(tx_bw_cfg_req)); ECR->set_tx_control_info(tx_control_info); } // Handle bandwidth requests from other nodes if ((ECR->CE_metrics.CE_event == ExtensibleCognitiveRadio::PHY_FRAME_RECEIVED) && (ECR->CE_metrics.control_valid)){ ECR->get_rx_control_info(rx_control_info); memcpy(&rx_bw_cfg_req, (void*)rx_control_info, sizeof(rx_bw_cfg_req)); // The other node has requested more bandwdith than currently allocated if (rx_bw_cfg_req > (2-bw_cfg)) { // Send control frames to grant the request if our transmitter is // currently not saturated or if we are currently using more bandwidth // than the other node if (((!net_saturated) || (bw_cfg == 2))) { dprintf("Granting request for bandwidth cfg %i\n", rx_bw_cfg_req); bw_cfg--; tx_bw_cfg_req--; memcpy(tx_control_info, (void*)&tx_bw_cfg_req, sizeof(tx_bw_cfg_req)); ECR->set_tx_control_info(tx_control_info); for (int i=0; i<3; i++) ECR->transmit_control_frame(NULL, 0); // Update tx/rx configuration set_bw_cfg(ECR, bw_cfg); } } } // If the other node has granted our request if ((ECR->CE_metrics.CE_frame == ExtensibleCognitiveRadio::CONTROL) && active_bw_cfg_req && (rx_bw_cfg_req == 2-tx_bw_cfg_req)){ dprintf("Request for increased bandwidth has been granted\n"); active_bw_cfg_req = false; bw_cfg = tx_bw_cfg_req; set_bw_cfg(ECR, bw_cfg); } }