Troy 88
Posts: 5
Joined: Thu Apr 13, 2017 3:50 am
Location: New Zealand

Python help please

Sat Oct 27, 2018 12:35 am

hi all i hope someone can help me thanks in advance

so im building a robot with a rpi0 and python and have two codes i want to be able to change in between

the 1st code witch is the main code it boots into is just a remote control code that uses a portable keyboard as remote
the 2nd code is one that uses a ultrasonic sensor to move around on is own

So iv looked around but cant find a way to use a keyboard button press or whatever to switch back and forth at will

any help much appreciated

thanks :D

PhatFil
Posts: 1685
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Python help please

Sat Oct 27, 2018 12:44 am

First ensure both your programs/scripts terminate cleanly. No endless loops without a guaranteed to execute (at some point) break.

For the keyboard input script an exit key or combo should do the trick,

And with the navigation script perhaps on reaching its termination destination and a timeout perhaps (in case it gets lost) ?

Then you could write a small 3rd script to call each of your action scripts on demand perhaps.

Troy 88
Posts: 5
Joined: Thu Apr 13, 2017 3:50 am
Location: New Zealand

Re: Python help please

Sat Oct 27, 2018 1:01 am

thanks for a quick reply

both code run fine on their own and have a push button that "breaks" the code as follows

try:
while True:
char = screen.getch()
if char == ord('q'):
break

so at any time i press "q" it stops code and terminates

are you saying i should set it up so that it will kill the code and go back to this other 3rd program to witch i could start ether code i want with a keyboard input?

thanks

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python help please

Sat Oct 27, 2018 8:47 am

I think I would aim to combine both programs into (effectively) one. i.e. all the common code (presumably for steering and robot control) and main loop would go in a third program and the code specific to keyboard steering or ultrasonic steering would stay in their own file but made more function or class structured. The third program would then import the two originals and toggle between functionality.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”