Python: функция в параметрах exe c () не работает - PullRequest
0 голосов
/ 02 августа 2020

У меня есть код:

exec("""
def degreestoservostart(trim, degrees):
    return round(degrees * 11.1111111111 + 500 + """ + ("trim" + (str(trim)) + ")"))
trimflf = 0
print(degreestoservostart("flf", 500))

, но он вызывает ошибку:

Traceback (most recent call last):
  File "/home/pi/Desktop/Servo_Driver_HAT/Rasspberry Pi/python3/start.py", line 3, in <module>
    return round(degrees * 11.1111111111 + 500 + """ + ("trim" + (str(trim)) + ")"))
NameError: name 'trim' is not defined

Я хочу, чтобы он принимал flf как переменную trimflf

Изменить: Полный код:

#import trim
legsleft = {"rlf": 0,"rlk": 1,"rlh": 2,"mlf": 4,"mlk": 5,"mlh": 6,"flf": 12,"flk": 13,"flh": 14}
legsright = {"rrf": 0,"rrk": 1,"rrh": 2,"mrf": 4,"mrk": 5,"mrh": 6,"frf": 12,"frk": 13,"frh": 14}
legs = {"left": legsleft,"right": legsright}
ride = 50
"""trims"""
trimrrf = 0
trimrrk = 0
trimrrh = 0
trimmrf = 0
trimmrk = 0
trimmrh = 0
trimfrf = 0
trimfrk = 0
trimfrh = 0
trimrlf = 0
trimrlk = 0
trimrlh = 0
trimmlf = 0
trimmlk = 0
trimmlh = 0
trimflf = 0
trimflk = 0
trimflh = 0
"""set pos to None for all servos"""
rrfpos, rrkpos, rrhpos, mrfpos, mrkpos, mrhpos, frfpos, frkpos,\
        frhpos, rlfpos, rlkpos, rlhpos, mlfpos, mlkpos, mlhpos,\
        flfpos, flkpos, flhpos = None, None, None, None, None, None,\
        None, None, None, None, None, None, None, None, None, None, None, None
def degreestoservo(left, degrees):
    if left.lower() == "left":
        return round(degrees * 11.1111111111 + 500)
    return round(degrees * 11.1111111111 - 500)
def lefttoright(x):
    a = 90
    b = a - x
    c = b + a
    #print("a is: ", a, "\nb is: ", b, "\nc is:", c)
    while True:
        if c > 180:
            c -= 180
            #print("Now c is: ", c)
        elif c < 0:
            c += 180
            #print("Now c is: ", c)
        else:
            return round(c)
#exec("""
#def degreestoservostart(trim, degrees):
exec("""
def degreestoservostart(trim, degrees):
    return round(degrees * 11.1111111111 + 500 + """ + ("trim" + str(trim) + ")"))
print(degreestoservostart("flf", 500))
#    """)
##print(lefttorightleg(int(input("num: "))))
allservos = list(list(legsleft) + list(legsright))
allknees = ["rlk", "mlk", "flk", "rrk", "frk", "mrk"]
allfeet = ["mlf", "", "", "", "", ""]
"""other programs edit this:"""
...