2019年4月16日火曜日

Connect Raspberry Pi and ACGAM R1 using Bluetooth

Amazonで購入したJoystickでロボットを動かしたく、設定した記録


搬送ロボットの学習行動や、所定の場所まで移動させるにはスマフォ片手というのは危険であり、やはり簡単な操作機器でするのが良いと思うわけでして、、、

通常Raspberry PiにはDesktop入れないので、コマンドラインでBluetoothとの接続をする。まずは、そこまでの記録。

sudo apt-get install -y pi-bluetooth

これによりsystemd配下のサービスとなる。はず、、、
で、

systemctl status bluetooth

なんとエラー、、、

Failed to obtain handles for "Service Changed" characteristic
Sap driver initialization failed.
sap-server: Operation not permitted (1)
Endpoint registered: sender=:1.26 path=/A2DP/SBC/Source/1
Endpoint registered: sender=:1.26 path=/A2DP/SBC/Sink/1

cat /etc/dbus-1/system.d/bluetooth.conf

確認するとパーミッション設定必要。

sudo usermod -a -G bluetooth pi
sudo reboot

bluetoothctl

scan on
おおおおお、、、で

scan off
connect FF:FF:30:01:0B:2A
Connection successful
pair FF:FF:30:01:0B:2A
Paring successful
trust FF:FF:30:01:0B:2A
Trust successful

info

device FF:FF:30:01:0B:2A
        Name: ACGAM R1
        Alias: ACGAM R1
        Paired: no
        Trusted: no
        Blocked: no
        Connected: yes
        LegacyPairing: no
        UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
        UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
        UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)
        UUID: Battery Service           (0000180f-0000-1000-8000-00805f9b34fb)
        UUID: Human Interface Device    (00001812-0000-1000-8000-00805f9b34fb)
        Modalias: usb:v248Ap8266d3634

めでたしめでたし(^^)

sudo apt-get install joystick
sudo apt-get install jstest-gtk

jstest-gtk

おっと、この画面が現れて "Properties" を選択して、ぐにゅにゅ



因みに、このパッドは、4つのモードがあって
・シャッターモード(自撮りモード)
・マウスモード
・音楽モード
・ゲームモード
デフォルト(初期設定)は「音楽モード」。
左側にある"M"キーを押しながら"B"キーを押すとモードが変わるけど、
ようわからん。青いLEDは光るので反応はしているようだ
ロボットの制御をするので、中間値はいらないので、これで十分なはず。

今度は、アプリをつくります。

sudo apt-get install libbluetooth-dev

Peace!!



2019年4月12日金曜日

lighttpd を一発でBuildする

lighttpd を一発でbuild する

まあまあ、この作業が僕は多いので、こんな感じのbash
Raspberry Pi用です。
あと、変数VERは下記を確認して必要に応じて修正する。

https://www.lighttpd.net/

Peace!!

#!/bin/bash

HOME=/home/pi
VER=1.4.54

if [ -e $HOME/tmp ]; then
    cd $HOME/tmp
else
    mkdir $HOME/tmp
    cd $HOME/tmp
fi

wget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-$VER.tar.gz
tar xvf lighttpd-$VER.tar.gz
cd  lighttpd-$VER
sudo apt-get -y install libpcre3-dev bzip2 libbz2-dev
./configure
make
sudo make install
sudo ldconfig
sudo mkdir /var/log/lighttpd
sudo chmod 777 /var/log/lighttpd



