Skip to content

Commit 46a2b10

Browse files
authored
Add motor_stop() API
* Addind motor_stop() API Whithout errors * Changed motor_move('stop') to motor_stop('both') Whithout errors
1 parent 0b5842c commit 46a2b10

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

neopia/neosoco.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,22 @@ def motor_move(self, direction='forward'):
512512
else:
513513
raise TypeError
514514
Runner.wait(100) # Since broadcast from controller is per 100ms
515+
516+
def motor_stop(self, which_motor: str):
517+
if isinstance(which_motor, str):
518+
if which_motor.lower() =='right':
519+
self.write(Neosoco.RIGHT_MOTOR, 0)
520+
elif which_motor.lower() =='left':
521+
self.write(Neosoco.LEFT_MOTOR, 0)
522+
elif which_motor.lower() =='both':
523+
self.write(Neosoco.LEFT_MOTOR, 0)
524+
self.write(Neosoco.RIGHT_MOTOR, 0)
525+
else:
526+
Runner.shutdown() # The motor will not stop unless the shutdown() function is used
527+
raise ValueError('Wrong value of motor')
528+
else:
529+
Runner.shutdown() # The motor will not stop unless the shutdown() function is used
530+
raise TypeError
515531

516532
def _convert_sacle_within_100(self, value, cvt_max_val):
517533
# Map to 0~limited_val from 0~100(max), it's same as Entry
@@ -706,4 +722,4 @@ def buzzer_by_port(self, port='in1'):
706722
value = self._convert_scale_from_input_port(port, 65)
707723
self.write(Neosoco.NOTE, value)
708724
else:
709-
raise TypeError
725+
raise TypeError

test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
# wait(500)
4848
# n.motor_rotate('both', 'backward', '30')
4949
# wait(500)
50-
# n.motor_move('stop')
50+
# n.motor_stop('both')
5151

5252
## Buzzer
5353
# case7) Play same note by pitch, sharp and flat, and a length of note
@@ -91,4 +91,4 @@
9191
# case 12) Rotate servo motor forward by 120 degrees at 50% speed within 3 seconds
9292
n.servo_reset_degree('out1')
9393
n.servo_rotate_by_degree('out1', 'forward', '50', '120')
94-
wait(3000)
94+
wait(3000)

0 commit comments

Comments
 (0)