前回、M5stickCからUDPで加速度センサーの位置情報を文字列としてRaspberry Piに送信してみました。↓
ここまで出来れば、後はRaspberry pi側で文字列に応じてGPIOの出力操作をしてあげればメカナムホイールの制御は簡単に出来ます。今回はそこの部分についてまとめようと思います。
メカナムホイールラジコンについては過去記事でまとめています。↓
初めに
おおまかな概要
今回作成するメカナムホイールラジコンの操作についての大まかな概要は下記のとおりです。
コントローラー(M5stickC) ⇒ UDP通信 ⇒ メカナムホイールラジコン(Raspberry Pi ZERO W)
M5stickCの加速度センサーを利用して、ピッチ角、ロー角の傾きに応じてその方向にメカナムホイールラジコンが動くようにします。
ラジコンの作成については過去記事でまとめているので、割愛します。
プログラム
M5stickC(micropython)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import network import time from socket import socket, AF_INET, SOCK_DGRAM from m5stack import * from m5ui import * from uiflow import * from m5stack import lcd import imu imu0 = imu.IMU() wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print('connecting to network...') wlan.connect('SSID', 'PASS') while not wlan.isconnected(): pass print('network config:', wlan.ifconfig()) serv_address = ("サーバーIP", ポート番号) s = socket(AF_INET, SOCK_DGRAM) #s.setsockopt(socket.SOL_SOCKET、socket.SO_REUSEADDR、1) while True: if imu0.ypr[1] < -15 and -10 < imu0.ypr[2] < 10: lcd.clear(lcd.BLACK) lcd.clear(lcd.BLUE) message = "forward" s.sendto(message.encode("utf-8"), serv_address) print(imu0.ypr[1]) time.sleep(0.1) elif imu0.ypr[1] > 30 and -10 < imu0.ypr[2] < 10: lcd.clear(lcd.BLACK) lcd.clear(lcd.RED) message = "backward" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) elif -10 < imu0.ypr[1] < 10 and 20 < imu0.ypr[2]: lcd.clear(lcd.BLACK) lcd.clear(lcd.GREEN) message = "right" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) elif -10 < imu0.ypr[1] < 10 and -80 < imu0.ypr[2] < -20: lcd.clear(lcd.BLACK) lcd.clear(lcd.ORANGE) message = "left" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) elif -15 > imu0.ypr[1] and -80 < imu0.ypr[2] < -10: lcd.clear(lcd.BLACK) lcd.clear(lcd.PINK) message = "leftforward" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) elif -15 > imu0.ypr[1] and 80 > imu0.ypr[2] > 10: lcd.clear(lcd.BLACK) lcd.clear(lcd.YELLOW) message = "rightforward" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) elif 30 < imu0.ypr[1] and -80 < imu0.ypr[2] < -10: lcd.clear(lcd.BLACK) lcd.clear(lcd.PURPLE) message = "leftback" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) elif 30 < imu0.ypr[1] and 80 > imu0.ypr[2] > 10: lcd.clear(lcd.BLACK) lcd.clear(lcd.BLUE) message = "rightback" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) elif 80 < imu0.ypr[2]: lcd.clear(lcd.BLACK) lcd.clear(lcd.RED) message = "r turn" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) elif -80 > imu0.ypr[2]: lcd.clear(lcd.BLACK) lcd.clear(lcd.RED) message = "l turn" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) else: lcd.clear(lcd.BLACK) message = "stop" s.sendto(message.encode("utf-8"), serv_address) time.sleep(0.1) s.close() |
Raspberry Pi ZERO W(python)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# -*- coding: utf-8 -*- from socket import socket, AF_INET, SOCK_DGRAM import RPi.GPIO as GPIO import time HOST = "サーバーIP" PORT = ポート番号 s = socket(AF_INET, SOCK_DGRAM) s.bind((HOST, PORT)) GPIO.setmode(GPIO.BCM) MOTOR_A1 = 5; MOTOR_A2 = 6; MOTOR_B1 = 13; MOTOR_B2 = 19; MOTOR_C1 = 26; MOTOR_C2 = 12; MOTOR_D1 = 16; MOTOR_D2 = 20; GPIO.setup(MOTOR_A1, GPIO.OUT) GPIO.setup(MOTOR_A2, GPIO.OUT) GPIO.setup(MOTOR_B1, GPIO.OUT) GPIO.setup(MOTOR_B2, GPIO.OUT) GPIO.setup(MOTOR_C1, GPIO.OUT) GPIO.setup(MOTOR_C2, GPIO.OUT) GPIO.setup(MOTOR_D1, GPIO.OUT) GPIO.setup(MOTOR_D2, GPIO.OUT) while(True): msg, address = s.recvfrom(8192) #print(f"message: {msg}\nfrom: {address}") msg = msg.decode() if msg == 'forward': GPIO.output(MOTOR_A1,GPIO.HIGH) GPIO.output(MOTOR_A2,GPIO.LOW) GPIO.output(MOTOR_B1,GPIO.HIGH) GPIO.output(MOTOR_B2,GPIO.LOW) GPIO.output(MOTOR_C1,GPIO.HIGH) GPIO.output(MOTOR_C2,GPIO.LOW) GPIO.output(MOTOR_D1,GPIO.HIGH) GPIO.output(MOTOR_D2,GPIO.LOW) #print('Content-type: text/html\n') #print(recieve) print(msg) elif msg == 'backward': GPIO.output(MOTOR_A1,GPIO.LOW) GPIO.output(MOTOR_A2,GPIO.HIGH) GPIO.output(MOTOR_B1,GPIO.LOW) GPIO.output(MOTOR_B2,GPIO.HIGH) GPIO.output(MOTOR_C1,GPIO.LOW) GPIO.output(MOTOR_C2,GPIO.HIGH) GPIO.output(MOTOR_D1,GPIO.LOW) GPIO.output(MOTOR_D2,GPIO.HIGH) #print('Content-type: text/html\n') #print(recieve) print(msg) elif msg == 'right': GPIO.output(MOTOR_A1,GPIO.HIGH) GPIO.output(MOTOR_A2,GPIO.LOW) GPIO.output(MOTOR_B1,GPIO.LOW) GPIO.output(MOTOR_B2,GPIO.HIGH) GPIO.output(MOTOR_C1,GPIO.LOW) GPIO.output(MOTOR_C2,GPIO.HIGH) GPIO.output(MOTOR_D1,GPIO.HIGH) GPIO.output(MOTOR_D2,GPIO.LOW) #print('Content-type: text/html\n') #print(recieve) print(msg) elif msg == 'left': GPIO.output(MOTOR_A1,GPIO.LOW) GPIO.output(MOTOR_A2,GPIO.HIGH) GPIO.output(MOTOR_B1,GPIO.HIGH) GPIO.output(MOTOR_B2,GPIO.LOW) GPIO.output(MOTOR_C1,GPIO.HIGH) GPIO.output(MOTOR_C2,GPIO.LOW) GPIO.output(MOTOR_D1,GPIO.LOW) GPIO.output(MOTOR_D2,GPIO.HIGH) #print('Content-type: text/html\n') #print(recieve) print(msg) elif msg == 'stop': #print('Content-type: text/html\n') #print(recieve) GPIO.output(MOTOR_A1,GPIO.LOW) GPIO.output(MOTOR_A2,GPIO.LOW) GPIO.output(MOTOR_B1,GPIO.LOW) GPIO.output(MOTOR_B2,GPIO.LOW) GPIO.output(MOTOR_C1,GPIO.LOW) GPIO.output(MOTOR_C2,GPIO.LOW) GPIO.output(MOTOR_D1,GPIO.LOW) GPIO.output(MOTOR_D2,GPIO.LOW) print(msg) elif msg == 'leftforward': #print('Content-type: text/html\n') #print(recieve) GPIO.output(MOTOR_A1,GPIO.LOW) GPIO.output(MOTOR_A2,GPIO.LOW) GPIO.output(MOTOR_B1,GPIO.HIGH) GPIO.output(MOTOR_B2,GPIO.LOW) GPIO.output(MOTOR_C1,GPIO.HIGH) GPIO.output(MOTOR_C2,GPIO.LOW) GPIO.output(MOTOR_D1,GPIO.LOW) GPIO.output(MOTOR_D2,GPIO.LOW) print(msg) elif msg == 'rightforward': #print('Content-type: text/html\n') #print(recieve) GPIO.output(MOTOR_A1,GPIO.HIGH) GPIO.output(MOTOR_A2,GPIO.LOW) GPIO.output(MOTOR_B1,GPIO.LOW) GPIO.output(MOTOR_B2,GPIO.LOW) GPIO.output(MOTOR_C1,GPIO.LOW) GPIO.output(MOTOR_C2,GPIO.LOW) GPIO.output(MOTOR_D1,GPIO.HIGH) GPIO.output(MOTOR_D2,GPIO.LOW) print(msg) elif msg == 'l turn': #print('Content-type: text/html\n') #print(recieve) GPIO.output(MOTOR_A1,GPIO.LOW) GPIO.output(MOTOR_A2,GPIO.HIGH) GPIO.output(MOTOR_B1,GPIO.HIGH) GPIO.output(MOTOR_B2,GPIO.LOW) GPIO.output(MOTOR_C1,GPIO.LOW) GPIO.output(MOTOR_C2,GPIO.HIGH) GPIO.output(MOTOR_D1,GPIO.HIGH) GPIO.output(MOTOR_D2,GPIO.LOW) print(msg) elif msg == 'r turn': #print('Content-type: text/html\n') #print(recieve) GPIO.output(MOTOR_A1,GPIO.HIGH) GPIO.output(MOTOR_A2,GPIO.LOW) GPIO.output(MOTOR_B1,GPIO.LOW) GPIO.output(MOTOR_B2,GPIO.HIGH) GPIO.output(MOTOR_C1,GPIO.HIGH) GPIO.output(MOTOR_C2,GPIO.LOW) GPIO.output(MOTOR_D1,GPIO.LOW) GPIO.output(MOTOR_D2,GPIO.HIGH) print(msg) elif msg == 'leftback': #print('Content-type: text/html\n') #print(recieve) GPIO.output(MOTOR_A1,GPIO.LOW) GPIO.output(MOTOR_A2,GPIO.HIGH) GPIO.output(MOTOR_B1,GPIO.LOW) GPIO.output(MOTOR_B2,GPIO.LOW) GPIO.output(MOTOR_C1,GPIO.LOW) GPIO.output(MOTOR_C2,GPIO.LOW) GPIO.output(MOTOR_D1,GPIO.LOW) GPIO.output(MOTOR_D2,GPIO.HIGH) print(msg) elif msg == 'rightback': #print('Content-type: text/html\n') #print(recieve) GPIO.output(MOTOR_A1,GPIO.LOW) GPIO.output(MOTOR_A2,GPIO.LOW) GPIO.output(MOTOR_B1,GPIO.LOW) GPIO.output(MOTOR_B2,GPIO.HIGH) GPIO.output(MOTOR_C1,GPIO.LOW) GPIO.output(MOTOR_C2,GPIO.HIGH) GPIO.output(MOTOR_D1,GPIO.LOW) GPIO.output(MOTOR_D2,GPIO.LOW) print(msg) time.sleep(0.01) s.close() |
完成
こんな感じで動きます。↓