Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果在线程a中使用共享变量一段时间,而线程B可能希望访问该变量,会发生什么情况?_Python_Multithreading_Ros - Fatal编程技术网

Python 如果在线程a中使用共享变量一段时间,而线程B可能希望访问该变量,会发生什么情况?

Python 如果在线程a中使用共享变量一段时间,而线程B可能希望访问该变量,会发生什么情况?,python,multithreading,ros,Python,Multithreading,Ros,我正在尝试使用ROS为机器人实现操纵杆控制。 因此,我使用了一个类操纵杆,在其中我读取线程守护进程中的操纵杆值 在主线程中,我使用值的映射,特别是操纵杆轴的值,执行机器人操作,并在循环中重复 操纵杆的代码如下所示: class Joystick: # We'll store the states here. axis_states = {} button_states = {} # this will be the joystick later on jsdev = file def ini

我正在尝试使用ROS为机器人实现操纵杆控制。 因此,我使用了一个类操纵杆,在其中我读取线程守护进程中的操纵杆值

在主线程中,我使用值的映射,特别是操纵杆轴的值,执行机器人操作,并在循环中重复

操纵杆的代码如下所示:

class Joystick:
# We'll store the states here.
axis_states = {}
button_states = {}

# this will be the joystick later on
jsdev = file

def init(self):
    # do some stuff, trial and error for all the inputs, etc.
    # which will lead to this
    jsdev = open("/dev/input/js$", 'rb')

    # then i start a thread to continuously read the values:
    thread_for_joy_input = Thread(target=self.get_js_values)
    thread_for_joy_input.daemon = True
    thread_for_joy_input.start() 
    pass

def get_js_values():
    while True: 
        evbuf = jsdev.read(8)
        #update axis_states
        #update button_states
        #...
    pass

def print_vals(self, forever = False, timeout = (1 / 60)):
    while True:
        b = "Buttons: "
        for key, val in self.button_states.iteritems():
            if not any(x in key for x in ["unkn", "dpad", "base"]):
                b += "%s:\t %d\t\t" % (key, val)
            pass
        print b
        a = "Axes: "
        for key, val in self.axis_states.iteritems():
            if not any(x in key for x in ["unkn", "mis"]):
                    a += "%s\t %.2f\t\t" % (key, val)
        print a
        curr_time = datetime.now().time()
        print "%d:%d:%d:%d" % (curr_time.hour,curr_time.minute,curr_time.second,curr_time.microsecond)
        if not forever:
            break
        else:
            time.sleep(timeout)
    pass
在rospy节点中,基本上是另一个python脚本,我尝试使用这些变量

# this function is called in the main() function of this module
def joy_ctrl():
    global joystick
    connected = joystick.init()
    if connected == -1:
        return

    print "Controlling robot with a PS3 Controller.\nPress R2 for cartesian control or L2 for jointwise control.\nPress \"SELECT\" to abort"
    # variable for switching between cartesian- (0) and jointwise (1) control
    mode = 0

    btn_x_previously = 0
    joint_selector_1 = 0
    joint_selector_2 = joint_selector_1 + 1

    while True:
        j_vals_unmoved = robot_commander.get_current_joint_values()
        xyz_vals_unmoved = robot_commander.get_current_pose().pose.position
        buttons = joystick.button_states
        axes = joystick.axis_states

        if buttons["select"] == 1:
            break

        if mode == 1:
            joy_x, joy_y, joy_z = axes["x"], axes["y"], axes["rz"]
            if any(x > 0.1 or x < -0.1 for x in [joy_x, joy_y, joy_z]) and buttons["R1"] == 1:
                    # this weirds me out. i'll explain why below
                    joystick.print_vals()
                    angle_increments = assign_steps([ joy_y, joy_z], mode, [joint_selector_1,joint_selector_2])
                    move_incremental_jointwise(angle_increments)
                    wait_for_execution_stop(j_vals_unmoved,angle_increments,mode)
                    robot_not_moving = False
                    pass

        #maybe we need a delay, to ensure the robot is standing still when it receives the next instruction?
        pass
    pass
当我自己测试操纵杆类时,一切正常。我写了一个无限循环,在其中我打印了值,它看起来非常快

但现在来谈谈我的问题 当我在另一个脚本中使用操纵杆类时,它开始变慢

以下是我所做的: 启动了一个脚本,其中我只使用了操纵杆类并将值打印到终端。 启动ros节点。 向上移动操纵杆并立即返回

发生的事情如下: move_incremental_jointwise函数被多次调用。。。 这就是为什么我把打印功能放在它上面的原因

我查看了时间戳和相应的值,在两个终端中,同一时间有不同的值

关于为什么会发生这种情况,到底发生了什么,以及如何解决这个问题,有什么想法吗???我希望我提供了所有必要的信息


注意:我几个月前开始学习python,所以请原谅我的风格不好。非常感谢您的建议/提示

如果我没有弄错的话,您有两个不同的问题:-第一方面,您的问题是每次与PS3控制器交互时,joy_crtl函数都会被调用几次。在这里,我认为您可以在主循环中处理此问题,检查操纵杆值是否已从1更改为以下循环。另一方面,您不知道为什么相同的操作会有不同的时间戳。这是来自不同节点的不同时间吗?如果是,是否正在同步节点?怎么做?@CésarHoyos:不完全是。1 joy_ctrl只调用一次。但是在while循环中,操纵杆的值并不保持实际。事实上,它们在几个迭代中保持不变。这应该不是问题,因为我用移动变量锁定了机器人。但即使在执行之后,这些值也会保持一段时间不变。我不同步。我只是在两个节点上打印了带有时间戳的操纵杆值,同时得到了不同的值。我假设在主线程中使用可变操纵杆可以防止第二个线程(它应该更新值)这样做