konchangakita

KPSを一番楽しんでいたブログ 会社の看板を背負いません 転載はご自由にどうぞ

【Xi IoTで映えるデモがしたい 第12回】BrickPiでLEGOを動かす

BrickPiを組み立て終わったので、お次はどんなもんなのかもうちょっと詳しくご紹介しましょう

RaspberryPi(ラズパイ)のGPIOピンに接続していますので、OS自体はラズパイへBrickPi専用OSをインストールします

今回は「2019.04.05_Raspbian_For_Robots_by_Dexter_Industries_Stretch」ていうのを使ってみます
ダウンロードはこちらから
Raspbian For Robots by Dexter Industries Activity


ダウンロードしたファイルを解凍してimgファイルを、ラズパイ用のマイクロSDカードにイメージコピーをします
あとはそのまま起動するだけ
初期パスワードは、

  • username: pi
  • password: robots1234

です

f:id:konchangakita:20191212214244p:plain
BrickPi用のOS

必要なライブラリやサンプルプログラムがまるっと入ってます
サンプルのプログラミング言語はScratchやC、Pythonなんかが入ってます
まぁどれもよくわからないんですが、ここから私のPython道がはじまりました

動作確認するために、センサーとモーターを接続してやります
LEGOマインドストームにはセンサーとモーターが付属していますので
これらと BrickPi を専用のケーブルでつなげることによって、プログラミング制御できるようになります

f:id:konchangakita:20191212135904p:plain
マインドストームのセンサーとモーター

BrickPi にはセンサー用のポートとモーター用のポートを別々にもっていますので、接続の際は気をつけましょう

f:id:konchangakita:20191212135658p:plain:w220f:id:konchangakita:20191212135513p:plain
黄色がセンサー青がモーター

S1に「タッチセンサー」、MAに「サーボモーター」を接続してテストを行ってみたいと思います

f:id:konchangakita:20191212221303p:plain:w300
タッチセンサーとモーターを接続

~/Dexter/BrickPi3/Software/Python/Examples配下にいろいろなサンプルコードが配置されています
あらかじめ用意されている「LEGO-Motors.py」という pythonサンプルコードでテストしてみましょう
初めて見たときはこれでもうサッパリサッパリって感じでした
よくよく見慣れてくると、やってることは至ってシンプルっすね

タッチセンサーを押してる間だけ、モーターが一定速度まで回転速度上がった後逆回転を繰り返します

#!/usr/bin/env python
#
# https://www.dexterindustries.com/BrickPi/
# https://github.com/DexterInd/BrickPi3
#
# Copyright (c) 2016 Dexter Industries
# Released under the MIT license (http://choosealicense.com/licenses/mit/).
# For more information, see https://github.com/DexterInd/BrickPi3/blob/master/LICENSE.md
#
# This code is an example for running all motors while a touch sensor connected to PORT_1 of the BrickPi3 is being pressed.
# 
# Hardware: Connect EV3 or NXT motor(s) to any of the BrickPi3 motor ports. Make sure that the BrickPi3 is running on a 9v power supply.
#
# Results:  When you run this program, the motor(s) speed will ramp up and down while the touch sensor is pressed. The position for each motor will be printed.

from __future__ import print_function # use python 3 syntax but make it compatible with python 2
from __future__ import division       #                           ''

import time     # import the time library for the sleep function
import brickpi3 # import the BrickPi3 drivers

BP = brickpi3.BrickPi3() # Create an instance of the BrickPi3 class. BP will be the BrickPi3 object.

BP.set_sensor_type(BP.PORT_1, BP.SENSOR_TYPE.TOUCH) # Configure for a touch sensor. If an EV3 touch sensor is connected, it will be configured for EV3 touch, otherwise it'll configured for NXT touch.

try:
    print("Press touch sensor on port 1 to run motors")
    value = 0
    while not value:
        try:
            value = BP.get_sensor(BP.PORT_1)
        except brickpi3.SensorError:
            pass
    
    speed = 0
    adder = 1
    while True:
        # BP.get_sensor retrieves a sensor value.
        # BP.PORT_1 specifies that we are looking for the value of sensor port 1.
        # BP.get_sensor returns the sensor value.
        try:
            value = BP.get_sensor(BP.PORT_1)
        except brickpi3.SensorError as error:
            print(error)
            value = 0
        
        if value:                             # if the touch sensor is pressed
            if speed <= -100 or speed >= 100: # if speed reached 100, start ramping down. If speed reached -100, start ramping up.
                adder = -adder
            speed += adder
        else:                                 # else the touch sensor is not pressed or not configured, so set the speed to 0
            speed = 0
            adder = 1
        
        # Set the motor speed for all four motors
        BP.set_motor_power(BP.PORT_A + BP.PORT_B + BP.PORT_C + BP.PORT_D, speed)
        
        try:
            # Each of the following BP.get_motor_encoder functions returns the encoder value (what we want to display).
            print("Encoder A: %6d  B: %6d  C: %6d  D: %6d" % (BP.get_motor_encoder(BP.PORT_A), BP.get_motor_encoder(BP.PORT_B), BP.get_motor_encoder(BP.PORT_C), BP.get_motor_encoder(BP.PORT_D)))
        except IOError as error:
            print(error)
        
        time.sleep(0.02)  # delay for 0.02 seconds (20ms) to reduce the Raspberry Pi CPU load.

except KeyboardInterrupt: # except the program gets interrupted by Ctrl+C on the keyboard.
    BP.reset_all()        # Unconfigure the sensors, disable the motors, and restore the LED to the control of the BrickPi3 firmware.


では、実行してみましょう

$ cd ~/Dexter/BrickPi3/Software/Python/Examples
$ python3 LEGO-Motors.py 


ハリボテだった LEGO に接続してみた