cat ./doc/systemd/lighttpd.service | sed s{usr{usr/local{ > qq
sudo cp qq /etc/systemd/system/lighttpd.service
rm qq
sudo systemctl enable lighttpd
sudo systemctl start lighttpd

2019年4月4日木曜日

mariaDB これだけ知っていれば困らないかも、、、

これだけ知っていれば、困らない
僕は、、、


(1) 検索について

結局大事なのは検索で、メモとして追加
NULL 検索をするとKeyも無視するらしいので、NULLを使ってはならない。

棚番号にXXXXが入って無く、かつdataに20%の値が設定されている。
select tana,hinc from zaiko where tana != "XXXX" and date like '20%';

カラムをグレイヴ アクセントで囲み検索条件をシングルクォーテーション
select count(*) from zaiko
where
`tana` like '%CHC%'
and
`data` like '20%';

(2) Login

mariadb -uroot db -pxxxxx
mariadb -uroot -p -h ホスト名


(3) DB処理

select * from zaiko;
select * from zaiko where id = 10;
select * from zaiko where hinc like('% style="font-size: 10pt;"%');

insert into users (name) values ('かばかば');
insert into profiles (name) select users from users where users.id = 1;
update users set name='kayak', status=3 where id=1;
delete from wakuwaku where kimoti like "waku%" and fuku <> "hadaka";

・例)検索してREPLACEで置換
update ship set post_kayakp = replace(post_content, 'style="font-size: 12pt;"', '') where kayak like('% style="font-size: 12pt;"%');

(4) DB import export

・import
入力ファイルはテーブル名
mysqlimport -uthn -pxxxxux --fields-terminated-by="|" 
 --lines-terminated-by="\n" --local test /home/pi/tmp/t1.csv

test.t1: Records: 21  Deleted: 0  Skipped: 0  Warnings: 0

・export
出力先は/tmpにしないとエラーになる。
DB名test, DB内にはtable名t1のみ、結果は  /tmp/t1.txt  となっている。
sudo rm /tmp/t*; mysqldump -uroot -pxxxxx --tab=/tmp  
 --fields-terminated-by='|' test

・コマンドライン
load data local infile “./test.csv” into table t1
 fields terminated by “,” lines terminated by “\r\n”;

出力先は/tmpにしないとエラーになる。

select * fromt1 into outfile ‘/tmp/te.csv’ 
 fields terminated by ‘|’ lines terminated by ‘\n’;


(5) DB, Table 作成、管理

・DB作成
create database DB名称;

・DB削除
drop database DB名称;

・例)DB作成とテーブル作成
create database wm;
create table wm.zaiko(hinc CHAR(32),
 hin CHAR(35),
 kazu int(6),
 hink CHAR(3),
 kata CHAR(200),
 hin2 VARCHAR(300),
 tana CHAR(32),
 yearin char(4),
 monthin char(2),
 date char(14),
 koushin datetime,
 other VARCHAR(256)
);
Query OK, 0 rows affected (5.89 sec)

・例)テーブル作成
cretat table t1(id int, name varchar(128), atr varchar(128));
  `id` int(11) default null,
  `name` varchar(128) default null,
  `atr` varchar(128) default null,
  `txt1` varchar(128) default null,
  `txt2` varchar(128) default null
) engine=InnoDB default charset=utf8mb4;

・テーブル確認
describe t1; 

・プライマリーキーの追加
alter table テーブル名 add primary key (カラム名);

・カラム名と型を変更
alter table `posts` change column `txt` `caption` varchar(255);

カラムの名前だけを変更する場合でも、上記のように型指定が必要
not null制約を追加する場合です。カラム名の制約を追加

alter table `posts` change column `txt` `caption` varchar(255) not null;

・カラムの型を変える
alter table `comments` modify column `post_id` bigint not null;

・カラム追加
alter tableとADDを使用してカラムを追加。
after 指定でpasswordカラムの後ろに、kayak_idカラムが追加
先頭に追加する時は、first  を指定する。

alter table `users` add `kayak_id` int(11) default null;
alter table `users` add `kayak_id` int(11) default null after `password`;
alter table `users` add `kayak_id` int(11) default null first;
alter table t1 addtxt2 varchar(128);
alter table t2 addtxt1 varchar(128) after txt1;

・テーブル内のデータを削除する。データだけの削除。
truncate t1;

(6) Utility

status;
show databases;
show columns from zaiko;
select count(*) from zaiko;
select version();
show variables;
show variables like "char%";
show variables like 'slow%';

use mysql;
select * from user;


(7) ユーザ管理

・ユーザー確認
select user from mysql.user;

・ユーザ追加とパスワード設定
create user 'ユーザ名'@'localhost' identified by 'ここにパスワード';

create user 'kayak'@'localhost' identified by 'password';

・ユーザーのパスワードを変更
set password for kayak@localhost=password( 'kayakPassword' );

・ユーザーを削除
drop user ユーザー名@ホスト名;

・ユーザー権限を確認
show grants for wordpress@localhost;
show grants for 'kayak'@'%';

