Module: GPS_PVT::RTCM3::Packet

Defined in:
lib/gps_pvt/rtcm3.rb

Defined Under Namespace

Modules: GLONASS_Ephemeris, GLONASS_Observation, GPS_Ephemeris, GPS_Observation, MSM, MSM1_2_3, MSM4_6, MSM5_7, MSM_Header, QZSS_Ephemeris, SBAS_Ephemeris

Constant Summary collapse

DataFrame =
proc{
  unum_gen = proc{|n, sf|
    next [n, proc{|v| v}] unless sf
    [n, sf.kind_of?(Rational) ? proc{|v| (sf * v).to_f} : proc{|v| sf * v}]
  }
  num_gen = proc{|n, sf|
    lim = 1 << (n - 1)
    lim2 = lim << 1
    next [n, proc{|v| v >= lim ? v - lim2 : v}] unless sf
    [n, sf.kind_of?(Rational) ?
        proc{|v| v -= lim2 if v >= lim; (sf * v).to_f} :
        proc{|v| v -= lim2 if v >= lim; sf * v}]
  }
  num_sign_gen = proc{|n, sf|
    lim = 1 << (n - 1)
    next [n, proc{|v| v >= lim ? lim - v : v}] unless sf
    [n, sf.kind_of?(Rational) ?
        proc{|v| v = lim - v if v >= lim; (sf * v).to_f} :
        proc{|v| v = lim - v if v >= lim; sf * v}]
  }
  invalidate = proc{|orig, err|
    [orig[0], proc{|v| v == err ? nil : orig[1].call(v)}]
  }
  idx_list_gen = proc{|n, start|
    start ||= 0
    idx_list = (start...(start+n)).to_a.reverse
    [n, proc{|v| idx_list.inject([]){|res, idx|
      res.unshift(idx) if (v & 0x1) > 0
      break res unless (v >>= 1) > 0
      res
    } }]
  }
  sc2rad = 3.1415926535898
  df = { # {df_num => [bits, post_process] or generator_proc, ...}
    1 => proc{|n| n},
    2 => 12,
    3 => 12,
    4 => unum_gen.call(30, Rational(1, 1000)), # [sec]
    5 => 1,
    6 => 5,
    7 => 1,
    8 => 3,
    9 => 6,
    10 => 1,
    11 => invalidate.call(unum_gen.call(24, Rational(2, 100)), 0x800000), # [m]
    12 => invalidate.call(num_gen.call(20, Rational(5, 10000)), 0x80000), # [m]
    13 => 7,
    14 => unum_gen.call(8, 299_792.458), # [m]
    15 => invalidate.call(unum_gen.call(8, Rational(1, 4)), 0), # [db-Hz],
    16 => 2,
    17 => invalidate.call(num_gen.call(14, Rational(2, 100)), 0x2000), # [m]
    18 => num_gen.call(20, Rational(5, 10000)), # [m]
    19 => 7,
    20 => invalidate.call(unum_gen.call(8, Rational(1, 4)), 0), # [db-Hz]
    21 => 6,
    22 => 1,
    23 => 1,
    24 => 1,
    25 => num_gen.call(38, Rational(1, 10000)), # [m]
    34 => unum_gen.call(27, Rational(1, 1000)), # [sec]
    35 => 5,
    36 => 1,
    37 => 3,
    38 => 6,
    39 => 1,
    40 => [5, proc{|v| v - 7}],
    41 => invalidate.call(unum_gen.call(25, Rational(2, 100)), 0x1000000), # [m]
    42 => invalidate.call(num_gen.call(20, Rational(5, 10000)), 0x80000), # [m]
    43 => 7,
    44 => unum_gen.call(7, 599_584.916), # [m]
    45 => invalidate.call(unum_gen.call(8, Rational(1, 4)), 0), # [db-Hz],
    46 => 2,
    47 => invalidate.call(num_gen.call(14, Rational(2, 100)), 0x2000), # [m]
    48 => invalidate.call(num_gen.call(20, Rational(5, 10000)), 0x80000), # [m]
    49 => 7,
    50 => invalidate.call(unum_gen.call(8, Rational(1, 4)), 0), # [db-Hz]
    51 => 16,
    52 => 17,
    53 => 5,
    54 => 8,
    55 => 12,
    56 => 1,
    57 => 16,
    71 => 8,
    76 => 10,
    77 => 4,
    78 => 2,
    79 => num_gen.call(14, Rational(sc2rad, 1 << 43)), # [rad/s]
    81 => unum_gen.call(16, 1 << 4), # [sec]
    82 => num_gen.call(8, Rational(1, 1 << 55)), # [s/s^2]
    83 => num_gen.call(16, Rational(1, 1 << 43)), # [s/s]
    84 => num_gen.call(22, Rational(1, 1 << 31)), # [sec]
    85 => 10,
    86 => num_gen.call(16, Rational(1, 1 << 5)), # [m]
    87 => num_gen.call(16, Rational(sc2rad, 1 << 43)), # [rad/s]
    88 => num_gen.call(32, Rational(sc2rad, 1 << 31)), # [rad/s]
    89 => num_gen.call(16, Rational(1, 1 << 29)), # [rad]
    90 => unum_gen.call(32, Rational(1, 1 << 33)),
    91 => num_gen.call(16, Rational(1, 1 << 29)), # [rad]
    92 => unum_gen.call(32, Rational(1, 1 << 19)), # [m^1/2]
    93 => unum_gen.call(16, 1 << 4), # [sec]
    94 => num_gen.call(16, Rational(1, 1 << 29)), # [rad]
    95 => num_gen.call(32, Rational(sc2rad, 1 << 31)), # [rad/s]
    96 => num_gen.call(16, Rational(1, 1 << 29)), # [rad]
    97 => num_gen.call(32, Rational(sc2rad, 1 << 31)), # [rad/s]
    98 => num_gen.call(16, Rational(1, 1 << 5)), # [m]
    99 => num_gen.call(32, Rational(sc2rad, 1 << 31)), # [rad]
    100 => num_gen.call(24, Rational(sc2rad, 1 << 43)), # [rad/s]
    101 => num_gen.call(8, Rational(1, 1 << 31)), # [sec]
    102 => 6,
    103 => 1,
    104 => 1,
    105 => 1,
    106 => 2,
    107 => [12, proc{|v|
      hh, mm, ss = [v >> 7, (v & 0x7E) >> 1, (v & 0x1) > 0 ? 30 : 0]
      hh * 3600 + mm * 60 + ss # [sec]
    }],
    108 => 1,
    109 => 1,
    110 => unum_gen.call(7, 15 * 60), # [sec]
    111 => num_sign_gen.call(24, Rational(1000, 1 << 20)), # [m/s]
    112 => num_sign_gen.call(27, Rational(1000, 1 << 11)), # [m]
    113 => num_sign_gen.call(5, Rational(1000, 1 << 30)), # [m/s^2]
    120 => 1,
    121 => num_sign_gen.call(11, Rational(1, 1 << 40)),
    122 => 2, # (M)
    123 => 1, # (M)
    124 => num_sign_gen.call(22, Rational(1, 1 << 30)), # [sec]
    125 => num_sign_gen.call(5, Rational(1, 1 << 30)), # [sec], (M)
    126 => 5, # [day]
    127 => 1, # (M)
    128 => 4, # (M)
    129 => invalidate.call(unum_gen.call(11), 0), # [day]
    130 => 2, # 1 => GLONASS-M, (M) fields are active 
    131 => 1,
    132 => invalidate.call(unum_gen.call(11), 0), # [day]
    133 => num_sign_gen.call(32, Rational(1, 1 << 31)), # [sec]
    134 => invalidate.call(unum_gen.call(5), 0), # [4year], (M)
    135 => num_sign_gen.call(22, Rational(1, 1 << 30)), # [sec], (M)
    136 => 1, # (M)
    137 => 1,
    141 => 1,
    142 => 1,
    248 => 30,
    364 => 2,
    393 => 1,
    394 => idx_list_gen.call(64, 1),
    395 => idx_list_gen.call(32, 1),
    396 => proc{|df394, df395|
      x_list = df394.product(df395)
      idx_list = idx_list_gen.call(x_list.size)[1]
      [x_list.size, proc{|v| x_list.values_at(*idx_list.call(v))}]
    },
    397 => invalidate.call(unum_gen.call(8, Rational(1, 1000)), 0xFF), # [sec]
    398 => unum_gen.call(10, Rational(1, 1000 << 10)), # [sec]
    399 => invalidate.call(num_gen.call(14), 0x2000), # [m/s]
    400 => invalidate.call(num_gen.call(15, Rational(1, 1000 << 24)), 0x4000), # [sec],
    401 => invalidate.call(num_gen.call(22, Rational(1, 1000 << 29)), 0x200000), # [sec],
    402 => 4,
    403 => invalidate.call(unum_gen.call(6), 0), # [dB-Hz],
    404 => invalidate.call(num_gen.call(15, Rational(1, 10000)), 0x4000), # [m/s]
    405 => invalidate.call(num_gen.call(20, Rational(1, 1000 << 29)), 0x80000), # [sec]
    406 => invalidate.call(num_gen.call(24, Rational(1, 1000 << 31)), 0x800000), # [sec]
    407 => 10,
    408 => invalidate.call(unum_gen.call(10, Rational(1, 1 << 4)), 0), # [dB-Hz]
    409 => 3,
    411 => 2,
    412 => 2,
    416 => 3,
    417 => 1,
    418 => 3,
    420 => 1,
    429 => 4,
    :uint => proc{|n| n},
  }
  df[27] = df[26] = df[25]
  df[117] = df[114] = df[111]
  df[118] = df[115] = df[112]
  df[119] = df[116] = df[113]
  {430..433 => 81..84, 434 => 71, 435..449 => 86..100, 450 => 79, 451 => 78,
      452 => 76, 453 => 77, 454 => 102, 455 => 101, 456 => 85, 457 => 137}.each{|dst, src|
    # QZSS ephemeris => GPS
    src = (src.to_a rescue [src]).flatten
    (dst.to_a rescue ([dst] * src.size)).flatten.zip(src).each{|i, j| df[i] = df[j]}
  }
  df.merge!({
    :SBAS_prn => [6, proc{|v| v + 120}],
    :SBAS_iodn => 8,
    :SBAS_tod => num_gen.call(13, 1 << 4),
    :SBAS_ura => df[77],
    :SBAS_xy => num_gen.call(30, Rational(8, 100)),
    :SBAS_z => num_gen.call(25, Rational(4, 10)),
    :SBAS_dxy => num_gen.call(17, Rational(1, 1600)),
    :SBAS_dz => num_gen.call(18, Rational(1, 250)),
    :SBAS_ddxy => num_gen.call(10, Rational(1, 80000)),
    :SBAS_ddz => num_gen.call(10, Rational(1, 16000)),
    :SBAS_agf0 => num_gen.call(12, Rational(1, 1 << 31)),
    :SBAS_agf1 => num_gen.call(8, Rational(1, 1 << 40)),
  })
  df.define_singleton_method(:generate_prop){|idx_list|
    hash = Hash[*([:bits, :op].collect.with_index{|k, i|
      [k, idx_list.collect{|idx, *args|
        case prop = self[idx]
        when Proc; prop = prop.call(*args)
        end
        [prop].flatten(1)[i]
      }]
    }.flatten(1))].merge({:df => idx_list})
    hash[:bits_total] = hash[:bits].inject{|a, b| a + b} || 0
    hash
  }
  df
}.call
MessageType =

