2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry Piで工場ラインを自動化する方法【初心者〜実務者向け】

Posted at

はじめに

本記事では、Raspberry Pi を活用して工場ラインの自動化を実現する具体的な方法を解説します。初心者の方でも理解できるように基礎から説明しつつ、実務者が現場で活用できる構成や実装方法も含めています。Python・FastAPI・MariaDB・OpenCV を用いたリアルタイム監視と制御の仕組みを紹介します。


目次

  1. 工場ライン自動化における Raspberry Pi の役割
  2. 必要な機材とソフトウェア構成
  3. Raspberry Pi の初期設定
  4. センサ・PLC との接続方法
  5. MariaDB を用いたデータベース構築
  6. FastAPI によるデータ提供 API の作成
  7. OpenCV を用いたリアルタイム画像判定
  8. ダッシュボード構築で見える化する
  9. トラブルシューティング
  10. まとめ

1. 工場ライン自動化における Raspberry Pi の役割

Raspberry Pi は小型で低コストながら、豊富なインターフェース(GPIO、USB、LAN)と Linux OS を搭載しているため、工場内のセンサ情報収集、画像判定、PLC 連携、データ蓄積、Web API 提供などに適しています。

主な役割は次のとおりです。

  • 設備やセンサのデータ収集
  • Python による判定ロジック処理
  • 画像処理による OK/NG 判定
  • PLC 連携による設備制御
  • MariaDB へのデータ記録
  • ダッシュボード配信

2. 必要な機材とソフトウェア構成

2-1. 必要な機材

  • Raspberry Pi 4 / 5 本体
  • microSD または NVMe SSD
  • USB カメラ(UVC 対応)
  • LAN ケーブル
  • PLC(例:Keyence KV シリーズ)
  • 各種センサ(光電センサ、近接センサなど)

2-2. ソフトウェア構成

  • OS: Raspberry Pi OS
  • 言語: Python3
  • Web API: FastAPI
  • DB: MariaDB
  • 画像処理: OpenCV
  • 通信: pyModbusTCP / PLC SDK

3. Raspberry Pi の初期設定

sudo apt update
sudo apt upgrade -y
sudo apt install python3 python3-venv python3-pip mariadb-server -y

4. センサ・PLC との接続方法

PLC と通信する場合、Modbus/TCP を利用する方法が一般的です。

Python例:ModbusでPLC読み取り

from pyModbusTCP.client import ModbusClient

client = ModbusClient(host="192.168.0.10", port=502, auto_open=True)

def read_signal():
  coil = client.read_coils(0, 1)
  return coil[0] if coil else False

5. MariaDB を用いたデータベース構築

データベース作成

sudo mysql -u root -e "CREATE DATABASE factory;"

Python: MariaDBへ記録

import pymysql

conn = pymysql.connect(
  host="localhost",
  user="root",
  password="",
  database="factory"
)

def insert_log(status):
  with conn.cursor() as cur:
    cur.execute("INSERT INTO logs(status) VALUES(%s)", (status,))
    conn.commit()

6. FastAPI によるデータ提供 API の作成

from fastapi import FastAPI
import uvicorn

app = FastAPI()

@app.get("/api/status")
def status():
  return {"status": "running"}

if __name__ == "__main__":
  uvicorn.run(app, host="0.0.0.0", port=8000)

7. OpenCV を用いたリアルタイム画像判定

USBカメラからフレーム取得

import cv2

cap = cv2.VideoCapture(0)

while True:
  ret, frame = cap.read()
  if not ret:
    break

  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  _, th = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)

  cv2.imshow("frame", th)
  if cv2.waitKey(1) == 27:
    break

8. ダッシュボード構築で見える化する

FastAPI と組み合わせて設備稼働・画像判定・生産量・CT(Cycle Time)をブラウザで可視化できます。


9. トラブルシューティング

  • カメラが認識しない → ls /dev/video* 確認
  • FastAPI が動かない → ポート占有確認
  • MariaDB が遅い → インデックスの再確認

10. まとめ

本記事では、Raspberry Pi を用いて工場ラインを自動化するための全体構成と主要技術を解説しました。低コストながら、Python・FastAPI・MariaDB・OpenCV を組み合わせることで、実務レベルの監視・判定システムを構築できます。


2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?