ロボクメモ Robojimamemo

ロボットを研究する、ある学生のメモ。日々、勉強したことをメモ代わりに残して共有します(ROS,C++,python,linux,etc...)

ubuntu16.04でSMACH ロボットの状態遷移を可視化

ロボットの状態遷移ツール

ロボットが複数のタスクをしたり、複数人でロボットを開発するときにはこのような状態遷移の可視化ツールがあると便利。

http://wiki.ros.org/ja/smach

http://wiki.ros.org/smach_viewer

1 . 依存パッケージのインストール(そのままだとpython関係でエラーが出る)  

$sudo add-apt-repository ppa:nilarimogard/webupd8    
$sudo apt-get install python-wxgtk2.8

2 . git clone   

$cd ~/catkin_ws/src/    
$git clone https://github.com/ros-visualization/executive_smach_visualization.git
$git clone https://github.com/ros/executive_smach.git

3 . サンプルコード  

#!/usr/bin/env python

import rospy
import smach
import smach_ros

# define state Foo
class Foo(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['outcome1','outcome2'])
        self.counter = 0

    def execute(self, userdata):
        rospy.loginfo('Executing state FOO')
        rospy.sleep(1)
        if self.counter < 3:
            self.counter += 1
            return 'outcome1'
        else:
            return 'outcome2'


# define state Bar
class Bar(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['outcome2'])

    def execute(self, userdata):
        rospy.loginfo('Executing state BAR')
        rospy.sleep(1)
        return 'outcome2'
        



# main
def main():
    rospy.init_node('smach_example_state_machine')

    # Create a SMACH state machine
    sm = smach.StateMachine(outcomes=['outcome4', 'outcome5'])

    # Open the container
    with sm:
        # Add states to the container
        smach.StateMachine.add('FOO', Foo(), 
                               transitions={'outcome1':'BAR', 
                                            'outcome2':'outcome4'})
        smach.StateMachine.add('BAR', Bar(), 
                               transitions={'outcome2':'FOO'})

    # Create and start the introspection server
    sis = smach_ros.IntrospectionServer('server_name', sm, '/SM_ROOT')
    sis.start()

    # Execute SMACH plan
    outcome = sm.execute()
    rospy.spin()


if __name__ == '__main__':
    main()

何をしているのかの詳細はここがわかりやすい。

smach/Tutorials/Getting Started - ROS Wiki

4 . viewer の起動  

rosrun smach_viewer smach_viewer.py 状態遷移の状態確認

f:id:shun0612:20181029203322p:plain

linux 使用メモリをros topic としてpublish

使用メモリをros topic としてpublish

グラフでメモリ使用量がどうなっているか評価するときにbagの中にメモリの使用量も入れておこう考えた。

メモ

#!/usr/bin/env python
import rospy
import sys
import os
import commands

from std_msgs.msg import MultiArrayLayout
from std_msgs.msg import MultiArrayDimension
from geometry_msgs.msg import PoseStamped


if __name__ == '__main__':

    args = sys.argv

    pub = rospy.Publisher('memory', PoseStamped, queue_size=10)
    rospy.init_node('system_monitor', anonymous=True)
    r = rospy.Rate(10) 


    while not rospy.is_shutdown():
        msg = PoseStamped()
        msg.header.frame_id = "memory_swap"
        msg.header.stamp = rospy.Time.now()
        memory = commands.getoutput('free -m --si | grep -e Mem -e Swap | while read a b c d e f g; do echo -n "$b $c "; done | tr -d GB').split()
        #msg.pose.orientation.x,msg.pose.orientation.y,msg.pose.orientation.z, msg.pose.orientation.w = commands.getoutput("free -m --si | grep -e Mem -e Swap | while read a b c d e f g; do echo $b $c; done | tr -d GB")
        print memory
        msg.pose.orientation.x = float(memory[0])
        msg.pose.orientation.y = float(memory[1])
        msg.pose.orientation.z = float(memory[2])
        msg.pose.orientation.w = float(memory[3])

        pub.publish(msg)
        r.sleep()

だいぶ無理がある。

linux , bibtexで論文の管理を簡単にしたい

論文を書くときにリファレンスに時間をかけない。

論文を書いている時に、リファレンスは学会や論文の種類によってフォーマットが全然違う。毎回気を使いながら少しずつ直すのは手間である。 例えば in proceedings のところはイタリックだったり違ったり。。。 そんな時bibtexというものを知った。厳密には知っていたが、ネットなどを見ても使い方がうまく調べられず。疎遠になってしまっていた。 最低限使えるようになったので備忘録。

環境
ubuntu16.04
linux

bibtexの概要

