{"id":70,"date":"2021-04-18T18:20:50","date_gmt":"2021-04-18T09:20:50","guid":{"rendered":"https:\/\/rfsec.ddns.net\/db\/?p=70"},"modified":"2021-07-27T08:56:41","modified_gmt":"2021-07-26T23:56:41","slug":"tof","status":"publish","type":"post","link":"https:\/\/rfsec.ddns.net\/db\/?p=70","title":{"rendered":"ToF(Time of Flight)\u30c7\u30d0\u30a4\u30b9"},"content":{"rendered":"\n<p>ToF(Time of Flight)\u30c7\u30d0\u30a4\u30b9VL530X\u3092\u5229\u7528\u3057\u3066\u3001\u8d85\u8fd1\u63a5\u30ec\u30fc\u30c0\u3082\u3069\u304d\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u306a\u30d1\u30fc\u30c4<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>VL53L0X   \u3000\u3000\u3000\u3000\u8ddd\u96e2\u30bb\u30f3\u30b5\u30e2\u30b8\u30e5\u30fc\u30eb<\/li><li>Arduino nano\u3000\u3000CPU<\/li><li>A4998     \u3000\u3000\u3000\u3000\u30b9\u30c6\u30c3\u30d4\u30f3\u30b0\u30e2\u30fc\u30bf\u30c9\u30e9\u30a4\u30d0\u30fc<\/li><li>Stepping motor\u3000<\/li><li>Slip ring<\/li><\/ul>\n\n\n\n<p>VL53L0X Time-of-Flight \u8ddd\u96e2\u30bb\u30f3\u30b5\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u6982\u8981\u306f\u3001\u4f8b\u3048\u3070<a href=\"https:\/\/www.switch-science.com\/catalog\/2894\/\">\u3053\u3061\u3089\u306e\u30da\u30fc\u30b8<\/a>\u3092\u53c2\u7167\u3002<\/p>\n\n\n\n<p>arduino nano\u3067\u30b9\u30c6\u30c3\u30d4\u30f3\u30b0\u30e2\u30fc\u30bf\u3092\u56de\u8ee2\u3055\u305b\u306a\u304c\u3089\u3001ToF\u30c7\u30d0\u30a4\u30b9\u3067\u5149\u3092\u53cd\u5c04\u3059\u308b\u7269\u4f53\u307e\u3067\u306e\u8ddd\u96e2\u3092\u6e2c\u5b9a\u3057\u307e\u3059\u3002<a href=\"https:\/\/youtu.be\/Rt7eVn3ku10?loop=1&amp;playlist=Rt7eVn3ku10\">\u30e2\u30fc\u30bf\u306e\u56de\u8ee2\u89d2\u5ea6\u3068\u8ddd\u96e2<\/a>\u3092USB I\/F\u3067PC\u3078\u8ee2\u9001\u3057\u3001PC\u5074\u3067Python\uff08Jupyter notebook\uff09\u3067\u53ef\u8996\u5316\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"VL53L0X+Stepping Motor+SlipRing\" width=\"625\" height=\"352\" src=\"https:\/\/www.youtube.com\/embed\/Rt7eVn3ku10?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Jupyter notebook\u3067\u53ef\u8996\u5316\u3057\u305f\u69d8\u5b50\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"VL5310ToF\u3092\u5229\u7528\u3057\u305f\u8d85\u8fd1\u63a5\u30ec\u30fc\u30c0\" width=\"625\" height=\"469\" src=\"https:\/\/www.youtube.com\/embed\/M_Qcqwn9cKw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><strong>Jupyter notebook\u3067\u53ef\u8996\u5316<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># -*- coding: utf-8 -*-\nimport sys\nimport  glob\n\nimport serial\nimport asyncio, sys\nimport cv2\nimport numpy as np\nimport pygame\nimport math\nfrom pygame.locals import *\n\ndef main():\n    hist=350\n    buf={}\n    (x,y) = (1024,1024)   # \u753b\u9762\u30b5\u30a4\u30ba\n\n    pygame.init()       # pygame\u521d\u671f\u5316\n    pygame.display.set_mode((x, y), 0, 32)  # \u753b\u9762\u8a2d\u5b9a\n    pygame.display.set_caption(&#39;ToF VL53L0X ranging...&#39;)\n    screen = pygame.display.get_surface()\n    fontFace =cv2.FONT_HERSHEY_SIMPLEX\n    dev=glob.glob(&quot;\/dev\/ttyUSB*&quot;)[0]\n    dev=dev.replace(&#39;[&#39;,&#39;&#39;)\n    dev=dev.replace(&#39;]&#39;,&#39;&#39;)\n    ser =serial.Serial(dev, 115200) \n    \n    for i in range(5):\n        line=ser.readline()\n        \n    font = pygame.font.Font(None, 55)\n    #print(buf)\n    while (1):\n        for i in range(1,3):\n            try:\n                deg,distance=ser.readline().decode().split(&#39;,&#39;)\n                deg=int(deg)\n                distance=int(distance)\n                buf[deg]=distance\n                # \u30b4\u30df\u30c7\u30fc\u30bf\u5bfe\u7b56\n                for k in range(4):\n                    tmp=deg-k\n                    if  tmp&lt;0:\n                        tmp=tmp+360\n                    buf[deg-k]=distance\n            except:\n                continue\n\n\n        # \u30ec\u30fc\u30c0\u30fc\u753b\u9762\u306e\u80cc\u666f\u63cf\u753b\n        pygame.draw.circle(screen, (0, 200, 0), (int(x\/2), int(y\/2)), int(x\/2), 1)\n        pygame.draw.circle(screen, (0, 200, 0), (int(x\/2), int(y\/2)), int(x\/4), 1)\n        pygame.draw.line(screen, (0, 200, 0), (0, int(y\/2)), (x, int(y\/2)))\n        pygame.draw.line(screen, (0, 200, 0), (int(x\/2), 0), (int(x\/2), y))\n        # \u30ec\u30fc\u30c0\u30fc\u30d3\u30fc\u30e0\u306e\u8ecc\u8de1\u63cf\u753b\n        dx = x\/2 + x\/2 * math.cos(math.radians(deg))\n        dy = y\/2 + x\/2 * math.sin(math.radians(deg))\n        pygame.draw.line(screen, (0, 255, 0), (int(x\/2), int(y\/2)), (int(dx), int(dy)))\n        for k in range(hist):\n            tmp = deg-k\n            if tmp&lt;0:\n                tmp=tmp+360\n            if tmp in buf:\n                x0 = x\/2 + buf[tmp]*0.8*math.cos(math.radians(tmp))\n                y0=  y\/2 + buf[tmp]*0.8*math.sin(math.radians(tmp))\n                blip=int(255*(hist-k)\/hist)\n                pygame.draw.line(screen, (blip, 0, 0), (int(x\/2), int(y\/2)), (int(x0), int(y0)))\n                pygame.draw.circle(screen, (blip, 0, 0), (int(x0), int(y0)), 4, 2)\n                \n        pygame.display.update()     # \u753b\u9762\u66f4\u65b0\n        pygame.time.wait(10)        # \u66f4\u65b0\u6642\u9593\u9593\u9694\n        screen.fill((0, 20, 0, 0))  # \u753b\u9762\u306e\u80cc\u666f\u8272\n\n        # \u30a4\u30d9\u30f3\u30c8\n        for event in pygame.event.get():\n            if event.type == QUIT:      # \u9589\u3058\u308b\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u3089\u7d42\u4e86\n                pygame.quit()           # Pygame\u306e\u7d42\u4e86(\u753b\u9762\u9589\u3058\u3089\u308c\u308b)\n                ser.close()\n                sys.exit()\n                \nif __name__ == &quot;__main__&quot;:\n        main()<\/code><\/pre><\/div>\n\n\n\n<p><strong>Arduino\u3067(\u30b9\u30c6\u30c3\u30d4\u30f3\u30b0\u30e2\u30fc\u30bf\u3068\u8ddd\u96e2\u30bb\u30f3\u30b5\u30fc\u306e\u5236\u5fa1\uff09<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>#include &lt;vl53l0xTOFA.h&gt;\n#include &lt;Wire.h&gt;\n\n#include &lt;Stepper.h&gt;\n\/\/#define PIN_xDIR   D3 \/\/direction\n\/\/#define PIN_xSTEP  D4 \/\/step\n#define PIN_xDIR   PD3 \/\/direction\n#define PIN_xSTEP  PD2 \/\/step\n#define PIN_MODE   A1 \/\/ MODE\n\/\/#define LED       A2\n#define ST        4\nVL53L0xTOFA sensor;\n\n\/\/ for your motor\nint val = 0;\n\/\/ initialize the stepper library on pins 8 through 11:\n\/\/Stepper myStepper(stepsPerRevolution, D4, D3);\n\nfloat dist_TOF(int pos, int n) {\n  float sum = 0;\n  for ( int j = 0; j &lt; n; j++) {\n    sensor.readTOFA();\n    sum += sensor.tofa.distancemm;\n  }\n  int distance = int(sum \/ n);\n  int deg = int(pos *  36.0 \/ 40.0);\n  \/\/  Serial.print(pos);\n  \/\/  Serial.print(&#39;,&#39;);\n  Serial.print(deg);\n  Serial.print(&#39;,&#39;);\n  Serial.println(distance);\n}\n\nvoid setup() {\n  pinMode(PIN_xDIR, OUTPUT);\n  pinMode(PIN_xSTEP, OUTPUT);\n  \/\/  pinMode(LED, OUTPUT);\n  pinMode(PIN_MODE, INPUT_PULLUP);\n  \/\/ set the speed at 60 rpm:\n  \/\/  myStepper.setSpeed(60);\n  \/\/ initialize the serial port:\n  Serial.begin(115200);\n  delay(1000);\n  Wire.begin();\n  delay(1000);\n\n  sensor.setTimeout(500);\n  if (!sensor.init())\n  {\n    Serial.println(&quot;Failed to detect and initialize sensor!&quot;);\n    while (1) {}\n  }\n\n  sensor.startContinuous(5);\n\n  Serial.println(&quot;Radar Start&quot;);\n}\n\nvoid rot(int dir, int n) {\n  for (int i = 0; i &lt; n; i++) {\n    for (int j = 0; j &lt; 2 * ST; j++) {\n      digitalWrite(PIN_xSTEP, !(digitalRead(PIN_xSTEP)));\n      delayMicroseconds(800);\n    }\n    dist_TOF(i, 1);\n    delayMicroseconds(800);\n  }\n}\n\nvoid loop() {\n  if (digitalRead(PIN_MODE) == LOW) {\n    digitalWrite(PIN_xDIR, LOW);\n    rot(1, 400);\n  } else {\n    digitalWrite(PIN_xDIR, HIGH);\n    rot(0, 400);\n  }\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>ToF(Time of Flight)\u30c7\u30d0\u30a4\u30b9VL530X\u3092\u5229\u7528\u3057\u3066\u3001\u8d85\u8fd1\u63a5\u30ec\u30fc\u30c0\u3082\u3069\u304d\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002 \u4e3b\u8981\u306a\u30d1\u30fc\u30c4 VL53L0X \u3000\u3000\u3000\u3000\u8ddd\u96e2\u30bb\u30f3\u30b5\u30e2\u30b8\u30e5\u30fc\u30eb Arduino nano\u3000\u3000CPU A4998 \u3000\u3000\u3000\u3000 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-make"],"featured_image_src":null,"author_info":{"display_name":"mars","author_link":"https:\/\/rfsec.ddns.net\/db\/?author=1"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=70"}],"version-history":[{"count":9,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":297,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions\/297"}],"wp:attachment":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}