・権限を追加 
grant all privileges on DB名.テーブル名 to 'ユーザ名'@'localhost';
mariadb 10.1.37では、上記のDB名.テーブル名は、シングルコーテーションで
囲むとエラーになる。従って、下記のようにする。
grant all privileges on TESTDB.* to 'user'@'localhost';

grant select on `kayakdb`.`users` to 'kayak'@'%'
grant file on *.* to kayak@localhost;

まとめると、、、
・グローバルレベル.sql
全DBの全テーブルに対して、与える権限を与える
grant 与える権限 on *.* to user名;

・データベースレベル.sql
指定したDB名の全テーブルに与える権限を与える
grant 与える権限 on DB名.* to user名;

・テーブルレベル.sql
指定したDB名の指定したテーブル名に与える権限を与える
grant 与える権限 on DB名.テーブル名 to user名;

・変更を反映さえる
flush privileges;


(8)開発環境を作る

sudo apt-get install default-libmysqlclient-dev
gcc slct01.c -o slct01 -lmysqlclient -L/usr/lib/arm-linux-gnueabihf

libmariadbclient-dev は、必要無いのかしら、、、

 Peace!!

memo:Raspberry Piにmysqlをインストールする

インストールでの問題は、rootのパスワード設定に技が必要な事
将来は解りませんが、、、

sudo apt-get -y install mysql-server

190722  OS:2019-07-10-raspbian-buster-lite
マジかー、、、

$ sudo apt-get -y install mysql-server
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
パッケージ mysql-server は使用できませんが、別のパッケージから参照されます。
これは、パッケージが欠落しているか、廃止されたか、または別のソース
からのみ利用可能であることを意味します。
しかし、以下のパッケージが置き換えます:
  mariadb-server-10.0
E: パッケージ 'mysql-server' にはインストール候補がありません
で、、、
sudo apt-get -y install mariadb-server
OK
クライアントモジュールが必要な場合は、mariadb-client
僕は、サーバ側でCで開発しているので使わない。
その代わりに、これを使うのでインストール


sudo apt-get install default-libmysqlclient-dev

20210428 問題
以下のrootのパスワードを設定をしようとしたら、エラー
サーバのsocketに繋がらない。
いろいろ調べたけど、、、不明
一つ一つ確認作業、、、、、
/lib/systemd/systemを確認して、、、
systemctl status も確認して、、、

sudo systemctl enable mariadb
sudo systemctl start mariadb

結局、serviceとして、手作業で設定し実行する。
以前は、インストール時に自動的にやっていたのに、、、

rootパスワードの設定をします。
いろいろ聞いてきますが、return, パスワード設定設定して、後はYで進めます。

sudo /usr/bin/mysql_secure_installation
return password password y,y,,,,,

でてくるメッセージは、ちゃんと読まないとならないかもです。
この設定は、そのままでは有効になりません。
root は、デフォルトでpluginがunix_socketに設定されているので、
それを削除します。sudoで軌道していますが、、、

sudo mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.1.37-MariaDB-0+deb9u1 Raspbian 9.0

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select user,password,plugin from user;
ERROR 1046 (3D000): No database selected
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select user,password,plugin from user;
+------+-------------------------------------------+-------------+
| user | password                                  | plugin      |
+------+-------------------------------------------+-------------+
| root | *8637AD513CC7ED5F46D72CD7C3C587CFC9522613 | unix_socket |
+------+-------------------------------------------+-------------+
1 row in set (0.00 sec)

MariaDB [mysql]> update user set plugin='' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mysql]> select user,password,plugin from user;
+------+-------------------------------------------+--------+
| user | password                                  | plugin |
+------+-------------------------------------------+--------+
| root | *8637AD513CC7ED5F46D72CD7C3C587CFC9522613 |        
+------+-------------------------------------------+--------+
1 row in set (0.00 sec)

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

MariaDB [mysql]> exit
Bye

rootで、設定したパスワードでloginできるか確認します。

mysql -u root -p
mariadb -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.1.37-MariaDB-0+deb9u1 Raspbian 9.0

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

完了です。

Peace !!

2019年3月24日日曜日

