SunFounder PiDog Kit, Release 1.0
(continued from previous page)
def set_head_pitch_init(pitch):
global head_pitch_init
head_pitch_init = pitch
my_dog.head_move([head_yrp], pitch_comp=pitch,
immediately=True, speed=HEAD_SPEED)
def change_status(status):
global current_status
current_status = status
if status == STATUS_STAND:
set_head_pitch_init(STAND_HEAD_PITCH)
my_dog.do_action('stand', wait=False, speed=70)
elif status == STATUS_SIT:
set_head_pitch_init(SIT_HEAD_PITCH)
my_dog.do_action('sit', wait=False, speed=70)
elif status == STATUS_LIE:
set_head_pitch_init(STAND_HEAD_PITCH)
my_dog.do_action('lie', wait=False, speed=70)
my_dog.wait_all_done()
def run_command():
global command, head_pitch_init
if not my_dog.is_legs_done() or not my_dog.is_head_done():
return
if command is None:
return
for name in COMMANDS:
if command in COMMANDS[name]["commands"]:
if "status" in COMMANDS[name]:
if current_status != COMMANDS[name]["status"]:
change_status(COMMANDS[name]["status"])
if "head_pitch" in COMMANDS[name]:
head_pitch_init = COMMANDS[name]["head_pitch"]
if "before" in COMMANDS[name]:
before_command = COMMANDS[name]["before"]
COMMANDS[before_command]["function"]()
if "function" in COMMANDS[name]:
COMMANDS[name]["function"]()
if "after" in COMMANDS[name]:
command = COMMANDS[name]["after"]
else:
command = None
break
COMMAND_KEY_MAP = {
"W": "trot",
"w": "forward",
"s": "backward",
"a": "turn left",
"d": "turn right",
"z": "lie down",
"x": "stand up",
"c": "sit",
"q": "bark",
(continues on next page)
68 Chapter 2. Play with Python