Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98324
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: raw_input and break |
| Date | 2015-11-05 20:09 -0500 |
| Organization | IISS Elusive Unicorn |
| Message-ID | <mailman.66.1446772190.16136.python-list@python.org> (permalink) |
| References | <mailman.58.1446743214.16136.python-list@python.org> <8lM_x.24516$cf7.4240@fx02.am1> |
On Thu, 05 Nov 2015 17:28:36 GMT, input/ldompeling@casema.nl declaimed the
following:
>Oke, lets try your code.Can you help me with that.
>This is my code:
>-------------------------------------------------
>from gopigo import *
>import time
>
>set_right_speed(150)
>set_left_speed(105)
Is there a typo there? If those are setting rotation speeds for
independent drive wheels, you will have a slow left turn enabled.
>enable_servo()
>fwd()
>print("forward 1x")
>time.sleep(4)
>stop()
>
>while True:
> servo(90)
> mindist = 80
>
> if mindist > us_dist(15):
"mindist" will always be 80 -- there is no code shown below that ever
changes it (so the above assignment can be put before the "while"
statement). That means your us_dist() function must somewhere return a
value greater than 80.
> bwd()
> print ("backward 1x")
> time.sleep(2)
> stop()
> right()
> time.sleep(1)
> stop()
> print("right 1x")
> time.sleep(2)
You stopped the right() call before ever doing any output
> stop()
> fwd()
> print("forward 2x")
> time.sleep(3)
> stop()
> left()
> time.sleep(1)
> print("left 1x")
> stop()
> fwd()
> print("forward 3x")
> time.sleep(2)
> stop()
>------------------------------------------------------
Off-hand, I'd try to get rid of a lot of that redundancy by defining
functions to encapsulate your sequence... (Python 2.x syntax -- not tested)
(FORWARD, BACKWARD, LEFT, RIGHT) = (1, 2, 3, 4)
SEQUENCE = [ (BACKWARD, 2),
(RIGHT, 1),
(FORWARD, 3),
(LEFT, 1),
(FORWARD, 2) ]
def move(drct, dly):
if drct == FORWARD:
fwd()
print "Forward"
elif drct == BACKWARD:
bwd()
print "Backward"
elif drct == LEFT:
left()
print "Left"
elif drct == RIGHT:
right()
print "Right"
else:
print "Invalid command: %s" % drct
time.sleep(dly)
stop()
notDone = True
servo(90)
mindist = 80
while notDone:
if mindist > us_dist(15):
for (dir, tm) in SEQUENCE:
move(dir, tm)
-=-=-=-=-
Now, if you don't mind having to also press the <enter> key, you could
use the threading module to handle the keyboard shutdown command instead of
using system specific modules to do raw keystroke input. You'd add
something before the while loop (and this is really pseudo-code, barely any
attempt at Python):
import threading
def cmdHandler():
global notDone
while True:
cmd = raw_input("Enter command> ").lower()
if cmd.startswith("s"):
notDone = False
break
else:
print "Unimplemented command: %s" % cmd
cmdThread = threading.Thread(target=cmdHandler)
cmdThread.start()
This concept doesn't require changes if the underlying OS changes, and
does open things up to having more complex commands (one could have
commands with arguments since the input is line oriented).
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
raw_input and break input/ldompeling@casema.nl - 2015-11-04 21:44 +0000
Re: raw_input and break Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-04 14:54 -0700
Re: raw_input and break Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-04 16:56 -0500
Re: raw_input and break input/ldompeling@casema.nl - 2015-11-04 22:37 +0000
Re: raw_input and break Steven D'Aprano <steve@pearwood.info> - 2015-11-05 11:44 +1100
Re: raw_input and break tian.su.yale@gmail.com - 2015-11-04 21:39 -0800
Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 09:22 +0000
Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 11:01 +0100
Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 10:40 +0000
Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 12:38 +0100
Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 12:59 +0000
Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 15:16 +0100
Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 14:34 +0000
Re: raw_input and break Peter Otten <__peter__@web.de> - 2015-11-05 18:06 +0100
Re: raw_input and break input/ldompeling@casema.nl - 2015-11-05 17:28 +0000
Re: raw_input and break Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-05 20:09 -0500
Re: raw_input and break input/ldompeling@casema.nl - 2015-11-06 05:08 +0000
Re: raw_input and break input/ldompeling@casema.nl - 2015-11-06 11:50 +0000
Re: raw_input and break Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-06 08:25 -0500
csiph-web