Skip to content

Commit 8620164

Browse files
committed
First commit motor_rotate() API
1 parent e14152c commit 8620164

File tree

2 files changed

+78
-29
lines changed

2 files changed

+78
-29
lines changed

neopia/neosoco.py

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def convert_scale(self, port='in1', omin=0, omax=255, tmin=0, tmax=100):
400400
raise TypeError
401401

402402
def led_on(self, port='out1', brightness='100'):
403-
cvt_dic = {
403+
percent_cvt = {
404404
'100': 255,
405405
'90': 230,
406406
'80': 204,
@@ -412,8 +412,8 @@ def led_on(self, port='out1', brightness='100'):
412412
'20': 51,
413413
'10': 26
414414
}
415-
if brightness in cvt_dic.keys():
416-
cvt_val = cvt_dic[brightness]
415+
if brightness in percent_cvt.keys():
416+
cvt_val = percent_cvt[brightness]
417417
else:
418418
raise ValueError('Wrong value of percentage')
419419
if isinstance(port, str):
@@ -451,18 +451,19 @@ def led_off(self, port='port1'):
451451

452452
def motor_move(self, direction='forward'):
453453
if isinstance(direction, str):
454+
speed = self._MOTOR_PERCENT_CVT['60']
454455
if direction.lower() =='forward':
455-
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['forward']+self._MOTOR_PERCENT_CVT['60'])
456-
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['forward']+self._MOTOR_PERCENT_CVT['60'])
456+
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['forward']+speed)
457+
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['forward']+speed)
457458
elif direction.lower() =='backward':
458-
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['backward']+self._MOTOR_PERCENT_CVT['60'])
459-
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['backward']+self._MOTOR_PERCENT_CVT['60'])
459+
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['backward']+speed)
460+
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['backward']+speed)
460461
elif direction.lower() =='left':
461-
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['backward']+self._MOTOR_PERCENT_CVT['60'])
462-
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['forward']+self._MOTOR_PERCENT_CVT['60'])
462+
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['backward']+speed)
463+
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['forward']+speed)
463464
elif direction.lower() =='right':
464-
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['forward']+self._MOTOR_PERCENT_CVT['60'])
465-
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['backward']+self._MOTOR_PERCENT_CVT['60'])
465+
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['forward']+speed)
466+
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['backward']+speed)
466467
elif direction.lower() =='stop':
467468
self.write(Neosoco.LEFT_MOTOR, 0)
468469
self.write(Neosoco.RIGHT_MOTOR, 0)
@@ -472,6 +473,59 @@ def motor_move(self, direction='forward'):
472473
raise TypeError
473474
Runner.wait(100) # Since broadcast from controller is per 100ms
474475