----------------------------------------------------------------
pi-packages
sudo -A rp-prefapps
rp-prefapps
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
gps_dmm2xy 3424.0094559 13520.3414517 6

cu -s 115200 -l /dev/ttyUSB0

sudo  apt-get install gpsd gpsd-clients
sudo vi /etc/default/gpsd
sudo systemctl restart gpsd
systemctl status gpsd

sudo apt-get install libgps-dev
gcc -o q1 q1.c -lm -lgps

確認 cu -s 115200 -l /dev/ttyUSB0
cu -s 115200 -l /dev/ttyUSB0 << EOS
stty -a -F /dev/ttyUSB0 | grep speed

設定
sudo stty -F /dev/ttyUSB0 115200

----------------------------------------------------------------
$ hciconfig
hci0:   Type: Primary  Bus: UART
        BD Address: B8:27:EB:04:7C:5E  ACL MTU: 1021:8  SCO MTU: 64:1
        DOWN
        RX bytes:654 acl:0 sco:0 events:33 errors:0
        TX bytes:419 acl:0 sco:0 commands:33 errors:0

$ sudo hciconfig hci0 up

Can't init device hci0: Operation not possible due to RF-kill (132)

$ rfkill list
0: phy0: Wireless LAN
        Soft blocked: no
        Hard blocked: no
1: hci0: Bluetooth
        Soft blocked: yes
        Hard blocked: no

$ sudo rfkill unblock bluetooth

$ rfkill list
0: phy0: Wireless LAN
        Soft blocked: no
        Hard blocked: no
1: hci0: Bluetooth
        Soft blocked: no
        Hard blocked: no

 $ sudo hciconfig hci0 reset

$ sudo hciconfig hci0
hci0:   Type: Primary  Bus: UART
        BD Address: B8:27:EB:04:7C:5E  ACL MTU: 1021:8  SCO MTU: 64:1
        UP RUNNING
        RX bytes:2109 acl:0 sco:0 events:120 errors:0
        TX bytes:2011 acl:0 sco:0 commands:120 errors:0

$ sudo hciconfig hci0 up


$ echo N | sudo tee /sys/module/bluetooth/parameters/disable_ertm

$ sudo chmod 666 js*

$ ll
total 0
drwxr-xr-x  4 root root     240 Mar 24 13:06 .
drwxr-xr-x 15 root root    3400 Mar 24 13:06 ..
drwxr-xr-x  2 root root     100 Mar 24 12:29 by-id
drwxr-xr-x  2 root root     100 Mar 24 12:29 by-path
crw-rw----  1 root input 13, 64 Mar 24 12:29 event0
crw-rw----  1 root input 13, 65 Mar 24 12:29 event1
crw-rw----  1 root input 13, 66 Mar 24 13:06 event2
crw-rw-rw-  1 root input 13,  0 Mar 24 12:29 js0
crw-rw-rw-  1 root input 13,  1 Mar 24 13:06 js1
crw-rw----  1 root input 13, 63 Mar 24 12:29 mice
crw-rw----  1 root input 13, 32 Mar 24 12:29 mouse0
crw-rw----  1 root input 13, 33 Mar 24 13:06 mouse1

$ sudo apt-get install joystick
$ sudo apt-get install jstest-gtk

$ jstest-gtk


Opencv 3.4.4

$ cmake -D CMAKE_BUILD_TYPE=RELEASE     -D CMAKE_INSTALL_PREFIX=/usr/local     -D INSTALL_PYTHON_EXAMPLES=OFF     -D PLANTUML_JAR=/usr/share/java/plantuml.jar     -D ENABLE_NEON:BOOL=ON     -D WITH_TBB=ON -D BUILD_TBB=ON     -D WITH_QT=OFF -D WITH_OPENGL=OFF     -D BUILD_EXAMPLES=ON ..

2019年3月22日金曜日

memo Raspberry Piのpython3でpigpioを使う

そもそも、pigpioのインストールでしたが、、、
いろいろ自分用のメモ

sudo apt install -y python3-dev python3-pip
sudo pip3 install --upgrade pip3
sudo pip3 install pigpio

現在の大きな問題は、GPSとRaspberry Piの接続問題
下の内容をまとめると、以下をrc.localに仕込むのが良い

