Skip to content

rt-net/RaspberryPiMouse

Repository files navigation

RaspberryPiMouse

CI

This repository has the source code and kernel objects for the Raspberry Pi Mouse.

Installation

Run the following scripts.

以下のスクリプトを実行します

for Raspberry Pi OSUbuntu

以下のコマンドで本リポジトリをダウンロードし、Raspberry Pi本体の設定を行います。

$ git clone https://github.com/rt-net/RaspberryPiMouse.git
$ cd RaspberryPiMouse/utils
$ ./set_configs.bash

Raspberry Piを再起動し、以下のコマンドを実行してビルドに必要なファイルをインストールします。UbuntuRaspberry Pi OSでコマンドが違います。

# Ubuntu Serverの場合
$ sudo apt install linux-headers-$(uname -r) build-essential

# Raspberry Pi OSの場合
$ sudo apt install raspberrypi-kernel-headers build-essential

以下のコマンドでビルドを実行します。

$ cd RaspberryPiMouse/utils
$ ./build_install.bash

Manual installation

$ git clone https://github.com/rt-net/RaspberryPiMouse.git
$ cd RaspberryPiMouse/src/drivers
$ make
$ sudo insmod rtmouse.ko

Notes for the installation (ドライバの導入の際の注意)

特定の機能の有効化や、プログラムのビルドのために設定が必要な項目があります。以下の設定をご確認ください。

./utils/set_configs.bashを実行すると、自動で適切な設定に書き換わります。

for Raspberry Pi OS

Raspberry Pi OSでは、SPIとI2Cがデフォルトで無効となっているため、有効化する必要があります。

for SPI and I2C

Enable SPI and I2C functions via raspi-config command.

raspi-config コマンドで設定します。

  • SPI機能を「入」にする。
  • I2C機能を「入」にする。

for Raspberry Pi 4

Edit rtmouse.c to change the defined value RASPBERRYPI from2to4.

