moodoki / radical_sdk

RaDICaL dataset SDK
https://moodoki.github.io/radical_sdk/
Apache License 2.0
43 stars 12 forks source link

Parse radar data in ros bag file #8

Closed kathy-lee closed 1 year ago

kathy-lee commented 1 year ago

Hi @moodoki , according to your info(https://github.com/moodoki/radical_sdk/issues/2) and the info in radarcfg.tar.gz, I wrote the following code to parse the radar messages in raw ros bag file, also used the function definition "reshape_frame" you mentioned in iwr_raw_rosnode project and tried with the following parameters. But from the result range-azimuth plots below it seems not so correct like yours in https://github.com/moodoki/radical_sdk/blob/master/nbs/Beamforming.ipynb. Could you help me to get the right parsing please, many thanks to you again!

import rosbag
import matplotlib.pyplot as plt
import numpy as np
import os

from radicalsdk.h5dataset import H5DatasetLoader
from radicalsdk.radar.config_v1 import read_radar_params
from radicalsdk.radar.v1 import RadarFrame

os.chdir("/home/USER/dataset/radical")
# indoor_human: 155648=304*32*8*2
bag = rosbag.Bag('indoor_human/2020-03-10-15-55-32.bag')

def reshape_frame(data, samples_per_chirp, n_receivers, n_tdm, n_chirps_per_frame):
  _data = data.reshape(-1, 8)
  _data = _data[:, :4] + 1j * _data[:, 4:]
  _data = _data.reshape(n_chirps_per_frame, samples_per_chirp, n_receivers)

  #deinterleve if theres TDM
  if n_tdm > 1:
      _data_i = [_data[i::n_tdm, :, :] for i in range(n_tdm)]
      _data = np.concatenate(_data_i, axis=-1)

  return _data

count = 0
radar_config = read_radar_params('projects/radical_sdk/samples/indoor_human_rcs.cfg')
rf = RadarFrame(radar_config)
for topic, msg, t in bag.read_messages(topics=['/radar_data']):
  count = count+1
  print("\nProcessing no.", count, "th radar msg:")
  print(len(msg.data))
  arr = np.array(msg.data)
  complex_arr = reshape_frame(arr,304,8,1,32)
  print(complex_arr.shape) # (32, 304, 8)
  complex_arr = np.reshape(complex_arr, (32, 8, 304)) # Acoording to: the demo h5 file has (32,8,304) shape
  beamformed_range_azimuth = rf.compute_range_azimuth(complex_arr) 
  if(count == 100):
    plt.figure(figsize=(5, 8))
    plt.imshow(np.log(np.abs(beamformed_range_azimuth)))
    # plt.show()
    plt.savefig("projects/radical_sdk/samples/indoor_human_ra.png")

  if(count == 100):
    break

bag.close()

As you see, I used the following parameter values, not sure if they're all correct.

# indoor
# raw radar msg data length is 155648
samples_per_chirp = 304
n_receivers = 8
n_tdm = 1
n_chirps_per_frame = 32

(I tried also with samples_per_chirp = 304, n_receivers=4, n_tdm=2, n_chirps_per_frame = 32 but this is not valid input to reshape_frame func) Range-Azimuth results of the 1st message: indoor_human_ra_001


Updated: I think I figured out the parsing:

  1. parameters:
    # indoor
    # raw radar msg data length is 155648
    samples_per_chirp = 304
    n_receivers = 4
    n_tdm = 2
    n_chirps_per_frame = 64
    1. change complex_arr = np.reshape(complex_arr, (32, 8, 304)) to complex_arr = np.swapaxes(complex_arr, 1, 2)

Please correct me if I'm wrong. Thanks!

moodoki commented 1 year ago

Hi, your updated parameters are correct for the indoor config. However, you shouldn't need to set these yourself. If you have ROS setup, playing back the ros bags will allow you to read the config parameters. Alternatively, a function in the ros node code will convert the radar commands to a Python dictionary for easier use. Check cfg_list_to_dict(). This dictionary is the what's read from ROS hereparams = rospy.get_param('iwr_cfg').

Hope this helps! It's a little messy as there's a need to convert between serial commands that the radar firmware uses and convenient datatypes to work with in Python.

kathy-lee commented 1 year ago

Thanks a lot for the info, that's very helpful for my understanding!