View Source Use on Nerves

rclex can be operated onto Nerves. In this case, you do not need to prepare the ROS 2 environment on the host computer to build Nerves project (so awesome!).

This doc shows the steps on how to use Rclex on Nerves from scratch.

We have also published the Nerves project that has been prepared and includes example code at b5g-ex/rclex_on_nerves. Please also refer to this repository.

supported-targets

Supported Targets

Currently, we have confirmed the following boards as the Nerves device that can operate Rclex (good luck to get one!).

boardtagarchsupport for nerves_system
Raspberry Pi 4rpi4arm64v8Officially supported, recommended
BeagleBone Greenbbbarm32v7Officially supported
Kria KR260kr260arm64v8Third-party supported
ODYSSEY - STM32MP157Cstm32mp157c_odysseyarm32v7Third-party supported
F3RP70 (e-RT3 Plus)f3rp70arm32v7Third-party supported

preliminaries

Preliminaries

During the procedure for Rclex on Nerves, the docker command is used to copy the necessary directory in mix rclex.prep.ros2. Please install Docker Desktop or Docker Engine, and start it first.
And also, Rclex on Nerves will deploy an docker container for arm64 arch. If you want to operate this project by Docker Engine on other platforms (x86_64), you need to install qemu as the follows: sudo apt-get install qemu binfmt-support qemu-user-static

It should be noted that do not perform the following steps inside a docker container.
Once again, they can be operated even if ROS 2 is not installed on the host machine!

procedure

Procedure

Target device

The following steps assume that rpi4 and arm64v8 will be used as the target Nerves device. You may change the values of MIX_TARGET and --arch to match the "tag" and "arch" columns on the supported target list according to the board you want to use.

create-nerves-project

Create Nerves Project

mix nerves.new rclex_usage_on_nerves --target rpi4
cd rclex_usage_on_nerves
export MIX_TARGET=rpi4
mix deps.get

Note

If mix deps.get failed, you may need to create SSH key and configure config/target.exs.

install-rclex

Install rclex

rclex is available in Hex.

You can install this package into your project by adding rclex to your list of dependencies in mix.exs:

  defp deps do
    [
      ...
      {:rclex, "~> 0.8.3"},
      ...
    ]
  end

After that, execute mix deps.get into the project repository.

mix deps.get

prepare-ros-2-resoures

Prepare ROS 2 resoures

The following command extracts the ROS 2 Docker image and copies resources required for Rclex to the Nerves file system. You may change the value of --arch according to the architecture of your target board (see the "arch" column on the supported target list)

export ROS_DISTRO=foxy
mix rclex.prep.ros2 --arch arm64v8

Note

The following warning messages will occur at several times when the host and target architectures are different. These can be ignored.

WARNING: The requested image's platform (linux/arm/v7) does not match the detected host platform (linux/amd64) and no specific platform was requested

configure-ros-2-message-types-you-want-to-use

Configure ROS 2 message types you want to use

Rclex provides pub/sub based topic communication using the message type defined in ROS 2. Please refer here for more details about message types in ROS 2.

The message types you want to use in your project can be specified in ros2_message_types in config/config.exs. Multiple message types can be specified separated by comma ,.

The following config/config.exs example wants to use String type.

config :rclex, ros2_message_types: ["std_msgs/msg/String"]

Then, execute the following Mix task to generate required definitions and files for message types.

mix rclex.gen.msgs

If you want to change the message types in config, do mix rclex.gen.msgs again.

copy-erlinit-config-to-rootfs_overlay-etc-and-add-ld_library_path

Copy erlinit.config to rootfs_overlay/etc and add LD_LIBRARY_PATH

Copy erlinit.config from nerves_system_***.

cp deps/nerves_system_rpi4/rootfs_overlay/etc/erlinit.config rootfs_overlay/etc

Add -e LD_LIBRARY_PATH=/opt/ros/foxy/lib line like following.
ROS_DISTRO is needed to be written directly, following is the case of foxy.

# Enable UTF-8 filename handling in Erlang and custom inet configuration
-e LANG=en_US.UTF-8;LANGUAGE=en;ERL_INETRC=/etc/erl_inetrc;ERL_CRASH_DUMP=/root/crash.dump
-e LD_LIBRARY_PATH=/opt/ros/foxy/lib

Why add LD_LIBRARY_PATH explicitly

ROS 2 needs the path. If you want to know the details, please read followings

write-rclex-code

Write Rclex code

Now, you can acquire the environment for Rclex API! Of course, you can execute APIs on IEx directly.

Here is the simplest implementation example lib/rclex_usage_on_nerves.ex that will publish the string to /chatter topic.

defmodule RclexUsageOnNerves do
  def publish_message do
    context = Rclex.rclexinit()
    {:ok, node} = Rclex.ResourceServer.create_node(context, 'talker')
    {:ok, publisher} = Rclex.Node.create_publisher(node, 'StdMsgs.Msg.String', 'chatter')

    msg = Rclex.Msg.initialize('StdMsgs.Msg.String')
    data = "Hello World from Rclex!"
    msg_struct = %Rclex.StdMsgs.Msg.String{data: String.to_charlist(data)}
    Rclex.Msg.set(msg, msg_struct, 'StdMsgs.Msg.String')

    # This sleep is essential for now, see Issue #212
    Process.sleep(100)

    IO.puts("Rclex: Publishing: #{data}")
    Rclex.Publisher.publish([publisher], [msg])

    Rclex.Node.finish_job(publisher)
    Rclex.ResourceServer.finish_node(node)
    Rclex.shutdown(context)
  end
end

Please also check the examples for Rclex.

create-fw-and-burn-or-upload

Create fw, and burn (or, upload)

mix firmware
mix burn # or, mix upload

execute

Execute

Connect the Nerves device via ssh.

ssh nerves.local

Operate the following command on IEx.

iex()> RclexUsageOnNerves.publish_message
Rclex: Publishing: Hello World from Rclex!
{:ok, #Reference<0.2970499651.1284374532.3555>}

You can confirm the above operation by subscribing with ros2 topic echo on the machine where ROS 2 env has been installed.

$ source /opt/ros/foxy/setup.bash
$ ros2 topic echo /chatter std_msgs/msg/String
data: Hello World from Rclex!
---