Instance Method Summary collapse

Instance Method Details

#decode(bits_list, offset = nil) ⇒ Object



15
16
17
18
# File 'lib/gps_pvt/rtcm3.rb', line 15

def decode(bits_list, offset = nil)
  # 24 is offset of header in transport layer
  Util::BitOp::extract(self, bits_list, offset || 24)
end

#message_numberObject



19
20
21
# File 'lib/gps_pvt/rtcm3.rb', line 19

def message_number
  decode([12]).first
end

#parseObject



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/gps_pvt/rtcm3.rb', line 460

def parse
  msg_num = message_number
  return nil unless (mt = MessageType[msg_num])
  # return [[value, df], ...]
  values, df_list, attributes = [[], [], []]
  add_proc = proc{|target, offset|
    values += decode(target[:bits], offset).zip(target[:op]).collect{|v, op|
      op ? op.call(v) : v
    }
    df_list += target[:df]
  }
  add_proc.call(mt)
  case msg_num
  when 1001..1004
    nsat = values[4]
    offset = 24 + mt[:bits_total]
    add_proc.call(DataFrame.generate_prop(([{
          1001 => (9..13).to_a,
          1002 => (9..15).to_a,
          1003 => (9..13).to_a + (16..19).to_a,
          1004 => (9..20).to_a,
        }[msg_num]] * nsat).flatten), offset)
    attributes << GPS_Observation
  when 1009..1012
    nsat = values[4]
    offset = 24 + mt[:bits_total]
    add_proc.call(DataFrame.generate_prop(([{
          1009 => (38..43).to_a,
          1010 => (38..45).to_a,
          1011 => (38..43).to_a + (46..49).to_a,
          1012 => (38..50).to_a,
        }[msg_num]] * nsat).flatten), offset)
    attributes << GLONASS_Observation
  when 1013
    add_proc.call(DataFrame.generate_prop(
        ((55..57).to_a * values[4]).flatten), 24 + mt[:bits_total])
  when 1019
    attributes << GPS_Ephemeris
  when 1020
    attributes << GLONASS_Ephemeris
  when 1043
    attributes << SBAS_Ephemeris
  when 1044
    attributes << QZSS_Ephemeris
  when 1071..1077, 1081..1087, 1091..1097, 1101..1107, 1111..1117, 1121..1127
    # 107X(GPS), 108X(GLONASS), 109X(GALILEO), 110X(SBAS), 111X(QZSS), 112X(Beidou)
    nsat, nsig = [-2, -1].collect{|i| values[i].size}
    offset = 24 + mt[:bits_total]
    df396 = DataFrame.generate_prop([[396, values[-2], values[-1]]])
    add_proc.call(df396, offset)
    ncell = values[-1].size
    offset += df396[:bits_total]
    msm_proc = proc{|sat_data, signal_data|
      msm_sat = DataFrame.generate_prop(([sat_data] * nsat).transpose.flatten(1))
      add_proc.call(msm_sat, offset)
      offset += msm_sat[:bits_total]
      msm_sig = DataFrame.generate_prop(([signal_data] * ncell).transpose.flatten(1))
      add_proc.call(msm_sig, offset)
    }
    case msg_num % 10
    when 1
      attributes << MSM1_2_3
      msm_proc.call([398], [400])
    when 2
      attributes << MSM1_2_3
      msm_proc.call([398], [401, 402, 420])
    when 3
      attributes << MSM1_2_3
      msm_proc.call([398], [400, 401, 402, 420])
    when 4
      attributes << MSM4_6
      msm_proc.call([397, 398], [400, 401, 402, 420, 403])
    when 5
      attributes << MSM5_7
      msm_proc.call([397, [:uint, 4], 398, 399], [400, 401, 402, 420, 403, 404])
    when 6
      attributes << MSM4_6
      msm_proc.call([397, 398], [405, 406, 407, 420, 408])
    when 7
      attributes << MSM5_7
      msm_proc.call([397, [:uint, 4], 398, 399], [405, 406, 407, 420, 408, 404])
    else
      attributes << MSM # for #range
    end
  end
  attributes << MSM_Header if (1070..1229).include?(msg_num)
  res = values.zip(df_list)
  attributes.empty? ? res : res.extend(*attributes)
end