{"id":1893,"date":"2021-03-15T21:18:23","date_gmt":"2021-03-15T12:18:23","guid":{"rendered":"http:\/\/naochang.me\/?p=1893"},"modified":"2021-03-15T21:18:23","modified_gmt":"2021-03-15T12:18:23","slug":"raspberry-pi-%e3%81%a8-unity","status":"publish","type":"post","link":"https:\/\/stacstar.jp\/blog\/?p=1893","title":{"rendered":"Raspberry Pi \u3068 Unity"},"content":{"rendered":"<p><div style=\"width: 450px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-1893-1\" width=\"450\" height=\"800\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"\/blog\/wp-content\/uploads\/switch_unity.mp4?_=1\" \/><a href=\"\/blog\/wp-content\/uploads\/switch_unity.mp4\">\/blog\/wp-content\/uploads\/switch_unity.mp4<\/a><\/video><\/div><br \/>\nRaspberry Pi4 \u3092\u8cb7\u3063\u3066\u3061\u3087\u3063\u3068\u904a\u3093\u3067\u308b\u3002<br \/>\n\u3044\u3063\u305f\u3093\u7c21\u5358\u306a\u30b5\u30f3\u30d7\u30eb\u304c\u3067\u304d\u305f\u306e\u3067\u81ea\u5206\u30e1\u30e2\u3002<br \/>\n\u4f5c\u3063\u305f\u306e\u306f\u30bf\u30af\u30c8\u30b9\u30a4\u30c3\u30c1\u3092\u62bc\u3057\u305f\u3089 Python \u304b\u3089 LED \u3092\u5149\u3089\u305b\u3064\u3064 OSC \u3092 Mac \u306b\u9001\u308a\u3001Mac \u5074\u306e Max \u3067\u53d7\u4fe1\u3002\u305d\u308c\u3092\u305d\u306e\u307e\u307e Unity \u3067\u518d\u3073 OSC \u3067 localhost \u306b\u6e21\u3057\u3066\u30ad\u30e5\u30fc\u30d6\u306b AddForce() \u3068\u304b\u3092\u3055\u305b\u308b\u3002<br \/>\nMax \u3092\u4f7f\u308f\u305a Unity \u3067\u76f4\u63a5 OSC \u3067\u53d7\u3051\u3066\u3082\u3044\u3044\u3051\u3069\u5358\u7d14\u306b\u4f7f\u3063\u3066\u307f\u305f\u304b\u3063\u305f\u306e\u3060\u3051\u3002<\/p>\n<p><!--more--><br \/>\nPython3 \u3067 <span class=\"keyword\">python-osc<\/span> \u3092\u4f7f\u3046\u306e\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip3 install python-osc\r\n<\/pre>\n<p>\u30bf\u30af\u30c8\u30b9\u30a4\u30c3\u30c1\u306e\u56de\u7dda\u306b\u306f\u30d7\u30eb\u30c0\u30a6\u30f3\u62b5\u6297\u3092 On\u3002<br \/>\nLED \u3092\u4e00\u77ac\u5149\u3089\u305b\u3064\u3064 OSC\u3067 Mac \u5074\u3078\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u6295\u3052\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport socket\r\nimport random\r\nimport RPi.GPIO as GPIO\r\nfrom pythonosc import udp_client\r\nfrom pythonosc.osc_message_builder import OscMessageBuilder\r\nfrom time import sleep\r\n\r\nLED_GPIO = 4\r\nTACT_GPIO = 17\r\nIP = socket.gethostbyname(&quot;\u30db\u30b9\u30c8\u540d&quot;); # \u3082\u3057\u304f\u306f IP \u76f4\r\nPORT = 6700\r\n\r\nGPIO.setmode(GPIO.BCM)\r\nGPIO.setup(LED_GPIO, GPIO.OUT)\r\nGPIO.setup(TACT_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # \u30d7\u30eb\u30c0\u30a6\u30f3\u62b5\u6297\u6709\u52b9\r\n\r\nclient = udp_client.UDPClient(IP, PORT)\r\n\r\ntry:\r\n    while True:\r\n        if GPIO.input(TACT_GPIO) == GPIO.HIGH:\r\n            # OSC \u9001\u4fe1\r\n            print(&quot;Send&quot;)\r\n            msg = OscMessageBuilder(address='\/testmessage')\r\n            msg.add_arg(&quot;Hello World&quot;)\r\n            msg.add_arg(random.randrange(-10, 10))\r\n\r\n            client.send(msg.build())\r\n            \r\n            # LED \u3092\u4e00\u77ac\u5149\u3089\u305b\u308b\r\n            GPIO.output(LED_GPIO, GPIO.HIGH)\r\n            sleep(0.1)\r\n            GPIO.output(LED_GPIO, GPIO.LOW)            \r\nexcept KeyboardInterrupt:\r\n    pass\r\nfinally:\r\n    GPIO.cleanup()\r\n    print('--- stop program ---');\r\n<\/pre>\n<p>\u7d9a\u3044\u3066 Max\u3002\u4eca\u56de\u59cb\u3081\u3066\u4f7f\u3046\u306e\u3067\u307e\u3060\u826f\u304f\u308f\u304b\u3063\u3066\u306a\u3044\u3002OSC \u3067\u306e\u901a\u4fe1\u3092\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u306e\u304b\u5206\u304b\u3089\u306a\u304b\u3063\u305f\u3051\u3069\u3001\u5358\u7d14\u306b UDP \u3067\u306e\u53d7\u3051\u53d6\u308a\u9001\u4fe1\u306f\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u7c21\u5358\u3002\u4eca\u56de\u306f\u304d\u305f\u3082\u306e\u3092\u305d\u306e\u307e\u307e\u9055\u3046\u30dd\u30fc\u30c8\u756a\u53f7\u3067 Send \u3059\u308b\u3060\u3051\u3002(OSC \u81ea\u4f53\u306f UDP \u3067\u3082 TCP \u3067\u3082\u3067\u304d\u308b\u4f01\u753b\u3063\u307d\u3044)<br \/>\n\u3042\u3068\u306f\u8a66\u3057\u306b\u97f3\u3092\u9cf4\u3089\u3057\u3066\u307f\u305f\u3002\u30aa\u30fc\u30c7\u30a3\u30aa\u306e\u30c7\u30d0\u30a4\u30b9\u306e\u8a2d\u5b9a\u304c\u3069\u3053\u304b\u5206\u304b\u3089\u306a\u304b\u3063\u305f\u3051\u3069(\u30b9\u30d4\u30fc\u30ab\u30fc\u304b\u3089\u51fa\u3057\u305f\u304b\u3063\u305f) [Option] > [AudioStatus] \u306b\u3042\u3063\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/unity_swithc_max.png\" alt=\"\" width=\"397\" height=\"260\" class=\"aligncenter size-full wp-image-1902\" srcset=\"https:\/\/stacstar.jp\/blog\/.\/wp-content\/uploads\/unity_swithc_max.png 397w, https:\/\/stacstar.jp\/blog\/.\/wp-content\/uploads\/unity_swithc_max-300x196.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/p>\n<p>\u7d9a\u3044\u3066 Unity \u5074\u3002\u3053\u3063\u3061\u306f\u306a\u308c\u305f\u3082\u3093\u306a\u306e\u3067\u5e73\u6c17\u3002\u3068\u601d\u3063\u3066\u305f\u3051\u3069 \u30c6\u30b9\u30c8\u3067 Python \u5074\u3067 OSC \u306e\u30b5\u30fc\u30d0\u30fc\u3092\u7acb\u3066\u3066\u305f\u305b\u3044\u3067 Unity \u5074\u3067\u53d7\u3051\u53d6\u308c\u305a\u5c11\u3057\u30cf\u30de\u3063\u305f\u3002\u30e9\u30a4\u30d6\u30e9\u30ea\u306f keijiro \u3055\u3093\u306e <a href=\"https:\/\/github.com\/keijiro\/OscJack\" rel=\"noopener noreferrer\" target=\"_blank\">OscJack<\/a>\u3002\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u304c Timeline 1.5 \u3068\u304b\u3060\u3068\u30a8\u30e9\u30fc\u304c\u3042\u3063\u305f\u306e\u3067 UPM \u304b\u3089\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4e0b\u3052\u305f\u3002\u3042\u3068\u306f\u30a4\u30d9\u30f3\u30c8\u304c\u6765\u305f\u969b\u306e\u30b9\u30ec\u30c3\u30c9\u304c\u30e1\u30a4\u30f3\u3067\u306f\u306a\u3044\u306e\u3067\u3001Unity \u5468\u308a\u306e\u51e6\u7406\u3092\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u3067\u3084\u308b\u3088\u3046\u306b\u5909\u3048\u305f\u3002<\/p>\n<pre class=\"brush: unitycsharp; title: ; notranslate\" title=\"\">\r\nusing System.Threading;\r\nusing OscJack;\r\nusing UnityEngine;\r\n\r\n&#x5B;RequireComponent(typeof(Rigidbody))]\r\npublic class AddForceFromOSC : MonoBehaviour\r\n{\r\n    private OscServer server;\r\n\r\n    private Rigidbody rigidbody;\r\n\r\n    void Start()\r\n    {\r\n        SynchronizationContext synchronizationContext = SynchronizationContext.Current;\r\n\r\n        this.rigidbody = this.GetComponent&lt;Rigidbody&gt;();\r\n\r\n        this.server = new OscServer(6701);\r\n        this.server.MessageDispatcher.AddCallback(&quot;\/testmessage&quot;,\r\n            (address, data) =&gt;\r\n            {\r\n                Debug.Log($&quot;Message : {data.GetElementAsString(0)} : {data.GetElementAsInt(1)}&quot;);\r\n                synchronizationContext.Post(this.AddRandomForce, default);\r\n            });\r\n    }\r\n\r\n    private void OnDestroy()\r\n    {\r\n        this.server.Dispose();\r\n    }\r\n\r\n    private void AddRandomForce(object o)\r\n    {\r\n        this.rigidbody.AddForce(new Vector3(Random.Range(-2f, 2f), Random.Range(0f, 10f), Random.Range(-2f, 2f)), ForceMode.Impulse);\r\n        this.rigidbody.AddTorque(new Vector3(Random.Range(-5f, -5f), Random.Range(-5f, 5f), Random.Range(-5f, 5f)), ForceMode.Impulse);\r\n    }\r\n}\r\n<\/pre>\n<p>\u3042\u3068\u306f\u901a\u4fe1\u304c\u6765\u3066\u3044\u308b\u306e\u304b\u3069\u3046\u304b\u3092\u898b\u308b\u305f\u3081\u306b <span class=\"keyword\">tcpdump<\/span> \u3068\u3044\u3046\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u3063\u305f\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo tcpdump -A -n udp port 6700\r\n<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u76e3\u8996\u3057\u3066\u3001\u3053\u3053\u304c\u6765\u3066\u306a\u3051\u308c\u3070\u305d\u3082\u305d\u3082\u9001\u3089\u308c\u3066\u3044\u306a\u3044\u3002<\/p>\n<p>Unity \u306e OSCJack \u3067 \u518d\u751f\u3057\u305f\u308a OSCMonitor \u3092\u7acb\u3061\u4e0a\u3052\u308b\u3068\u30b5\u30fc\u30d0\u30fc\u304c\u751f\u6210\u3055\u308c\u308b\u306e\u3067\u305d\u306e\u3042\u3068\u30d3\u30eb\u30c9\u306a\u3093\u304b\u3092\u3057\u3066\u3057\u307e\u3046\u3068\u305d\u3063\u3061\u5074\u3067\u78ba\u7acb\u304c\u3067\u304d\u306a\u304f\u52d5\u304b\u306a\u304b\u3063\u305f\u3044\u3059\u308b\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Raspberry Pi4 \u3092\u8cb7\u3063\u3066\u3061\u3087\u3063\u3068\u904a\u3093\u3067\u308b\u3002 \u3044\u3063\u305f\u3093\u7c21\u5358\u306a\u30b5\u30f3\u30d7\u30eb\u304c\u3067\u304d\u305f\u306e\u3067\u81ea\u5206\u30e1\u30e2\u3002 \u4f5c\u3063\u305f\u306e\u306f\u30bf\u30af\u30c8\u30b9\u30a4\u30c3\u30c1\u3092\u62bc\u3057\u305f\u3089 Python \u304b\u3089 LED \u3092\u5149\u3089\u305b\u3064\u3064 OSC \u3092 Mac \u306b\u9001\u308a\u3001Mac \u5074\u306e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,2],"tags":[],"class_list":["post-1893","post","type-post","status-publish","format-standard","hentry","category-raspberrypi","category-unity"],"_links":{"self":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1893"}],"version-history":[{"count":12,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1893\/revisions"}],"predecessor-version":[{"id":2414,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1893\/revisions\/2414"}],"wp:attachment":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}