はじめに
先日、
![画像](/assets/images/dev/feature/01/php_n-gauge/0001/thumb/TH800_000.jpg)
ガンダムに対抗するわけではないのですが、
こんなふうに動きます
まずは写真のような一直線のレイアウトを動かしてみましょう。普通のNゲージでは電源は1ヵ所だけ供給しますが、
![図1 直線レイアウト 図1 直線レイアウト](/assets/images/dev/feature/01/php_n-gauge/0001/thumb/TH800_001.jpg)
![画像](/assets/images/dev/feature/01/php_n-gauge/0001/thumb/TH800_001a.jpg)
そして、
#! /usr/local/bin/php
<?php
$url = "http://192.168.0.100/";
file_get_contents($url."?00=C");
file_get_contents($url."?01=D");
file_get_contents($url."?02=D");
file_get_contents($url."?03=C");
?>
往復運転させるには、
#! /usr/local/bin/php
<?php
$url = "http://192.168.0.100/";
for (;;) {
file_get_contents($url."?00=C");
file_get_contents($url."?01=D");
file_get_contents($url."?02=D");
file_get_contents($url."?03=C");
sleep(30);
file_get_contents($url."?00=A");
file_get_contents($url."?01=A");
file_get_contents($url."?02=A");
file_get_contents($url."?03=A");
file_get_contents($url."?04=c");
file_get_contents($url."?03=d");
file_get_contents($url."?02=d");
file_get_contents($url."?01=c");
sleep(30);
file_get_contents($url."?04=A");
file_get_contents($url."?03=A");
file_get_contents($url."?02=A");
file_get_contents($url."?01=A");
}
?>
見ての通り、
LANから制御する仕掛け
Nゲージの動く仕組みは、
今回のシステムでは、
![絶縁レール。矢印のところで分割されている。 絶縁レール。矢印のところで分割されている。](/assets/images/dev/feature/01/php_n-gauge/0001/thumb/TH800_002.jpg)
このようなシステムには、
![コントローラ コントローラ](/assets/images/dev/feature/01/php_n-gauge/0001/thumb/TH800_003.jpg)
コントローラはHTTPサーバになっており、http://
のリクエストを送ると、
コマンド | 機能 |
---|---|
A | 停止 |
B | 走行1/ |
C | 走行1/ |
D | 走行1/ |
E | 走行1/ |
F | 走行100% |
b | 逆走行1/ |
c | 逆走行1/ |
d | 逆走行1/ |
e | 逆走行1/ |
f | 逆走行100% |
P | ポイント分岐 |
p | ポイント直進 |
S | 車両検出 |
では、
PHPのプログラム
先ほどのプログラム
![図2 給電のようす 図2 給電のようす](/assets/images/dev/feature/01/php_n-gauge/0001/002ai.jpg)
走り終わるのを時間待ちして、
![図3 給電のようす(その2) 図3 給電のようす(その2)](/assets/images/dev/feature/01/php_n-gauge/0001/003a.jpg)
時間待ちでは信頼性が低いので、
while (file_get_contents("http://192.168.0.100/?00=S") + 0 == 0)
;
Sコマンドは、
#! /usr/local/bin/php
<?php
$url = "http://192.168.0.100/";
for (;;) {
file_get_contents($url."?00=C");
file_get_contents($url."?01=D");
file_get_contents($url."?02=D");
file_get_contents($url."?03=C");
while (file_get_contents($url."?04=S") + 0 == 0)
;
file_get_contents($url."?00=A");
file_get_contents($url."?01=A");
file_get_contents($url."?02=A");
file_get_contents($url."?03=A");
file_get_contents($url."?04=c");
file_get_contents($url."?03=d");
file_get_contents($url."?02=d");
file_get_contents($url."?01=c");
while (file_get_contents($url."?00=S") + 0 == 0)
;
file_get_contents($url."?04=A");
file_get_contents($url."?03=A");
file_get_contents($url."?02=A");
file_get_contents($url."?01=A");
}
?>
なお、
ポイントも動かそう
ポイントの制御も、
![図4 引き込み線のあるレイアウト 図4 引き込み線のあるレイアウト](/assets/images/dev/feature/01/php_n-gauge/0001/thumb/TH379_004.jpg)
![図4 引き込み線のあるレイアウト 図4 引き込み線のあるレイアウト](/assets/images/dev/feature/01/php_n-gauge/0001/004ai.jpg)
リスト4のプログラムでは、
#! /usr/local/bin/php
<?php
$url = "http://192.168.0.100/";
#
# +--------|---------+
# | |
# +---03---|<<<02<<<<p1
# <<<<<<<|<<<00<<<|
#
file_get_contents($url."?01=p");
file_get_contents($url."?00=D");
file_get_contents($url."?02=D");
while (file_get_contents($url."?03=S") + 0 == 0)
;
file_get_contents($url."?00=A");
file_get_contents($url."?02=A");
sleep(5); # wait in station
#
# +>>>>>>>>|---------+
# | |
# +<<<03<<<|---02---<p1
# -------|---00---|
#
file_get_contents($url."?01=P");
file_get_contents($url."?03=D");
sleep(5); # make sure of leaving rail 02
while (file_get_contents($url."?02=S") + 0 == 0)
;
file_get_contents($url."?03=A");
sleep(5); # wait in station
#
# +--------|>>>>>>>>>+
# | |
# +---03---|<<<02<<<<p1
# -------|---00---|
#
file_get_contents($url."?01=P");
file_get_contents($url."?02=D");
sleep(5); # make sure of leaving rail 03
while (file_get_contents($url."?03=S") + 0 == 0)
;
file_get_contents($url."?02=A");
sleep(5); # wait in station
#
# +>>>>>>>>|---------+
# | |
# +<<<03<<<|---02---<p1
# -------|---00---|
#
file_get_contents($url."?01=P");
file_get_contents($url."?03=D");
sleep(5); # make sure of leaving rail 02
while (file_get_contents($url."?02=S") + 0 == 0)
;
file_get_contents($url."?03=A");
sleep(5); # wait in station
#
# +--------|>>>>>>>>>+
# | |
# +---03---|<<<02<<<<p1
# -------|---00---|
#
file_get_contents($url."?01=P");
file_get_contents($url."?02=D");
sleep(5); # make sure of leaving rail 03
while (file_get_contents($url."?03=S") + 0 == 0)
;
file_get_contents($url."?02=A");
sleep(5); # wait in station
#
# +<<<<<<<<|---------+
# | |
# +>>>03>>>|>>>02>>><p1
# >>>>>>|---00---|
#
file_get_contents($url."?01=p");
file_get_contents($url."?03=d");
file_get_contents($url."?02=d");
while (file_get_contents($url."?00=S") + 0 == 0)
;
file_get_contents($url."?03=A");
file_get_contents($url."?02=A");
?>
リスト4の実行結果を動画で見てみましょう。
車両が進むまで待って、