计算机‎ > ‎软件‎ > ‎Linux‎ > ‎

在 raspbian 上搭建 arduino 开发环境

发布者:guo rue,发布时间:2016年3月5日 21:32   [ 更新时间:2016年3月5日 22:15 ]
硬件确认
Raspberry Pi 2B
Arduino UNO

定位串口
先不接 Arduino 输入
ls /dev/tty*
连接 Arduino 再次输入
ls /dev/tty*
对比两次的输出结果,判断 Arduino 串口
看到开头
/dev/ttyUSB
或者
/dev/ttyACM
的就是证明已经发现 Arduino 设备

搭建 arduino 开发环境的两种方法
一种是安装 arduino 的 IDE (集成开发环境)。
还有一种是安装 ino ,一个命令行下的开发工具链,包括了工程目录的建立,源代码的编译,目标文件的上传,经过简单的使用之后,感觉非常好用。我个人比较推荐在树莓派上使用。

由于 ino 只是一个构建工具, 编译时依赖于 arduino 的库。所以先
安装 arduino IDE (集成开发环境)
sudo apt-get update
sudo apt-get install arduino
sudo apt-get install arduino-mk
当 Arduino IDE 运行时,它会轮询所有的 USB 设备。在这之前确保 pi 用户拥有足够的权限去操作串口设备,可以吧 pi 用户加到 tty 和 dialout 用户组来满足这一点。所以在 Arduino IDE 启动之前就要执行下面的操作
sudo usermod -a -G tty pi
sudo usermod -a -G dialout pi
安装好之后就可以用 IDE 编写代码编译上传到 arduino 了。

继续 安装 ino
sudo apt-get install build-essential
sudo apt-get install python-dev
sudo apt-get install python-setuptools
sudo apt-get install python-serial python3-serial
sudo apt-get install python-pip
sudo pip install upgrade pip
sudo pip install ino

如果以上命令不能自动安装 ino 那么只能手动下载编译了。

这里是 ino 的文档。
http://inotool.org/

前往下载页面
https://pypi.python.org/pypi/ino/#downloads
或者
git clone git://github.com/amperka/ino.git
然后
cd ino
chmod +x setup.py
./setup.py build
sudo ./setup.py install
这个 install 命令过程还会下载很多东西,然后我这里可能因为 GFW 的祸害,多次中断,我只好又重复执行了多次之后,不也不知道是不是安装好了。无妄之灾简直让人倍感挫折又浪费时间。

安装好之后看看 ino 都有什么功能:
ino --help
这是说明
usage: ino [-h] {build,clean,init,list-models,preproc,serial,upload} ...

Ino is a command-line toolkit for working with Arduino hardware.

It is intended to replace Arduino IDE UI for those who prefer to work in
terminal or want to integrate Arduino development in a 3rd party IDE.

Ino can build sketches, libraries, upload firmwares, establish
serial-communication. For this it is split in a bunch of subcommands, like git
or mercurial do. The full list is provided below. You may run any of them with
--help to get further help. E.g.:

    ino build --help

positional arguments:
  {build,clean,init,list-models,preproc,serial,upload}
    build               Build firmware from the current directory project
    clean               Remove intermediate compilation files completely
    init                Setup a new project in the current directory
    list-models         List supported Arduino board models
    preproc             Transform a sketch file into valid C++ source
    serial              Open a serial monitor
    upload              Upload built firmware to the device

optional arguments:
  -h, --help            show this help message and exit
命令包含了 build, upload, serial monitor 等功能, 已经基本覆盖了 IDE 提供的所有功能。

进入当前用户主目录
cd

测试整个工程流程

创建一个叫 blink 的工程文件夹
mkdir -p blink
进入 blink 文件夹
cd blink

通过 ino init 创建一个 arudino 工程
参数 -t 可以
指定 blink 来创建一个演示工程。(一般用来演示)
或者指定 empty 来创建一个标准的空工程。(以后经常用到)
创建后可以看到工程的目录结构包含 src 和 lib 分别存放工程源码 ino 文件和将来要引入的库。
ino init -t blink

显示工程的文件树
tree
如果没有 tree 可以安装
sudo apt-get install tree

查看 ino 程序
cat src/sketch.ino
显示内容
#define LED_PIN 13

void setup()
{
    pinMode(LED_PIN, OUTPUT);
}

void loop()
{
    digitalWrite(LED_PIN, HIGH);
    delay(100);
    digitalWrite(LED_PIN, LOW);
    delay(900);
}
从 blink 模板创建的源码包含一个简单的闪烁 led 的示例. 程序将反复将 13 口( arduino 板子上 13 口接着一个 led )在高低电平间切换。

接下来编译刚才的程序
ino build
输出
Searching for Board description file (boards.txt) ... /usr/share/arduino/hardware/arduino/boards.txt
Searching for Arduino lib version file (version.txt) ... /usr/share/arduino/lib/version.txt
Detecting Arduino software version ...  1.0.0 (1.0)
Searching for Arduino core library ... /usr/share/arduino/hardware/arduino/cores/arduino
Searching for Arduino standard libraries ... /usr/share/arduino/libraries
Searching for Arduino variants directory ... /usr/share/arduino/hardware/arduino/variants
Searching for make ... /usr/bin/make
Searching for avr-gcc ... /usr/bin/avr-gcc
Searching for avr-g++ ... /usr/bin/avr-g++
Searching for avr-ar ... /usr/bin/avr-ar
Searching for avr-objcopy ... /usr/bin/avr-objcopy
src/sketch.ino
Searching for Arduino lib version file (version.txt) ... /usr/share/arduino/lib/version.txt
Detecting Arduino software version ...  1.0.0 (1.0)
Scanning dependencies of src
Scanning dependencies of arduino
src/sketch.cpp
arduino/WInterrupts.c
arduino/wiring_digital.c
arduino/wiring_pulse.c
arduino/wiring.c
arduino/wiring_shift.c
arduino/wiring_analog.c
arduino/new.cpp
arduino/IPAddress.cpp
arduino/HardwareSerial.cpp
arduino/Print.cpp
arduino/Stream.cpp
arduino/HID.cpp
arduino/WMath.cpp
arduino/WString.cpp
arduino/Tone.cpp
arduino/main.cpp
arduino/USBCore.cpp
arduino/CDC.cpp
Linking libarduino.a
Linking firmware.elf
Converting to firmware.hex

上传到 arduino 上执行
ino upload
输出
Searching for stty ... /bin/stty
Searching for avrdude ... /usr/share/arduino/hardware/tools/avrdude
Searching for avrdude.conf ... /usr/share/arduino/hardware/tools/avrdude.conf
Guessing serial port ... /dev/ttyACM0
avrdude: stk500_recv(): programmer is not responding

avrdude done.  Thank you.

参考
https://github.com/arduino/Arduino
http://playground.arduino.cc/Linux/Raspbian
http://inotool.org/#installation
http://inotool.org/quickstart
http://coney.github.io/2013/11/compile-and-upload-adruino-source-form-cli/
http://bbs.ecovacs.cn/thread-89737-1-1.html