cdsousa / SymPyBotics

[UNMAINTAINED] Symbolic Framework for Modeling and Identification of Robot Dynamics
Other
203 stars 83 forks source link

Certain joint offsets lead to "NameError: global name 'sqrt' is not defined" in calc_base_parms() #10

Open pRosenberger opened 9 years ago

pRosenberger commented 9 years ago

The model in the following code leads to an error:

import sympy
import sympybotics
rbtdef = sympybotics.RobotDef('left_arm',
                         [('pi/2', 0, 0, 'pi/2'),
                         ('pi/2', 0.03, 0.246, '-pi'),
                         ('-pi/2', -0.03, 0, '-pi/4'),
                         ('pi/2', 0, 0.186, 'pi'),
                         ('pi/2', 0, 0, 'pi'),
                         ('pi', 0, 0, 'pi/2')],
                        dh_convention='standard'
                         )
rbt = sympybotics.RobotDynCode(rbtdef, verbose=True)
rbt.calc_base_parms(verbose=True)

Whereby changing the offsets of the first four joints works fine:

import sympy
import sympybotics
rbtdef = sympybotics.RobotDef('left_arm',
                         [('pi/2', 0, 0, '-pi/2'),
                         ('pi/2', 0.03, 0.246, '-pi/2'),
                         ('-pi/2', -0.03, 0,' 0'),
                         ('pi/2', 0, 0.186, '0'),
                         ('pi/2', 0, 0, 'pi'),
                         ('pi', 0, 0, 'pi/2')],
                         dh_convention='standard'
                         )
rbt = sympybotics.RobotDynCode(rbtdef, verbose=True)
rbt.calc_base_parms(verbose=True)

Console output and error message:

generating geometric model
generating kinematic model
generating inverse dynamics code
generating gravity term code
generating coriolis term code
generating coriolis matrix code
generating inertia matrix code
generating regressor matrix code
done
calculating base parameters and regressor code
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    202             else:
    203                 filename = fname
--> 204             __builtin__.execfile(filename, *where)

/home/philipp/thor_identification/left_arm/URDF_file_handling/dh_to_baseparams.py in        <module>()
     17 rbt = sympybotics.RobotDynCode(rbtdef, verbose=True)
     18 
---> 19 rbt.calc_base_parms(verbose=True)
     20 print rbt.dyn.baseparms
     21 

/usr/local/lib/python2.7/dist-packages/sympybotics-1.0_dev-py2.7.egg/sympybotics            /robotmodel.pyc in calc_base_parms(self, verbose)
    120             _fprint('calculating base parameters and regressor code')
    121 
--> 122         self.dyn.calc_base_parms(regressor_func)
    123 
    124         H_se = Subexprs()

/usr/local/lib/python2.7/dist-packages/sympybotics-1.0_dev-py2.7.egg/sympybotics/dynamics   /dynamics.pyc in calc_base_parms(self, regressor_func)
     53 
     54         Pb, Pd, Kd = find_dyn_parm_deps(
---> 55             self.dof, self.n_dynparms, regressor_func)
     56 
     57         self.Pb = sympy.Matrix(Pb).applyfunc(lambda x: x.nsimplify())

/usr/local/lib/python2.7/dist-packages/sympybotics-1.0_dev-py2.7.egg/sympybotics/dynamics   /dyn_parm_dep.pyc in find_dyn_parm_deps(dof, parm_num, regressor_func)
     20                for j in range(dof)]
     21         Z[i * dof: i * dof + dof, :] = numpy.matrix(
---> 22             regressor_func(q, dq, ddq)).reshape(dof, parm_num)
     23 
     24     R1_diag = numpy.linalg.qr(Z, mode='economic').diagonal().round(round)

/usr/local/lib/python2.7/dist-packages/sympybotics-1.0_dev-py2.7.egg/sympybotics            /robotmodel.pyc in regressor_func(q, dq, ddq)

NameError: global name 'sqrt' is not defined
ashlin-k commented 5 years ago

I get a similar error for the same lines of code, only it's global name 'math' not defined. This is using the example code from the Git page.

ashlin-k commented 5 years ago

I manage to fix mine by changing the following code in robotmodel.py:

def calc_base_parms(self, verbose=False):
        . . .
        global sin, cos, sign   
        sin = math.sin # used to be numpy.sin
        cos = math.cos # used to be numpy.cos
        sign = numpy.sign
        . . .

Since sin, cos and sign are exported to regressor_fun, they need to be from the correct library, which I guess is math instead of numpy. This seemed to do the trick.

cdsousa commented 5 years ago

Thanks @ashlin-k , I remember I had some trouble with the sin, cos, sign being local or global or whatever when developing the package, and I had never fully understood if that was the right way to do it.