<jupyter_start><jupyter_text>Interactive visualization of a robot in pydrake *Copyright (c) 2018, Toyota Research InstituteAll rights reserved.*See license in `LICENSE`.  IntroductionThis notebook shows how to use Drake and [ipywidgets](https://github.com/jupyter-widgets/ipywidgets) to create an interactive visualization of a robot.<jupyter_code>import os.path
from pydrake.common import FindResourceOrThrow
from pydrake.multibody.rigid_body_tree import (
    AddModelInstanceFromUrdfStringSearchingInRosPackages,
    FloatingBaseType,
    RigidBodyTree,
)
from pydrake.lcm import DrakeLcm
from pydrake.systems.framework import BasicVector
from pydrake.multibody.rigid_body_plant import DrakeVisualizer
import numpy as np
import ipywidgets as widgets<jupyter_output><empty_output><jupyter_text>Convenience class for interactive visualizationThe following class wraps a `DrakeVisualizer` system and connects it to Jupyter widgets.<jupyter_code>class DrakeVisualizerHelper:
    def __init__(self, tree):
        lcm = DrakeLcm()
        self.tree = tree
        self.visualizer = DrakeVisualizer(tree=self.tree, lcm=lcm, enable_playback=True)
        self.x = np.concatenate([robot.getZeroConfiguration(), 
                                 np.zeros(tree.get_num_velocities())])
        self.visualizer.PublishLoadRobot()
        self.draw(self.tree.getZeroConfiguration())
    def draw(self, q = None):
        if q is not None:
            self.x[:self.tree.get_num_positions()] = q
        context = self.visualizer.CreateDefaultContext()
        context.FixInputPort(0, BasicVector(self.x))
        self.visualizer.Publish(context)
        
    def inspect(self, slider_scaling = 1):
        # Setup widgets
        for i in range(robot.number_of_positions()):
            widgets.interact(
                self.__slider_callback,
                slider_value = widgets.FloatSlider(
                    value=slider_scaling * self.x[i],
                    min=slider_scaling * self.tree.joint_limit_min[i],
                    max=slider_scaling * self.tree.joint_limit_max[i],
                    description=self.tree.get_position_name(i)
                ),
                index=widgets.fixed(i),
                slider_scaling=widgets.fixed(slider_scaling)
            )

    def __slider_callback(self, slider_value, index, slider_scaling):
        self.x[index] = slider_value / slider_scaling
        self.draw()<jupyter_output><empty_output><jupyter_text>Demo Instantiate a model of the IIWA robot arm along with a visualization helper.<jupyter_code>robot = RigidBodyTree("urdf/cassie.urdf", FloatingBaseType.kFixed)
#add_fixed_model(robot, iiwa_file)
vis_helper = DrakeVisualizerHelper(robot)<jupyter_output><empty_output><jupyter_text>Draw the arm in a random configuration.<jupyter_code>q = robot.getRandomConfiguration()
vis_helper.draw(q)<jupyter_output><empty_output><jupyter_text>Show the interactive sliders (with positions scaled to be in degrees).<jupyter_code>vis_helper.inspect(np.rad2deg(1))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08634274458305975, 'fraction_numerical': 0.0026263952724885093, 'mean_word_length': 3.659021406727829, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '587305', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 969, 'n_tokens_neox': 904, 'n_words': 184, 'path': 'examples/Cassie/interactive_visualization.ipynb', 'repo_name': 'mposa/dairlib-1', 'size': '3046'}