#!/bin/bash
sudo systemctl stop serial-getty@ttyS0.service
sudo chmod 666 /dev/ttyAMA0
sudo chmod 666  /dev/ttyS0
sudo chmod 666  /dev/ttyUSB0
stty -F /dev/serial0 115200
stty -F /dev/ttyAMA0 115200
stty -F /dev/ttyUSB0 115200
sudo chmod 666 /dev/ttyAMA0  /dev/ttyS0 /dev/ttyUSB0

1. 手順

1.1 新しいSDカードをつくる
起動用のSDカード作ります

1.2 raspi-configでserial設定
5 Interfaceing Options >  P6 Serial >
 Would you like a login shell to be accessible over  serial?    Yes & No
基本は、Noだと思うけど、YESで説明しているブログもある
 Would you like the serial port hardware to be enabled?
間違いなくYes

The serial login shell is disabled
The serial interface is enabled
 <Ok>

1.3 /boot/cmdline.txt

結局、次の文字列を削除する。これにより tty1を使う設定をする
console=serial0,115200

1.4 /boot/config.txt

enable_uart1      // raspi-config 設定値 1: serial enable 0: disable
dtoverlay=pi3-disable-bt         // bluetooth 無効

reboot後
/dev/ttyserial0, /dev/ttyAMA0 が、GPIO接続時の対象
/dev/ttyUSB0    これはシリアル通信用のUSB接続した時
                          例: TTL-232R-3V3

sudo systemctl stop serial-getty@ttyS0.service

1.5 chmod  再起動後は大事

sudo chmod 666 /dev/ttyAMA0 /dev/ttyS0  /dev/ttyUSB0

この3つは、crw-rw---- なので、
/etc/rc.local に記述した方が良いです。

lrwxrwxrwx  1 root root           7 Mar 23 13:53 serial0 -> ttyAMA0
lrwxrwxrwx  1 root root           5 Mar 23 13:53 serial1 -> ttyS0
crw-rw----  1 root dialout 204,  64 Mar 23 13:53 ttyAMA0
crw-rw----  1 root dialout   4,  64 Mar 23 13:53 ttyS0
/dev/ttyUSB0は、使用する時のみ

一番簡単な確認方法は、、、cat/dev/ttyAMA0

1.6 python3

python3の開発環境を作り、pip3をインストール
sudo apt install -y python3-dev python3-pip
???      sudo pip3 install --upgrade pip3

pip3 install pyserial
python の serial モジュールは手動でinstall してはいけない
もしinstallしてしまったら、、、 pip3 uninstall serial
sudo pip3 uninstall serial

下のスクリプトでテスト、、、でもうまくいかない
9600のGPSなら上手くいっている

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import serial
s = serial.Serial('/dev/ttyAMA0', 115200, timeout=1)
for i in range(10):
    print(s.readline())


#include <stdio.h>
int main(int argc, char *argv[]) {
        unsigned char   szBuf[256];
        int     i = 0;
        FILE *infl;
        if ((infl = fopen("/dev/ttyAMA0", "rb")) == NULL) {
                printf("file open error\n");
                return -1;
        }
        printf("success file open\n");
        while(fread(szBuf, sizeof( unsigned char ), 256, infl) != 0) {
            printf("%5d: %s.\n", i++, szBuf);
        }
        return 0;
}

1.7 通信ソフトでの確認

通信ソフトでどうなんだろう?
sudo apt -y install cu minicom

cu -s 115200 -l /dev/ttyAMA0
終わる時は、~.
ダメだー、、、、

 sudo systemctl stop serial-getty@ttyS0.service

ダメだ―――
あとは、、、、

