0
from ev3dev.ev3 import *

from time import sleep


ml = LargeMotor('outB')
mr = LargeMotor('outC')
ts = TouchSensor()
cs = ColorSensor()
us = UltrasonicSensor()

cs.mode = 'COL-REFLECT'
us.mode = 'US-DIST-CM'


def motorenStop(stopaction):
    ml.stop(stop_action=stopaction)
    mr.stop(stop_action=stopaction)


def motorenRun(speed_sp, time_sp = None, stop_action = None):
    if time_sp is not None and stop_action is not None:
        ml.run_timed(time_sp=time_sp, speed_sp=speed_sp, stop_action=stop_action)
        mr.run_timed(time_sp=time_sp, speed_sp=speed_sp, stop_action=stop_action)
    else:
        ml.run_forever(speed_sp)
        mr.run_forever(speed_sp)




def drive():

    while 1==1:
        motorenRun(300)

输出:

Traceback (most recent call last):
  File "/home/robot/helloworld/main.py", line 21, in <module>
    drive()
  File "/home/robot/helloworld/drive.py", line 35, in drive
    motorenRun(300)
  File "/home/robot/helloworld/drive.py", line 26, in motorenRun
    ml.run_forever(speed_sp)
TypeError: run_forever() takes 1 positional argument but 2 were given

我试图用 python 运行我的 EV3,现在我遇到了问题,即 2 个参数被传递给一个函数。我搜索了我的问题,但找不到任何解决方案。我该如何解决这个错误?

4

1 回答 1

1

run_forever是一个默认不接受任何参数的函数。但是,您会注意到它有参数**kwargs。这意味着您可以像这样传入可选的命名参数:

ml.run_forever(speed_sp=speed_sp)

试试看是否有效。

于 2019-12-24T23:17:42.973 回答