つじるの忘れんぼ鉄道

DCC自動運転で秋葉原にあった交通博物館のジオラマを目指すブログ

DSbasic 入門してみる(7)スイッチバック往復(機関車2両)

 

f:id:tsujiru:20180828231247j:plain

上の写真の通り、敷設した線路全体としては、スイッチバックをイメージした線路となっています。

以前に、DesktopStationソフトウェアのイベント処理では、1両をスイッチバックで自動往復させているので、今回は機関車2両を順番にスイッチバック往復させていきたいと思います。

前回の入門してみる(6)のコードをもとに作りましたが、大きな変更点は以下の通り。

  • スイッチバック往復の上り/下り方向を管理する変数「DEST」を追加。
    上り方向を1、下り方向を2 とした。

上記以外は、検知区間ごとにほぼこれまでのコピペで、DESTの値によってポイントの分岐/直進を制御する処理の追加程度です。

 コードは以下の通り。

screen(7)
s88start

print "START S88 Control -Shuttle switchback + 2xLoco-"
sleep 5000
pwr 1
acc 30,1
acc 31,1
acc 40,1
acc 41,1
sleep 5000

'動かす2両のアドレスをSlot0と1に設定する
SLOT0 = getslotaddr(0)
SLOT1 = getslotaddr(1)
print "SLOT 0 = " + SLOT0
print "SLOT 1 = " + SLOT1

'前提:2両の初期位置は、SLOT0を検知区間①、SLOT1を検知区間②にすること。
'S0Pos:SLOT0のPosition(在線する区間)を格納していく変数
S0Pos = 2
S1Pos = 1

'先発をSlot0に設定
NextLoco = 0

'スイッチバック往復時の上り(1)/下り(2)を管理
DEST = 1

'SLOT0を起動(ESU BR151)
print "BR151 booting..."
dir SLOT0,2
fnc SLOT0,0,1
sleep 5000
fnc SLOT0,1,1
sleep 2000
fnc SLOT0,3,1
sleep 2000
fnc SLOT0,4,1
sleep 2000
fnc SLOT0,9,1
sleep 2000
fnx SLOT0,2
print "BR151 boot completed"
sleep 2000
'SLOT0を発進(ESU BR151)
spd SLOT0,200

sleep 10000

'SLOT1を起動(NGC DD51)
print "DD51 booting..."
dir SLOT1,2
fnc SLOT1,0,1
fnc SLOT1,1,1
print "DD51 boot completed"
sleep 2000
fnx SLOT1,2


while 1


if s88get(2)=1 then
if S0Pos = 2 then
else
print "S88.2 Detected"
S0Pos = 2
sleep 500
'SLO0を停止
spd SLOT0,0
sleep 10000
'ポイントを分岐
acc 31,0
'SLOT1を発進
NextLoco = 1
DEST = 1
dir SLOT1,2
sleep 2000
fnx SLOT1,2
sleep 2000
spd SLOT1,200
end if
end if


if s88get(1)=1 then
if S1Pos = 1 then
else
print "S88.1 Detected"
S1Pos = 1
sleep 1000
'SLO1を停止
spd SLOT1,0
sleep 10000
'ポイントを直進
acc 31,1
'SLOT0を発進
NextLoco = 0
DEST = 1
dir SLOT0,2
sleep 2000
fnx SLOT0,2
sleep 2000
spd SLOT0,200
end if
end if


if s88get(3)=1 then
'検知区間③に進入した車両がSLOT0の場合
if NextLoco = 0 then
if S0Pos = 3 then
else
print "S88.3 Detected as SLOT0"
S0Pos = 3
spd SLOT0,0
sleep 10000
dir SLOT0,1
sleep 2000
'上り/下りの判定
if DEST = 1 then
acc 41,0
end if
if DEST = 2 then
acc 41,1
end if
fnx SLOT0,2
sleep 2000
spd SLOT0,200
end if
end if
'検知区間③に進入した車両がSLOT1の場合
if NextLoco = 1 then
if S1Pos = 3 then
else
print "S88.3 Detected as SLOT1"
S1Pos = 3
spd SLOT1,0
sleep 10000
dir SLOT1,1
sleep 2000
'上り/下りの判定
if DEST = 1 then
acc 41,0
end if
if DEST = 2 then
acc 41,1
end if
fnx SLOT1,2
sleep 2000
spd SLOT1,200
end if
end if
end if

