Monday, June 17, 2019

Android Studio: Resolving PreferenceFragment is deprecated warning

I encountered the following warning message in Android Studio while converting some Android Java code to Kotlin:
'PreferenceFragment' is deprecated. Deprecated in Java

Some digging through the Android documentation resulted in a solution; the android.preference.PreferenceFragment class is being deprecated and can be replaced with the new Jetpack androidx preference classes. So I needed to do the following:
  1. Add a new dependency to the new Androidx preference library in the app's build.gradle file

    implementation "androidx.preference:preference:1.0.0"
  2. In the Kotlin source code, remove the following namespace import statement:

    import android.preference.PreferenceFragment
  3. Insert the following namespace import statement:

    import androidx.preference.PreferenceFragmentCompat
  4. Then override the abstract onCreatePreferences method.

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

Monday, June 10, 2019

Android Studio shortcut to insert a Kotlin logging code using Live Template


Android Studio has a Live Template system for storing predefined code templates which can be recalled via an abbreviation keyword and automatically inserted at the cursor location in the source code editor. This can ease the task of typing in frequently used code syntax.

In this example, a template with the abbreviation logd shall be created which will insert the following code at the cursor location when activated only within a Kotlin language file. Once the code is inserted, the cursor will be placed inside the quotation marks.

Log.d(TAG, "")

Do the following:
  1. In Android Studio, select File | Settings.

    The Settings dialog box appears.
     
  2.  Select Editor | Live Templates.

  3. Then click the + button on the right. Choose Live Template. In the Abbreviation field, type in the keyword, e.g. logd that will activate this template. In the Description field, type in a accurate value e.g. Log debug message.

  4. In the Template text field, type in the code syntax and/or any macro, e.g. Log.d(TAG, "$END$")

    Note: $END$ is a macro which will position the cursor after the template has been inserted.
  5. Next, click Define to choose the environment in which this template can be activated.

  6. In the popup, choose a language or group construct e.g. Kotlin. Click OK

    The template is created.
Now, to use the newly created template, open a Kotlin file in the code editor. Place the cursor on the location in the code and press CTRL-j on the keyboard. A pop up menu with a list of template keywords will appear.



Type in the keyword logd or choose the logd keyword with the mouse and press Enter. The template is inserted and the cursor placed between the quote marks as shown below.

Monday, June 3, 2019

Installing and running the Velodyne Height Map package on ROS Melodic distribution

