knorth55's memo

Technical memo by @knorth55

Python2.7でUnicode文字をjoin

Python2.7でUnicode文字をjoinすると不思議なことになる

>>> a = u'\U0001f607'
>>> a
u'\U0001f607'
>>> print(a)
😇
>>> a = a.join(u'\U0001f607')
>>> a
u'\ud83d\U0001f607\ude07'
>>> print(a)
???😇???

Unicode文字をつなげる際は

>>> a = u'\U0001f607'
>>> a
u'\U0001f607'
>>> print(a)
😇
>>> a = a + a
>>> a
u'\U0001f607\U0001f607'
>>> print(a)
😇😇

原因は探っていませんが見ていて面白いです

OSXのTerminalが起動はするがすぐ落ちる

OSXのTerminalを起動すると、起動はするがすぐに落ちて消えてしまう
一旦デフォルトのログインシェルに戻した上でbashを実行すると以下のエラーが返ってきた。

dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
  Referenced from: /usr/local/bin/bash
  Reason: image not found
Trace/BPT trap: 5

原因はよくわからないがbrewでbashをupgradeすれば解決する

brew upgrade bash


github.com

C++でuint8配列に32FC1などのfloat配列を格納する

Depth Imageは32FC1などのfloat型(32bit, 4byte)の配列である場合があります。
しかしROSのsensor_msgs/Imageのdataはuint8_t型(8bit, 1byte)の配列であり直接追加することはできません。

ここでやるべきことはfloat型を4つのuin8_tに分割する変換です。
floatの配列からuint8_t型の配列への変換はmemcopyを使うことでできますが、pixel単位での変換ではありません。
pixel単位で行う場合は以下のように行います。

  float depth_pixel;
  uint8_t *depth_pixel_array = reinterpret_cast<uint8_t *>(&depth_pixel);

これをsensor_msgs/Imageのdataに順に格納していけばOKです。


具体的な32FC1の変換は以下のGistです。

PyGraphvizのインストールで "No package 'libcgraph' found" (OS X)

pipでpygraphvizを入れようとすると下記のようなエラーが出た。

$ sudo pip install pygraphviz
Collecting pygraphviz
  Downloading pygraphviz-1.3.1.tar.gz (103kB)
    100% |████████████████████████████████| 112kB 4.8MB/s 
Installing collected packages: pygraphviz
  Running setup.py install for pygraphviz ... error
    Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-x1jnXT/pygraphviz/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-2c5L5E-record/install-record.txt --single-version-externally-managed --compile:
    running install
    Trying pkg-config
    Package libcgraph was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libcgraph.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libcgraph' found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/tmp/pip-build-x1jnXT/pygraphviz/setup.py", line 87, in <module>
        tests_require=['nose>=0.10.1', 'doctest-ignore-unicode>=0.1.0',],
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 151, in setup
        dist.run_commands()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
        self.run_command(cmd)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
        cmd_obj.run()
      File "setup_commands.py", line 44, in modified_run
        self.include_path, self.library_path = get_graphviz_dirs()
      File "setup_extra.py", line 121, in get_graphviz_dirs
        include_dirs, library_dirs = _pkg_config()
      File "setup_extra.py", line 44, in _pkg_config
        output = S.check_output(['pkg-config', '--libs-only-L', 'libcgraph'])
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 573, in check_output
        raise CalledProcessError(retcode, cmd, output=output)
    subprocess.CalledProcessError: Command '['pkg-config', '--libs-only-L', 'libcgraph']' returned non-zero exit status 1

問題はlibcgraphがない、つまりgraphvizを入れ忘れていることです。(入れていると思っていた)
graphvizを入れることで解決です。

brew install graphviz
sudo pip install pygraphviz

rosjava + gradleでROSプログラムをscalaで書く

rosjava + gradle でscalaプログラムを書きました。
ROSのパッケージとしてgithubにあげているのでよければどうぞ。
基本的にはcatkin_create_rosjava_pkgとcatkin_create_rosjava_projectによって生成された設定ファイルを変更しているだけです。
rosjavaをlaunchしやすいようにbuild.gradleやCMakeList.txtも変更して、catkin buildをするだけで大丈夫になってます。
とりあえずPublisherとSubscriberしかサンプルプログラムはおいてません。

github.com

参考サイト
rosjava_build_tools/Tutorials/indigo - ROS Wiki
rosjava_core/graveyard/rosscala_project_setup - ROS Wiki
RosJavaを入れた記録 - Qiita
Can I roslaunch a rosjava node? - ROS Answers: Open Source Q&A Forum

Pythonの引数が参照渡しである影響

Python 2.7でちょっとつまってしまったので検証してみたら、関数の引数は以下のような例で変更されるらしい。
Pythonは参照渡しなので可変オブジェクトであるリストに関してはこういった破壊的(?)な処理が行われます。

参考サイト
Pythonの引数は全て参照渡しの件について - k_kinukawa's diary

$python 
>>> list = [1,2,3,4]
>>> def f(list, x):
...     list[x] = 0
...     return list
... 
>>> list_ = f(list,1)
>>> print list
[1, 0, 3, 4]