LoginSignup
5
5

More than 5 years have passed since last update.

コマンドラインからArduinoの書き込みなどを行う (Arduino-Makefile を使う 編)

Last updated at Posted at 2016-02-26

はじめに

前回の記事同様、Arduinoを使うときに便利なArduino-Makefileについてご説明したいと思います。
前回の記事で、コマンドラインからArduinoを好き勝手することができるようになりました。
しかし、Emacs使ってコードを編集したいし、Emacs使うんなら、M-x compile で何とかしたい
なんて野望が、Emacs教の方には起こるはず。。。いや、起こるに違いありません
そこで、本記事では、そんなブラザー達のために、方法を紹介したいと思います。

インストール

arduino-mkaptを使ってインストールする。

$ sudo apt-get install arduino-mk

これでインストールは完了です。

シリアル通信のための必要条件

シリアル通信にはscreenコマンドを使います。

まだインストールしていない場合は、aptで手に入ります。

$ sudo apt-get install screen

基本的に、make monitorと入力すると、シリアル通信が始まります。
終了したいときには、Ctrl+a kで終了するかどうか聞かれるので、yを入力後、<RET>で終了します。

Emacsの設定

Arduino-Makefileを使った方法は、前回紹介した、inoを使う方法と違い、すべての手順をEmacs上で操作でき、コンパイルエラーの際に、原因箇所に飛びやすいという、大変素晴らしい機能を手に入れることができます。(布教中)
このような素晴らしい機能を利用するために、EmacsにArduino-modeを適用します。
Github : bookest/arduino-modeより、arduino-mode.elを、init.elのパスが通ったところにインストールしましょう。
init.elのパスって何?という方は、Youhei SASAKI’s official site : Emacsの基本設定をひと通り読むことをおすすめします。
いずれにせよ、arduino-mode.elをインストールしたら、*.inoファイルや*.pdeファイルをarduino-modeで開くために、以下をinit.elに追加しましょう。

add_arduino-mode.el
;; ----------------------------------------------------------------------------
;; arduino-mode
;; ----------------------------------------------------------------------------
; arduino-mode.elへのパス
(add-to-list 'load-path "arduino-mode.elのあるディレクトリのパス")
; arduino-modeのセット
(autoload 'arduino-mode "arduino-mode" "Arduino editing mode." t)
; 拡張子の関連付け
(setq auto-mode-alist (cons '("\\.\\(pde\\|ino\\)$" . arduino-mode) auto-mode-alist))
;; ----------------------------------------------------------------------------

以上でEmacsの設定は完了です。

使い方

  1. .inoファイルの編集
  2. Makefileの編集
  3. コンパイル

    • コンパイルのみ
    $ make
    
    • 書き込み
    $ make upload
    
  • シリアル通信

    $ make monitor
    

Makefileについて

Arduino-Makefileでは、Makefileにコンパイル時に必要な内容を記述する必要があります。
以下の様な条件でArduinoを使いたいとき

Board Uno
Port /dev/ttyACM0
Baudrate 9600

Makefileに以下のように記述します。

Makefile
# Arduinoのボードの種類
BOARD_TAG           = uno

# シリアルポートの設定
MONITOR_PORT        = /dev/ttyACM0

# ボーレートの設定
MONITOR_BAUDRATE    = 9600

# 独自のライブラリの追加
ARDUINO_LIBS    += 

# Arduino.mkのインクルード
include /usr/share/arduino/Arduino.mk

これで望みの条件でコンパイルできるようになります。

尚、ARDUINO_LIBSは、ライブラリを追加するときに定義しますが、これについては、ArduinoをROS化する際に述べます。

実際の例(Blink)は私のGitHubに置いてますので、よかったらご覧下さい。
https://github.com/RyodoTanaka/Arduino

参考

https://github.com/sudar/Arduino-Makefile
http://www.emacswiki.org/emacs/ArduinoSupport
http://www.gfd-dennou.org/member/uwabami/cc-env/EmacsBasic.html

動作環境

PC Lenovo ThinkPad X240
Prosessor Intel Core i7-4600U (2.10GHz, 4MB, 1600MHz)
RAM PC3-12800 DDR3L (8GB)
OS Ubuntu 14.04 LTS 64bit
Kernel 3.13.0-44-generic
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5