{"id":1073365,"date":"2025-01-08T11:24:51","date_gmt":"2025-01-08T03:24:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073365.html"},"modified":"2025-01-08T11:24:53","modified_gmt":"2025-01-08T03:24:53","slug":"%e5%a6%82%e4%bd%95%e5%b0%86webots%e8%ae%be%e7%bd%ae%e6%88%90python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073365.html","title":{"rendered":"\u5982\u4f55\u5c06webots\u8bbe\u7f6e\u6210python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103316\/c9b14320-0d35-4f1f-a1a4-e2c2801d4a5b.webp\" alt=\"\u5982\u4f55\u5c06webots\u8bbe\u7f6e\u6210python\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Webots\u8bbe\u7f6e\u4e3aPython\u7f16\u7a0b\u73af\u5883\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u6240\u9700\u7684Python\u5e93\u3001\u914d\u7f6eWebots\u548c\u521b\u5efaPython\u63a7\u5236\u5668\u6765\u5b9e\u73b0\u3002<\/strong> \u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Webots\u548cPython\uff0c\u7136\u540e\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u9010\u6b65\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Webots\u548cPython<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Webots<\/strong>\uff1a\u9996\u5148\uff0c\u4e0b\u8f7d\u5e76\u5b89\u88c5Webots\u3002\u4f60\u53ef\u4ee5\u4eceCyberbotics\u5b98\u65b9\u7f51\u7ad9\u83b7\u53d6\u6700\u65b0\u7248\u672c\u7684Webots\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6253\u5f00Webots\u4ee5\u786e\u8ba4\u5176\u6b63\u5e38\u8fd0\u884c\u3002<\/li>\n<li><strong>\u5b89\u88c5Python<\/strong>\uff1aWebots\u9ed8\u8ba4\u652f\u6301Python 3.x\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python 3.x\u7248\u672c\u3002\u4f60\u53ef\u4ee5\u4ecePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u5b89\u88c5\u6240\u9700Python\u5e93<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5webots\u5e93<\/strong>\uff1aWebots\u63d0\u4f9b\u4e86\u4e00\u4e2aPython\u5e93\uff0c\u540d\u4e3a<code>webots<\/code>. \u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install webots<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u5176\u4ed6\u4f9d\u8d56\u5e93<\/strong>\uff1a\u6839\u636e\u4f60\u7684\u9879\u76ee\u9700\u6c42\uff0c\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u5176\u4ed6Python\u5e93\uff0c\u4f8b\u5982<code>numpy<\/code>\u3001<code>matplotlib<\/code>\u7b49\u3002\u4f7f\u7528<code>pip<\/code>\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install numpy matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u914d\u7f6eWebots\u4f7f\u7528Python<\/h3>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Webots\u9879\u76ee<\/strong>\uff1a\u5728Webots\u4e2d\uff0c\u9009\u62e9\u201cFile\u201d &gt; \u201cNew World\u201d\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u3002\u4fdd\u5b58\u9879\u76ee\u6587\u4ef6\u3002<\/li>\n<li><strong>\u521b\u5efa\u4e00\u4e2aPython\u63a7\u5236\u5668<\/strong>\uff1a\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2aPython\u63a7\u5236\u5668\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_controller.py<\/code>\u7684\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u7f16\u5199Python\u63a7\u5236\u5668<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165Webots\u6a21\u5757<\/strong>\uff1a\u5728\u4f60\u7684Python\u63a7\u5236\u5668\u6587\u4ef6\u4e2d\uff0c\u9996\u5148\u5bfc\u5165\u5fc5\u8981\u7684Webots\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from controller import Robot<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521d\u59cb\u5316\u673a\u5668\u4eba<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u673a\u5668\u4eba\u5b9e\u4f8b\u5e76\u8bbe\u7f6e\u65f6\u95f4\u6b65\u957f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">robot = Robot()<\/p>\n<p>timestep = int(robot.getBasicTimeStep())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u8bbe\u5907<\/strong>\uff1a\u6839\u636e\u4f60\u7684\u673a\u5668\u4eba\u914d\u7f6e\uff0c\u83b7\u53d6\u6240\u9700\u7684\u8bbe\u5907\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u8f6e\u5b50\u548c\u4f20\u611f\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">left_motor = robot.getDevice(&#39;left wheel motor&#39;)<\/p>\n<p>right_motor = robot.getDevice(&#39;right wheel motor&#39;)<\/p>\n<p>left_motor.setPosition(float(&#39;inf&#39;))<\/p>\n<p>right_motor.setPosition(float(&#39;inf&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u4e3b\u5faa\u73af<\/strong>\uff1a\u7f16\u5199\u63a7\u5236\u5668\u7684\u4e3b\u5faa\u73af\uff0c\u5904\u7406\u4f20\u611f\u5668\u6570\u636e\u5e76\u63a7\u5236\u673a\u5668\u4eba\u884c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while robot.step(timestep) != -1:<\/p>\n<p>    # \u83b7\u53d6\u4f20\u611f\u5668\u6570\u636e<\/p>\n<p>    # \u63a7\u5236\u673a\u5668\u4eba\u884c\u4e3a<\/p>\n<p>    left_motor.setVelocity(1.0)<\/p>\n<p>    right_motor.setVelocity(1.0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u63a7\u5236\u5668<\/strong>\uff1a\u5728Webots\u4e2d\uff0c\u9009\u62e9\u4f60\u7684Python\u63a7\u5236\u5668\u5e76\u8fd0\u884c\u4eff\u771f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u8c03\u8bd5\u548c\u4f18\u5316<\/h3>\n<\/p>\n<ol>\n<li><strong>\u8c03\u8bd5Python\u63a7\u5236\u5668<\/strong>\uff1a\u5728\u7f16\u5199\u548c\u8fd0\u884c\u63a7\u5236\u5668\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u9519\u8bef\u3002\u4f7f\u7528Python\u7684\u8c03\u8bd5\u5de5\u5177\uff08\u5982<code>print<\/code>\u8bed\u53e5\u6216<code>pdb<\/code>\uff09\u6765\u8c03\u8bd5\u548c\u4fee\u590d\u8fd9\u4e9b\u9519\u8bef\u3002<\/li>\n<li><strong>\u4f18\u5316\u6027\u80fd<\/strong>\uff1a\u6839\u636e\u673a\u5668\u4eba\u884c\u4e3a\u548c\u4eff\u771f\u9700\u6c42\uff0c\u4f18\u5316\u63a7\u5236\u5668\u4ee3\u7801\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3001\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u7b49\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u63a7\u5236\u5668\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u63a7\u5236\u4e00\u4e2aWebots\u673a\u5668\u4eba\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from controller import Robot<\/p>\n<h2><strong>\u521b\u5efa\u673a\u5668\u4eba\u5b9e\u4f8b<\/strong><\/h2>\n<p>robot = Robot()<\/p>\n<p>timestep = int(robot.getBasicTimeStep())<\/p>\n<h2><strong>\u83b7\u53d6\u8bbe\u5907<\/strong><\/h2>\n<p>left_motor = robot.getDevice(&#39;left wheel motor&#39;)<\/p>\n<p>right_motor = robot.getDevice(&#39;right wheel motor&#39;)<\/p>\n<p>left_motor.setPosition(float(&#39;inf&#39;))<\/p>\n<p>right_motor.setPosition(float(&#39;inf&#39;))<\/p>\n<h2><strong>\u8bbe\u7f6e\u521d\u59cb\u901f\u5ea6<\/strong><\/h2>\n<p>left_motor.setVelocity(0.0)<\/p>\n<p>right_motor.setVelocity(0.0)<\/p>\n<h2><strong>\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>while robot.step(timestep) != -1:<\/p>\n<p>    # \u83b7\u53d6\u4f20\u611f\u5668\u6570\u636e<\/p>\n<p>    # \u63a7\u5236\u673a\u5668\u4eba\u884c\u4e3a<\/p>\n<p>    left_motor.setVelocity(1.0)<\/p>\n<p>    right_motor.setVelocity(1.0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u6210\u529f\u5730\u5c06Webots\u8bbe\u7f6e\u4e3aPython\u7f16\u7a0b\u73af\u5883\uff0c\u5e76\u7f16\u5199\u81ea\u5df1\u7684Python\u63a7\u5236\u5668\u6765\u63a7\u5236\u673a\u5668\u4eba\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u548c\u4f18\u5316\u63a7\u5236\u5668\u4ee3\u7801\uff0c\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u548c\u667a\u80fd\u7684\u673a\u5668\u4eba\u884c\u4e3a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Webots\u4e2d\u5b89\u88c5\u548c\u914d\u7f6ePython\uff1f<\/strong><br \/>\u5728Webots\u4e2d\u4f7f\u7528Python\u8fdb\u884c\u4eff\u771f\u9700\u8981\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548c\u76f8\u5173\u7684Webots\u63d2\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7Webots\u7684\u8bbe\u7f6e\u83dc\u5355\u9009\u62e9Python\u4f5c\u4e3a\u9ed8\u8ba4\u8bed\u8a00\u3002\u786e\u4fdd\u5728Webots\u7684\u914d\u7f6e\u4e2d\u6b63\u786e\u6307\u5411Python\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u8fd9\u6837\u5c31\u80fd\u5728\u521b\u5efa\u65b0\u9879\u76ee\u65f6\u76f4\u63a5\u9009\u62e9Python\u4f5c\u4e3a\u7f16\u7a0b\u8bed\u8a00\u3002<\/p>\n<p><strong>Webots\u652f\u6301\u54ea\u4e9b\u7248\u672c\u7684Python\uff1f<\/strong><br \/>Webots\u901a\u5e38\u652f\u6301\u591a\u79cd\u7248\u672c\u7684Python\uff0c\u5c24\u5176\u662fPython 3.x\u7cfb\u5217\u3002\u4e3a\u4e86\u786e\u4fdd\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u4f7f\u7528\u6700\u65b0\u7684\u7a33\u5b9a\u7248\u672c\uff0c\u5e76\u786e\u4fdd\u5df2\u5b89\u88c5\u4e0eWebots\u76f8\u5339\u914d\u7684Python\u5e93\u3002\u4f60\u53ef\u4ee5\u5728Webots\u7684\u5b98\u65b9\u6587\u6863\u4e2d\u627e\u5230\u63a8\u8350\u7684Python\u7248\u672c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7f16\u5199Webots\u4eff\u771f\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u7f16\u7801\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884cWebots\u4eff\u771f\u65f6\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u9519\u8bef\u5305\u62ec\u8bed\u6cd5\u9519\u8bef\u3001\u5e93\u5bfc\u5165\u9519\u8bef\u3001\u4ee5\u53ca\u5bf9API\u8c03\u7528\u7684\u8bef\u7528\u3002\u786e\u4fdd\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u4ed4\u7ec6\u68c0\u67e5\u8bed\u6cd5\uff0c\u7279\u522b\u662f\u7f29\u8fdb\u548c\u5192\u53f7\u7684\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u67e5\u770bWebots\u7684\u6587\u6863\u4ee5\u786e\u8ba4\u6240\u8c03\u7528\u7684API\u662f\u5426\u6b63\u786e\u65e0\u8bef\uff0c\u907f\u514d\u4f7f\u7528\u8fc7\u65f6\u6216\u4e0d\u9002\u7528\u7684\u51fd\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Webots\u8bbe\u7f6e\u4e3aPython\u7f16\u7a0b\u73af\u5883\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u6240\u9700\u7684Python\u5e93\u3001\u914d\u7f6eWebots\u548c\u521b\u5efaPytho [&hellip;]","protected":false},"author":3,"featured_media":1073368,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073365"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1073365"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073365\/revisions"}],"predecessor-version":[{"id":1073371,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073365\/revisions\/1073371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073368"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}