Welcome

Welcome to my blog. Built with HTML in Pittsburgh, PA.

How to use import statement inside of an eval in a ROS roslaunch file

2021-02-05


Since ROS Kinetic (Ubuntu 16.04), roslaunch allows you to eval arbitrary Python expressions.


For example, you can do something like the below to manipulate parameters found in two different files.


<arg name="parameter" default="$(eval float(open('config1').read().strip()) + float(open('config2').read().strip()))"/>

Something I've always wanted to do is to check for the existence of a file. However, since the os module has not been imported in roslaunch's eval environment, this seems impossible to do. Furthermore, roslaunch explicitly forbids double underscores, precluding the use of an expression import with __import__. However, it does so by searching for "__"! See line 345 at the roslaunch source, which references https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html


We can get around this by nesting evals and creating the __import__ double underscore method using string addition.


<arg name="configfile" default="$(eval 'alternate.txt' if eval('_' + '_import_' + '_(\'os\')').path.exists('alternate.txt') else 'main.txt')"/>

How to wire Pixracer Serial to TX2 J121 UART 2

If pin 1 is the GND pin for both the Molex Pico (plugging into J7) and the JST GH (plugging into a UART on the Pixracer), then pin 2 on J7 connects to pin 5 on the JST GH and pin 3 on the J7 connects to pin 4 on the JST GH.

To talk to this port from Linux, you may need to use /dev/ttyTHS1 or /dev/ttyTHS3.