はじめに
先日、

ガンダムに対抗するわけではないのですが、
こんなふうに動きます
まずは写真のような一直線のレイアウトを動かしてみましょう。普通のNゲージでは電源は1ヵ所だけ供給しますが、


そして、
#! /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ゲージの動く仕組みは、
今回のシステムでは、

このようなシステムには、

コントローラは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のプログラム
先ほどのプログラム

走り終わるのを時間待ちして、

時間待ちでは信頼性が低いので、
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のプログラムでは、
#! /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の実行結果を動画で見てみましょう。
車両が進むまで待って、