From c5eb7ce4da78a964fb53a72c6146e58dc69b2b11 Mon Sep 17 00:00:00 2001 From: Xiao Gao Date: Tue, 10 Dec 2024 20:55:49 +0100 Subject: [PATCH] update readme --- descriptions/v2/hand_6_fingers_v2.xml | 3 +- descriptions/v2/hand_v2.xml | 6 +- descriptions/v2/hand_v2_bodies.xml | 220 +++++------ gait_test/GA_crawling_full_test_new.py | 2 +- gait_test/original_hand.xml | 183 ++++++++++ kinematics/reachability_map.ipynb | 362 +++---------------- motion_planning/crawl_and_grasp_test04.ipynb | 270 ++++++++++++++ readme.md | 52 ++- 8 files changed, 644 insertions(+), 454 deletions(-) create mode 100644 gait_test/original_hand.xml create mode 100644 motion_planning/crawl_and_grasp_test04.ipynb diff --git a/descriptions/v2/hand_6_fingers_v2.xml b/descriptions/v2/hand_6_fingers_v2.xml index d7cdef4..e362f63 100644 --- a/descriptions/v2/hand_6_fingers_v2.xml +++ b/descriptions/v2/hand_6_fingers_v2.xml @@ -1,5 +1,5 @@ - + @@ -19,7 +19,6 @@ - diff --git a/descriptions/v2/hand_v2.xml b/descriptions/v2/hand_v2.xml index 2fd0096..79cded7 100644 --- a/descriptions/v2/hand_v2.xml +++ b/descriptions/v2/hand_v2.xml @@ -1,5 +1,5 @@ - + @@ -93,8 +93,8 @@ - - + + diff --git a/descriptions/v2/hand_v2_bodies.xml b/descriptions/v2/hand_v2_bodies.xml index af9da41..adc6386 100644 --- a/descriptions/v2/hand_v2_bodies.xml +++ b/descriptions/v2/hand_v2_bodies.xml @@ -1,27 +1,27 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + @@ -29,28 +29,28 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + @@ -58,28 +58,28 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + @@ -87,28 +87,28 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + @@ -116,28 +116,28 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + diff --git a/gait_test/GA_crawling_full_test_new.py b/gait_test/GA_crawling_full_test_new.py index 546f588..d8983dd 100644 --- a/gait_test/GA_crawling_full_test_new.py +++ b/gait_test/GA_crawling_full_test_new.py @@ -18,7 +18,7 @@ # obj_nums = 1 pinch_one_only = 0 -for i in [5000]: +for i in [1000, 2000, 3000, 4000, 5000]: obj_nums = int(i / 1000) if obj_nums >= 4: pinch_one_only = 1 diff --git a/gait_test/original_hand.xml b/gait_test/original_hand.xml new file mode 100644 index 0000000..55a416d --- /dev/null +++ b/gait_test/original_hand.xml @@ -0,0 +1,183 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/kinematics/reachability_map.ipynb b/kinematics/reachability_map.ipynb index f082f04..f4c1249 100644 --- a/kinematics/reachability_map.ipynb +++ b/kinematics/reachability_map.ipynb @@ -2,14 +2,10 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "initial_id", "metadata": { - "collapsed": true, - "ExecuteTime": { - "end_time": "2024-11-14T18:36:52.230832014Z", - "start_time": "2024-11-14T18:36:51.145074452Z" - } + "collapsed": true }, "outputs": [], "source": [ @@ -19,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "outputs": [], "source": [ "xml_path = '../descriptions/2_finger_hand_ssss_v2.xml'\n", @@ -27,17 +23,13 @@ "\n" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:36:52.324811729Z", - "start_time": "2024-11-14T18:36:52.232512871Z" - } + "collapsed": false }, "id": "da1dc354739c9e32" }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "outputs": [], "source": [ "finger_joint_limit = np.array([[-np.pi / 2, -np.pi / 2, -np.pi / 2, -np.pi / 2],\n", @@ -55,67 +47,37 @@ " end_only=False)" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:55:27.514519268Z", - "start_time": "2024-11-14T18:55:27.482244300Z" - } + "collapsed": false }, "id": "bf26562036566e4c" }, { "cell_type": "code", - "execution_count": 4, - "outputs": [ - { - "data": { - "text/plain": "dict_keys(['hand', 'finger_1', 'MCP_spread_motor_1', 'MCP_motor_1', 'PIP_motor_1', 'DIP_motor_1', 'finger_1_site_1', 'finger_1_tip', 'finger_1_site_2', 'finger_1_site_3', 'finger_1_site_4', 'finger_2', 'MCP_spread_motor_2', 'MCP_motor_2', 'PIP_motor_2', 'DIP_motor_2', 'finger_2_site_1', 'finger_2_tip', 'finger_2_site_2', 'finger_2_site_3', 'finger_2_site_4'])" - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "outputs": [], "source": [ "ret0.keys()" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:36:52.352472536Z", - "start_time": "2024-11-14T18:36:52.349200971Z" - } + "collapsed": false }, "id": "e086b479586efa71" }, { "cell_type": "code", - "execution_count": 5, - "outputs": [ - { - "data": { - "text/plain": "tensor([[[1.0000, 0.0000, 0.0000, 0.0000],\n [0.0000, 1.0000, 0.0000, 0.0725],\n [0.0000, 0.0000, 1.0000, 0.0140],\n [0.0000, 0.0000, 0.0000, 1.0000]]])" - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "outputs": [], "source": [ "ret0['finger_1_site_4'].get_matrix()" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:36:52.375815860Z", - "start_time": "2024-11-14T18:36:52.353510878Z" - } + "collapsed": false }, "id": "c84b3d32a0999d4f" }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "outputs": [], "source": [ "color = np.array([[[254,240,217],\n", @@ -132,116 +94,50 @@ " [4,90,141]]]) / 255" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T19:01:06.532255985Z", - "start_time": "2024-11-14T19:01:06.530027805Z" - } + "collapsed": false }, "id": "8750e45dd6c79bb1" }, { "cell_type": "code", - "execution_count": 28, - "outputs": [ - { - "data": { - "text/plain": "(2, 6, 3)" - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "outputs": [], "source": [ "color.shape" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T19:01:29.578088172Z", - "start_time": "2024-11-14T19:01:29.529882554Z" - } + "collapsed": false }, "id": "ac2c0d23a9675693" }, { "cell_type": "code", - "execution_count": 33, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.45490196 0.6627451 0.81176471]\n" - ] - } - ], + "execution_count": null, + "outputs": [], "source": [ "print(color[1,3,:])" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T19:02:46.855340797Z", - "start_time": "2024-11-14T19:02:46.846320388Z" - } + "collapsed": false }, "id": "dc38b3985d927ad6" }, { "cell_type": "code", - "execution_count": 41, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.65098039 0.74117647 0.85882353]\n" - ] - } - ], + "execution_count": null, + "outputs": [], "source": [ "print(np.array([166,189,219])/255)" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T19:13:38.493352232Z", - "start_time": "2024-11-14T19:13:38.449865594Z" - } + "collapsed": false }, "id": "cb378e92c7127ff0" }, { "cell_type": "code", - "execution_count": 8, - "outputs": [ - { - "data": { - "text/plain": "", - "application/javascript": "/* Put everything inside the global mpl namespace */\n/* global mpl */\nwindow.mpl = {};\n\nmpl.get_websocket_type = function () {\n if (typeof WebSocket !== 'undefined') {\n return WebSocket;\n } else if (typeof MozWebSocket !== 'undefined') {\n return MozWebSocket;\n } else {\n alert(\n 'Your browser does not have WebSocket support. ' +\n 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n 'Firefox 4 and 5 are also supported but you ' +\n 'have to enable WebSockets in about:config.'\n );\n }\n};\n\nmpl.figure = function (figure_id, websocket, ondownload, parent_element) {\n this.id = figure_id;\n\n this.ws = websocket;\n\n this.supports_binary = this.ws.binaryType !== undefined;\n\n if (!this.supports_binary) {\n var warnings = document.getElementById('mpl-warnings');\n if (warnings) {\n warnings.style.display = 'block';\n warnings.textContent =\n 'This browser does not support binary websocket messages. ' +\n 'Performance may be slow.';\n }\n }\n\n this.imageObj = new Image();\n\n this.context = undefined;\n this.message = undefined;\n this.canvas = undefined;\n this.rubberband_canvas = undefined;\n this.rubberband_context = undefined;\n this.format_dropdown = undefined;\n\n this.image_mode = 'full';\n\n this.root = document.createElement('div');\n this.root.setAttribute('style', 'display: inline-block');\n this._root_extra_style(this.root);\n\n parent_element.appendChild(this.root);\n\n this._init_header(this);\n this._init_canvas(this);\n this._init_toolbar(this);\n\n var fig = this;\n\n this.waiting = false;\n\n this.ws.onopen = function () {\n fig.send_message('supports_binary', { value: fig.supports_binary });\n fig.send_message('send_image_mode', {});\n if (fig.ratio !== 1) {\n fig.send_message('set_device_pixel_ratio', {\n device_pixel_ratio: fig.ratio,\n });\n }\n fig.send_message('refresh', {});\n };\n\n this.imageObj.onload = function () {\n if (fig.image_mode === 'full') {\n // Full images could contain transparency (where diff images\n // almost always do), so we need to clear the canvas so that\n // there is no ghosting.\n fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n }\n fig.context.drawImage(fig.imageObj, 0, 0);\n };\n\n this.imageObj.onunload = function () {\n fig.ws.close();\n };\n\n this.ws.onmessage = this._make_on_message_function(this);\n\n this.ondownload = ondownload;\n};\n\nmpl.figure.prototype._init_header = function () {\n var titlebar = document.createElement('div');\n titlebar.classList =\n 'ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix';\n var titletext = document.createElement('div');\n titletext.classList = 'ui-dialog-title';\n titletext.setAttribute(\n 'style',\n 'width: 100%; text-align: center; padding: 3px;'\n );\n titlebar.appendChild(titletext);\n this.root.appendChild(titlebar);\n this.header = titletext;\n};\n\nmpl.figure.prototype._canvas_extra_style = function (_canvas_div) {};\n\nmpl.figure.prototype._root_extra_style = function (_canvas_div) {};\n\nmpl.figure.prototype._init_canvas = function () {\n var fig = this;\n\n var canvas_div = (this.canvas_div = document.createElement('div'));\n canvas_div.setAttribute('tabindex', '0');\n canvas_div.setAttribute(\n 'style',\n 'border: 1px solid #ddd;' +\n 'box-sizing: content-box;' +\n 'clear: both;' +\n 'min-height: 1px;' +\n 'min-width: 1px;' +\n 'outline: 0;' +\n 'overflow: hidden;' +\n 'position: relative;' +\n 'resize: both;' +\n 'z-index: 2;'\n );\n\n function on_keyboard_event_closure(name) {\n return function (event) {\n return fig.key_event(event, name);\n };\n }\n\n canvas_div.addEventListener(\n 'keydown',\n on_keyboard_event_closure('key_press')\n );\n canvas_div.addEventListener(\n 'keyup',\n on_keyboard_event_closure('key_release')\n );\n\n this._canvas_extra_style(canvas_div);\n this.root.appendChild(canvas_div);\n\n var canvas = (this.canvas = document.createElement('canvas'));\n canvas.classList.add('mpl-canvas');\n canvas.setAttribute(\n 'style',\n 'box-sizing: content-box;' +\n 'pointer-events: none;' +\n 'position: relative;' +\n 'z-index: 0;'\n );\n\n this.context = canvas.getContext('2d');\n\n var backingStore =\n this.context.backingStorePixelRatio ||\n this.context.webkitBackingStorePixelRatio ||\n this.context.mozBackingStorePixelRatio ||\n this.context.msBackingStorePixelRatio ||\n this.context.oBackingStorePixelRatio ||\n this.context.backingStorePixelRatio ||\n 1;\n\n this.ratio = (window.devicePixelRatio || 1) / backingStore;\n\n var rubberband_canvas = (this.rubberband_canvas = document.createElement(\n 'canvas'\n ));\n rubberband_canvas.setAttribute(\n 'style',\n 'box-sizing: content-box;' +\n 'left: 0;' +\n 'pointer-events: none;' +\n 'position: absolute;' +\n 'top: 0;' +\n 'z-index: 1;'\n );\n\n // Apply a ponyfill if ResizeObserver is not implemented by browser.\n if (this.ResizeObserver === undefined) {\n if (window.ResizeObserver !== undefined) {\n this.ResizeObserver = window.ResizeObserver;\n } else {\n var obs = _JSXTOOLS_RESIZE_OBSERVER({});\n this.ResizeObserver = obs.ResizeObserver;\n }\n }\n\n this.resizeObserverInstance = new this.ResizeObserver(function (entries) {\n var nentries = entries.length;\n for (var i = 0; i < nentries; i++) {\n var entry = entries[i];\n var width, height;\n if (entry.contentBoxSize) {\n if (entry.contentBoxSize instanceof Array) {\n // Chrome 84 implements new version of spec.\n width = entry.contentBoxSize[0].inlineSize;\n height = entry.contentBoxSize[0].blockSize;\n } else {\n // Firefox implements old version of spec.\n width = entry.contentBoxSize.inlineSize;\n height = entry.contentBoxSize.blockSize;\n }\n } else {\n // Chrome <84 implements even older version of spec.\n width = entry.contentRect.width;\n height = entry.contentRect.height;\n }\n\n // Keep the size of the canvas and rubber band canvas in sync with\n // the canvas container.\n if (entry.devicePixelContentBoxSize) {\n // Chrome 84 implements new version of spec.\n canvas.setAttribute(\n 'width',\n entry.devicePixelContentBoxSize[0].inlineSize\n );\n canvas.setAttribute(\n 'height',\n entry.devicePixelContentBoxSize[0].blockSize\n );\n } else {\n canvas.setAttribute('width', width * fig.ratio);\n canvas.setAttribute('height', height * fig.ratio);\n }\n /* This rescales the canvas back to display pixels, so that it\n * appears correct on HiDPI screens. */\n canvas.style.width = width + 'px';\n canvas.style.height = height + 'px';\n\n rubberband_canvas.setAttribute('width', width);\n rubberband_canvas.setAttribute('height', height);\n\n // And update the size in Python. We ignore the initial 0/0 size\n // that occurs as the element is placed into the DOM, which should\n // otherwise not happen due to the minimum size styling.\n if (fig.ws.readyState == 1 && width != 0 && height != 0) {\n fig.request_resize(width, height);\n }\n }\n });\n this.resizeObserverInstance.observe(canvas_div);\n\n function on_mouse_event_closure(name) {\n /* User Agent sniffing is bad, but WebKit is busted:\n * https://bugs.webkit.org/show_bug.cgi?id=144526\n * https://bugs.webkit.org/show_bug.cgi?id=181818\n * The worst that happens here is that they get an extra browser\n * selection when dragging, if this check fails to catch them.\n */\n var UA = navigator.userAgent;\n var isWebKit = /AppleWebKit/.test(UA) && !/Chrome/.test(UA);\n if(isWebKit) {\n return function (event) {\n /* This prevents the web browser from automatically changing to\n * the text insertion cursor when the button is pressed. We\n * want to control all of the cursor setting manually through\n * the 'cursor' event from matplotlib */\n event.preventDefault()\n return fig.mouse_event(event, name);\n };\n } else {\n return function (event) {\n return fig.mouse_event(event, name);\n };\n }\n }\n\n canvas_div.addEventListener(\n 'mousedown',\n on_mouse_event_closure('button_press')\n );\n canvas_div.addEventListener(\n 'mouseup',\n on_mouse_event_closure('button_release')\n );\n canvas_div.addEventListener(\n 'dblclick',\n on_mouse_event_closure('dblclick')\n );\n // Throttle sequential mouse events to 1 every 20ms.\n canvas_div.addEventListener(\n 'mousemove',\n on_mouse_event_closure('motion_notify')\n );\n\n canvas_div.addEventListener(\n 'mouseenter',\n on_mouse_event_closure('figure_enter')\n );\n canvas_div.addEventListener(\n 'mouseleave',\n on_mouse_event_closure('figure_leave')\n );\n\n canvas_div.addEventListener('wheel', function (event) {\n if (event.deltaY < 0) {\n event.step = 1;\n } else {\n event.step = -1;\n }\n on_mouse_event_closure('scroll')(event);\n });\n\n canvas_div.appendChild(canvas);\n canvas_div.appendChild(rubberband_canvas);\n\n this.rubberband_context = rubberband_canvas.getContext('2d');\n this.rubberband_context.strokeStyle = '#000000';\n\n this._resize_canvas = function (width, height, forward) {\n if (forward) {\n canvas_div.style.width = width + 'px';\n canvas_div.style.height = height + 'px';\n }\n };\n\n // Disable right mouse context menu.\n canvas_div.addEventListener('contextmenu', function (_e) {\n event.preventDefault();\n return false;\n });\n\n function set_focus() {\n canvas.focus();\n canvas_div.focus();\n }\n\n window.setTimeout(set_focus, 100);\n};\n\nmpl.figure.prototype._init_toolbar = function () {\n var fig = this;\n\n var toolbar = document.createElement('div');\n toolbar.classList = 'mpl-toolbar';\n this.root.appendChild(toolbar);\n\n function on_click_closure(name) {\n return function (_event) {\n return fig.toolbar_button_onclick(name);\n };\n }\n\n function on_mouseover_closure(tooltip) {\n return function (event) {\n if (!event.currentTarget.disabled) {\n return fig.toolbar_button_onmouseover(tooltip);\n }\n };\n }\n\n fig.buttons = {};\n var buttonGroup = document.createElement('div');\n buttonGroup.classList = 'mpl-button-group';\n for (var toolbar_ind in mpl.toolbar_items) {\n var name = mpl.toolbar_items[toolbar_ind][0];\n var tooltip = mpl.toolbar_items[toolbar_ind][1];\n var image = mpl.toolbar_items[toolbar_ind][2];\n var method_name = mpl.toolbar_items[toolbar_ind][3];\n\n if (!name) {\n /* Instead of a spacer, we start a new button group. */\n if (buttonGroup.hasChildNodes()) {\n toolbar.appendChild(buttonGroup);\n }\n buttonGroup = document.createElement('div');\n buttonGroup.classList = 'mpl-button-group';\n continue;\n }\n\n var button = (fig.buttons[name] = document.createElement('button'));\n button.classList = 'mpl-widget';\n button.setAttribute('role', 'button');\n button.setAttribute('aria-disabled', 'false');\n button.addEventListener('click', on_click_closure(method_name));\n button.addEventListener('mouseover', on_mouseover_closure(tooltip));\n\n var icon_img = document.createElement('img');\n icon_img.src = '_images/' + image + '.png';\n icon_img.srcset = '_images/' + image + '_large.png 2x';\n icon_img.alt = tooltip;\n button.appendChild(icon_img);\n\n buttonGroup.appendChild(button);\n }\n\n if (buttonGroup.hasChildNodes()) {\n toolbar.appendChild(buttonGroup);\n }\n\n var fmt_picker = document.createElement('select');\n fmt_picker.classList = 'mpl-widget';\n toolbar.appendChild(fmt_picker);\n this.format_dropdown = fmt_picker;\n\n for (var ind in mpl.extensions) {\n var fmt = mpl.extensions[ind];\n var option = document.createElement('option');\n option.selected = fmt === mpl.default_extension;\n option.innerHTML = fmt;\n fmt_picker.appendChild(option);\n }\n\n var status_bar = document.createElement('span');\n status_bar.classList = 'mpl-message';\n toolbar.appendChild(status_bar);\n this.message = status_bar;\n};\n\nmpl.figure.prototype.request_resize = function (x_pixels, y_pixels) {\n // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n // which will in turn request a refresh of the image.\n this.send_message('resize', { width: x_pixels, height: y_pixels });\n};\n\nmpl.figure.prototype.send_message = function (type, properties) {\n properties['type'] = type;\n properties['figure_id'] = this.id;\n this.ws.send(JSON.stringify(properties));\n};\n\nmpl.figure.prototype.send_draw_message = function () {\n if (!this.waiting) {\n this.waiting = true;\n this.ws.send(JSON.stringify({ type: 'draw', figure_id: this.id }));\n }\n};\n\nmpl.figure.prototype.handle_save = function (fig, _msg) {\n var format_dropdown = fig.format_dropdown;\n var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n fig.ondownload(fig, format);\n};\n\nmpl.figure.prototype.handle_resize = function (fig, msg) {\n var size = msg['size'];\n if (size[0] !== fig.canvas.width || size[1] !== fig.canvas.height) {\n fig._resize_canvas(size[0], size[1], msg['forward']);\n fig.send_message('refresh', {});\n }\n};\n\nmpl.figure.prototype.handle_rubberband = function (fig, msg) {\n var x0 = msg['x0'] / fig.ratio;\n var y0 = (fig.canvas.height - msg['y0']) / fig.ratio;\n var x1 = msg['x1'] / fig.ratio;\n var y1 = (fig.canvas.height - msg['y1']) / fig.ratio;\n x0 = Math.floor(x0) + 0.5;\n y0 = Math.floor(y0) + 0.5;\n x1 = Math.floor(x1) + 0.5;\n y1 = Math.floor(y1) + 0.5;\n var min_x = Math.min(x0, x1);\n var min_y = Math.min(y0, y1);\n var width = Math.abs(x1 - x0);\n var height = Math.abs(y1 - y0);\n\n fig.rubberband_context.clearRect(\n 0,\n 0,\n fig.canvas.width / fig.ratio,\n fig.canvas.height / fig.ratio\n );\n\n fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n};\n\nmpl.figure.prototype.handle_figure_label = function (fig, msg) {\n // Updates the figure title.\n fig.header.textContent = msg['label'];\n};\n\nmpl.figure.prototype.handle_cursor = function (fig, msg) {\n fig.canvas_div.style.cursor = msg['cursor'];\n};\n\nmpl.figure.prototype.handle_message = function (fig, msg) {\n fig.message.textContent = msg['message'];\n};\n\nmpl.figure.prototype.handle_draw = function (fig, _msg) {\n // Request the server to send over a new figure.\n fig.send_draw_message();\n};\n\nmpl.figure.prototype.handle_image_mode = function (fig, msg) {\n fig.image_mode = msg['mode'];\n};\n\nmpl.figure.prototype.handle_history_buttons = function (fig, msg) {\n for (var key in msg) {\n if (!(key in fig.buttons)) {\n continue;\n }\n fig.buttons[key].disabled = !msg[key];\n fig.buttons[key].setAttribute('aria-disabled', !msg[key]);\n }\n};\n\nmpl.figure.prototype.handle_navigate_mode = function (fig, msg) {\n if (msg['mode'] === 'PAN') {\n fig.buttons['Pan'].classList.add('active');\n fig.buttons['Zoom'].classList.remove('active');\n } else if (msg['mode'] === 'ZOOM') {\n fig.buttons['Pan'].classList.remove('active');\n fig.buttons['Zoom'].classList.add('active');\n } else {\n fig.buttons['Pan'].classList.remove('active');\n fig.buttons['Zoom'].classList.remove('active');\n }\n};\n\nmpl.figure.prototype.updated_canvas_event = function () {\n // Called whenever the canvas gets updated.\n this.send_message('ack', {});\n};\n\n// A function to construct a web socket function for onmessage handling.\n// Called in the figure constructor.\nmpl.figure.prototype._make_on_message_function = function (fig) {\n return function socket_on_message(evt) {\n if (evt.data instanceof Blob) {\n var img = evt.data;\n if (img.type !== 'image/png') {\n /* FIXME: We get \"Resource interpreted as Image but\n * transferred with MIME type text/plain:\" errors on\n * Chrome. But how to set the MIME type? It doesn't seem\n * to be part of the websocket stream */\n img.type = 'image/png';\n }\n\n /* Free the memory for the previous frames */\n if (fig.imageObj.src) {\n (window.URL || window.webkitURL).revokeObjectURL(\n fig.imageObj.src\n );\n }\n\n fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n img\n );\n fig.updated_canvas_event();\n fig.waiting = false;\n return;\n } else if (\n typeof evt.data === 'string' &&\n evt.data.slice(0, 21) === 'data:image/png;base64'\n ) {\n fig.imageObj.src = evt.data;\n fig.updated_canvas_event();\n fig.waiting = false;\n return;\n }\n\n var msg = JSON.parse(evt.data);\n var msg_type = msg['type'];\n\n // Call the \"handle_{type}\" callback, which takes\n // the figure and JSON message as its only arguments.\n try {\n var callback = fig['handle_' + msg_type];\n } catch (e) {\n console.log(\n \"No handler for the '\" + msg_type + \"' message type: \",\n msg\n );\n return;\n }\n\n if (callback) {\n try {\n // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n callback(fig, msg);\n } catch (e) {\n console.log(\n \"Exception inside the 'handler_\" + msg_type + \"' callback:\",\n e,\n e.stack,\n msg\n );\n }\n }\n };\n};\n\nfunction getModifiers(event) {\n var mods = [];\n if (event.ctrlKey) {\n mods.push('ctrl');\n }\n if (event.altKey) {\n mods.push('alt');\n }\n if (event.shiftKey) {\n mods.push('shift');\n }\n if (event.metaKey) {\n mods.push('meta');\n }\n return mods;\n}\n\n/*\n * return a copy of an object with only non-object keys\n * we need this to avoid circular references\n * https://stackoverflow.com/a/24161582/3208463\n */\nfunction simpleKeys(original) {\n return Object.keys(original).reduce(function (obj, key) {\n if (typeof original[key] !== 'object') {\n obj[key] = original[key];\n }\n return obj;\n }, {});\n}\n\nmpl.figure.prototype.mouse_event = function (event, name) {\n if (name === 'button_press') {\n this.canvas.focus();\n this.canvas_div.focus();\n }\n\n // from https://stackoverflow.com/q/1114465\n var boundingRect = this.canvas.getBoundingClientRect();\n var x = (event.clientX - boundingRect.left) * this.ratio;\n var y = (event.clientY - boundingRect.top) * this.ratio;\n\n this.send_message(name, {\n x: x,\n y: y,\n button: event.button,\n step: event.step,\n modifiers: getModifiers(event),\n guiEvent: simpleKeys(event),\n });\n\n return false;\n};\n\nmpl.figure.prototype._key_event_extra = function (_event, _name) {\n // Handle any extra behaviour associated with a key event\n};\n\nmpl.figure.prototype.key_event = function (event, name) {\n // Prevent repeat events\n if (name === 'key_press') {\n if (event.key === this._key) {\n return;\n } else {\n this._key = event.key;\n }\n }\n if (name === 'key_release') {\n this._key = null;\n }\n\n var value = '';\n if (event.ctrlKey && event.key !== 'Control') {\n value += 'ctrl+';\n }\n else if (event.altKey && event.key !== 'Alt') {\n value += 'alt+';\n }\n else if (event.shiftKey && event.key !== 'Shift') {\n value += 'shift+';\n }\n\n value += 'k' + event.key;\n\n this._key_event_extra(event, name);\n\n this.send_message(name, { key: value, guiEvent: simpleKeys(event) });\n return false;\n};\n\nmpl.figure.prototype.toolbar_button_onclick = function (name) {\n if (name === 'download') {\n this.handle_save(this, null);\n } else {\n this.send_message('toolbar_button', { name: name });\n }\n};\n\nmpl.figure.prototype.toolbar_button_onmouseover = function (tooltip) {\n this.message.textContent = tooltip;\n};\n\n///////////////// REMAINING CONTENT GENERATED BY embed_js.py /////////////////\n// prettier-ignore\nvar _JSXTOOLS_RESIZE_OBSERVER=function(A){var t,i=new WeakMap,n=new WeakMap,a=new WeakMap,r=new WeakMap,o=new Set;function s(e){if(!(this instanceof s))throw new TypeError(\"Constructor requires 'new' operator\");i.set(this,e)}function h(){throw new TypeError(\"Function is not a constructor\")}function c(e,t,i,n){e=0 in arguments?Number(arguments[0]):0,t=1 in arguments?Number(arguments[1]):0,i=2 in arguments?Number(arguments[2]):0,n=3 in arguments?Number(arguments[3]):0,this.right=(this.x=this.left=e)+(this.width=i),this.bottom=(this.y=this.top=t)+(this.height=n),Object.freeze(this)}function d(){t=requestAnimationFrame(d);var s=new WeakMap,p=new Set;o.forEach((function(t){r.get(t).forEach((function(i){var r=t instanceof window.SVGElement,o=a.get(t),d=r?0:parseFloat(o.paddingTop),f=r?0:parseFloat(o.paddingRight),l=r?0:parseFloat(o.paddingBottom),u=r?0:parseFloat(o.paddingLeft),g=r?0:parseFloat(o.borderTopWidth),m=r?0:parseFloat(o.borderRightWidth),w=r?0:parseFloat(o.borderBottomWidth),b=u+f,F=d+l,v=(r?0:parseFloat(o.borderLeftWidth))+m,W=g+w,y=r?0:t.offsetHeight-W-t.clientHeight,E=r?0:t.offsetWidth-v-t.clientWidth,R=b+v,z=F+W,M=r?t.width:parseFloat(o.width)-R-E,O=r?t.height:parseFloat(o.height)-z-y;if(n.has(t)){var k=n.get(t);if(k[0]===M&&k[1]===O)return}n.set(t,[M,O]);var S=Object.create(h.prototype);S.target=t,S.contentRect=new c(u,d,M,O),s.has(i)||(s.set(i,[]),p.add(i)),s.get(i).push(S)}))})),p.forEach((function(e){i.get(e).call(e,s.get(e),e)}))}return s.prototype.observe=function(i){if(i instanceof window.Element){r.has(i)||(r.set(i,new Set),o.add(i),a.set(i,window.getComputedStyle(i)));var n=r.get(i);n.has(this)||n.add(this),cancelAnimationFrame(t),t=requestAnimationFrame(d)}},s.prototype.unobserve=function(i){if(i instanceof window.Element&&r.has(i)){var n=r.get(i);n.has(this)&&(n.delete(this),n.size||(r.delete(i),o.delete(i))),n.size||r.delete(i),o.size||cancelAnimationFrame(t)}},A.DOMRectReadOnly=c,A.ResizeObserver=s,A.ResizeObserverEntry=h,A}; // eslint-disable-line\nmpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Left button pans, Right button zooms\\nx/y fixes axis, CTRL fixes aspect\", \"fa fa-arrows\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\\nx/y fixes axis\", \"fa fa-square-o\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o\", \"download\"]];\n\nmpl.extensions = [\"eps\", \"jpeg\", \"pgf\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\", \"webp\"];\n\nmpl.default_extension = \"png\";/* global mpl */\n\nvar comm_websocket_adapter = function (comm) {\n // Create a \"websocket\"-like object which calls the given IPython comm\n // object with the appropriate methods. Currently this is a non binary\n // socket, so there is still some room for performance tuning.\n var ws = {};\n\n ws.binaryType = comm.kernel.ws.binaryType;\n ws.readyState = comm.kernel.ws.readyState;\n function updateReadyState(_event) {\n if (comm.kernel.ws) {\n ws.readyState = comm.kernel.ws.readyState;\n } else {\n ws.readyState = 3; // Closed state.\n }\n }\n comm.kernel.ws.addEventListener('open', updateReadyState);\n comm.kernel.ws.addEventListener('close', updateReadyState);\n comm.kernel.ws.addEventListener('error', updateReadyState);\n\n ws.close = function () {\n comm.close();\n };\n ws.send = function (m) {\n //console.log('sending', m);\n comm.send(m);\n };\n // Register the callback with on_msg.\n comm.on_msg(function (msg) {\n //console.log('receiving', msg['content']['data'], msg);\n var data = msg['content']['data'];\n if (data['blob'] !== undefined) {\n data = {\n data: new Blob(msg['buffers'], { type: data['blob'] }),\n };\n }\n // Pass the mpl event to the overridden (by mpl) onmessage function.\n ws.onmessage(data);\n });\n return ws;\n};\n\nmpl.mpl_figure_comm = function (comm, msg) {\n // This is the function which gets called when the mpl process\n // starts-up an IPython Comm through the \"matplotlib\" channel.\n\n var id = msg.content.data.id;\n // Get hold of the div created by the display call when the Comm\n // socket was opened in Python.\n var element = document.getElementById(id);\n var ws_proxy = comm_websocket_adapter(comm);\n\n function ondownload(figure, _format) {\n window.open(figure.canvas.toDataURL());\n }\n\n var fig = new mpl.figure(id, ws_proxy, ondownload, element);\n\n // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n // web socket which is closed, not our websocket->open comm proxy.\n ws_proxy.onopen();\n\n fig.parent_element = element;\n fig.cell_info = mpl.find_output_cell(\"
\");\n if (!fig.cell_info) {\n console.error('Failed to find cell for figure', id, fig);\n return;\n }\n fig.cell_info[0].output_area.element.on(\n 'cleared',\n { fig: fig },\n fig._remove_fig_handler\n );\n};\n\nmpl.figure.prototype.handle_close = function (fig, msg) {\n var width = fig.canvas.width / fig.ratio;\n fig.cell_info[0].output_area.element.off(\n 'cleared',\n fig._remove_fig_handler\n );\n fig.resizeObserverInstance.unobserve(fig.canvas_div);\n\n // Update the output cell to use the data from the current canvas.\n fig.push_to_output();\n var dataURL = fig.canvas.toDataURL();\n // Re-enable the keyboard manager in IPython - without this line, in FF,\n // the notebook keyboard shortcuts fail.\n IPython.keyboard_manager.enable();\n fig.parent_element.innerHTML =\n '';\n fig.close_ws(fig, msg);\n};\n\nmpl.figure.prototype.close_ws = function (fig, msg) {\n fig.send_message('closing', msg);\n // fig.ws.close()\n};\n\nmpl.figure.prototype.push_to_output = function (_remove_interactive) {\n // Turn the data on the canvas into data in the output cell.\n var width = this.canvas.width / this.ratio;\n var dataURL = this.canvas.toDataURL();\n this.cell_info[1]['text/html'] =\n '';\n};\n\nmpl.figure.prototype.updated_canvas_event = function () {\n // Tell IPython that the notebook contents must change.\n IPython.notebook.set_dirty(true);\n this.send_message('ack', {});\n var fig = this;\n // Wait a second, then push the new image to the DOM so\n // that it is saved nicely (might be nice to debounce this).\n setTimeout(function () {\n fig.push_to_output();\n }, 1000);\n};\n\nmpl.figure.prototype._init_toolbar = function () {\n var fig = this;\n\n var toolbar = document.createElement('div');\n toolbar.classList = 'btn-toolbar';\n this.root.appendChild(toolbar);\n\n function on_click_closure(name) {\n return function (_event) {\n return fig.toolbar_button_onclick(name);\n };\n }\n\n function on_mouseover_closure(tooltip) {\n return function (event) {\n if (!event.currentTarget.disabled) {\n return fig.toolbar_button_onmouseover(tooltip);\n }\n };\n }\n\n fig.buttons = {};\n var buttonGroup = document.createElement('div');\n buttonGroup.classList = 'btn-group';\n var button;\n for (var toolbar_ind in mpl.toolbar_items) {\n var name = mpl.toolbar_items[toolbar_ind][0];\n var tooltip = mpl.toolbar_items[toolbar_ind][1];\n var image = mpl.toolbar_items[toolbar_ind][2];\n var method_name = mpl.toolbar_items[toolbar_ind][3];\n\n if (!name) {\n /* Instead of a spacer, we start a new button group. */\n if (buttonGroup.hasChildNodes()) {\n toolbar.appendChild(buttonGroup);\n }\n buttonGroup = document.createElement('div');\n buttonGroup.classList = 'btn-group';\n continue;\n }\n\n button = fig.buttons[name] = document.createElement('button');\n button.classList = 'btn btn-default';\n button.href = '#';\n button.title = name;\n button.innerHTML = '';\n button.addEventListener('click', on_click_closure(method_name));\n button.addEventListener('mouseover', on_mouseover_closure(tooltip));\n buttonGroup.appendChild(button);\n }\n\n if (buttonGroup.hasChildNodes()) {\n toolbar.appendChild(buttonGroup);\n }\n\n // Add the status bar.\n var status_bar = document.createElement('span');\n status_bar.classList = 'mpl-message pull-right';\n toolbar.appendChild(status_bar);\n this.message = status_bar;\n\n // Add the close button to the window.\n var buttongrp = document.createElement('div');\n buttongrp.classList = 'btn-group inline pull-right';\n button = document.createElement('button');\n button.classList = 'btn btn-mini btn-primary';\n button.href = '#';\n button.title = 'Stop Interaction';\n button.innerHTML = '';\n button.addEventListener('click', function (_evt) {\n fig.handle_close(fig, {});\n });\n button.addEventListener(\n 'mouseover',\n on_mouseover_closure('Stop Interaction')\n );\n buttongrp.appendChild(button);\n var titlebar = this.root.querySelector('.ui-dialog-titlebar');\n titlebar.insertBefore(buttongrp, titlebar.firstChild);\n};\n\nmpl.figure.prototype._remove_fig_handler = function (event) {\n var fig = event.data.fig;\n if (event.target !== this) {\n // Ignore bubbled events from children.\n return;\n }\n fig.close_ws(fig, {});\n};\n\nmpl.figure.prototype._root_extra_style = function (el) {\n el.style.boxSizing = 'content-box'; // override notebook setting of border-box.\n};\n\nmpl.figure.prototype._canvas_extra_style = function (el) {\n // this is important to make the div 'focusable\n el.setAttribute('tabindex', 0);\n // reach out to IPython and tell the keyboard manager to turn it's self\n // off when our div gets focus\n\n // location in version 3\n if (IPython.notebook.keyboard_manager) {\n IPython.notebook.keyboard_manager.register_events(el);\n } else {\n // location in version 2\n IPython.keyboard_manager.register_events(el);\n }\n};\n\nmpl.figure.prototype._key_event_extra = function (event, _name) {\n // Check for shift+enter\n if (event.shiftKey && event.which === 13) {\n this.canvas_div.blur();\n // select the cell after this one\n var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n IPython.notebook.select(index + 1);\n }\n};\n\nmpl.figure.prototype.handle_save = function (fig, _msg) {\n fig.ondownload(fig, null);\n};\n\nmpl.find_output_cell = function (html_output) {\n // Return the cell and output element which can be found *uniquely* in the notebook.\n // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n // IPython event is triggered only after the cells have been serialised, which for\n // our purposes (turning an active figure into a static one), is too late.\n var cells = IPython.notebook.get_cells();\n var ncells = cells.length;\n for (var i = 0; i < ncells; i++) {\n var cell = cells[i];\n if (cell.cell_type === 'code') {\n for (var j = 0; j < cell.output_area.outputs.length; j++) {\n var data = cell.output_area.outputs[j];\n if (data.data) {\n // IPython >= 3 moved mimebundle to data attribute of output\n data = data.data;\n }\n if (data['text/html'] === html_output) {\n return [cell, data, j];\n }\n }\n }\n }\n};\n\n// Register the function which deals with the matplotlib target/channel.\n// The kernel may be null if the page has been refreshed.\nif (IPython.notebook.kernel !== null) {\n IPython.notebook.kernel.comm_manager.register_target(\n 'matplotlib',\n mpl.mpl_figure_comm\n );\n}\n" - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/plain": "", - "text/html": "
" - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/tmp/ipykernel_2968691/3873924012.py:19: UserWarning: *c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*. Please use the *color* keyword-argument or provide a 2D array with a single row if you intend to specify the same RGB or RGBA value for all points.\n", - " ax.scatter(link_position[:, 0], link_position[:, 1], link_position[:, 2], c=tuple(color[iF-1, j, :].flatten()) + (alpha,), s=0.1, marker='o')\n" - ] - } - ], + "execution_count": null, + "outputs": [], "source": [ "%matplotlib notebook\n", "\n", @@ -270,17 +166,13 @@ "fig.savefig('reachability_map_2_fingers.jpg', format='jpg', bbox_inches='tight', pad_inches=0.2, dpi=600)" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:36:53.342799449Z", - "start_time": "2024-11-14T18:36:52.413079264Z" - } + "collapsed": false }, "id": "9d066bc33ab00f54" }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "outputs": [], "source": [ "import xml.etree.ElementTree as ET\n", @@ -301,156 +193,56 @@ "\n" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:36:53.345913223Z", - "start_time": "2024-11-14T18:36:53.344641218Z" - } + "collapsed": false }, "id": "9202e4f723c7503d" }, { "cell_type": "code", - "execution_count": 10, - "outputs": [ - { - "data": { - "text/plain": "[['finger_1_tip',\n 'finger_1_site_1',\n 'finger_1_site_2',\n 'finger_1_site_3',\n 'finger_1_site_4'],\n ['finger_2_tip',\n 'finger_2_site_1',\n 'finger_2_site_2',\n 'finger_2_site_3',\n 'finger_2_site_4']]" - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "outputs": [], "source": [ "link_names" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:36:53.358756022Z", - "start_time": "2024-11-14T18:36:53.346242688Z" - } + "collapsed": false }, "id": "41db27761697eed7" }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "outputs": [], "source": [ "import faiss\n", "def remove_abnormal_points(points):\n", - " # 定义 k 值(KNN 中的邻居数)\n", + " # k from k-NN\n", " k = 5\n", " \n", - " # 使用 Faiss 构建索引\n", - " index = faiss.IndexFlatL2(points.shape[1]) # 使用 L2 距离计算\n", - " index.add(points) # 添加点云数据到索引\n", + " index = faiss.IndexFlatL2(points.shape[1]) \n", + " index.add(points) \n", " \n", - " # 查询每个点的 K 近邻\n", - " distances, indices = index.search(points, k + 1) # 查询自身时最近距离为0,忽略第一个距离\n", + " distances, indices = index.search(points, k + 1) \n", " \n", - " # 计算每个点的平均距离(忽略第一个距离,即自身距离0)\n", " mean_distances = distances[:, 1:].mean(axis=1)\n", " \n", - " # 设置一个阈值,过滤离群点(例如平均距离超过均值加两倍标准差的点)\n", " threshold = np.mean(mean_distances) + 1.4 * np.std(mean_distances)\n", " filtered_points = points[mean_distances < threshold]\n", " \n", - " print(f\"原始点数: {len(points)}\")\n", - " print(f\"过滤后点数: {len(filtered_points)}\")\n", + " print(f\"number of points: {len(points)}\")\n", + " print(f\"number of filtered points: {len(filtered_points)}\")\n", "\n", " return filtered_points" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:53:27.021384366Z", - "start_time": "2024-11-14T18:53:26.976811833Z" - } + "collapsed": false }, "id": "909b74cb84334c01" }, { "cell_type": "code", - "execution_count": 24, - "outputs": [ - { - "data": { - "text/plain": "", - "application/javascript": "/* Put everything inside the global mpl namespace */\n/* global mpl */\nwindow.mpl = {};\n\nmpl.get_websocket_type = function () {\n if (typeof WebSocket !== 'undefined') {\n return WebSocket;\n } else if (typeof MozWebSocket !== 'undefined') {\n return MozWebSocket;\n } else {\n alert(\n 'Your browser does not have WebSocket support. ' +\n 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n 'Firefox 4 and 5 are also supported but you ' +\n 'have to enable WebSockets in about:config.'\n );\n }\n};\n\nmpl.figure = function (figure_id, websocket, ondownload, parent_element) {\n this.id = figure_id;\n\n this.ws = websocket;\n\n this.supports_binary = this.ws.binaryType !== undefined;\n\n if (!this.supports_binary) {\n var warnings = document.getElementById('mpl-warnings');\n if (warnings) {\n warnings.style.display = 'block';\n warnings.textContent =\n 'This browser does not support binary websocket messages. ' +\n 'Performance may be slow.';\n }\n }\n\n this.imageObj = new Image();\n\n this.context = undefined;\n this.message = undefined;\n this.canvas = undefined;\n this.rubberband_canvas = undefined;\n this.rubberband_context = undefined;\n this.format_dropdown = undefined;\n\n this.image_mode = 'full';\n\n this.root = document.createElement('div');\n this.root.setAttribute('style', 'display: inline-block');\n this._root_extra_style(this.root);\n\n parent_element.appendChild(this.root);\n\n this._init_header(this);\n this._init_canvas(this);\n this._init_toolbar(this);\n\n var fig = this;\n\n this.waiting = false;\n\n this.ws.onopen = function () {\n fig.send_message('supports_binary', { value: fig.supports_binary });\n fig.send_message('send_image_mode', {});\n if (fig.ratio !== 1) {\n fig.send_message('set_device_pixel_ratio', {\n device_pixel_ratio: fig.ratio,\n });\n }\n fig.send_message('refresh', {});\n };\n\n this.imageObj.onload = function () {\n if (fig.image_mode === 'full') {\n // Full images could contain transparency (where diff images\n // almost always do), so we need to clear the canvas so that\n // there is no ghosting.\n fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n }\n fig.context.drawImage(fig.imageObj, 0, 0);\n };\n\n this.imageObj.onunload = function () {\n fig.ws.close();\n };\n\n this.ws.onmessage = this._make_on_message_function(this);\n\n this.ondownload = ondownload;\n};\n\nmpl.figure.prototype._init_header = function () {\n var titlebar = document.createElement('div');\n titlebar.classList =\n 'ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix';\n var titletext = document.createElement('div');\n titletext.classList = 'ui-dialog-title';\n titletext.setAttribute(\n 'style',\n 'width: 100%; text-align: center; padding: 3px;'\n );\n titlebar.appendChild(titletext);\n this.root.appendChild(titlebar);\n this.header = titletext;\n};\n\nmpl.figure.prototype._canvas_extra_style = function (_canvas_div) {};\n\nmpl.figure.prototype._root_extra_style = function (_canvas_div) {};\n\nmpl.figure.prototype._init_canvas = function () {\n var fig = this;\n\n var canvas_div = (this.canvas_div = document.createElement('div'));\n canvas_div.setAttribute('tabindex', '0');\n canvas_div.setAttribute(\n 'style',\n 'border: 1px solid #ddd;' +\n 'box-sizing: content-box;' +\n 'clear: both;' +\n 'min-height: 1px;' +\n 'min-width: 1px;' +\n 'outline: 0;' +\n 'overflow: hidden;' +\n 'position: relative;' +\n 'resize: both;' +\n 'z-index: 2;'\n );\n\n function on_keyboard_event_closure(name) {\n return function (event) {\n return fig.key_event(event, name);\n };\n }\n\n canvas_div.addEventListener(\n 'keydown',\n on_keyboard_event_closure('key_press')\n );\n canvas_div.addEventListener(\n 'keyup',\n on_keyboard_event_closure('key_release')\n );\n\n this._canvas_extra_style(canvas_div);\n this.root.appendChild(canvas_div);\n\n var canvas = (this.canvas = document.createElement('canvas'));\n canvas.classList.add('mpl-canvas');\n canvas.setAttribute(\n 'style',\n 'box-sizing: content-box;' +\n 'pointer-events: none;' +\n 'position: relative;' +\n 'z-index: 0;'\n );\n\n this.context = canvas.getContext('2d');\n\n var backingStore =\n this.context.backingStorePixelRatio ||\n this.context.webkitBackingStorePixelRatio ||\n this.context.mozBackingStorePixelRatio ||\n this.context.msBackingStorePixelRatio ||\n this.context.oBackingStorePixelRatio ||\n this.context.backingStorePixelRatio ||\n 1;\n\n this.ratio = (window.devicePixelRatio || 1) / backingStore;\n\n var rubberband_canvas = (this.rubberband_canvas = document.createElement(\n 'canvas'\n ));\n rubberband_canvas.setAttribute(\n 'style',\n 'box-sizing: content-box;' +\n 'left: 0;' +\n 'pointer-events: none;' +\n 'position: absolute;' +\n 'top: 0;' +\n 'z-index: 1;'\n );\n\n // Apply a ponyfill if ResizeObserver is not implemented by browser.\n if (this.ResizeObserver === undefined) {\n if (window.ResizeObserver !== undefined) {\n this.ResizeObserver = window.ResizeObserver;\n } else {\n var obs = _JSXTOOLS_RESIZE_OBSERVER({});\n this.ResizeObserver = obs.ResizeObserver;\n }\n }\n\n this.resizeObserverInstance = new this.ResizeObserver(function (entries) {\n var nentries = entries.length;\n for (var i = 0; i < nentries; i++) {\n var entry = entries[i];\n var width, height;\n if (entry.contentBoxSize) {\n if (entry.contentBoxSize instanceof Array) {\n // Chrome 84 implements new version of spec.\n width = entry.contentBoxSize[0].inlineSize;\n height = entry.contentBoxSize[0].blockSize;\n } else {\n // Firefox implements old version of spec.\n width = entry.contentBoxSize.inlineSize;\n height = entry.contentBoxSize.blockSize;\n }\n } else {\n // Chrome <84 implements even older version of spec.\n width = entry.contentRect.width;\n height = entry.contentRect.height;\n }\n\n // Keep the size of the canvas and rubber band canvas in sync with\n // the canvas container.\n if (entry.devicePixelContentBoxSize) {\n // Chrome 84 implements new version of spec.\n canvas.setAttribute(\n 'width',\n entry.devicePixelContentBoxSize[0].inlineSize\n );\n canvas.setAttribute(\n 'height',\n entry.devicePixelContentBoxSize[0].blockSize\n );\n } else {\n canvas.setAttribute('width', width * fig.ratio);\n canvas.setAttribute('height', height * fig.ratio);\n }\n /* This rescales the canvas back to display pixels, so that it\n * appears correct on HiDPI screens. */\n canvas.style.width = width + 'px';\n canvas.style.height = height + 'px';\n\n rubberband_canvas.setAttribute('width', width);\n rubberband_canvas.setAttribute('height', height);\n\n // And update the size in Python. We ignore the initial 0/0 size\n // that occurs as the element is placed into the DOM, which should\n // otherwise not happen due to the minimum size styling.\n if (fig.ws.readyState == 1 && width != 0 && height != 0) {\n fig.request_resize(width, height);\n }\n }\n });\n this.resizeObserverInstance.observe(canvas_div);\n\n function on_mouse_event_closure(name) {\n /* User Agent sniffing is bad, but WebKit is busted:\n * https://bugs.webkit.org/show_bug.cgi?id=144526\n * https://bugs.webkit.org/show_bug.cgi?id=181818\n * The worst that happens here is that they get an extra browser\n * selection when dragging, if this check fails to catch them.\n */\n var UA = navigator.userAgent;\n var isWebKit = /AppleWebKit/.test(UA) && !/Chrome/.test(UA);\n if(isWebKit) {\n return function (event) {\n /* This prevents the web browser from automatically changing to\n * the text insertion cursor when the button is pressed. We\n * want to control all of the cursor setting manually through\n * the 'cursor' event from matplotlib */\n event.preventDefault()\n return fig.mouse_event(event, name);\n };\n } else {\n return function (event) {\n return fig.mouse_event(event, name);\n };\n }\n }\n\n canvas_div.addEventListener(\n 'mousedown',\n on_mouse_event_closure('button_press')\n );\n canvas_div.addEventListener(\n 'mouseup',\n on_mouse_event_closure('button_release')\n );\n canvas_div.addEventListener(\n 'dblclick',\n on_mouse_event_closure('dblclick')\n );\n // Throttle sequential mouse events to 1 every 20ms.\n canvas_div.addEventListener(\n 'mousemove',\n on_mouse_event_closure('motion_notify')\n );\n\n canvas_div.addEventListener(\n 'mouseenter',\n on_mouse_event_closure('figure_enter')\n );\n canvas_div.addEventListener(\n 'mouseleave',\n on_mouse_event_closure('figure_leave')\n );\n\n canvas_div.addEventListener('wheel', function (event) {\n if (event.deltaY < 0) {\n event.step = 1;\n } else {\n event.step = -1;\n }\n on_mouse_event_closure('scroll')(event);\n });\n\n canvas_div.appendChild(canvas);\n canvas_div.appendChild(rubberband_canvas);\n\n this.rubberband_context = rubberband_canvas.getContext('2d');\n this.rubberband_context.strokeStyle = '#000000';\n\n this._resize_canvas = function (width, height, forward) {\n if (forward) {\n canvas_div.style.width = width + 'px';\n canvas_div.style.height = height + 'px';\n }\n };\n\n // Disable right mouse context menu.\n canvas_div.addEventListener('contextmenu', function (_e) {\n event.preventDefault();\n return false;\n });\n\n function set_focus() {\n canvas.focus();\n canvas_div.focus();\n }\n\n window.setTimeout(set_focus, 100);\n};\n\nmpl.figure.prototype._init_toolbar = function () {\n var fig = this;\n\n var toolbar = document.createElement('div');\n toolbar.classList = 'mpl-toolbar';\n this.root.appendChild(toolbar);\n\n function on_click_closure(name) {\n return function (_event) {\n return fig.toolbar_button_onclick(name);\n };\n }\n\n function on_mouseover_closure(tooltip) {\n return function (event) {\n if (!event.currentTarget.disabled) {\n return fig.toolbar_button_onmouseover(tooltip);\n }\n };\n }\n\n fig.buttons = {};\n var buttonGroup = document.createElement('div');\n buttonGroup.classList = 'mpl-button-group';\n for (var toolbar_ind in mpl.toolbar_items) {\n var name = mpl.toolbar_items[toolbar_ind][0];\n var tooltip = mpl.toolbar_items[toolbar_ind][1];\n var image = mpl.toolbar_items[toolbar_ind][2];\n var method_name = mpl.toolbar_items[toolbar_ind][3];\n\n if (!name) {\n /* Instead of a spacer, we start a new button group. */\n if (buttonGroup.hasChildNodes()) {\n toolbar.appendChild(buttonGroup);\n }\n buttonGroup = document.createElement('div');\n buttonGroup.classList = 'mpl-button-group';\n continue;\n }\n\n var button = (fig.buttons[name] = document.createElement('button'));\n button.classList = 'mpl-widget';\n button.setAttribute('role', 'button');\n button.setAttribute('aria-disabled', 'false');\n button.addEventListener('click', on_click_closure(method_name));\n button.addEventListener('mouseover', on_mouseover_closure(tooltip));\n\n var icon_img = document.createElement('img');\n icon_img.src = '_images/' + image + '.png';\n icon_img.srcset = '_images/' + image + '_large.png 2x';\n icon_img.alt = tooltip;\n button.appendChild(icon_img);\n\n buttonGroup.appendChild(button);\n }\n\n if (buttonGroup.hasChildNodes()) {\n toolbar.appendChild(buttonGroup);\n }\n\n var fmt_picker = document.createElement('select');\n fmt_picker.classList = 'mpl-widget';\n toolbar.appendChild(fmt_picker);\n this.format_dropdown = fmt_picker;\n\n for (var ind in mpl.extensions) {\n var fmt = mpl.extensions[ind];\n var option = document.createElement('option');\n option.selected = fmt === mpl.default_extension;\n option.innerHTML = fmt;\n fmt_picker.appendChild(option);\n }\n\n var status_bar = document.createElement('span');\n status_bar.classList = 'mpl-message';\n toolbar.appendChild(status_bar);\n this.message = status_bar;\n};\n\nmpl.figure.prototype.request_resize = function (x_pixels, y_pixels) {\n // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n // which will in turn request a refresh of the image.\n this.send_message('resize', { width: x_pixels, height: y_pixels });\n};\n\nmpl.figure.prototype.send_message = function (type, properties) {\n properties['type'] = type;\n properties['figure_id'] = this.id;\n this.ws.send(JSON.stringify(properties));\n};\n\nmpl.figure.prototype.send_draw_message = function () {\n if (!this.waiting) {\n this.waiting = true;\n this.ws.send(JSON.stringify({ type: 'draw', figure_id: this.id }));\n }\n};\n\nmpl.figure.prototype.handle_save = function (fig, _msg) {\n var format_dropdown = fig.format_dropdown;\n var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n fig.ondownload(fig, format);\n};\n\nmpl.figure.prototype.handle_resize = function (fig, msg) {\n var size = msg['size'];\n if (size[0] !== fig.canvas.width || size[1] !== fig.canvas.height) {\n fig._resize_canvas(size[0], size[1], msg['forward']);\n fig.send_message('refresh', {});\n }\n};\n\nmpl.figure.prototype.handle_rubberband = function (fig, msg) {\n var x0 = msg['x0'] / fig.ratio;\n var y0 = (fig.canvas.height - msg['y0']) / fig.ratio;\n var x1 = msg['x1'] / fig.ratio;\n var y1 = (fig.canvas.height - msg['y1']) / fig.ratio;\n x0 = Math.floor(x0) + 0.5;\n y0 = Math.floor(y0) + 0.5;\n x1 = Math.floor(x1) + 0.5;\n y1 = Math.floor(y1) + 0.5;\n var min_x = Math.min(x0, x1);\n var min_y = Math.min(y0, y1);\n var width = Math.abs(x1 - x0);\n var height = Math.abs(y1 - y0);\n\n fig.rubberband_context.clearRect(\n 0,\n 0,\n fig.canvas.width / fig.ratio,\n fig.canvas.height / fig.ratio\n );\n\n fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n};\n\nmpl.figure.prototype.handle_figure_label = function (fig, msg) {\n // Updates the figure title.\n fig.header.textContent = msg['label'];\n};\n\nmpl.figure.prototype.handle_cursor = function (fig, msg) {\n fig.canvas_div.style.cursor = msg['cursor'];\n};\n\nmpl.figure.prototype.handle_message = function (fig, msg) {\n fig.message.textContent = msg['message'];\n};\n\nmpl.figure.prototype.handle_draw = function (fig, _msg) {\n // Request the server to send over a new figure.\n fig.send_draw_message();\n};\n\nmpl.figure.prototype.handle_image_mode = function (fig, msg) {\n fig.image_mode = msg['mode'];\n};\n\nmpl.figure.prototype.handle_history_buttons = function (fig, msg) {\n for (var key in msg) {\n if (!(key in fig.buttons)) {\n continue;\n }\n fig.buttons[key].disabled = !msg[key];\n fig.buttons[key].setAttribute('aria-disabled', !msg[key]);\n }\n};\n\nmpl.figure.prototype.handle_navigate_mode = function (fig, msg) {\n if (msg['mode'] === 'PAN') {\n fig.buttons['Pan'].classList.add('active');\n fig.buttons['Zoom'].classList.remove('active');\n } else if (msg['mode'] === 'ZOOM') {\n fig.buttons['Pan'].classList.remove('active');\n fig.buttons['Zoom'].classList.add('active');\n } else {\n fig.buttons['Pan'].classList.remove('active');\n fig.buttons['Zoom'].classList.remove('active');\n }\n};\n\nmpl.figure.prototype.updated_canvas_event = function () {\n // Called whenever the canvas gets updated.\n this.send_message('ack', {});\n};\n\n// A function to construct a web socket function for onmessage handling.\n// Called in the figure constructor.\nmpl.figure.prototype._make_on_message_function = function (fig) {\n return function socket_on_message(evt) {\n if (evt.data instanceof Blob) {\n var img = evt.data;\n if (img.type !== 'image/png') {\n /* FIXME: We get \"Resource interpreted as Image but\n * transferred with MIME type text/plain:\" errors on\n * Chrome. But how to set the MIME type? It doesn't seem\n * to be part of the websocket stream */\n img.type = 'image/png';\n }\n\n /* Free the memory for the previous frames */\n if (fig.imageObj.src) {\n (window.URL || window.webkitURL).revokeObjectURL(\n fig.imageObj.src\n );\n }\n\n fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n img\n );\n fig.updated_canvas_event();\n fig.waiting = false;\n return;\n } else if (\n typeof evt.data === 'string' &&\n evt.data.slice(0, 21) === 'data:image/png;base64'\n ) {\n fig.imageObj.src = evt.data;\n fig.updated_canvas_event();\n fig.waiting = false;\n return;\n }\n\n var msg = JSON.parse(evt.data);\n var msg_type = msg['type'];\n\n // Call the \"handle_{type}\" callback, which takes\n // the figure and JSON message as its only arguments.\n try {\n var callback = fig['handle_' + msg_type];\n } catch (e) {\n console.log(\n \"No handler for the '\" + msg_type + \"' message type: \",\n msg\n );\n return;\n }\n\n if (callback) {\n try {\n // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n callback(fig, msg);\n } catch (e) {\n console.log(\n \"Exception inside the 'handler_\" + msg_type + \"' callback:\",\n e,\n e.stack,\n msg\n );\n }\n }\n };\n};\n\nfunction getModifiers(event) {\n var mods = [];\n if (event.ctrlKey) {\n mods.push('ctrl');\n }\n if (event.altKey) {\n mods.push('alt');\n }\n if (event.shiftKey) {\n mods.push('shift');\n }\n if (event.metaKey) {\n mods.push('meta');\n }\n return mods;\n}\n\n/*\n * return a copy of an object with only non-object keys\n * we need this to avoid circular references\n * https://stackoverflow.com/a/24161582/3208463\n */\nfunction simpleKeys(original) {\n return Object.keys(original).reduce(function (obj, key) {\n if (typeof original[key] !== 'object') {\n obj[key] = original[key];\n }\n return obj;\n }, {});\n}\n\nmpl.figure.prototype.mouse_event = function (event, name) {\n if (name === 'button_press') {\n this.canvas.focus();\n this.canvas_div.focus();\n }\n\n // from https://stackoverflow.com/q/1114465\n var boundingRect = this.canvas.getBoundingClientRect();\n var x = (event.clientX - boundingRect.left) * this.ratio;\n var y = (event.clientY - boundingRect.top) * this.ratio;\n\n this.send_message(name, {\n x: x,\n y: y,\n button: event.button,\n step: event.step,\n modifiers: getModifiers(event),\n guiEvent: simpleKeys(event),\n });\n\n return false;\n};\n\nmpl.figure.prototype._key_event_extra = function (_event, _name) {\n // Handle any extra behaviour associated with a key event\n};\n\nmpl.figure.prototype.key_event = function (event, name) {\n // Prevent repeat events\n if (name === 'key_press') {\n if (event.key === this._key) {\n return;\n } else {\n this._key = event.key;\n }\n }\n if (name === 'key_release') {\n this._key = null;\n }\n\n var value = '';\n if (event.ctrlKey && event.key !== 'Control') {\n value += 'ctrl+';\n }\n else if (event.altKey && event.key !== 'Alt') {\n value += 'alt+';\n }\n else if (event.shiftKey && event.key !== 'Shift') {\n value += 'shift+';\n }\n\n value += 'k' + event.key;\n\n this._key_event_extra(event, name);\n\n this.send_message(name, { key: value, guiEvent: simpleKeys(event) });\n return false;\n};\n\nmpl.figure.prototype.toolbar_button_onclick = function (name) {\n if (name === 'download') {\n this.handle_save(this, null);\n } else {\n this.send_message('toolbar_button', { name: name });\n }\n};\n\nmpl.figure.prototype.toolbar_button_onmouseover = function (tooltip) {\n this.message.textContent = tooltip;\n};\n\n///////////////// REMAINING CONTENT GENERATED BY embed_js.py /////////////////\n// prettier-ignore\nvar _JSXTOOLS_RESIZE_OBSERVER=function(A){var t,i=new WeakMap,n=new WeakMap,a=new WeakMap,r=new WeakMap,o=new Set;function s(e){if(!(this instanceof s))throw new TypeError(\"Constructor requires 'new' operator\");i.set(this,e)}function h(){throw new TypeError(\"Function is not a constructor\")}function c(e,t,i,n){e=0 in arguments?Number(arguments[0]):0,t=1 in arguments?Number(arguments[1]):0,i=2 in arguments?Number(arguments[2]):0,n=3 in arguments?Number(arguments[3]):0,this.right=(this.x=this.left=e)+(this.width=i),this.bottom=(this.y=this.top=t)+(this.height=n),Object.freeze(this)}function d(){t=requestAnimationFrame(d);var s=new WeakMap,p=new Set;o.forEach((function(t){r.get(t).forEach((function(i){var r=t instanceof window.SVGElement,o=a.get(t),d=r?0:parseFloat(o.paddingTop),f=r?0:parseFloat(o.paddingRight),l=r?0:parseFloat(o.paddingBottom),u=r?0:parseFloat(o.paddingLeft),g=r?0:parseFloat(o.borderTopWidth),m=r?0:parseFloat(o.borderRightWidth),w=r?0:parseFloat(o.borderBottomWidth),b=u+f,F=d+l,v=(r?0:parseFloat(o.borderLeftWidth))+m,W=g+w,y=r?0:t.offsetHeight-W-t.clientHeight,E=r?0:t.offsetWidth-v-t.clientWidth,R=b+v,z=F+W,M=r?t.width:parseFloat(o.width)-R-E,O=r?t.height:parseFloat(o.height)-z-y;if(n.has(t)){var k=n.get(t);if(k[0]===M&&k[1]===O)return}n.set(t,[M,O]);var S=Object.create(h.prototype);S.target=t,S.contentRect=new c(u,d,M,O),s.has(i)||(s.set(i,[]),p.add(i)),s.get(i).push(S)}))})),p.forEach((function(e){i.get(e).call(e,s.get(e),e)}))}return s.prototype.observe=function(i){if(i instanceof window.Element){r.has(i)||(r.set(i,new Set),o.add(i),a.set(i,window.getComputedStyle(i)));var n=r.get(i);n.has(this)||n.add(this),cancelAnimationFrame(t),t=requestAnimationFrame(d)}},s.prototype.unobserve=function(i){if(i instanceof window.Element&&r.has(i)){var n=r.get(i);n.has(this)&&(n.delete(this),n.size||(r.delete(i),o.delete(i))),n.size||r.delete(i),o.size||cancelAnimationFrame(t)}},A.DOMRectReadOnly=c,A.ResizeObserver=s,A.ResizeObserverEntry=h,A}; // eslint-disable-line\nmpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Left button pans, Right button zooms\\nx/y fixes axis, CTRL fixes aspect\", \"fa fa-arrows\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\\nx/y fixes axis\", \"fa fa-square-o\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o\", \"download\"]];\n\nmpl.extensions = [\"eps\", \"jpeg\", \"pgf\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\", \"webp\"];\n\nmpl.default_extension = \"png\";/* global mpl */\n\nvar comm_websocket_adapter = function (comm) {\n // Create a \"websocket\"-like object which calls the given IPython comm\n // object with the appropriate methods. Currently this is a non binary\n // socket, so there is still some room for performance tuning.\n var ws = {};\n\n ws.binaryType = comm.kernel.ws.binaryType;\n ws.readyState = comm.kernel.ws.readyState;\n function updateReadyState(_event) {\n if (comm.kernel.ws) {\n ws.readyState = comm.kernel.ws.readyState;\n } else {\n ws.readyState = 3; // Closed state.\n }\n }\n comm.kernel.ws.addEventListener('open', updateReadyState);\n comm.kernel.ws.addEventListener('close', updateReadyState);\n comm.kernel.ws.addEventListener('error', updateReadyState);\n\n ws.close = function () {\n comm.close();\n };\n ws.send = function (m) {\n //console.log('sending', m);\n comm.send(m);\n };\n // Register the callback with on_msg.\n comm.on_msg(function (msg) {\n //console.log('receiving', msg['content']['data'], msg);\n var data = msg['content']['data'];\n if (data['blob'] !== undefined) {\n data = {\n data: new Blob(msg['buffers'], { type: data['blob'] }),\n };\n }\n // Pass the mpl event to the overridden (by mpl) onmessage function.\n ws.onmessage(data);\n });\n return ws;\n};\n\nmpl.mpl_figure_comm = function (comm, msg) {\n // This is the function which gets called when the mpl process\n // starts-up an IPython Comm through the \"matplotlib\" channel.\n\n var id = msg.content.data.id;\n // Get hold of the div created by the display call when the Comm\n // socket was opened in Python.\n var element = document.getElementById(id);\n var ws_proxy = comm_websocket_adapter(comm);\n\n function ondownload(figure, _format) {\n window.open(figure.canvas.toDataURL());\n }\n\n var fig = new mpl.figure(id, ws_proxy, ondownload, element);\n\n // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n // web socket which is closed, not our websocket->open comm proxy.\n ws_proxy.onopen();\n\n fig.parent_element = element;\n fig.cell_info = mpl.find_output_cell(\"
\");\n if (!fig.cell_info) {\n console.error('Failed to find cell for figure', id, fig);\n return;\n }\n fig.cell_info[0].output_area.element.on(\n 'cleared',\n { fig: fig },\n fig._remove_fig_handler\n );\n};\n\nmpl.figure.prototype.handle_close = function (fig, msg) {\n var width = fig.canvas.width / fig.ratio;\n fig.cell_info[0].output_area.element.off(\n 'cleared',\n fig._remove_fig_handler\n );\n fig.resizeObserverInstance.unobserve(fig.canvas_div);\n\n // Update the output cell to use the data from the current canvas.\n fig.push_to_output();\n var dataURL = fig.canvas.toDataURL();\n // Re-enable the keyboard manager in IPython - without this line, in FF,\n // the notebook keyboard shortcuts fail.\n IPython.keyboard_manager.enable();\n fig.parent_element.innerHTML =\n '';\n fig.close_ws(fig, msg);\n};\n\nmpl.figure.prototype.close_ws = function (fig, msg) {\n fig.send_message('closing', msg);\n // fig.ws.close()\n};\n\nmpl.figure.prototype.push_to_output = function (_remove_interactive) {\n // Turn the data on the canvas into data in the output cell.\n var width = this.canvas.width / this.ratio;\n var dataURL = this.canvas.toDataURL();\n this.cell_info[1]['text/html'] =\n '';\n};\n\nmpl.figure.prototype.updated_canvas_event = function () {\n // Tell IPython that the notebook contents must change.\n IPython.notebook.set_dirty(true);\n this.send_message('ack', {});\n var fig = this;\n // Wait a second, then push the new image to the DOM so\n // that it is saved nicely (might be nice to debounce this).\n setTimeout(function () {\n fig.push_to_output();\n }, 1000);\n};\n\nmpl.figure.prototype._init_toolbar = function () {\n var fig = this;\n\n var toolbar = document.createElement('div');\n toolbar.classList = 'btn-toolbar';\n this.root.appendChild(toolbar);\n\n function on_click_closure(name) {\n return function (_event) {\n return fig.toolbar_button_onclick(name);\n };\n }\n\n function on_mouseover_closure(tooltip) {\n return function (event) {\n if (!event.currentTarget.disabled) {\n return fig.toolbar_button_onmouseover(tooltip);\n }\n };\n }\n\n fig.buttons = {};\n var buttonGroup = document.createElement('div');\n buttonGroup.classList = 'btn-group';\n var button;\n for (var toolbar_ind in mpl.toolbar_items) {\n var name = mpl.toolbar_items[toolbar_ind][0];\n var tooltip = mpl.toolbar_items[toolbar_ind][1];\n var image = mpl.toolbar_items[toolbar_ind][2];\n var method_name = mpl.toolbar_items[toolbar_ind][3];\n\n if (!name) {\n /* Instead of a spacer, we start a new button group. */\n if (buttonGroup.hasChildNodes()) {\n toolbar.appendChild(buttonGroup);\n }\n buttonGroup = document.createElement('div');\n buttonGroup.classList = 'btn-group';\n continue;\n }\n\n button = fig.buttons[name] = document.createElement('button');\n button.classList = 'btn btn-default';\n button.href = '#';\n button.title = name;\n button.innerHTML = '';\n button.addEventListener('click', on_click_closure(method_name));\n button.addEventListener('mouseover', on_mouseover_closure(tooltip));\n buttonGroup.appendChild(button);\n }\n\n if (buttonGroup.hasChildNodes()) {\n toolbar.appendChild(buttonGroup);\n }\n\n // Add the status bar.\n var status_bar = document.createElement('span');\n status_bar.classList = 'mpl-message pull-right';\n toolbar.appendChild(status_bar);\n this.message = status_bar;\n\n // Add the close button to the window.\n var buttongrp = document.createElement('div');\n buttongrp.classList = 'btn-group inline pull-right';\n button = document.createElement('button');\n button.classList = 'btn btn-mini btn-primary';\n button.href = '#';\n button.title = 'Stop Interaction';\n button.innerHTML = '';\n button.addEventListener('click', function (_evt) {\n fig.handle_close(fig, {});\n });\n button.addEventListener(\n 'mouseover',\n on_mouseover_closure('Stop Interaction')\n );\n buttongrp.appendChild(button);\n var titlebar = this.root.querySelector('.ui-dialog-titlebar');\n titlebar.insertBefore(buttongrp, titlebar.firstChild);\n};\n\nmpl.figure.prototype._remove_fig_handler = function (event) {\n var fig = event.data.fig;\n if (event.target !== this) {\n // Ignore bubbled events from children.\n return;\n }\n fig.close_ws(fig, {});\n};\n\nmpl.figure.prototype._root_extra_style = function (el) {\n el.style.boxSizing = 'content-box'; // override notebook setting of border-box.\n};\n\nmpl.figure.prototype._canvas_extra_style = function (el) {\n // this is important to make the div 'focusable\n el.setAttribute('tabindex', 0);\n // reach out to IPython and tell the keyboard manager to turn it's self\n // off when our div gets focus\n\n // location in version 3\n if (IPython.notebook.keyboard_manager) {\n IPython.notebook.keyboard_manager.register_events(el);\n } else {\n // location in version 2\n IPython.keyboard_manager.register_events(el);\n }\n};\n\nmpl.figure.prototype._key_event_extra = function (event, _name) {\n // Check for shift+enter\n if (event.shiftKey && event.which === 13) {\n this.canvas_div.blur();\n // select the cell after this one\n var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n IPython.notebook.select(index + 1);\n }\n};\n\nmpl.figure.prototype.handle_save = function (fig, _msg) {\n fig.ondownload(fig, null);\n};\n\nmpl.find_output_cell = function (html_output) {\n // Return the cell and output element which can be found *uniquely* in the notebook.\n // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n // IPython event is triggered only after the cells have been serialised, which for\n // our purposes (turning an active figure into a static one), is too late.\n var cells = IPython.notebook.get_cells();\n var ncells = cells.length;\n for (var i = 0; i < ncells; i++) {\n var cell = cells[i];\n if (cell.cell_type === 'code') {\n for (var j = 0; j < cell.output_area.outputs.length; j++) {\n var data = cell.output_area.outputs[j];\n if (data.data) {\n // IPython >= 3 moved mimebundle to data attribute of output\n data = data.data;\n }\n if (data['text/html'] === html_output) {\n return [cell, data, j];\n }\n }\n }\n }\n};\n\n// Register the function which deals with the matplotlib target/channel.\n// The kernel may be null if the page has been refreshed.\nif (IPython.notebook.kernel !== null) {\n IPython.notebook.kernel.comm_manager.register_target(\n 'matplotlib',\n mpl.mpl_figure_comm\n );\n}\n" - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/plain": "", - "text/html": "
" - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "原始点数: 200000\n", - "过滤后点数: 188629\n", - "原始点数: 200000\n", - "过滤后点数: 178521\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "原始点数: 200000\n", - "过滤后点数: 188736\n", - "原始点数: 200000\n", - "过滤后点数: 178698\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n", - "WARNING:root:Singular matrix. Likely caused by all points lying in an N-1 space.\n" - ] - } - ], + "execution_count": null, + "outputs": [], "source": [ "import alphashape\n", "import matplotlib.pyplot as plt\n", @@ -485,14 +277,10 @@ " faces = mesh.faces \n", " poly3d = [[vertices[vertex] for vertex in face] for face in faces]\n", "\n", - " # 创建多边形集合,并将其添加到3D绘图中\n", " mesh_collection = art3d.Poly3DCollection(poly3d, facecolor=colors[i])\n", " ax.add_collection3d(mesh_collection)\n", "\n", "\n", - "from sklearn.neighbors import NearestNeighbors\n", - "from sklearn.cluster import DBSCAN\n", - "\n", "\n", "ps = []\n", "for iF in [1, 2]:\n", @@ -505,21 +293,21 @@ "\n", " ps.append(points)\n", " # \n", - " # # 使用 alphashape 生成 alpha shape\n", - " alpha = 0.0001 # 可以尝试不同的 alpha 值\n", + " # # \n", + " alpha = 0.0001 # may try different aplha value\n", " mesh = alphashape.alphashape(points, alpha)\n", " mesh.export(mesh_dir + str(iF) + str(i) + '.stl')\n", " \n", " # \n", - " # # 绘制点云\n", + " # # plot pointcloud\n", " # ax.scatter(points[:, 0], points[:, 1], points[:, 2], s=10, color='blue', alpha=0.1)\n", - " # 绘制 alpha shape 多面体\n", + " # plot alpha shape \n", " faces = np.array(mesh.faces)\n", " vertices = np.array(mesh.vertices)\n", " poly3d = [[vertices[vertex] for vertex in face] for face in faces]\n", " ax.add_collection3d(Poly3DCollection(poly3d, alpha=0.5, color=tuple(color[iF-1, i+2, :].flatten()), edgecolor='none'))\n", "\n", - "# 设置轴范围\n", + "\n", "ax.set_xlabel('X')\n", "ax.set_ylabel('Y')\n", "ax.set_zlabel('Z')\n", @@ -529,7 +317,6 @@ "min_y, max_y = np.min(ps[:, 1]), np.max(ps[:, 1])\n", "min_z, max_z = np.min(ps[:, 2]), np.max(ps[:, 2])\n", "\n", - "# 设置轴范围\n", "ax.set_xlim([min_x, max_x])\n", "ax.set_ylim([min_y, max_y])\n", "ax.set_zlim([min_z, max_z])\n", @@ -538,17 +325,13 @@ "fig.savefig('reachability_map_2_fingers_v2.jpg', format='jpg', bbox_inches='tight', pad_inches=0.2, dpi=600)\n" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T19:01:05.209029295Z", - "start_time": "2024-11-14T18:55:32.024982257Z" - } + "collapsed": false }, "id": "a1869ebe4e890c74" }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "outputs": [], "source": [ "points = np.concatenate([ret[link_names[iF-1][i]].get_matrix()[:, :3, 3].cpu().numpy(),\n", @@ -556,68 +339,33 @@ "a = Delaunay(points)\n" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:37:13.575638408Z", - "start_time": "2024-11-14T18:37:13.333804865Z" - } + "collapsed": false }, "id": "bdca362d062f5907" }, { "cell_type": "code", - "execution_count": 19, - "outputs": [ - { - "data": { - "text/plain": "array([[-0.06438765, 0.15339802, 0.08208616],\n [-0.08189254, 0.05856388, 0.10956756],\n [-0.14313343, -0.00531648, -0.03868315],\n ...,\n [-0.12605143, 0.06001517, 0.03786667],\n [-0.04412298, 0.09731709, -0.03292758],\n [-0.08835173, 0.02669315, 0.06151159]], dtype=float32)" - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "outputs": [], "source": [ "points" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:43:57.554023238Z", - "start_time": "2024-11-14T18:43:57.511497780Z" - } + "collapsed": false }, "id": "b4c0a86a0375685f" }, { "cell_type": "code", - "execution_count": 20, - "outputs": [ - { - "ename": "ValueError", - "evalue": "Input volume should be a 3D numpy array.", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mValueError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[20], line 3\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mskimage\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m measure\n\u001B[0;32m----> 3\u001B[0m verts, faces, normals, values \u001B[38;5;241m=\u001B[39m \u001B[43mmeasure\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mmarching_cubes\u001B[49m\u001B[43m(\u001B[49m\u001B[43mpoints\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mlevel\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m0.5\u001B[39;49m\u001B[43m)\u001B[49m\n", - "File \u001B[0;32m~/.local/lib/python3.8/site-packages/skimage/measure/_marching_cubes_lewiner.py:131\u001B[0m, in \u001B[0;36mmarching_cubes\u001B[0;34m(volume, level, spacing, gradient_direction, step_size, allow_degenerate, method, mask)\u001B[0m\n\u001B[1;32m 129\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m method \u001B[38;5;241m!=\u001B[39m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mlewiner\u001B[39m\u001B[38;5;124m'\u001B[39m:\n\u001B[1;32m 130\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmethod should be either \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mlewiner\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m or \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mlorensen\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m--> 131\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43m_marching_cubes_lewiner\u001B[49m\u001B[43m(\u001B[49m\u001B[43mvolume\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mlevel\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mspacing\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 132\u001B[0m \u001B[43m \u001B[49m\u001B[43mgradient_direction\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mstep_size\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 133\u001B[0m \u001B[43m \u001B[49m\u001B[43mallow_degenerate\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43muse_classic\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43muse_classic\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 134\u001B[0m \u001B[43m \u001B[49m\u001B[43mmask\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmask\u001B[49m\u001B[43m)\u001B[49m\n", - "File \u001B[0;32m~/.local/lib/python3.8/site-packages/skimage/measure/_marching_cubes_lewiner.py:147\u001B[0m, in \u001B[0;36m_marching_cubes_lewiner\u001B[0;34m(volume, level, spacing, gradient_direction, step_size, allow_degenerate, use_classic, mask)\u001B[0m\n\u001B[1;32m 145\u001B[0m \u001B[38;5;66;03m# Check volume and ensure its in the format that the alg needs\u001B[39;00m\n\u001B[1;32m 146\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(volume, np\u001B[38;5;241m.\u001B[39mndarray) \u001B[38;5;129;01mor\u001B[39;00m (volume\u001B[38;5;241m.\u001B[39mndim \u001B[38;5;241m!=\u001B[39m \u001B[38;5;241m3\u001B[39m):\n\u001B[0;32m--> 147\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mInput volume should be a 3D numpy array.\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[1;32m 148\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m volume\u001B[38;5;241m.\u001B[39mshape[\u001B[38;5;241m0\u001B[39m] \u001B[38;5;241m<\u001B[39m \u001B[38;5;241m2\u001B[39m \u001B[38;5;129;01mor\u001B[39;00m volume\u001B[38;5;241m.\u001B[39mshape[\u001B[38;5;241m1\u001B[39m] \u001B[38;5;241m<\u001B[39m \u001B[38;5;241m2\u001B[39m \u001B[38;5;129;01mor\u001B[39;00m volume\u001B[38;5;241m.\u001B[39mshape[\u001B[38;5;241m2\u001B[39m] \u001B[38;5;241m<\u001B[39m \u001B[38;5;241m2\u001B[39m:\n\u001B[1;32m 149\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mInput array must be at least 2x2x2.\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n", - "\u001B[0;31mValueError\u001B[0m: Input volume should be a 3D numpy array." - ] - } - ], + "execution_count": null, + "outputs": [], "source": [ "from skimage import measure\n", "\n", "verts, faces, normals, values = measure.marching_cubes(points, level=0.5)\n" ], "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-11-14T18:44:01.702266957Z", - "start_time": "2024-11-14T18:44:01.669965338Z" - } + "collapsed": false }, "id": "e923e2e43ccfd9a1" } diff --git a/motion_planning/crawl_and_grasp_test04.ipynb b/motion_planning/crawl_and_grasp_test04.ipynb new file mode 100644 index 0000000..daca9b7 --- /dev/null +++ b/motion_planning/crawl_and_grasp_test04.ipynb @@ -0,0 +1,270 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "initial_id", + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import time\n", + "\n", + "import numpy as np\n", + "\n", + "import tools.rotations as rot\n", + "\n", + "from controller import locomotion\n", + "import mujoco\n", + "\n", + "import copy\n", + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, + { + "cell_type": "markdown", + "source": [ + "# real robot structure, 5 fingers, 4Dofs for each finger" + ], + "metadata": { + "collapsed": false + }, + "id": "36882751dab20f4c" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "xml_path = 'descriptions/v2/hand_v2.xml'\n", + "q = np.array([0,0, 0.13, 9.99999738e-01,\n", + " -4.46340938e-04, 5.67392530e-04, -4.89152197e-05, \n", + " 0., 7.91144283e-01, 0.3, 0.3,\n", + " 0.3, 7.91144283e-01, 0.3, 0.3,\n", + " 0., 7.91144283e-01, 0.3, 0.3,\n", + " 0., 7.91144283e-01, 0.3, 0.3,\n", + " -0.5, 7.91144283e-01, 0.3, 0.3\n", + " ])\n", + "obj_names = ['sphere_1','cylinder_1', 'box_1', 'cube_1', ]\n", + "# obj_names = ['box_1', 'cube_1']\n", + "r = locomotion(xml_path, q0=q, finger_num=5, path_suffix='../',obj_names=obj_names, v='v2')\n", + "\n", + "print('locomotion freq', r.f)\n", + "# take a rest to let the acceleration converge to zero\n", + "t0 = time.time()\n", + "q0 = copy.deepcopy(q[7:])\n", + "while 1:\n", + " r.joint_computed_torque_control(q0)\n", + " if time.time() - t0 > 4:\n", + " break\n", + "r.xh_local_init = copy.deepcopy(r.xh_local) " + ], + "metadata": { + "collapsed": false + }, + "id": "5d2df1278b623993" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "r.obj_names" + ], + "metadata": { + "collapsed": false + }, + "id": "a3b1389cbea392a4" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false + }, + "id": "1e698b546c78d422" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "obj_1st = 0\n", + "offset = np.array([0.2, 0]) # offset along xy axis\n", + " \n", + "attractor_1 = r.x_obj[obj_1st][:2] + offset\n", + "print(attractor_1)\n" + ], + "metadata": { + "collapsed": false + }, + "id": "1ea6f745fa816f87" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "\n", + "\n", + "i = 2 # the object to be grasped\n", + "radius = [0.2, 0.2, 0.3, 0.3]\n", + "\n", + "attractor_1 = r.x_obj[i][:3] + rot.quat2mat(r.x_obj[i][3:7]) @ np.array([0, 0 ,0.35]) # offset along z axis of the obj frame, y is the long size of the box\n", + "# attractor_1 = attractor_1[:2]\n", + "# attractor_1 = np.array([-1,0])\n", + "ori_d = rot.euler2quat([0,0, np.pi/2 + np.pi/2/4])\n", + "ori_tmp = rot.quat_mul(r.x_obj[i][3:7], rot.euler2quat([-np.pi/2, 0, np.pi/2]))\n", + "ori_d = rot.quat_mul(ori_d, ori_tmp)\n", + "\n", + "\n", + "ref = np.concatenate([attractor_1, ori_d])\n", + "# visualize the attractor\n", + "r.d.mocap_pos[:] = ref[:3]\n", + "r.d.mocap_quat[:] = ref[3:]\n", + "# r.step()\n", + "r.moveto_attractor(ref, threshold=0.015, stance_legs=[0,2,3], swing_legs=[1,4], max_vel=0.005, radius=radius)\n", + "\n" + ], + "metadata": { + "collapsed": false + }, + "id": "1a5190e4d8234915" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "# prepare to grasp\n", + "\n", + "i = 2 # the object index to be grasped\n", + "radius = [0.2, 0.2, 0.03, 0.3]\n", + "\n", + "attractor_1 = r.x_obj[i][:3] + rot.quat2mat(r.x_obj[i][3:7]) @ np.array([0, 0 ,0.15]) # offset along z axis of the obj frame, y is the long size of the box\n", + "\n", + "ori_d = rot.euler2quat([0,0, np.pi/2 + np.pi/2/4])\n", + "ori_tmp = rot.quat_mul(r.x_obj[i][3:7], rot.euler2quat([-np.pi/2, 0, np.pi/2]))\n", + "ori_d = rot.quat_mul(ori_d, ori_tmp)\n", + "\n", + "\n", + "ref = np.concatenate([attractor_1, ori_d])\n", + "# visualize the attractor\n", + "r.d.mocap_pos[:] = ref[:3]\n", + "r.d.mocap_quat[:] = ref[3:]\n", + "# r.step()\n", + "r.moveto_attractor(ref, threshold=0.015, stance_legs=[0,2,3], swing_legs=[1,4], max_vel=0.005, radius=radius)\n" + ], + "metadata": { + "collapsed": false + }, + "id": "6cd1638a8c9430bf" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "# change the standing configuration\n", + "# _ = r.move_along_z(z=-0.04)\n", + "q_cmd = copy.deepcopy(q0)\n", + "q_cmd[4] = -0.5\n", + "q_cmd[16] = 0.5\n", + "r.joint_motion_generator(q_cmd, vel=2)" + ], + "metadata": { + "collapsed": false + }, + "id": "419918d0235d41d0" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false + }, + "id": "46a6a1ab40925d86" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "# 3 grasping\n", + "q_23 = np.array([-0.4, 7.91144283e-01, 0.3, 0.3,\n", + " 0.4, 7.91144283e-01, 0.3, 0.3])\n", + "q_cmd = copy.deepcopy(q0)\n", + "q_cmd[8:16] = q_23\n", + "q_cmd[4] = -0.5\n", + "q_cmd[16] = 0.5\n", + "nums = 2000\n", + "q_23_list = np.linspace(q0[8:16], q_23, num=nums)\n", + "\n", + "\n", + "r.joint_motion_generator(q_cmd, vel=1)\n" + ], + "metadata": { + "collapsed": false + }, + "id": "2674453ba5c79ec9" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "q_23 = np.array([-0.5, -1.4, -1.3, -0.3,\n", + " 0.5, -1.4, -1.3, -0.3])\n", + "q_cmd = copy.deepcopy(q0)\n", + "q_cmd[8:16] = q_23\n", + "q_cmd[4] = -0.5\n", + "q_cmd[16] = 0.5\n", + "\n", + "r.joint_motion_generator(q_cmd, vel=1)\n", + "\n", + " " + ], + "metadata": { + "collapsed": false + }, + "id": "ee6aec004d2ac19d" + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false + }, + "id": "e378226b3be2c787" + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/readme.md b/readme.md index 5daa104..09a885c 100644 --- a/readme.md +++ b/readme.md @@ -1,25 +1,33 @@ # Crawling Hand Project - -## Instructions -- This repo is based on [MuJoCo 2.3.6](https://github.com/deepmind/mujoco) -- Create a new Conda env with Python>= 3.8 -- Install mujoco by `pip installl mujoco` - - -## Visulization +## System requirements + - Ubuntu 20.04 with `ROS-noetic` + +# Installation guide +- Create a `conda` environment with `python==3.8.18`, `pygad=3.2.0` +- Install `Jupyter notebook` +- Install mujoco by `pip install mujoco` under the `conda` environment. The MuJoCo version is tested on `3.0.0` +- Several minutes should be enough for a normal computer. + +### Kinematics-related packages +- `conda install -c conda-forge python-orocos-kdl` for PyKDL from https://github.com/orocos/orocos_kinematics_dynamics \ +- `pip install urdf-parser-py`\ +- `conda install -c conda-forge matplotlib`\ +- `conda install -c conda-forge quaternion` `conda install numpy scipy numba` + +# Visulization +- Run `crawling_and_grasp_test04.ipynb`, a robotic hand with 5 fingers will be loaded. The robot can crawl to an object and pick it up. - In mujoco conda env, `python -m mujoco.viewer --mjcf=five_finger_hand_ssss.xml` - With ROS, `roslaunch urdf_tutorial display.launch model:=URDF_finger_ssss.urdf` -## Explanation of code +# Explanation of code - `gait_test/GA_crawling_full_test_new.py`, the optimal structures achieved by optimizing the link lengths and finger placements for grasping one, two, or three objects, respectively. - `*_replay_*.py` replay the crawling in simulation. - -## Notes +# Notes - Under folder `singer_finger/`, `URDF_finger_xxxx.urdf `is the urdf file for a single finger. - For six fingers, see `descriptions/six_fingers_llll.xml`, which makes 6 copies of the single finger. - To use [pytorch_kinematics](https://github.com/UM-ARM-Lab/pytorch_kinematics), modify the function `build_chain_from_mjcf` @@ -30,29 +38,12 @@ xml_path = 'descriptions/five_finger_hand_ssss.xml' chain = pk.build_chain_from_mjcf(xml_path, 'hand') ``` - -## Real robot control (with ROS) -- install ROS2. I use [ros-iron](https://docs.ros.org/en/iron/Installation/Ubuntu-Install-Debians.html) - - build from source of [dynamixel](https://github.com/dynamixel-community/dynamixel_hardware) with ROS2. - - make sure that we can connect the control all motors in `Dynamixel Wizard`. `Scan` and enable `torque` control to test each motor. - - `source ~/research/lasa/open_source/crawling_robot/dynamixel_new/install/setup.bash` - - launch the robot `ros2 launch pantilt_bot_description pantilt_bot.launch.py`. The `usb_port, baud_rate, id` in `ros2_control.xacro` should be the same as `Dynamixel Wizard` - - to switch controllers, `ros2 control switch_controllers --activate joint_state_broadcaster --activate joint_trajectory_controller --deactivate velocity_controller` - - run `control_interface_real_robot.py` for an example. - - For velocity control `ros2 control switch_controllers --deactivate joint_trajectory_controller` - -## Dynamixel control (without ROS) +## Dynamixel motor control (without ROS) - Current-based position control https://github.com/leap-hand/LEAP_Hand_API 8ms for reading position only and sending position commands - https://github.com/AlanSunHR/DynamixelController has full access to all parameters of motors. +- A wrapper is written in the `control_utils/control_interface_v3.py` - -## KDL -`conda install -c conda-forge python-orocos-kdl` for PyKDL from https://github.com/orocos/orocos_kinematics_dynamics \ -`pip install urdf-parser-py`\ -`conda install -c conda-forge matplotlib`\ -`conda install -c conda-forge quaternion` `conda install numpy scipy numba` - ## Grasping stability in MuJoCo The following parameters are important: ``` @@ -63,7 +54,6 @@ The following parameters are important: ``` we also need to set the friction coefficients - ## Steps to connect motors - Give access to USB port `/dev/ttyUSB0` and `/dev/ttyUSB0` by `sudo chmod -R 777 /dev/ttyUSB0` - Open Dynamixel Wizard and scan motors