@@ -243,6 +243,46 @@ class Neosoco(Robot):
243
243
'0' : 0
244
244
}
245
245
246
+ _SERVO_MOTOR_DEGREE_CVT = {
247
+ '180' : 180 ,
248
+ '175' : 175 ,
249
+ '170' : 170 ,
250
+ '165' : 165 ,
251
+ '160' : 160 ,
252
+ '155' : 155 ,
253
+ '150' : 150 ,
254
+ '145' : 145 ,
255
+ '140' : 140 ,
256
+ '135' : 135 ,
257
+ '130' : 130 ,
258
+ '125' : 125 ,
259
+ '120' : 120 ,
260
+ '115' : 115 ,
261
+ '110' : 110 ,
262
+ '105' : 105 ,
263
+ '100' : 100 ,
264
+ '95' : 95 ,
265
+ '90' : 90 ,
266
+ '85' : 85 ,
267
+ '80' : 80 ,
268
+ '75' : 75 ,
269
+ '70' : 70 ,
270
+ '65' : 65 ,
271
+ '60' : 60 ,
272
+ '55' : 55 ,
273
+ '50' : 50 ,
274
+ '45' : 45 ,
275
+ '40' : 40 ,
276
+ '35' : 35 ,
277
+ '30' : 30 ,
278
+ '25' : 25 ,
279
+ '20' : 20 ,
280
+ '15' : 15 ,
281
+ '10' : 10 ,
282
+ '5' : 5 ,
283
+ '0' : 0
284
+ }
285
+
246
286
_MOTOR_DIR = {
247
287
'forward' : 16 ,
248
288
'backward' : 32
@@ -252,6 +292,14 @@ class Neosoco(Robot):
252
292
'forward' : 192 ,
253
293
'backward' : 208
254
294
}
295
+
296
+ _SERVO_MOTOR_DIR_FOR_DEG = {
297
+ 'forward' : 188 ,
298
+ 'backward' : 189
299
+ }
300
+
301
+ _SERVO_MOTOR_STOP = 254
302
+ _SERVO_MOTOR_RESET_DEG = 186
255
303
256
304
_robots = {}
257
305
@@ -522,6 +570,20 @@ def motor_rotate(self, motor='both', direction='forward', speed='100'):
522
570
else :
523
571
raise TypeError
524
572
573
+ def _write_to_output_port (self , port , out_val ):
574
+ if port .lower () == 'out1' :
575
+ self .write (Neosoco .OUTPUT_1 , out_val )
576
+ elif port .lower () == 'out2' :
577
+ self .write (Neosoco .OUTPUT_2 , out_val )
578
+ elif port .lower () == 'out3' :
579
+ self .write (Neosoco .OUTPUT_3 , out_val )
580
+ elif port .lower () == 'all' :
581
+ self .write (Neosoco .OUTPUT_1 , out_val )
582
+ self .write (Neosoco .OUTPUT_2 , out_val )
583
+ self .write (Neosoco .OUTPUT_3 , out_val )
584
+ else :
585
+ raise ValueError ('Wrong value of out port' )
586
+
525
587
def servo_motor_rotate (self , port = 'out1' , direction = 'forward' , speed = '100' ):
526
588
if isinstance (port , str ) and isinstance (direction , str ) and isinstance (speed , str ):
527
589
if speed == 'in1' or speed == 'in2' or speed == 'in3' :
@@ -539,24 +601,69 @@ def servo_motor_rotate(self, port='out1', direction='forward', speed='100'):
539
601
else :
540
602
raise ValueError ('Wrong value of direction' )
541
603
542
- outValue = direction + speed
543
- if outValue == direction :
544
- outValue = 254
604
+ out_val = direction + speed
605
+ if out_val == direction : # Speed is 0
606
+ out_val = self ._SERVO_MOTOR_STOP
607
+ else :
608
+ out_val = out_val - 1
609
+
610
+ self ._write_to_output_port (port , out_val )
611
+ else :
612
+ raise TypeError
613
+
614
+ def servo_motor_reset_degree (self , port = 'out1' ):
615
+ if isinstance (port , str ):
616
+ self ._write_to_output_port (port , self ._SERVO_MOTOR_RESET_DEG )
617
+ Runner .wait (100 )
618
+ self ._write_to_output_port (port , 1 ) # Set where the motor is to 1 degree
619
+ Runner .wait (100 )
620
+ else :
621
+ raise TypeError
622
+
623
+ def servo_motor_rotate_by_degree (self , port = 'out1' , direction = 'forward' , speed = '100' , degree = '90' ):
624
+ if isinstance (port , str ) and isinstance (direction , str ) and \
625
+ isinstance (speed , str ) and isinstance (degree , str ):
626
+ if direction .lower () == 'forward' :
627
+ direction = self ._SERVO_MOTOR_DIR_FOR_DEG ['forward' ]
628
+ elif direction .lower () == 'backward' :
629
+ direction = self ._SERVO_MOTOR_DIR_FOR_DEG ['backward' ]
630
+ else :
631
+ raise ValueError ('Wrong value of direction' )
632
+
633
+ if speed .lower () == 'in1' :
634
+ speed = self .read (Neosoco .INPUT_1 )
635
+ elif speed .lower () == 'in2' :
636
+ speed = self .read (Neosoco .INPUT_2 )
637
+ elif port .lower () == 'in3' :
638
+ speed = self .read (Neosoco .INPUT_3 )
639
+ elif speed in self ._SERVO_MOTOR_PERCENT_CVT .keys ():
640
+ speed = self ._SERVO_MOTOR_PERCENT_CVT [speed ]
545
641
else :
546
- outValue = outValue - 1
642
+ raise ValueError ('Wrong value of speed' )
643
+ speed = max (speed , 0 )
644
+ speed = min (speed , 100 )
645
+ speed = math .ceil (speed / 10 ) + 240 # range of speed is 240 ~ 250
547
646
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 )
647
+ if degree .lower () == 'in1' :
648
+ degree = self .read (Neosoco .INPUT_1 )
649
+ elif port .lower () == 'in2' :
650
+ degree = self .read (Neosoco .INPUT_2 )
651
+ elif port .lower () == 'in3' :
652
+ degree = self .read (Neosoco .INPUT_3 )
653
+ elif degree in self ._SERVO_MOTOR_DEGREE_CVT .keys ():
654
+ degree = self ._SERVO_MOTOR_DEGREE_CVT [degree ]
558
655
else :
559
- raise ValueError ('Wrong value of out port' )
656
+ raise ValueError ('Wrong value of degree' )
657
+ degree = max (degree , 0 )
658
+ degree = min (degree , 180 ) # max of degree is 180
659
+ degree = degree + 1
660
+
661
+ self ._write_to_output_port (port , direction )
662
+ Runner .wait (100 )
663
+ self ._write_to_output_port (port , speed )
664
+ Runner .wait (100 )
665
+ self ._write_to_output_port (port , degree )
666
+ Runner .wait (100 )
560
667
else :
561
668
raise TypeError
562
669
0 commit comments