Skip to content

Commit

Permalink
Added motor braking bugfix by Steve Castellotti.
Browse files Browse the repository at this point in the history
Got rid of too much whitespace in motor module.
  • Loading branch information
marcusw@cox.net committed Jul 24, 2009
1 parent fe322c6 commit 05d6ae9
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions nxt/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,68 +75,59 @@ def get_output_state(self):
def reset_position(self, relative):
self.brick.reset_motor_position(self.port, relative)

def update(self, power, tacho_limit, braking=False):
'''Use this to run a motor. power is a value between -127 and 128, tacho_limit is the
number of degrees to apply power for. Braking is wether or not to stop the motor
after turning the specified degrees (Under construction).'''
def update(self, power, tacho_limit, braking=False, max_retries=5):
'''Use this to run a motor. power is a value between -127 and 128, tacho_limit is
the number of degrees to apply power for (unreliable). Braking is wether or not
to stop the motor after turning the specified degrees. max_retries is the
maximum times an internal loop of the braking function runs, so it doesn't get
caught in an infinite loop.'''

if braking:
direction = (power > 0)*2-1
self.get_output_state()
starting_tacho_count = self.tacho_count

self._debug_out('tachocount: '+str(starting_tacho_count))

tacho_target = starting_tacho_count + tacho_limit*direction

self._debug_out('tacho target: '+str(tacho_target))

self.mode = MODE_MOTOR_ON
self.run_state = RUN_STATE_RUNNING
self.power = power
self.tacho_limit = tacho_limit

self._debug_out('Updating motor information...')

if braking:

self.update(power, 1, 0)

current_tacho = 0
retries = 0

while 1:

self._debug_out('checking tachocount...')

olderTachoCount = current_tacho
self.get_output_state()
current_tacho = self.tacho_count

self._debug_out('tachocount: '+str(current_tacho))

if current_tacho<tacho_target+10 and current_tacho>tacho_target-10:

self._debug_out('tachocount good, breaking from loop...')

break

else:
if current_tacho==olderTachoCount: #motor hasn't moved

self._debug_out('tachocount bad, giving extra power...')

retries = retries + 1
self.update(power, 1, 0)
else:
self._debug_out('tachocount bad, waiting...')

if (retries == max_retries):
self._debug_out('max retries reached, breaking from loop...')
break

self._debug_out('going in reverse...')

self.update(direction*60, 1)
time.sleep(0.01)
self.mode = MODE_BRAKE
self.set_output_state()

self._debug_out('difference from goal: '+str(self.get_output_state()[7]-tacho_target))

self.set_output_state()
self._debug_out('Updating finished. ending tachocount: '+str(self.get_output_state()[7]))

0 comments on commit 05d6ae9

Please sign in to comment.