SunFounder PiCrawler Kit
(continued from previous page)
def show_info():
print("\033[H\033[J",end='') # clear terminal windows
print(manual)
def main():
show_info()
while True:
key = readchar.readkey()
key = key.lower()
if key in('wsad'):
if 'w' == key:
crawler.do_action('forward',1,speed)
elif 's' == key:
crawler.do_action('backward',1,speed)
elif 'a' == key:
crawler.do_action('turn left',1,speed)
elif 'd' == key:
crawler.do_action('turn right',1,speed)
sleep(0.05)
show_info()
elif key == readchar.key.CTRL_C:
print("\n Quit")
break
sleep(0.02)
if __name__ == "__main__":
main()
How it works?
PiCrawler should take appropriate action based on the keyboard characters read. The lower() function converts upper
case characters into lower case characters, so that the letter remains valid regardless of case.
def main():
show_info()
while True:
key = readchar.readkey()
key = key.lower()
if key in('wsad'):
if 'w' == key:
crawler.do_action('forward',1,speed)
elif 's' == key:
crawler.do_action('backward',1,speed)
elif 'a' == key:
crawler.do_action('turn left',1,speed)
elif 'd' == key:
crawler.do_action('turn right',1,speed)
sleep(0.05)
show_info()
(continues on next page)
3.4. Keyboard Control 43