基本的に論文の情報を一つのファイルにまとめて書いてあって、それを自由に引き出したりしている。
- hoge.bst 設定ファイル(各会議や雑誌が用意しているbibtexの設定ファイル
- huga.bib 自分が調べた論文
基本的には2つのファイルがあればできる。

  1. bibtexが使える環境準備
    基本的にはlatexmkでpdfを作れているのであれば、問題がないみたい.
    memo-methods-in-researching.hatenablog.com

  2. bst ファイルを手に入れる
    基本的には会議などのテンプレートを手に入れた時についてくる。 どんなファイルを用いているのかきちんと確認する。

  3. bib ファイルを作成する.
    ここには自分の読んだ論文の情報をbibtex方式で書く。 大体、google scholarでもIEEE exploreでもbibtex形式でcitation をダウンロードすることができる。 試しにここではreference.bibを作成

@article{toaru,
title={robot},
author={toaru gakusei and shun niijima},
journal={Journal of robot},
volume={22},
number={6},
pages={758-766},
year={2010}}
  • 自分のtexファイルでの書き方
toaru gakusei no bibouroku \cite{toaru}
\bibliographystyle{tADR}
\bibliography{reference}
  • 結果pdf f:id:shun0612:20181012233857p:plain

これは便利、論文の管理まで楽になる。

Linux で eps 編集

inkscape が使いやすそう

$sudo apt-get install inkscape

Times new romanフォントを使用する

Winsowsで使用しているフォントはデフォルトでは入っていない。

sudo apt-get install ttf-mscorefonts-installer

以上 使い心地はいずれ。。。

Ubntu16.04 でvim-rosを使用するときに必要なもの

vim でROSを開発している人はvim-rosがおすすめ

ROS用のおすすめVim設定&プラグイン - MyEnigma

vim-ros install error

vim-ros をubuntu16.04 で扱おうとするとpython3がデフォルトになっているのでうまく使えない。以下のコマンドで解決。

sudo apt-get update
sudo apt-get install vim-nox-py2

vimでlatexを編集してかっこ良く論文を書きたい

Vimlatexを編集してかっこ良く論文を書きたい

論文を書かなければいけない時期が必ずある。今まではWindowsのtexworksを用いて本文も書いていたが、普段の開発はLinuxのため行き来がかなり面倒に感じていた。そこでvim を用いて一つのターミナルでプログラムの実行と本文を書くことができたらいいと思いいろいろ調べて環境を整えてみた。

環境

 Linux ubuntu16.04

参考にしたサイト

 

qiita.com

qiita.com

設定

    1. latexmk を使用してtexファイルをpdfへ
      tex wikiに簡単にinstall する方法がのっている。

      Latexmk - TeX Wiki

      追記:
      $ sudo apt install latexmk
      $ sudo apt install texlive-lang-japanese
      $ sudo apt install mupdf

    2. latexmkの設定
      ~/.latexmkrcに設定を書くことでコマンドからのコンパイルの設定を決めることができる。
      #!/usr/bin/perl
      $latex = 'platex -kanji=utf-8 -synctex=1 %S';
      $dvipdf = 'dvipdfmx %S';
      $bibtex = 'pbibtex';
      $pdf_mode = 3; # use dvipdf
      $pdf_update_method = 2;
      $pdf_previewer = "start mupdf %O %S";
      $max_repeat       = 5;
      # Prevent latexmk from removing PDF after typeset.
      $pvc_view_file_via_temporary = 0;

      とりあえずここまでで.texを.pdfにできる
      $ latexmk main.tex

    3. vim plugin の準備

      github.com基本的にinstalationにしたがって行う

      $curl -fLo ~/.vim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

    4. .vimrcに設定を追加

      call plug#begin('~/.vim/plugged')

      Plug 'lervag/vimtex'

      call plug#end()

      "部分的にコンパイルする設定

      function! s:previewTex() range
      let l:tmp = @@
      silent normal gvy
      let l:selected = split(@@, "\n")
      let @@ = l:tmp

      " \"\\documentclass{tADR2e}",
      "" let l:template1 = ["\\documentclass[a4paper]{jsarticle}",
      " \"\\usepackage[dvipdfmx]{graphicx}",
      " \"\\usepackage{amsmath,amssymb,bm}",
      " \"\\pagestyle{empty}",
      let l:template1 = ["\\documentclass{tADR2e}",
      \"\\begin{document}"]
      let l:template2 = ["\\end{document}"]

      let l:output_file = "preview.tex"
      call writefile(extend(extend(l:template1, l:selected), template2), l:output_file)
      call system("latexmk preview.tex")
      call system("mupdf preview.pdf")
      endfunction
      autocmd FileType tex
      \ | vnoremap <buffer> <localleader>la :call <SID>previewTex()<CR>

使用方法

  1. プレビュー 
    \lvでプレビュー 画像ではmupdfを使用

    f:id:shun0612:20180930225036p:plain

  2. 自動コンパイル設定
    \llで自動コンパイルのON/OFFを設定
    これはevinceなどでpdfファイルを開いているとリロードが自動でされてかっこ良い
  3. 部分コンパイル
    vim のvisualモードで囲まれている時に\laで部分コンパイル
    やっていることは単純で,選択範囲をもとにpreview.texを作成して,それをコンパイル します.また,上の例ではvim-latexの関数を使って,ノーマルモード<localleader>laを実行した場合現在のセクションをプレビュー

    LaTeX-suite じゃない vim-latex を使ってvimによるLaTeX作成環境を整える - 睡分不足

    .vimrcで書いた関数のteplate1のところでtexに合わせたテンプレートを書く。
    自分はmupdfをviewerに設定している

    f:id:shun0612:20180930225635p:plain

 

以上