Python CANopen 控制力不会更新到目标速度

问题描述 投票:0回答:0

我正在尝试使用 CANopen 控制 ATV320 电机的速度。我想出了以下代码:

import canopen
import logging
import time
import threading

logging.basicConfig(level=logging.CRITICAL)

network = canopen.Network()
network.connect(bustype='pcan', channel='PCAN_USBBUS1', bitrate=125_000)

ATV = 'atv'
keep_reading = True
keep_posting = True
current_vel_value = 0
target_vel_value = 1000

nodes = {}

atv_node = canopen.BaseNode402(32, 'ATV320_eds/SEATV320_010305E.eds')
nodes[ATV] = network.add_node(atv_node)


def process_tpdo_1():
    global current_vel_value
    while keep_reading:
        atv_node.tpdo[1].wait_for_reception()
        current_vel_value = atv_node.tpdo[1][1].phys
        print("\tTPDO[1][1].phys:" + str(current_vel_value))


def process_rpdo_1():
    global target_vel_value
    while target_vel_value > 600:
        print(f"Setting velocity to {target_vel_value}")
        atv_node.rpdo[1][1].phys = target_vel_value
        network.sync.transmit()
        target_vel_value -= 100
        time.sleep(1)
    atv_node.rpdo[1].stop()


tpdo_1_thread = threading.Thread(target=process_tpdo_1, args=())
rpdo_1_thread = threading.Thread(target=process_rpdo_1, args=())

print("SETTING NMT TO OPERATIONAL")
atv_node.nmt.state = 'OPERATIONAL'
print("SETTING NODE STATE TO 'SWITCHED ON'")
atv_node.state = 'SWITCHED ON'

atv_node.rpdo.read()
atv_node.tpdo.read()

tpdo_1_thread.start()
atv_node.rpdo[1][1].phys = target_vel_value
atv_node.trans_type = 'PERIODIC'
atv_node.rpdo[1].start(100)

atv_node.state = 'OPERATION ENABLED'

tolerance = 10
while current_vel_value < target_vel_value - tolerance:
    time.sleep(0.2)

rpdo_1_thread.start()
rpdo_1_thread.join()


try:
    atv_node.state = 'SWITCHED ON'
except RuntimeError as e:
    print(e)

while current_vel_value > 0:
    time.sleep(0.2)

keep_reading = False

代码本身按预期工作。电机开始加速到预期的目标速度。但是当我的

rpdo_1_thread
开始应用新值时,电机不会改变其控制力,我不明白为什么。这是将记录器设置为
DEBUG
时输出的相关部分:

DEBUG:canopen.variable:Value of Control Effort (0x6044:0) is 1000
    TPDO[1][1].phys:1000
DEBUG:canopen.variable:Writing Target Velocity (0x6042:0) = 600
DEBUG:canopen.pdo.base:Updating Target Velocity to b'5802' in RxPDO1_node32
DEBUG:can.pcan:Data: bytearray(b'')
DEBUG:can.pcan:Type: <class 'bytearray'>
Setting velocity to 600
DEBUG:canopen.variable:Value of Control Effort (0x6044:0) is 1000
    TPDO[1][1].phys:1000
DEBUG:canopen.variable:Writing Target Velocity (0x6042:0) = 500
DEBUG:canopen.pdo.base:Updating Target Velocity to b'f401' in RxPDO1_node32
DEBUG:can.pcan:Data: bytearray(b'')
DEBUG:can.pcan:Type: <class 'bytearray'>
Setting velocity to 500
DEBUG:canopen.variable:Value of Control Effort (0x6044:0) is 1000

我尝试了

rpdo[1].transmit()
,它将正确的速度应用于控制力,但电机电源状态变为
SWITCHED_ON_DISABLED
。然后,我必须通过发送
OPERATION_ENABLED
再次启用电机。然而,我想不断更新值,而不是每次更新都停止电机。

非常感谢任何帮助。

python python-3.x can-bus canopen python-can
© www.soinside.com 2019 - 2024. All rights reserved.