本文へスキップ

このページは趣味のアマチュア無線から得た知識を纏めたものです。/ This page summarizes the knowledge gained from my hobby of amateur radio. 

QTH Osaka JAPAN

Zone25-ITU45 Locator PM74tu

N-ゲージ / Model RailRoad

2022年1月上旬、手持ちのRaspberryPi Zero wにモーターコントローラーTA8428Kと反射型フォトセンサーPPR-220を組み合わせてNゲージを制御しようと考えた。 回転数の勉強を始めて、曲がりなりにも鉄道模型をコントロールできるようになりました。 その後、手持ちのRaspberry Pi3Bに差し替え、最後にHamPiとdmonitorをインストールしたRaspberry Pi4に鉄道模型の制御プログラムを追加しました。 下記@レイアウト決定からCプログラム成功までの経験をまとめました。


Early January 2022, I thought of controlling the N-gauge by combining holdings the RaspberryPi Zero w with the motor controller TA8428K and the reflective photosensor PPR-220. I started to study about the rotation speed, and now I can control the model train after a fashion. After that, I replaced it with the holdings Raspberry Pi3B I had on hand, and finally added the model train control program to the Raspberry Pi4 on which HamPi and dmonitor were installed. Below is a summary of my experience from (1) layout decision to (4) program success.

@ Layout

○○○○○○○○イメージ

「○○○○○○○○」

This is a layout in which rail pattern B is added to TOMIX's plan DX-PC (rail pattern A+B+C). Program the driving pattern of the vehicle in Python based on this layout.

TOMIXのプランDX-PC(レールパターンA+B+C)にレールパターンBを追加したレイアウトです。このレイアウトに基づいて車両の走行パターンをPythonでプログラムします。


A Pin Allocation / GPIOのpinとTA8428KとPPR-220の端子の割付

○○○○○○○○イメージ ○○○○○○○○イメージ

「○○○○○○○○」

GPIOのPin
RaspberryPi4のGPIOのどのpinからモーターや電動ポイントを制御するPWM信号を出力してTA8428Kに渡すか、また、どのpinで反射型フォトセンサRPR-220の信号を受け取る(入力する)かをPythonのプログラムの約束に従ってpin番号を割り付けます。 T A8428KとRPR-220の端子 ブレッドボードに取りつけたTA8428Kとレイアウト上に設置する反射型フォトセンサRPR-220の端子を先に割り付けたGPIOのpin番号に合わせて割り付けます。

GPIO-Pin
Which pin of the GPIO of the RaspberryPi4 outputs the PWM signal that controls the motor and "Electric Point" and passes it to the TA8428K, and which pin receives (inputs) the signal of the reflective photosensor RPR-220, assign pin numbers according to Python conventions. Tr.A8428K and RPR-220 terminals Assign the pins of the TA8428K attached to the breadboard and the reflective photo sensor RPR-220 installed on the layout according to the GPIO pin numbers assigned earlie.


B Controller / コントローラーr

○○○○○○○○イメージ ○○○○○○○○イメージ

RaspberryPi4にFREENOVEの「スターターキット」FNK0019のブレッドボードを繋いで、模型電車の速度制御用にTA8428Kを1個とTOMIXの電動式ポイント切り替え用にTA8428Kを4個の合計5個のTA8428Kを取りつけています。TA8428Kはモータードライバー用ICですがポイント切り替え用にも使えました。また、6個の反射型フォトセンサRPR-220 は電車の位置を検出してポイント切り替えの信号にしたり、プラットホームやヤードで車両を停止させるための信号に使います。

I connected the FNK0019 breadboard of FREENOVE's "starter kit" to the RaspberryPi4, and attached 1(one) TA8428K for speed control of the model train and 4(four) TA8428K for TOMIX electric point switching, for a total of 5(five) TA8428Ks. TA8428K is a motor driver IC, but it could also be used for point switching. In addition, 6 (six) reflective photo sensors RPR-220 detect the position of the train and use it as a signal for point switching, or as a signal to stop the vehicle at the platform or yard.


C Programs

○○○○○○○○イメージ

PythonはもともとRaspberry Pi OSのイメージに付いていたプログラミング言語で、Nゲージを制御するために必要なライブラリの組み込み方をインターネット上のサンプルプログラムを参考にしながら学びました。その結果、モータドライバTA8428と反射型フォトセンサの駆動を扱うGPIO用ライブラリと、写真のような制御画面を表示するライブラリをロードするだけで、Nゲージ制御プログラムが初めて完成しました。 def文で8種類の走行パターンの関数を作成し、コントロール画面上で操作することで、単独で操作したり、これらの関数を組み合わせてパターンを変更したりできるようにしています。

Python is a programming language originally attached to the image of the Raspberry Pi OS, and I learned how to incorporate the necessary libraries to control the N-gauge by referring to sample programs on the Internet. As a result, the N-gauge control program was completed for the first time just by loading the library for GPIO that handles the drive of the motor driver TA8428 and the reflective photosensor and the library that displays the control screen like the picture. I create functions for 8 types of running patterns with def statements and operate them on the control screen so that you can operate them independently or combine these functions to change the patterns.

C Problems experienced before completing the program / プログラム完成までに体験した問題点

1) 制御装置の電源を入れたときにTOMIX電動ポイントを作動させるために使用されたTA8424の制御ピン1および2の1つが、プログラムされた信号を受信せず、そのうちの1つがLOWでした。 TOMIXの電動ポイントのコイルに12V近くの電圧がかかりっぱなしになって、何度も電動ポイントのコイルを焼損しました。 原因はGPIO端子とTA8424の制御端子をつなぐジャンパー線の接触不良でした。 (はんだ付けしないデメリット?) 電動ポイントのコイルにLEDを並列に接続して動作状態を可視化する対策を行いました。
2) 反射型ホトセンサーPPR-220は、曇りの日と夜間 (晴れた日でも) はプログラム通り正常に動作しましたが、晴れた日の日中に誤動作しました。 原因はセンサーが太陽の赤外線に反応していたためです。(窓のカーテンを閉めることで問題は解決しました。)

1) TOMIX electric point when the control device was powered on does not receive a signal as programmed and one of them was LOW. There were many times that the coil of the TOMIX electric point was left with a voltage of nearly 12V, and the coil of the TOMIX electric point was burned out. Cause was poor contact of the jumper wire connecting the GPIO pin and the TA8424 control pin. (Disadvantage of not soldering?) I have countermeasured that visualize the operating state by connecting an LED in parallel with the coil of the electric point.

2) The Reflective photosensor PPR-220 worked normally on cloudy days and at night (even on sunny days) as programmed, but malfunctions during the day on sunny days. Cause was the sensor was reacting to the infrared rays of the sun (Closing the window curtains solved the problem).