LoginSignup
8
6

More than 5 years have passed since last update.

RaspberryPi + Cylon.js で サーボを動かす

Last updated at Posted at 2016-03-20

Raspberry Pi 上で Cylon.js を使ってサーボを動す

RaspberryPi 上で Cylon.js を使い サーボを動かす方法についてまとめます

【参考サイト】
RsspberryPi
Cylon.js

必要な物をいろいろインストールする

Cylon.js から PWM を使う際、pi-blaster を使用して一部のGPIOピンを PWM としてソフトウエア的に制御します。
ので、最初に pi-blaster をインストールしておきます。

autoconf が Install されていない場合あらかじめInstallしておきます。

sudo apt-get install autoconf

pi-blaster のコンパイル

git clone https://github.com/sarfata/pi-blaster.git
cd pi-blaster
./autogen.sh
./configure
make

sudo make install

pi-blaster を実行する

コマンド実行後 バックグラウンドで pi-blaster が実行されます
bash
sudo ./pi-blaster

Cylon.js を Install

npm を update しておく

Raspbian Default で、 nodejs v0.10.29, npm v1系 がInstallされている
念のため npm を Upgradeしておく(もしかしたら必要なっかも)
bash
sudo npm install -g npm@2

Cylon.js の Install

# Project 用フォルダを作る(名前は何でもいいです)
mkdir servo-control
cd servo-control

# package.json の生成
npm init

# Cylon.js Core Module を Install
npm install cylon cylon-firmata cylon-gpio cylon-i2c --save

# Cylon.js Raspberry PI 用モジュールをインストール
npm install cylon cylon-raspi --save

これで Install は完了

サンプルを動かしてみる

vi index.js

ソース

indexjs
"use strict";

var Cylon = require("cylon");

Cylon.robot({
  connections: {
    raspi: { adaptor: "raspi" }
  },

  devices: {
    servo: {
      driver: "servo",
      pin: 11,
      limits: { bottom: 20, top: 160 }
    }
  },

  work: function(my) {
    var angle = 30,
        increment = 5;

    every((1).seconds(), function() {
      angle += increment;
      my.servo.angle(angle);
      console.log("Current Angle: " + (my.servo.currentAngle()));

      if ((angle === 30) || (angle === 150)) { increment = -increment; }
    });
  }
}).start();

これで、#11 Pin にサーボのを繋いで、実行

node index.js

サーボが少しづつ回っていきます

pi-blaster で PWM 出来るピン番号について

いかが PWM として使えるはず

GPIO# Pin#
4 7
17 11
18 12
21 13
22 15
23 16
24 18
25 22

マニュアルによるとこの辺が使えるはずです

8
6
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
8
6