Pieces for electronic kits

For Ubiquitous Computing

Raspberry Piにゲームパッドを繋ぐ

公にDebianに対応しているゲームパッドはない。(当然だ)
よって、pygamesをドライバとして使う。

pygamesのインストール

apt-get install python-pygame

pygame実行

#! /usr/bin/env python
# coding: utf-8
# coding=utf-8
# -*- coding: utf-8 -*-
# vim: fileencoding=utf-8
import pygame
from pygame.locals import *

pygame.joystick.init()
try:
    j = pygame.joystick.Joystick(0) # create a joystick instance
    j.init() # init instance
    print 'Joystickの名称: ' + j.get_name()
    print 'ボタン数 : ' + str(j.get_numbuttons())
except pygame.error:
    print 'Joystickが見つかりませんでした。'

def main():
    pygame.init()

    while 1:
        for e in pygame.event.get(): # イベントチェック
            if e.type == QUIT: # 終了が押された?
                return
            if (e.type == KEYDOWN and
                e.key  == K_ESCAPE): # ESCが押された?
                return
            # Joystick関連のイベントチェック
            if e.type == pygame.locals.JOYAXISMOTION: # 7
                x , y = j.get_axis(0), j.get_axis(1)
                print 'x and y : ' + str(x) +' , '+ str(y)
            elif e.type == pygame.locals.JOYBALLMOTION: # 8
                print 'ball motion'
            elif e.type == pygame.locals.JOYHATMOTION: # 9
                print 'hat motion'
            elif e.type == pygame.locals.JOYBUTTONDOWN: # 10
                print str(e.button)+'番目のボタンが押された'
            elif e.type == pygame.locals.JOYBUTTONUP: # 11
                print str(e.button)+'番目のボタンが離された'

if __name__ == '__main__': main()
# end of file

以上

参考

Joystickの入力を取得する
http://d.hatena.ne.jp/nakamura001/20101212/1292163993

Raspberry Piでラジコンを作る

用意するもの

 カメラ、モータ、サーボモータ制御用PC。

 カメラ。小さいは正義。HDで撮れる。

 ネットワーク接続のためのWi-Fiアダプタ。

 タイヤ、モータ、ステアリング。
 エネループ単4x2本(2.4V)で150分程度連続で走る。

 モータの電流を制御する。

 ステアリング駆動用サーボモータ。トルク1.5kgで十分動いた。

 Raspberry pi用バッテリー。安定動作のためモータとサーボ用電源とは別系統にする。

構造

バギー工作基本セットに付いてくる板ではRPiがギリギリ乗らないのでアクリル2mm板をレーザーカッターで切り出してボディーに。穴は適宜開ける。
f:id:nekosystem:20140530053723p:plain:w500
body.ai

サーボモータとカメラのマウンタを3Dプリンタで出力。
f:id:nekosystem:20140530054213p:plain:w500
output.stl

ステアリングバーに釘を縦にハンダ付けし、釘をサーボモータのロッドに通す。

適当に2mmネジ、3mmネジ、250mmスペーサー等を用いて組むとこうなる
f:id:nekosystem:20140531073346p:plain:w500

電気

電源
Raspberry pi:モバイルブースター
サーボ&モーター:eneloop単4x4
の二系統を用意した。

モーター
ドライバはTA7291Pを使った。

pin 名前 説明
1 GND RPi/ServoのGNDと共有
2 OUT1 モータの端子へ
3    
4 Vref 速度制御するなら適当なGPIOへ、今回は3.3VでPULL-UPした
5 IN1 GPIO14へ
6 IN2 GPIO15へ
7 Vcc 制御回路用電源電圧、4.5V以上必要、RPi 5Vへ
8 Vs モータ用電源電圧、乾電池x4、+4.8Vへ
9    
10 OUT モータの端子へ

サーボモータ
赤:Vs +4.8Vへ
茶:GND
黄:PWM制御ピン、GPIO4へ

こうなる。
f:id:nekosystem:20140531073256p:plain:w500

Raspberry Piの設定

・カメラで撮った動画をストリーミング再生、操作側で見る
http://neko-kousaku.hatenadiary.jp/entry/2014/05/26/183522

・サーボをコマンドで操作する
ServoBlasterをインストール

・モータをコマンドで操作する
WiringPiをインストール

完成

f:id:nekosystem:20140531073843p:plain:w500