DIY RC Bowling [ESP32 ] Source code
Bowl side source code
//参考https://rikoubou.hatenablog.com/entry/2017/10/06/181805 #include <WiFi.h> #include <WiFiUdp.h> #include "esp_system.h" const char ssid[] = "ESP32_wifi"; // SSID const char pass[] = "esp32pass"; // password const int localPort = 10000; // ポート番号(pythonのポート番号に合わせる) const IPAddress ip(192, 168, 4, 1); // IPアドレス(pythonのIPアドレスに合わせる) const IPAddress subnet(255, 255, 255, 0); // サブネットマスク WiFiUDP udp; //https://wak-tech.com/archives/742 const int dcmotor = 32; const int svmotor = 2; void setup() { //UDP通信関係 Serial.begin(115200); WiFi.softAP(ssid, pass); // SSIDとパスの設定 delay(100); // 追記:このdelayを入れないと失敗する場合がある WiFi.softAPConfig(ip, ip, subnet); // IPアドレス、ゲートウェイ、サブネットマスクの設定 Serial.print("AP IP address: "); IPAddress myIP = WiFi.softAPIP(); Serial.println(myIP); Serial.println("Starting UDP"); udp.begin(localPort); // UDP通信の開始(引数はポート番号) Serial.print("Local port: "); Serial.println(localPort); //LEDC関係 // 使用するタイマーのチャネルと周波数を設定 ledcSetup(0, 12800, 8); ledcSetup(10, 50, 10); // ledPinをチャネル0へ接続 ledcAttachPin(dcmotor, 0); ledcAttachPin(svmotor, 10); } void loop() { // 初期の状態を指定 static uint8_t dc = 0; static uint8_t sv = 73; if (udp.parsePacket()) { char val = udp.read(); val = int(val); if (val == 48){//停止 dc = 0; sv = 73; } else if (val == 49){//前 dc = 50; sv = 73; } else if (val == 50){//右 dc = 0; sv = 26; } else if (val == 51){//後 dc = 0; sv = 73; } else if (val == 52){//左 dc = 0; sv = 123; } else if (val == 53){//右前 dc = 30; sv = 26; } else if (val == 56){//左前 dc = 30; sv = 123; } ledcWrite(0, dc); ledcWrite(10,sv); } }
Controller(PC and Plyastation3) side source code
#参考https://rikoubou.hatenablog.com/entry/2017/10/06/181805 #参考https://greenhornprofessional.hatenablog.com/entry/2020/09/13/223155 from __future__ import print_function import socket import time from time import sleep from contextlib import closing import pygame from pygame.locals import * ###ジョイスティック側 # ジョイスティックの初期化 pygame.joystick.init() try: # ジョイスティックインスタンスの生成 joystick = pygame.joystick.Joystick(0) joystick.init() print('ジョイスティックの名前:', joystick.get_name()) print('ボタン数 :', joystick.get_numbuttons()) except pygame.error: print('ジョイスティックが接続されていません') # pygameの初期化 pygame.init() # 画面の生成 SCREEN_SIZE = (640, 480) # 画面サイズ (横/縦) STEP = 10 #キーボード/ハットスイッチの反応の良さ X_CENTER = int(SCREEN_SIZE[0]/2) Y_CENTER = int(SCREEN_SIZE[1]/2) screen = pygame.display.set_mode(SCREEN_SIZE) screen2 = pygame.display.set_mode(SCREEN_SIZE) screen.fill((0,0,0)) screen2.fill((0,0,0)) #通信部分 def main(): host = '192.168.4.1' # IPアドレス port = 10000 # ポート番号 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #AFINETは、V4を使うという意味、DGRAMはUDP指定の意味 with closing(sock): while True: # イベントの取得 for e in pygame.event.get(): # 終了ボタン if e.type == QUIT: active = False # ジョイスティックのボタンの入力 if e.type == pygame.locals.JOYAXISMOTION: print(joystick.get_axis(0), -1*joystick.get_axis(1), joystick.get_axis(2), -1*joystick.get_axis(3)) command1 = -joystick.get_axis(1)+1 command1 = str(round(command1)) print(command1) print(type(command1)) command2 = joystick.get_axis(2)+1 command2 = str(round(command2)) print(command2) print(type(command2)) command = command1 + command2 if command == "21": #前 command = "1" elif command == "12": #右 command = "2" elif command == "01": #後 command = "3" elif command == "10": #左 command = "4" elif command == "22": #右前 command = "5" elif command == "02": #右後 command = "6" elif command == "00": #左後 command = "7" elif command == "20": #右前 command = "8" elif command == "11": #継続 command = "0" print(command) command = str(command).encode("UTF-8") print(command) sock.sendto(command,(host, port)) time.sleep(0.05) #sock.sendto(bytes(command, encoding='ascii'), (host, port)) return """ message = 'A'.encode('utf-8') # 送る文字列(ここでは「A」という文字列を送っている) print(message) sock.sendto(message, (host, port)) time.sleep(1) """ #return if __name__ == '__main__': main()