summaryrefslogtreecommitdiff
path: root/gr-radar/src/lib/sim-airplane2.cc
blob: 753b5952fff9999c2b76e49f2ebbec5375429e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* -*- c++ -*- */
/*
 * Copyright 2005 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <string>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <gr_complex.h>
#include <getopt.h>
#include <gr_misc.h>
#include <limits>
#include <gr_fxpt_nco.h>
#include "time_series.h"
#include "simulation.h"

static const double C = 3e8;	// sped of light, m/s


// ------------------------------------------------------------------------

class delay_line {
  std::vector<gr_complex>	d_z;
  const int			d_mask;
  int				d_newest;
public:
  delay_line(unsigned int max_delay)
    : d_z(gr_rounduppow2(max_delay)), d_mask(d_z.size()-1), d_newest(0)
  {
  }

  void
  push_item(gr_complex x)
  {
    d_newest = (d_newest - 1) & d_mask;
    d_z[d_newest] = x;
  }

  gr_complex
  ref_item(int delay) const 
  {
    return d_z[(d_newest + delay) & d_mask];
  }
};

// ------------------------------------------------------------------------

class aux_state {
public:
  dyn_object	*d_obj;
  double	 d_last_slant_range;
  gr_fxpt_nco	 d_nco;

  aux_state(dyn_object *obj) : d_obj(obj) {}
};

// ------------------------------------------------------------------------

class my_sim : public simulation
{
  FILE			 *d_output;
  time_series		 &d_ref;
  unsigned long long	  d_pos;		// position in time series
  delay_line		  d_z;
  dyn_object		 *d_tx;		// transmitter (not moving)
  dyn_object		 *d_rx0;		// receiver (not moving)
  std::vector<aux_state*> d_target;

  double		  d_baseline;		// length of baseline in meters
  double		  d_range_bin;		// meters/range_bin
  float			  d_tx_lambda;		// wavelength of tx signals in meters
  float			  d_sample_rate;
  float			  d_gain;		// linear scale factor

  void adjust_for_start_time(double start_time);
  
  bool write_output(gr_complex x)
  {
    return fwrite(&x, sizeof(x), 1, d_output) == 1;
  }

public:
  my_sim(FILE *output, time_series &ref, double timestep, float sample_rate,
	 double start_time, double tx_freq, double gain_db);
  ~my_sim();

  bool update();
  bool run(long long nsteps);
};


my_sim::my_sim(FILE *output, time_series &ref, double timestep,
	       float sample_rate, double start_time,
	       double tx_freq, double gain_db)
  : simulation(timestep),
    d_output(output), d_ref(ref), d_pos(0), d_z(1024),
    d_range_bin(C * timestep), d_tx_lambda(C/tx_freq), 
    d_sample_rate(sample_rate), d_gain(exp10(gain_db/10))
{
  d_tx = new dyn_object(point(0,0), point(0,0), "Tx");
  d_rx0 = new dyn_object(point(45e3,0), point(0,0), "Rx0");

  add_object(d_tx);
  add_object(d_rx0);
  d_baseline = dyn_object::distance(*d_tx, *d_rx0);

  {
    // add targets
    float aircraft_speed;
    float aircraft_angle;
    point aircraft_pos;
    dyn_object	*ac;

    // target 1
    aircraft_speed = 135; 			// m/s
    aircraft_angle = 240 * M_PI/180;
    aircraft_pos = point(55e3, 20e3);

    ac = new dyn_object(aircraft_pos,
			point(aircraft_speed * cos(aircraft_angle),
			      aircraft_speed * sin(aircraft_angle)),
			"Ac0");
    add_object(ac);
    d_target.push_back(new aux_state(ac));

    // target 2 
    aircraft_speed = 350; 			// m/s
    aircraft_angle = 0 * M_PI/180;
    aircraft_pos = point(-20e3, 60e3);

    ac = new dyn_object(aircraft_pos,
			point(aircraft_speed * cos(aircraft_angle),
			      aircraft_speed * sin(aircraft_angle)),
			"Ac1");
    add_object(ac);
    d_target.push_back(new aux_state(ac));
  }

  adjust_for_start_time(start_time);

  for (unsigned i = 0; i < d_target.size(); i++)
    d_target[i]->d_last_slant_range =
      (dyn_object::distance(*d_tx, *d_target[i]->d_obj)
       + dyn_object::distance(*d_target[i]->d_obj, *d_rx0));

}

my_sim::~my_sim()
{
}

void
my_sim::adjust_for_start_time(double start_time)
{
  for (unsigned i = 0; i < d_obj.size(); i++){
    // Adjust initial starting positions depending on simulation
    // start time.  FIXME Assumes velocity is constant
    point p = d_obj[i]->pos();
    point v = d_obj[i]->vel();
    p.set_x(p.x() + v.x() * start_time);
    p.set_y(p.y() + v.y() * start_time);
    d_obj[i]->set_pos(p);
  }
}

bool
my_sim::update()
{
  // std::cout << *d_ac0 << std::endl;

  // grab new item from input and insert it into delay line
  const gr_complex *in = (const gr_complex *) d_ref.seek(d_pos++, 1);
  if (in == 0)
    return false;
  d_z.push_item(*in);

  gr_complex s = 0;	// output sample
  // FIXME ought to add in attenuated direct path input


  // for each target, compute slant_range and slant_range'

  for (unsigned i = 0; i < d_target.size(); i++){
    aux_state *t = d_target[i];
    
    double slant_range = 
      (dyn_object::distance(*d_tx, *t->d_obj)
       + dyn_object::distance(*t->d_obj, *d_rx0));			  // meters

    double delta_slant_range = slant_range - t->d_last_slant_range;
    t->d_last_slant_range = slant_range;
    double deriv_slant_range_wrt_time = delta_slant_range / timestep();	  // m/sec

    //fprintf(stdout, "%10.3f\t%10.3f\n", slant_range, deriv_slant_range_wrt_time);

    // FIXME, may want to interpolate between two bins.
    int int_delay = lrint((slant_range - d_baseline) / d_range_bin);

    gr_complex x = d_z.ref_item(int_delay);

    // scale amplitude (this includes everything: RCS, antenna gain, losses, etc...)
    x = x * d_gain;

    if (1){
      // compute doppler and apply it
      float f_doppler = -deriv_slant_range_wrt_time / d_tx_lambda;

      t->d_nco.set_freq(f_doppler / d_sample_rate);
      gr_complex phasor(t->d_nco.cos(), t->d_nco.sin());
      x = x * phasor;
      t->d_nco.step();
    }

    s += x;		// add in this target's contribution
  }

  write_output(s);

  return simulation::update();		// run generic update
}

bool
my_sim::run(long long nsteps)
{
  //fprintf(stdout, "<%12.2f, %12.2f>\n", d_ac0->pos().x(), d_ac0->pos().y());
  //std::cout << *d_ac0 << std::endl;
  bool ok = simulation::run(nsteps);
  //std::cout << *d_ac0 << std::endl;
  //fprintf(stdout, "<%12.2f, %12.2f>\n", d_ac0->pos().x(), d_ac0->pos().y());
  return ok;
}

// ------------------------------------------------------------------------

static void
usage(const char *argv0)
{
  const char *progname;
  const char *t = std::strrchr(argv0, '/');
  if (t != 0)
    progname = t + 1;
  else
    progname = argv0;
    
  fprintf(stderr, "usage: %s [options] ref_file\n", progname);
  fprintf(stderr, "    -o OUTPUT_FILENAME [default=sim.dat]\n");
  fprintf(stderr, "    -n NSAMPLES_TO_PRODUCE [default=+inf]\n");
  fprintf(stderr, "    -s NSAMPLES_TO_SKIP [default=0]\n");
  fprintf(stderr, "    -g reflection gain in dB (should be <= 0) [default=0]\n");
  fprintf(stderr, "    -f transmitter freq in Hz [default=100MHz]\n");
  fprintf(stderr, "    -r sample rate in Hz [default=250kHz]\n");
  fprintf(stderr, "    -S simulation start time in seconds [default=0]\n");
}

int
main(int argc, char **argv)
{
  int	ch;
  const char *output_filename = "sim.dat";
  const char *ref_filename = 0;
  long long int nsamples_to_skip = 0;
  long long int nsamples_to_produce = std::numeric_limits<long long int>::max();
  double sample_rate = 250e3;
  double gain_db = 0;
  double tx_freq = 100e6;
  double start_time = 0;

  while ((ch = getopt(argc, argv, "o:s:n:g:f:S:")) != -1){
    switch (ch){
    case 'o':
      output_filename = optarg;
      break;
      
    case 's':
      nsamples_to_skip = (long long) strtod(optarg, 0);
      if (nsamples_to_skip < 0){
	usage(argv[0]);
	fprintf(stderr, "    nsamples_to_skip must be >= 0\n");
	exit(1);
      }
      break;

    case 'n':
      nsamples_to_produce = (long long) strtod(optarg, 0);
      if (nsamples_to_produce < 0){
	usage(argv[0]);
	fprintf(stderr, "    nsamples_to_produce must be >= 0\n");
	exit(1);
      }
      break;

    case 'g':
      gain_db = strtod(optarg, 0);
      break;

    case 'f':
      tx_freq = strtod(optarg, 0);
      break;

    case 'r':
      sample_rate = strtod(optarg, 0);
      break;

    case 'S':
      start_time = strtod(optarg, 0);
      break;

    case '?':
    case 'h':
    default:
      usage(argv[0]);
      exit(1);
    }
  } // while getopt

  if (argc - optind != 1){
    usage(argv[0]);
    exit(1);
  }

  ref_filename = argv[optind++];

  double timestep = 1.0/sample_rate;


  FILE *output = fopen(output_filename, "wb");
  if (output == 0){
    perror(output_filename);
    exit(1);
  }

  unsigned long long ref_starting_offset = 0;
  ref_starting_offset += nsamples_to_skip;

  try {
    time_series ref(sizeof(gr_complex), ref_filename, ref_starting_offset, 0);

    my_sim	simulator(output, ref, timestep, sample_rate, start_time,
			  tx_freq, gain_db);
    simulator.run(nsamples_to_produce);
  }
  catch (std::string &s){
    std::cerr << s << std::endl;
    exit(1);
  }

  return 0;
}