cu -s 115200 -l /dev/serial0
Connected.
ァァァァmォw褄pVY}ァァァァァァァ・_q[euァァeァァeァァeォ褄キeYqァY}ァァァァォ褄キeYqァY}ァァァァァ
ァァァァmォs褄・e?Y}ァァァァァァァァァ」ァァeァァeァァォ褄キ_q[euァァeァァeァァeォ褄キeYqァY}ァァァァ
ァァァァmォu褄クVY}ァァァァァァァァァ」ァァeァァeァァォ褄キ_q[euァァeァァeァァeォ褄キeYqァY}ァァァァ
ァァァァmォw褄p+e?Y}ァァァァァァァァァ」ァァeァァeァァォ褄キ_q[euァァeァァeァァeォ褄キeYqァY}ァァァァ
ァァァァmォw褄pVY}ァァァァァァァァァ」ァァeァァeァァォ褄キ_q[euァァeァァeァァeォ褄キeYqァY}ァァァァ
ァァァァmォu褄・e?Y}ァァァァァァァ」ァァeァァeァァォ褄キ_q[euァァeァァeァァeォ褄キeYqァY}ァァァァ
ァァァァmォw褄・e?Y}ァァァァァァァ」ァァeァァeァァォs褄キ_q[euァァeァァeァァeォ褄キeYqァY}ァァァァ
ァァァァmォu褄クVY}ァァァァァァァァァァァ」ァァeァァeァァォ褄キ_q[euァァeァァeァァeォ褄キeYqァY}ァァァァァ

TTL-232R-3V3で、USB接続時