476+
def _convert_input_port_scale(self, port, limit_val):
477+
if port.lower() =='in1':
478+
value = self.read(Neosoco.INPUT_1)
479+
elif port.lower() =='in2':
480+
value = self.read(Neosoco.INPUT_2)
481+
elif port.lower() =='in3':
482+
value = self.read(Neosoco.INPUT_3)
483+
else:
484+
raise ValueError('Wrong value of port')
485+
486+
if value:
487+
value = max(value, 0)
488+
value = min(value, 100)
489+
value = math.ceil(value / 100 * limit_val)
490+
return value
491+
492+
def motor_rotate(self, motor='both', direction='forward', speed='100'):
493+
if isinstance(motor, str) and isinstance(direction, str) and isinstance(speed, str):
494+
if speed == 'in1' or speed == 'in2' or speed == 'in3' :
495+
# Map to 0~15 from 0~100(max), it's same as Entry
496+
speed = self._convert_input_port_scale(speed, 15)
497+
elif speed in self._MOTOR_PERCENT_CVT.keys():
498+
speed = self._MOTOR_PERCENT_CVT[speed]
499+
else:
500+
raise ValueError('Wrong value of speed')
501+
502+
if direction.lower() =='forward':
503+
l_direction = self._MOTOR_DIR['forward']
504+
r_direction = self._MOTOR_DIR['forward']
505+
elif direction.lower() =='backward':
506+
l_direction = self._MOTOR_DIR['backward']
507+
r_direction = self._MOTOR_DIR['backward']
508+
elif direction.lower() =='left':
509+
l_direction = self._MOTOR_DIR['backward']
510+
r_direction = self._MOTOR_DIR['forward']
511+
elif direction.lower() =='right':
512+
l_direction = self._MOTOR_DIR['forward']
513+
r_direction = self._MOTOR_DIR['backward']
514+
else:
515+
raise ValueError('Wrong value of direction')
516+
517+
if motor.lower() == 'both':
518+
self.write(Neosoco.LEFT_MOTOR, l_direction + speed)
519+
self.write(Neosoco.RIGHT_MOTOR, r_direction + speed)
520+
elif motor.lower() == 'left':
521+
self.write(Neosoco.LEFT_MOTOR, l_direction + speed)
522+
elif motor.lower() == 'right':
523+
self.write(Neosoco.RIGHT_MOTOR, r_direction + speed)
524+
else:
525+
raise ValueError('Wrong value of motor')
526+
else:
527+
raise TypeError
528+
475529
def buzzer(self, pitch='3', note='c', beats='4'):
476530
self.write(Neosoco.NOTE, 0) # init
477531
if not isinstance(pitch, str) or not (int(pitch) >= 1 and int(pitch) <= 6):
@@ -507,21 +561,9 @@ def buzzer(self, pitch='3', note='c', beats='4'):
507561

508562
def buzzer_by_port(self, port='in1'):
509563
if isinstance(port, str):
510-
if port.lower() =='in1':
511-
value = self.read(Neosoco.INPUT_1)
512-
elif port.lower() =='in2':
513-
value = self.read(Neosoco.INPUT_2)
514-
elif port.lower() =='in3':
515-
value = self.read(Neosoco.INPUT_3)
516-
else:
517-
raise ValueError('Wrong value of port')
518-
519-
if value:
520-
# Map to 0~65 from 0~100, it's same as Entry
521-
value = max(value, 0)
522-
value = min(value, 100)
523-
value = math.ceil(value / 100 * 65)
524-
self.write(Neosoco.NOTE, value)
564+
# Map to 0~65 from 0~100(max), it's same as Entry
565+
value = self._convert_input_port_scale(port, 65)
566+
self.write(Neosoco.NOTE, value)
525567
else:
526568
raise TypeError
527569

test.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
# else:
1818
# n.led_off('out1')
1919

20-
# case4) Move forth and back during 1s and stop
20+
# case4-1) Move forth and back during 1s and stop
2121
# n.motor_move('forward')
2222
# wait(500)
2323
# n.motor_move('backward')
2424
# wait(500)
2525
# n.motor_move('stop')
2626

27-
# case5) Moving cotrol by direction keys on the keyboard
27+
# case4-2) Moving control by direction keys on the keyboard
2828
# while True:
2929
# key = Keyboard.read()
3030

@@ -39,6 +39,13 @@
3939
# elif key == ' ':
4040
# n.motor_move('stop')
4141

42+
# case5) Move forth and back with speed 30% during 1s and stop
43+
n.motor_rotate('both', 'forward', '30')
44+
wait(500)
45+
n.motor_rotate('both', 'backward', '30')
46+
wait(500)
47+
n.motor_move('stop')
48+
4249
# case6) Play same note by pitch, sharp and flat, and a length of note
4350
# n.buzzer('3', n.NOTE_NAME_C)
4451
# n.buzzer('3', 'c')
@@ -51,7 +58,7 @@
5158

5259
# case7) Play a sound by value from input port
5360
# while True:
54-
# n.buzzer_by_port('in1')
61+
# n.buzzer_by_port('in1')
5562

5663
# case8) Color LED on with variable color by input port
5764
# r = g = b = n.convert_scale('in1', 0, 255, 85, 170) # Limit to middle brightness

0 commit comments

Comments
 (0)