 

今回はラズパイで360°見守りカメラを自作していきます。
回転機構はステッピングモーター、操作パネルはDiscordBotで作成します。
今回の目標
今回使用する物
- RaspberryPi 4
- ステッピングモーター
- モータードライバー(付属品)
- WEBカメラ
- 5V USB電源
ステッピングモーターとは
カメラを回転させるために今回はステッピングモーターを使います。
ステッピングモーターは回転子が磁石、固定子にコイルが取り付けられているモーターです。
コイルに電流を流したり流さなかったりする制御を行うことで磁界の変化が生じ、中の磁石が回転します。
1stepという一定の角度で動作するため、正確に回転角度を決めることができます。
今回用いるステッピングモーターは「2相ユニポーラ型」のため3パターンの制御方法をが使えます。
今回は1相励磁と2相励磁よりも細かい制御ができる「1-2相励磁」を使っていきます。
1-2相励磁
次のように1step当たり8回の制御を行います。
ラズパイにはIN1~IN4をGPIOに接続します。
モータードライバーの電源はUSBなどから5Vを供給します。

順番にコイルに電流を流すことで軸を回します。
実現したい動き
DiscordBotにコマンドを送信(操作パネル)
    WEBカメラの角度をボタンで変えられるようにする(360°)
    キャプチャーボタンを押すと写真を撮る
    左右にスウィングする(180°)
映像をリアルタイムで見れるようにする(ローカル)
ステッピングモーターを動かしてみる
とりあえずステッピングモーターの動作確認をします。
キャプチャー機能とリアルタイム機能は未実装です。
コントロールパネル

[R]30 --- 右に30°回転 [R]90 --- 右に90°回転 [L]30 --- 左に30°回転 [L]90 --- 左に90°回転 Swing --- 左右に180°回転(2往復) Reset Position --- 初期位置まで回転
初期位置への戻し方
右方向の回転を+(プラス)、左方向の回転を-(マイナス)として、どれだけ移動したかを変数(position)に記録します。
(例)[R]30、[R]90、[L]30の操作 position = +3 +9 -3 = +9 position / 3 = +3 左方向に30°の回転を3回行うと戻れる(左に90°)
回転角の制限
無限に回転するとケーブルがねじれるので制限する必要があります。
今回は右に180°、左に180°まで回転するようにしています。
今、何°回転しているかは変数(position)の値によって分かります。
サンプルコード
import discord
from discord.ext import commands
import datetime
import RPi.GPIO as GPIO
import time
#GPIOの設定
IN1 = 2
IN2 = 3
IN3 = 4
IN4 = 5
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)
#変数の初期化
rotate = 0
p = 0
count = 0
step = 0
angle = 0
position = 0
#1-2相励磁パターン
pt_half = [[1,0,0,0],[1,1,0,0],[0,1,0,0],[0,1,1,0],[0,0,1,0],[0,0,1,1],[0,0,0,1],[1,0,0,1]]
#回転速度
interval = 0.001 #0.001
#[R]30 [R]90 [L]30 [L]90 の制御
def motor(p, rotate, angle, count, step):
    while angle < p:
        if rotate == 0:
            GPIO.output(IN1, pt_half[count][0])
            GPIO.output(IN2, pt_half[count][1])
            GPIO.output(IN3, pt_half[count][2])
            GPIO.output(IN4, pt_half[count][3])
        else:
            GPIO.output(IN1, pt_half[count][3])
            GPIO.output(IN2, pt_half[count][2])
            GPIO.output(IN3, pt_half[count][1])
            GPIO.output(IN4, pt_half[count][0])
         
        count += 1  
        if count == 7:
            count = 0
        else:
            pass
        
        step += 1
        if step == 10:
            step = 0
            angle += 1
        else:
            pass
        time.sleep(interval)
#スウィングの制御
def swing(p, rotate, angle, count, step, t):
    #スウィング中は回転速度を遅くする
    interval = 0.005
    
    #左右に合計4回ループする(2往復)
    while t < 4:
        if rotate == 0:
            GPIO.output(IN1, pt_half[count][0])
            GPIO.output(IN2, pt_half[count][1])
            GPIO.output(IN3, pt_half[count][2])
            GPIO.output(IN4, pt_half[count][3])
        else:
            GPIO.output(IN1, pt_half[count][3])
            GPIO.output(IN2, pt_half[count][2])
            GPIO.output(IN3, pt_half[count][1])
            GPIO.output(IN4, pt_half[count][0])
         
        count += 1  
        if count == 7:
            count = 0
        else:
            pass
        
        step += 1
        if step == 10:
            step = 0
            angle += 1
            if angle == 180:
                angle = 0
                
                #振り切った時に停止する時間
                time.sleep(2)
                if rotate == 0:
                    rotate = 1
                    t += 1
                else:
                    rotate = 0
                    t += 1
        else:
            pass
        time.sleep(interval)
#初期位置に戻す
def reset_p(reset, rotate, angle, count, step):
    while angle < reset:
        if rotate == 0:
            GPIO.output(IN1, pt_half[count][0])
            GPIO.output(IN2, pt_half[count][1])
            GPIO.output(IN3, pt_half[count][2])
            GPIO.output(IN4, pt_half[count][3])
        else:
            GPIO.output(IN1, pt_half[count][3])
            GPIO.output(IN2, pt_half[count][2])
            GPIO.output(IN3, pt_half[count][1])
            GPIO.output(IN4, pt_half[count][0])
         
        count += 1  
        if count == 7:
            count = 0
        else:
            pass
        
        step += 1
        if step == 10:
            step = 0
            angle += 1
        else:
            pass
        time.sleep(interval)
