@@ -229,10 +229,29 @@ class Neosoco(Robot):
229
229
'0' : 0
230
230
}
231
231
232
+ _SERVO_MOTOR_PERCENT_CVT = {
233
+ '100' : 100 ,
234
+ '90' : 90 ,
235
+ '80' : 80 ,
236
+ '70' : 70 ,
237
+ '60' : 60 ,
238
+ '50' : 50 ,
239
+ '40' : 40 ,
240
+ '30' : 30 ,
241
+ '20' : 20 ,
242
+ '10' : 10 ,
243
+ '0' : 0
244
+ }
245
+
232
246
_MOTOR_DIR = {
233
247
'forward' : 16 ,
234
248
'backward' : 32
235
249
}
250
+
251
+ _SERVO_MOTOR_DIR = {
252
+ 'forward' : 192 ,
253
+ 'backward' : 208
254
+ }
236
255
237
256
_robots = {}
238
257
@@ -446,7 +465,14 @@ def motor_move(self, direction='forward'):
446
465
raise TypeError
447
466
Runner .wait (100 ) # Since broadcast from controller is per 100ms
448
467
449
- def _convert_input_port_scale (self , port , limit_val ):
468
+ def _convert_sacle_within_100 (self , value , cvt_max_val ):
469
+ # Map to 0~limited_val from 0~100(max), it's same as Entry
470
+ value = max (value , 0 )
471
+ value = min (value , 100 )
472
+ value = math .ceil (value / 100 * cvt_max_val )
473
+ return value
474
+
475
+ def _convert_scale_from_input_port (self , port , cvt_max_val ):
450
476
if port .lower () == 'in1' :
451
477
value = self .read (Neosoco .INPUT_1 )
452
478
elif port .lower () == 'in2' :
@@ -457,16 +483,13 @@ def _convert_input_port_scale(self, port, limit_val):
457
483
raise ValueError ('Wrong value of port' )
458
484
459
485
if value :
460
- value = max (value , 0 )
461
- value = min (value , 100 )
462
- value = math .ceil (value / 100 * limit_val )
463
- return value
486
+ value = self ._convert_sacle_within_100 (value , cvt_max_val )
487
+ return value
464
488
465
489
def motor_rotate (self , motor = 'both' , direction = 'forward' , speed = '100' ):
466
490
if isinstance (motor , str ) and isinstance (direction , str ) and isinstance (speed , str ):
467
491
if speed == 'in1' or speed == 'in2' or speed == 'in3' :
468
- # Map to 0~15 from 0~100(max), it's same as Entry
469
- speed = self ._convert_input_port_scale (speed , 15 )
492
+ speed = self ._convert_scale_from_input_port (speed , 15 )
470
493
elif speed in self ._MOTOR_PERCENT_CVT .keys ():
471
494
speed = self ._MOTOR_PERCENT_CVT [speed ]
472
495
else :
@@ -499,6 +522,44 @@ def motor_rotate(self, motor='both', direction='forward', speed='100'):
499
522
else :
500
523
raise TypeError
501
524
525
+ def servo_motor_rotate (self , port = 'out1' , direction = 'forward' , speed = '100' ):
526
+ if isinstance (port , str ) and isinstance (direction , str ) and isinstance (speed , str ):
527
+ if speed == 'in1' or speed == 'in2' or speed == 'in3' :
528
+ speed = self ._convert_scale_from_input_port (speed , 10 )
529
+ elif speed in self ._SERVO_MOTOR_PERCENT_CVT .keys ():
530
+ speed = self ._SERVO_MOTOR_PERCENT_CVT [speed ]
531
+ speed = self ._convert_sacle_within_100 (speed , 10 )
532
+ else :
533
+ raise ValueError ('Wrong value of speed' )
534
+
535
+ if direction .lower () == 'forward' :
536
+ direction = self ._SERVO_MOTOR_DIR ['forward' ]
537
+ elif direction .lower () == 'backward' :
538
+ direction = self ._SERVO_MOTOR_DIR ['backward' ]
539
+ else :
540
+ raise ValueError ('Wrong value of direction' )
541
+
542
+ outValue = direction + speed
543
+ if outValue == direction :
544
+ outValue = 254
545
+ else :
546
+ outValue = outValue - 1
547
+
548
+ if port .lower () == 'out1' :
549
+ self .write (Neosoco .OUTPUT_1 , outValue )
550
+ elif port .lower () == 'out2' :
551
+ self .write (Neosoco .OUTPUT_2 , outValue )
552
+ elif port .lower () == 'out3' :
553
+ self .write (Neosoco .OUTPUT_3 , outValue )
554
+ elif port .lower () == 'all' :
555
+ self .write (Neosoco .OUTPUT_1 , outValue )
556
+ self .write (Neosoco .OUTPUT_2 , outValue )
557
+ self .write (Neosoco .OUTPUT_3 , outValue )
558
+ else :
559
+ raise ValueError ('Wrong value of out port' )
560
+ else :
561
+ raise TypeError
562
+
502
563
def buzzer (self , pitch = '3' , note = 'c' , beats = '4' ):
503
564
self .write (Neosoco .NOTE , 0 ) # init
504
565
if not isinstance (pitch , str ) or not (int (pitch ) >= 1 and int (pitch ) <= 6 ):
@@ -535,7 +596,7 @@ def buzzer(self, pitch='3', note='c', beats='4'):
535
596
def buzzer_by_port (self , port = 'in1' ):
536
597
if isinstance (port , str ):
537
598
# Map to 0~65 from 0~100(max), it's same as Entry
538
- value = self ._convert_input_port_scale (port , 65 )
599
+ value = self ._convert_scale_from_input_port (port , 65 )
539
600
self .write (Neosoco .NOTE , value )
540
601
else :
541
602
raise TypeError
0 commit comments