NameError:全局名称'obs_ja1'未定义

时间:2019-11-10 17:52:21

标签: python python-3.x ros

我正在尝试从已定义的函数获取输出,但是我无法访问在先前的函数中定义的特定变量。

按照下面的代码,我正在订阅主题(/ curr_pose_r2)和(/ joint-angles_r2)各自的功能-> observation_callback_pose() observation_callback_joint_angles()< / strong>。这两个函数将订阅的值存储到它们各自的变量中,即 obs_ja1 obs_pose

现在,我需要使用这两个变量来计算状态,该状态位于第三个函数 take_observation()中。但是,当我按行 obs = take_observation()调用此函数时,会引发以下错误

Traceback (most recent call last):
  File "./trial_pose.py", line 56, in <module>
    obs = take_observation()
  File "./trial_pose.py", line 38, in take_observation
    joint_angles = np.array(obs_ja1.positions)
NameError: global name 'obs_ja1' is not defined
from trajectory_msgs.msg import JointTrajectory
from std_msgs.msg import Header
from trajectory_msgs.msg import JointTrajectoryPoint
import rospy
import sys
import copy
import moveit_commander #allow us to communicate with the MoveIt Rviz interface
import moveit_msgs.msg
import geometry_msgs.msg
from geometry_msgs.msg import Pose
import std_msgs
import random
import numpy as np
from copy import deepcopy
import transforms3d as tf3d
import gym
from gym import error, spaces, utils
from gym.utils import seeding
import rospkg

targetPosition = np.asarray([0.0947665050276, -0.528568951669, 0.906398012978]) 
target_orientation = np.asarray([0.500077733518, -0.499998982263, 0.499900998414, 0.500022269464])


def observation_callback_joint_angles(joint_angles):
    obs_ja1 = joint_angles
    #print(obs_ja1.positions)
        #print(message)
    return obs_ja1
def observation_callback_pose(poses):
    obs_pose1 = poses
    #print(obs_pose1)
    return obs_pose1
def take_observation():
    # Check that the observation is not prior to the action
    joint_angles = np.array(obs_ja1.positions)
    print(joint_angles)
    curr_eef_position = np.array(obs_pose1.positions)
    curr_eef_quat = np.ndarray.flatten(obs_pose1.orientation)

    quat_error = tf3d.quaternions.qmult(curr_eef_quat, tf3d.quaternions.qconjugate(target_orientation))
    eef_points = curr_eef_position - targetPosition

    state = np.r_[np.reshape(joint_angles, -1),
                    np.reshape(eef_points, -1),
                    np.reshape(quat_error, -1)]

    print (state)
    return state
    '''
    RETURNS STATE
    '''

obs = take_observation()

0 个答案:

没有答案