if s88get(5)=1 then
'検知区間⑤に進入した車両がSLOT0の場合
if NextLoco = 0 then
if S0Pos = 5 then
else
print "S88.5 Detected as SLOT0"
S0Pos = 5
spd SLOT0,0
sleep 10000
dir SLOT0,2
sleep 2000
'上り/下りの判定
if DEST = 1 then
acc 40,0
end if
if DEST = 2 then
acc 40,1
end if
fnx SLOT0,2
sleep 2000
spd SLOT0,200
end if
end if
'検知区間⑤に進入した車両がSLOT1の場合
if NextLoco = 1 then
if S1Pos = 5 then
else
print "S88.5 Detected as SLOT1"
S1Pos = 5
spd SLOT1,0
sleep 10000
dir SLOT1,2
sleep 2000
'上り/下りの判定
if DEST = 1 then
acc 40,0
end if
if DEST = 2 then
acc 40,1
end if
fnx SLOT1,2
sleep 2000
spd SLOT1,200
end if
end if
end if

if s88get(6)=1 then
'検知区間⑥に進入した車両がSLOT0の場合
if NextLoco = 0 then
if S0Pos = 6 then
else
print "S88.6 Detected as SLOT0"
S0Pos = 6
spd SLOT0,0
sleep 10000
dir SLOT0,1
sleep 2000
'上り/下りの判定
if DEST = 1 then
acc 30,0
end if
if DEST = 2 then
acc 30,1
end if
fnx SLOT0,2
sleep 2000
spd SLOT0,200
end if
end if
'検知区間⑥に進入した車両がSLOT1の場合
if NextLoco = 1 then
if S1Pos = 6 then
else
print "S88.6 Detected as SLOT1"
S1Pos = 6
spd SLOT1,0
sleep 10000
dir SLOT1,1
sleep 2000
'上り/下りの判定
if DEST = 1 then
acc 30,0
end if
if DEST = 2 then
acc 30,1
end if
fnx SLOT1,2
sleep 2000
spd SLOT1,200
end if
end if
end if

if s88get(7)=1 then
'検知区間⑦に進入した車両がSLOT0の場合
if NextLoco = 0 then
if S0Pos = 7 then
else
print "S88.7 Detected as SLOT0"
S0Pos = 7
spd SLOT0,0
sleep 10000
dir SLOT0,2
sleep 2000
'下りに切り替え
DEST = 2
fnx SLOT0,2
sleep 2000
spd SLOT0,200
end if
end if
'検知区間⑦に進入した車両がSLOT1の場合
if NextLoco = 1 then
if S1Pos = 7 then
else
print "S88.7 Detected as SLOT1"
S1Pos = 7
spd SLOT1,0
sleep 10000
dir SLOT1,2
sleep 2000
'下りに切り替え
DEST = 2
fnx SLOT1,2
sleep 2000
spd SLOT1,200
end if
end if
end if


wend

end

BASファイルはここです。 

 

で、実際に動かした動画はこちら。

youtu.be

  

今後

手持ちの線路、機器でやりたいことはいったんできた。

次は、複数の車両を同時に動かす自動運転したいな~

ということで、レイアウトをちょっと変更し、待避線を追加したい。

 あと、動かしたい内容でコードを書きつづけるのも、今後の拡張性を考えると限界が見えてきたので、進行方向の閉塞状況をチェックしながら車両が勝手に動き続けるようなロジックを追加していきたい。うむ、ハードル高い。。。

 

つづく