Difference between revisions of "Nxt-python-hints"
Jump to navigation
Jump to search
(New page: Back to [https://wiki.cs.earlham.edu/index.php/Robotics-2010 Robotics Main Page] ---- == Python Programming Hints == *Wrap your code in a try/finally block so that you can stop the motors ...) |
|||
Line 7: | Line 7: | ||
finally: | finally: | ||
[stop all your motors] | [stop all your motors] | ||
+ | |||
+ | *Generalized kill-switch code | ||
+ | class thread_wait( threading.Thread ): | ||
+ | def __init__( self, condition, action ): | ||
+ | threading.Thread.__init__( self ) | ||
+ | self.condition = condition | ||
+ | self.action = action | ||
+ | |||
+ | def run( self ): | ||
+ | while not self.condition(): | ||
+ | sleep(0.1) | ||
+ | self.action | ||
+ | Usage: | ||
+ | kill_switch_thread = thread_wait( get_kill_switch_function, suicide_function ) | ||
+ | kill_switch_thread.start() |
Revision as of 08:14, 27 April 2010
Back to Robotics Main Page
Python Programming Hints
- Wrap your code in a try/finally block so that you can stop the motors on control-c.
try: [your code here] finally: [stop all your motors]
- Generalized kill-switch code
class thread_wait( threading.Thread ): def __init__( self, condition, action ): threading.Thread.__init__( self ) self.condition = condition self.action = action def run( self ): while not self.condition(): sleep(0.1) self.action
Usage:
kill_switch_thread = thread_wait( get_kill_switch_function, suicide_function ) kill_switch_thread.start()