petercorke / robotics-toolbox-python

Robotics Toolbox for Python
MIT License
2.07k stars 432 forks source link

pyplot = rtb.backends.PyPlot() # create a PyPlot backend TypeError: 'module' object is not callable #338

Open River-mao opened 1 year ago

River-mao commented 1 year ago

Hello! When I try the backends example code in https://petercorke.github.io/robotics-toolbox-python/arm_backend_pyplot.html, ''' import roboticstoolbox as rtb

robot = rtb.models.DH.Panda() # create a robot

pyplot = rtb.backends.PyPlot() # create a PyPlot backend pyplot.add(robot) # add the robot to the backend robot.q = robot.qz # set the robot configuration pyplot.step() # update the backend and graphical view ''' I encountered the following error: ''' pyplot = rtb.backends.PyPlot() # create a PyPlot backend TypeError: 'module' object is not callable ''' I need some help. Thanks a lot.

lulu110997 commented 10 months ago

I'm running into the same error when calling rtb.backends.PyPlot(). Did you find a solution?

lulu110997 commented 10 months ago

This worked for me

import roboticstoolbox as rtb

robot = rtb.models.DH.Panda()  # create a robot

pyplot = rtb.backends.PyPlot.PyPlot()  # create a PyPlot backend
pyplot.launch()
pyplot.add(robot)              # add the robot to the backend
robot.q = robot.qz             # set the robot configuration
pyplot.step()                  # update the backend and graphical view
pyplot.hold()

Alternatively, if you want to animate a robot, you can just use the figure returned by the robot.plot() method. For example

from spatialmath import SE3, SE2

import numpy as np
import math
import roboticstoolbox as rtb

robot = rtb.DHRobot(
    [
        rtb.RevoluteDH(a=1),
        rtb.RevoluteDH(a=1)
    ], name="2D_robot")
print(robot)

t = np.arange(0, 2, 0.1)  # time
T0 = SE3(-1, 0.5, 0)  # initial pose
T1 = SE3(1, 1.5, 0)  # final pose
Ts = rtb.tools.trajectory.ctraj(T0, T1, t)

robot_fig = robot.plot([0, 0])
robot_fig.ax.set(xlim=(-1.5, 2), ylim=(-1.5, 2))

sol = robot.ikine_LM(Ts)

for q in sol.q:
    robot.q = q  # set the robot configuration
    robot_fig.step()
robot_fig.hold()