The ROS Velodyne Height Map package (https://wiki.ros.org/velodyne_height_map) is a useful tool for identifying obstacles in a point cloud. An example of potential obstacles is shown below (the red squares).

However the last supported ROS distribution seems to be Indigo, a few releases from the latest Melodic distribution. But it is still possible to install and run the package on the latest ROS distribution. To do that, the source code from https://github.com/jack-oquin/velodyne_height_map must be downloaded and compiled on the ROS installation. The following steps illustrate the procedure.

Create a ROS Catkin workspace
  1. Follow the tutorial here at https://wiki.ros.org/catkin/Tutorials/create_a_workspace to create an empty workspace, e.g /path/to/catkin_ws/.
Download the Velodyne Height Map source code
  1. Open up a Terminal. Change the directory to the location of the Catkin workspace's src directory created earlier.

    $ cd /path/to/catkin_ws/src
  2.  Assuming git is installed, clone the velodyne height map source code.

    $ git clone https://github.com/jack-oquin/velodyne_height_map.git
Build the package
  1. In the Terminal, change the directory to the root of the Catkin workspace.

    $ cd /path/to/catkin_ws
  2. Type in the following command to build the packages.

    $ catkin_make
Once the package is built, the Velodyne Height Map package can be executed according to the instructions here at https://wiki.ros.org/velodyne_height_map. For example,

$ rosrun velodyne_height_map heightmap_node

Monday, May 27, 2019

Displaying Velodyne PCAP data in ROS' RViz

Pre-recorded data in PCAP format from Velodyne's LiDAR sensors such as the HDL-32E, VLP-16, etc. can be displayed in the RViz application software from the Robotic Operating System (ROS). The following steps show how:
  1. In Ubuntu, open up a Terminal. Change directory to the location of the PCAP file e.g. /path/to/download/HDL32-V2_Monterey_Highway.pcap.

    $ cd /path/to/download/
    $ roslaunch velodyne_pointcloud 32e_points.launch pcap:=$(pwd)/HDL32-V2_Monterey_Highway.pcap


    Note 1: 32e_points.launch is the sample launch file for HDL-32E sensors (change this for different sensors),
    $(pwd) is a macro that points to the current working directory, and
    pcap:= points to the input PCAP file name
    .

    Note 2: By default, the LiDAR points are published in the velodyne coordinate reference frame, i.e. the points are relative to the Velodyne sensor.
  2. Open up another Terminal. Startup RViz by entering the following commands.

    $ rosrun rviz rviz -f velodyne

    Note: the -f option tells RViz to use the velodyne coordinate transformation frame.

    The RViz application starts up.

  3. Click the Add button as shown above.

    The Choose visualization dialog box appears.

  4. Click By topic and choose the topic /velodyne_points/PointCloud2. click OK.

    The PCAP point cloud is displayed.


    Note: the HDL-32E sensor is mounted on top of the car so the road is below zero as shown below.


  5. To transform the PCAP data to the car (so called odom frame), we have to define the transformation parameters from the odom frame to the velodyne frame. For example, say the HDL-32E is 2 meters above the odom frame origin. Open another Terminal and type in the following command:

    $ rosrun tf static_transform_publisher 0 0 2 0 0 0 odom velodyne 100
    Note: 0 0 2 0 0 0 values are equal to dx, dy, dz, and yaw, pitch, roll (in rads),
    odom is the parent or from frame,
    velodyne is the child or to frame, and
    100 is the time interval for publishing the transformation in msec
    .
  6. In the Fixed frame field of the RViz application, change the value to odom.

    The point cloud is displayed in the odom frame as shown below.

Monday, May 20, 2019

Setup Ubuntu network settings to Velodyne VLP-16 for ROS

After installing ROS (Robotic Operating System) and the Velodyne drivers and utilities on Ubuntu 18.04 LTS, the computer needs to be able to communicate with the Velodyne VLP-16 sensor unit.

Note: this post is specific to the VLP-16 unit but should be applicable to the other units, with some address changes.

By default, the IP address of the VLP-16 is set to be 192.168.1.201 by the factory. The network interface for the computer should be set to be on the same network, i.e. 192.168.1.77 for example, with the network masks at 255.255.255.255 and the gateway to be 192.168.1.255.

  1. To set the Ubuntu network settings, click the network icon on the top left bar and choose Wired Settings in the pop down menu.


  2. In the Settings dialog box, click the gear icon as shown below.




    The Wired dialog box appears.

  3. Click the IPv4 tab. Toggle on the Manual IPv4 Method.


  4. Type in the following address values, as shown below.


  5. Scroll down. Type in the Routes (to the VLP-16) values as shown below.



  6. Click Apply. If all the values are correct, Ubuntu should show you a Network connected status. Otherwise, recheck the values and try again.
  7. Now to start ROS and run a sample Velodyne launch file, the following commands can be executed in separate terminals.


    $ roscore

    $ roslaunch velodyne_pointcloud VLP16_points.launch

    The VLP-16 /velodyne_points topic is published



Monday, May 13, 2019

Setting up Visual Studio Code for working with ROS C++ on Ubuntu

In order to use Visual Studio Code on Ubuntu to develop ROS (Robotic Operating System) C++ packages, a couple of Visual Studio Code extensions can be helpful.

If they are not already installed, simply run Visual Studio Code and install the following extensions:
  • Microsoft C/C++ extension
  • ROS extension

Another thing that can be helpful is to configure the Microsoft C/C++ extension to include the ROS header include files. If the extension is unable to locate the ROS header files, the code editor will show wavy green lines under the header files, as shown in the screenshot below.

To get rid of these wavy lines, the C/C++ properties (c_cpp_properties.json) file need to be edited to add in the ROS header include directory, e.g. /opt/ros/melodic/include/.

The following screenshots show the c_cpp_properties.json file and the includePath configuration array.

Append the path "/opt/ros/melodic/include" to the includePath array variable as shown below.

Then save and restart Visual Studio Code. Observe that the green wavy lines are no longer displayed.

Monday, May 6, 2019

Export Velodyne PCAP to LAS files in VeloView

VeloView is an open source application for working with Velodyne's popular LiDAR sensors (HDL-64E, HDL-32E, VLP-16 etc.). It is available from this website https://www.paraview.org/veloview/.

VeloView has an export to LAS file format command but you would not know it as it is not available from the graphical user interface. To generate LAS files, it is necessary to use the Python command line interface. The steps below show how it can be done.
  1. Run VeloView and load a Velodyne PCAP file.

  2. Choose Tools | Python Console.

    The Python Console appears.
  3. To export out the current frame, type in the following command at the Python prompt.

    >>> vv.saveLASCurrentFrame('c:/path/to/output.las', 0)

    Note 1: the last argument indicates the coordinate transformation mode. 0 = raw, 1 = absolute, 2 = relative.
    Note 2: Replace c:/path/to/ with your actual file system path
  4. To export a range of frames by frame numbers, type in the following command at the Python prompt.

    >>> vv.saveLASFrames('c:/path/to/output.las', 10, 20, 0)

    Note: in this example, 10 is the starting frame, 20 is the ending frame, and 0 is the raw transformation mode.
  5. To export a list of frames by time steps, type in the following command at the Python prompt.

    >>> vv.saveLAS('c:/path/to/output/las', timesteps, 0)

    Note: timesteps is an array of integers specifying the time steps e.g. [0,1,2,...10] and 0 is the raw transformation mode.

    To get the available time steps in the PCAP file, type in the following command at the Python prompt.

    >>> timesteps = vv.getCurrentTimesteps()
  6. Optional. Display the resultant LAS file in a viewer.