cu -s 115200 -l /dev/ttyUSB0
Connected.
[VォョZォ[VォョZォe;睆mヨ・[ョナ・・煜v澵レレレv煜v・咩揵ヨレZヨレvmュルVo・m?;+;+;+ュ驎V・eYqァァァァァァァmァァァァ[VォョZォe;睆mヨ・カヨv-カv-煜v煜vュ・・レv・v-v-v-vュ・カv-カv-v-煜[VォョZォe;郞vュ・レ[V[Vォョ[ョナ・ヨ6煜v澵レレレv煜v・咩揵ヨレZヨレvmュYVo・m?;+;+;+ュ驎V・eYqァ[VォョZォe;郞vュ・[V[Vォ[VォョZォe;郞v[ョナ・カ6煜v澵レレレv煜v・咩揵ヨレZヨレvmュルVo・m?;+;+;+ュ驎V・eYqァY}ァォョZォe;郞vュ・レ[Vォ[VォョZォe;郞[VォョZォe;郞v[ョナ
~.
Disconnected.
minicom -b 115200 -D /dev/ttyUSB0 -8




1.8 その他

stty -F /dev/ttyUSB0 115200
echo "Hello" > /dev/ttyUSB0

stty -a -F /dev/ttyUSB0 | grep speed
speed 115200 baud; rows 0; columns 0; line = 0;

minicom -s -c on
minicom -s           起動後パラメータ設定できる

cat /dev/ttyUSB0

Peace !!

参考
Linuxにおけるシリアルポートのトラブルシューティング
Raspberry Pi3のPythonでGPSを扱う
Pythonでシリアル通信
minicomの使い方
Raspberry Piに「みちびき」対応GPSモジュールを接続
Raspberry Pi Zero WH で「みちびき」対応 GPS を使う

------------------------------------------------------------------------------------

sudo blkid
/dev/mmcblk0p1: LABEL="boot" UUID="9304-D9FD" TYPE="vfat" PARTUUID="0527a029-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="29075e46-f0d4-44e2-a9e7-55ac02d6e6cc" TYPE="ext4" PARTUUID="0527a029-02"

/dev/mmcblk0: PTUUID="0527a029" PTTYPE="dos"

An installation of PICAN DUO2

CAN network制御を2系統実施する必要があり、、、、

中華製の数百円のMCP2515 モジュールを2つ使おうと試したけど
SPIまでは2系統利用できるようになるが、肝心のMCP2515モジュールが2つ認識せず、、、

多分、やり方あると思うけど、時間もかけられず
ここのモジュールを使う事にした。

http://skpang.co.uk/catalog/pican2-duo-canbus-board-for-raspberry-pi-23-p-1480.html

ドキュメントはこちらも参照
https://copperhilltech.com/pican2-duo-can-bus-board-for-raspberry-pi-2-3-with-smps/

Raspberry Piもこれ専用になってしまうので、Raspberry Pi Zero Wを使う


1. Raspberry Pi 設定


1.1最初の設定

/boot/config.txt には次の行を追加

dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=24
dtoverlay=spi-bcm2835

rebootをして確認
can0 と can1 があればOK

ls -al  /sys/class/net
total 0
drwxr-xr-x  2 root root 0 Mar 22 00:20 .
drwxr-xr-x 49 root root 0 Nov  4  2016 ..
lrwxrwxrwx  1 root root 0 Mar 21 21:16 can0 -> ../../devices/platform/soc/20204000.spi/spi_master/spi0/spi0.1/net/can0
lrwxrwxrwx  1 root root 0 Mar 21 21:16 can1 -> ../../devices/platform/soc/20204000.spi/spi_master/spi0/spi0.0/net/can1
lrwxrwxrwx  1 root root 0 Mar 21 21:16 lo -> ../../devices/virtual/net/lo
lrwxrwxrwx  1 root root 0 Mar 21 21:16 wlan0 -> ../../devices/platform/soc/20300000.mmc/mmc_host/mmc1/mmc1:0001/mmc1:0001:1/net/wlan0

dmesg      下記は表示される一部

[   16.752998] CAN device driver interface
[   16.805527] mcp251x spi0.1 can0: MCP2515 successfully initialized.
[   16.837173] mcp251x spi0.0 can1: MCP2515 successfully initialized.
[   18.013248] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready
[   18.013313] brcmfmac: power management disabled
[   19.588124] IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
[  309.214257] can: controller area network core (rev 20170425 abi 9)
[  309.214453] NET: Registered protocol family 29
[  309.243944] can: raw protocol (rev 20170425)

開発元のHPにはいろいろ書いてあるし、開発元のURLからソフトのdownloadとなっていたけど、自分で判断して

sudo apt-get install autoconf git libtool
git clone https://github.com/linux-can/can-utils.git
cd can-utils
./autogen.sh
./configure
make
sudo make install

これで設定は完了

1.2 テスト

テストは、モジュール内で確認する。(写真のまんま)

sudo /sbin/ip link set can0 up type can bitrate 500000 loopback on
sudo /sbin/ip link set can1 up type can bitrate 500000 loopback on
sudo ifconfig can0 up
sudo ifconfig can1 up

loopback onをつけているのは、モジュール内だけでチェックするため
実際に使うときは指定しない。

3つのターミナルを立ち上げて、確認。こんな感じ

candump でcan0 とcan1を監視して、cansend でそれぞれに送信する

以下の2枚の写真はサイトの説明から拝借
写真のようにループさせてテストする事もできる。
個の時は、ターミネータの120ΩをONにしている。

2 接続

2系統ののテスト。写真は全体像。
ブレッドボードで2つのバスを作っている。ターミネータには120Ωの抵抗が両端にある。

右端中にPiCAN DUO2を装着したRaspberry Pi。
右端下にRaspberry Pi Zero Wがあり、DUO2のCAN1とバスを共有
左端上にRaspberry Pi 3B+があり、DUO2のCAN0とバスを共有

3つのRaspberry Pi毎にCANの立上げを行い、DUO2以外の2つのRaspberry Piではcandumpで監視する。
DUO2からcansendでデータを送信する。



(1) PiCAN DUO2のCAN0とCAN1起動

sudo /sbin/ip link set can0 up type can bitrate 500000
sudo /sbin/ip link set can1 up type can bitrate 500000
sudo ifconfig can0 up     // 192.168.11.170 のRaspberry Pi
sudo ifconfig can1 up     // 192.168.11.162 のRaspberry Pi

(2) CAN0:CANB   Raspberry Pi  3B+ (192.168.11.170)

sudo /sbin/ip link set can0 up type can bitrate 500000
sudo ifconfig can0 up

(3) CAN1:CANA   Raspberry Pi Zero W (192.168.11.162)

sudo /sbin/ip link set can0 up type can bitrate 500000
sudo ifconfig can0 up

(4) テスト

こんな感じのテスト。
ただ、CAN0でcansendすると、2行になって表示される。原因不明。



今度は、DUO2以外からDUO2にcansend。これはOK

3.その他


3.1 ターミネータ

CANの仕様は両端に120Ωの抵抗をターミネータとするとなっている。
このボードは、120Ωの抵抗がreadyになっていて、ジャンパーで繋げば良いようになっている。

今回は、can0 をクローラのcanネットワークに接続し、またcan1は同時のCANネットワークとし、そちらにターミネータをつけるので、このモジュールの120Ωは使わない


プログラムはいろいろ考えないとならないけど、
繋がる事が確認できたので

Peace!!