Raspberry Pi 4ではCPUのレジスタがそれまでのRaspberry Piとは異なります(issues#21)。 Raspberry Pi 4で本ドライバを使用する際にはrtmouse.cの以下の行(2020年4月13日現在の最新版のv2.1.0では54行目)をRASPBERRYPI 4に書き換えてビルドする必要があります。

./utils/build_install.bashを実行すると、Raspberry Piのモデルに合わせてrtmouse.c自動で書き換わります

// define the Raspberry Pi version here
// Raspberry Pi 1 B/A/B+/A+: 1
// Raspberry Pi 2 B        : 2
// Raspberry Pi 3 B/A+/B+  : 2
// Raspberry Pi 4 B        : 4
#define RASPBERRYPI 2

for 32-bit OS

Set 32bit-setting to /boot/firmware/config.txt.

32-bit版のOSではビルドするために、/boot/firmware/config.txtに以下の1行を追加する必要があります。

arm_64bit=0

デバイスツリーオーバーレイについて

kernel 5.16以降では/boot/firmware/config.txtに以下の設定を記述し、dtoverlayを設定する必要があります。※./utils/set_configs.bashを実行すると、設定は自動で書き換わります。

dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000

パルスカウンタについて

パルスカウンタは値の読み取りにI2Cを使用しています。仕様上は400kHzまでbaudrateを上げることができます(※1)。 I2Cのbaudrateを上げると通信に失敗する場合がある(issues#13)ので、基本的にはI2Cのbaudrateはデフォルト値(※2)から変更して62.5kHzに固定してください。

According to issues#13, it may be necessary to set the I2C baudrate lower than the default value. Add a following new line in /boot/firmware/config.txt to change the i2c_baudrate to 62.5 kHz.

/boot/firmware/config.txtに以下の1行を追加することでI2Cのbaudrateを62.5kHzに固定することができます。

dtparam=i2c_baudrate=62500

※1 Raspberry Pi 4 Model B(Ubuntu Server 18.04 / 20.04 / 22.04 / 24.04)を搭載して400kHzで通信できることを確認しています。 ※2 現在設定されているI2Cのbaudrateは以下のコマンドを実行することで確認できます。

$ printf "%d\n" 0x$(xxd -ps /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency)

Device files

For example code of device files, please refer to SampleProgram.

デバイスファイルの使用例はサンプルプログラムを参考にしてください。

Light sensor x4 (Input)

Read /dev/rtlightsensor0 to get proximity (0:far ~ 4095:close) of objects detected by light sensors.

/dev/rtlightsensor0を読み取り、光センサで検出された物体の近接度 (0:遠い ~ 4095:近い)を取得します。

# cat /dev/rtlightsensor0
# Return value: [front right] [right] [left] [front left]
$ cat /dev/rtlightsensor0
9 2 13 3

Switch x3 (Input)

Read /dev/rtswitch0 ~ /dev/rtswitch2 to get the switches on/off state.

/dev/rtswitch0 ~ /dev/rtswitch2 を読み取りスイッチのON/OFF状態を取得します。

# cat /dev/rtswitch[0,1]
# Return value: 1(Open), 0(Pressed)
$ cat /dev/rtswitch0

Buzzer (Output)

Write 0 ~ 20000 to /dev/rtbuzzer0 to beep the buzzer.

/dev/rtbuzzer0 に0 ~ 20000を書き込みブザーを鳴らします。

# echo 0 ~ 20000(Hz) > /dev/rtbuzzer0
$ echo 440 > /dev/rtbuzzer0
$ echo 0 > /dev/rtbuzzer0

LED x4 (Output)

Write 1/0 to /dev/rtled0 ~ /dev/rtled3 to turn on/off the LEDs.

/dev/rtled0 ~ /dev/rtled3 に1/0を書き込みLEDを点灯/消灯します。

# echo 0(OFF) or 1(ON) > /dev/rtled[0,1,2,3]
$ echo 1 > /dev/rtled0
$ echo 0 > /dev/rtled1

Motor enable (Output)

Write 1/0 to /dev/rtmotoren0 to enable/disable motors control.

/dev/rtmotoren0 に 1/0 を書き込みモータ操作を有効/無効にします。

# echo 0(disable) or 1(enable) > /dev/rtmotoren0
$ echo 1 > /dev/rtmotoren0

PWM frequency for left/right motor driver (Output)

Write 0 ~ ±10000 to /dev/rtmotor_raw_l0 or /dev/rtmotor_raw_r0 to set PWM frequency for motor drivers.

※ 0 ~ ±4 Hz will be reset to 0 Hz.

/dev/rtmotor_raw_l0 または /dev/rtmotor_raw_r0 に 0 ~ ±10000 を書き込み、モータドライバへのPWM周波数を設定します。

※ 0 ~ ±4 Hzは0Hzへリセットされます

# echo 0 ~ ±10000(Hz) > /dev/rtmotor_raw_[l0, r0]
$ echo 1 > /dev/rtmotoren0
$ echo 400 > /dev/rtmotor_raw_l0

PWM frequencies and drive duration (Output)

Write left and right PWM frequencies and drive duration to /dev/rtmotor0 to drive both motors.

/dev/rtmotor0に左右のPWM周波数と動作時間を書き込み、左右のモータを回します。

# echo [left_freq Hz] [right_freq Hz] [duration ms] > /dev/rtmotor0
$ echo 1 > /dev/rtmotoren0
$ echo 400 800 1000 > /dev/rtmotor0

Pulse counter x2 (Input/Output)

Read /dev/rtcounter_* to get pulse counts of PWM for motor drivers or write values to reset counts.

/dev/rtcounter_*を読み取りモータドライバへのPWMパルス数を取得します。また、値を書き込みカウントをリセットします。

  • unsigned counters : /dev/rtcounter_l0, /dev/rtcounter_r0
  • signed counters : /dev/rtcounter_l1, /dev/rtcounter_r1
# cat /dev/rtcounter_[l0, r0]
# Return value: 0 ~ 65565 (counts of PWM pulse)
# cat /dev/rtcounter_[l1, r1]
# Return value: -32767 ~ 32767 (counts of PWM pulse)
$ cat /dev/rtcounter_l0
1104
$ echo 0 > /dev/rtcounter_l0
$ cat /dev/rtcounter_l0
0

その他

その他のよくある質問についてはwikiにまとめています。

日経Linux連載

連載(Raspberry Piで始めるかんたんロボット製作)で上田氏が書いた シェルスクリプトは下記にあります。

https://github.com/ryuichiueda/RPiM

License

This repository except for SampleProgram directory is licensed under the GPL-2.0-only License, see LICENSE.

SampleProgram directory is licensed under the Apache 2.0 License, see SampleProgram / LICENSE.

このリポジトリはSampleProgramディレクトリを除きGPL-2.0-onlyライセンスで公開されています。詳細はLICENSEを確認してください。

SampleProgramディレクトリはApache-2.0ライセンスで公開されています。詳細はSampleProgram / LICENSEを確認してください。

Includings

This repository contains the code of the repository shown below.

このリポジトリは以下に示すリポジトリのコードを一部含みます。