#DiscordBot
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(
    command_prefix=commands.when_mentioned_or("!"), debug_guilds=[サーバーID], intents=intents
)
@bot.event
async def on_ready():
    print('[INFO] <' + str(datetime.datetime.now().replace(microsecond=0)) + '> Bot is active')
#コントロールパネル作成(ボタンの配置)
class TestView(discord.ui.View):
    def __init__(self, timeout=180):
        super().__init__(timeout=timeout)
    @discord.ui.button(label='[R]30', row=0, style=discord.ButtonStyle.green)
    async def r_30(self, button: discord.ui.Button, interaction: discord.Interaction):
        print('[INFO] <' + str(datetime.datetime.now().replace(microsecond=0)) + '> [R]30')
        p = 30
        rotate = 0
        
        global position
        position += 3
        
        if abs(position) > 18:
            position -= 3
        else:
            motor(p, rotate, angle, count, step)
        await interaction.response.edit_message(content="Controlled : [R]30", view=self)
        
    @discord.ui.button(label='[R]90', row=0, style=discord.ButtonStyle.green)
    async def r_90(self, button: discord.ui.Button, interaction: discord.Interaction):
        print('[INFO] <' + str(datetime.datetime.now().replace(microsecond=0)) + '> [R]90')
        p = 90
        rotate = 0
        global position
        position += 9
        
        if abs(position) > 18:
            position -= 9
        else:
            motor(p, rotate, angle, count, step)
        await interaction.response.edit_message(content="Controlled : [R]90", view=self)
        
    @discord.ui.button(label='[L]30', row=0, style=discord.ButtonStyle.primary)
    async def l_30(self, button: discord.ui.Button, interaction: discord.Interaction):
        print('[INFO] <' + str(datetime.datetime.now().replace(microsecond=0)) + '> [L]30')
        p = 30
        rotate = 1
        
        global position
        position -= 3
        
        if abs(position) > 18:
            position += 3
        else:
            motor(p, rotate, angle, count, step)
        
        await interaction.response.edit_message(content="Controlled : [L]30", view=self)
        
    @discord.ui.button(label='[L]90', row=0, style=discord.ButtonStyle.primary)
    async def l_90(self, button: discord.ui.Button, interaction: discord.Interaction):
        print('[INFO] <' + str(datetime.datetime.now().replace(microsecond=0)) + '> [L]90')
        p = 90
        rotate = 1
        
        global position
        position -= 9
        
        if abs(position) > 18:
            position += 9
        else:
            motor(p, rotate, angle, count, step)
        
        await interaction.response.edit_message(content="Controlled : [L]90", view=self)
    #Capture 未実装
    @discord.ui.button(label='Capture', row=1, style=discord.ButtonStyle.gray)
    async def capture(self, button: discord.ui.Button, interaction: discord.Interaction):
        print('[INFO] <' + str(datetime.datetime.now().replace(microsecond=0)) + '> Capture')
        await interaction.response.edit_message(content="Controlled : Capture", view=self)
    @discord.ui.button(label='Swing', row=1, style=discord.ButtonStyle.gray)
    async def swing(self, button: discord.ui.Button, interaction: discord.Interaction):
        print('[INFO] <' + str(datetime.datetime.now().replace(microsecond=0)) + '> Swing')
        await interaction.response.defer()
        
        #まず初期位置に戻す
        global position
        position = position / 3
        if position > 0:
            rotate = 1
        elif position < 0:
            rotate = 0
        else:
            rotate = 1
            reset = 0
        reset = abs(position * 30)
        reset_p(reset, rotate, angle, count, step)
        position = 0
        
        #次に右方向に90°回転させる
        p = 90
        rotate = 0
        motor(p, rotate, angle, count, step)
        
        time.sleep(2)
        
        #スウィングさせる
        p = 180
        rotate = 1
        t = 0
        
        swing(p, rotate, angle, count, step, t)    
        
        #左方向に90°回転させる(初期位置に戻す)
        p = 90
        rotate = 1
        
        motor(p, rotate, angle, count, step)
        await interaction.followup.send(content="Controlled : Swing", view=self)
    @discord.ui.button(label='Reset Position', row=1, style=discord.ButtonStyle.gray)
    async def p_reset(self, button: discord.ui.Button, interaction: discord.Interaction):
        print('[INFO] <' + str(datetime.datetime.now().replace(microsecond=0)) + '> Reset')
        
        global position
        
        position = position / 3
        if position > 0:
            rotate = 1
        elif position < 0:
            rotate = 0
        else:
            rotate = 1
            reset = 0
            
        reset = abs(position * 30)
        
        reset_p(reset, rotate, angle, count, step)
        position = 0
        await interaction.response.edit_message(content="Controlled : Reset", view=self)
        
@bot.slash_command(name="camera")
async def view_test(ctx: discord.ApplicationContext):
    """[ Stepping Moter Camera ] Control Panel"""
    view = TestView()
    await ctx.interaction.response.send_message(content="Control Panel", view=view)
bot.run("DiscordBot TOKEN")
回転角度の制御
今回使用したステッピングモーターには1/64のギアが内蔵されています。
正確に計算するとstepを少数単位で制御する必要があり非常に大変です。
そのため、今回は「10stepで1°回転する」と定義 し、分かりやすくしました。
よって実際の角度には少しずれが生じます。

 
 
 
 
 
 
 
0 件のコメント:
コメントを投稿