Python Instruction For KT2

Welcome to the python insctruction of KT2, let's bootstrap you to run KT2 with simple guide!

Setting a Posture

Define a posture using the pos function:

pos(d1=0, d2=0, d3=0, d4=0, dur=0.75, dly=0, ofs=[0, 0, 0, 0], rev=False, inv=False)

d1, d2, d3, d4: Angles for front left, front right, rear left, and rear right legs. Positive for forward rotation, negative for backward.

dur: Duration of rotation.

dly: Delay after completing rotation.

ofs: Initial posture offsets.

rev: Reverse up/down.

inv: Invert front/back.

Example:

pos1 = pos(60, 60, -60, -60)
bot.exec({ n: [pos1] })

Designing an Action

Combine multiple postures into an action:

Example: Get down and stand up

def down_up():
    return [pos(0, 0, 0, 0), pos(-60, -60, 60, 60)]

bot.exec({ n: down_up() })

Combining Actions

Chain actions together:

Example: Combine two actions

def act1():
    return [pos(0, 0, 0, 0), pos(-60, -60, 60, 60)]

def act2():
    return [pos(-90, -90, 90, 90), pos(-60, -60, 60, 60)]

bot.exec({ n: act1() + act2() })

Using Sensors

Adapt movements based on sensor input:

Example: Follow pitch angle

car.motion.start_rpy()
while True:
    r, p, y = car.motion.rpy()
    if abs(p) > 5:
        bot.exec({ n:[pos(-90+p, -90+p, 90+p, 90+p, 0.2)] })
    else:
        bot.exec({ n:[pos(-90, -90, 90, 90, 0.2)] })
    sleep(0.1)

Built-in Postures and Actions

Utilize predefined postures like ofs_stand, ofs_fu, ofs_yang, and actions like flip, tui, pu.

Example: Pitch action

bot.exec({ n: fuyang() })

Gait and Movement

Control walking and turning:

Example: Move forward 5 steps

bot.exec([{ n: walk(), c:5 }])

Example: Corrected forward walk

twalk(5)

Example: Turn left 90 degrees

t_walk_left(90)

Synchronized Actions

Execute actions sequentially using wait=True:

Example: Move and pitch

bot.exec([{ n: walk(), c:5 }], wait=True)
sleep(0.1)
bot.exec([{ n: fuyang() }])

Immediate Stop

Stop all actions:

Example: Stop after 1 second

bot.exec([{ n: walk(), c:10 }])
sleep(1)
bot.stop()