11#!/usr/bin/env python
22from serial .tools import list_ports
33import serial , time
4- ##SDM
54import platform
65if platform .system () == 'Windows' :
76 import _winreg as winreg
87if platform .system () == 'Linux' :
98 import glob
109import itertools
1110
11+
1212def enumerate_serial_ports ():
1313 """ Uses the Win32 registry to return a iterator of serial
1414 (COM) ports existing on this computer.
@@ -25,7 +25,7 @@ def enumerate_serial_ports():
2525 yield (str (val [1 ]))#, str(val[0]))
2626 except EnvironmentError :
2727 break
28- ##SDM
28+
2929
3030class Arduino (object ):
3131 def __init__ (self ,baud = 9600 ,port = "" ,timeout = 2 ):
@@ -36,7 +36,6 @@ def __init__(self,baud=9600,port="",timeout=2):
3636 self .baud = baud
3737 self .timeout = timeout
3838 self .ss_connected = False
39- ##SDM
4039 self .port = port
4140 if self .port == "" :
4241 if platform .system () == 'Windows' :
@@ -45,18 +44,12 @@ def __init__(self,baud=9600,port="",timeout=2):
4544 ports = glob .glob ("/dev/ttyUSB*" )
4645 for p in ports :
4746 print 'Found ' , p
47+ version = None
4848 try :
4949 print 'Testing ' , p
5050 self .sr = serial .Serial (p , self .baud ,timeout = self .timeout )
5151 time .sleep (2 )
52- cmd_str = '' .join (["@version%$!" ])
53- try :
54- self .sr .write (cmd_str )
55- self .sr .flush ()
56- except :
57- pass
58- version = self .sr .readline ().replace ("\r \n " ,"" )
59- #print version
52+ version = self .version ()
6053 if version != 'version' :
6154 raise Exception ('This is not a Shrimp/Arduino!' )
6255 self .port = p
@@ -65,12 +58,21 @@ def __init__(self,baud=9600,port="",timeout=2):
6558 except Exception , e :
6659 print "Exception: " , e
6760 pass
68- ##SDM
69- #--self.sr = serial.Serial(self.port, self.baud,timeout =self.timeout)
70- #--time.sleep(2)
7161 self .SoftwareSerial = SoftwareSerial (self )
7262 self .Servos = Servos (self )
7363 self .sr .flush ()
64+
65+
66+ def version (self ):
67+ cmd_str = '' .join (["@version%$!" ])
68+ try :
69+ self .sr .write (cmd_str )
70+ self .sr .flush ()
71+ except :
72+ pass
73+ version = self .sr .readline ().replace ("\r \n " ,"" )
74+ return version
75+
7476
7577 def findPort (self ):
7678 """
@@ -81,6 +83,7 @@ def findPort(self):
8183 if ("FTDIBUS" in pt [- 1 ]) or ("usbserial" in pt [- 1 ]):
8284 self .port = pt [0 ]
8385 return
86+
8487
8588 def digitalWrite (self ,pin ,val ):
8689 """
@@ -102,6 +105,7 @@ def digitalWrite(self,pin,val):
102105 except :
103106 pass
104107
108+
105109 def analogWrite (self ,pin ,val ):
106110 """
107111 Sends analogWrite pwm command
@@ -122,6 +126,7 @@ def analogWrite(self,pin,val):
122126 except :
123127 pass
124128
129+
125130 def analogRead (self ,pin ):
126131 """
127132 Returns the value of a specified
@@ -161,6 +166,7 @@ def pinMode(self,pin,val):
161166 self .sr .flush ()
162167 except :
163168 pass
169+
164170
165171 def pulseIn (self ,pin ,val ):
166172 """
@@ -170,7 +176,6 @@ def pulseIn(self,pin,val):
170176 pin: pin number for pulse measurement
171177 returns:
172178 duration : pulse length measurement
173-
174179 """
175180 if val == "LOW" :
176181 pin_ = - pin
@@ -187,6 +192,7 @@ def pulseIn(self,pin,val):
187192 return float (rd )
188193 except :
189194 return - 1
195+
190196
191197 def pulseIn_set (self ,pin ,val ,numTrials = 5 ):
192198 """
@@ -233,7 +239,6 @@ def pulseIn_set(self,pin,val,numTrials=5):
233239 if rd .isdigit () == True :
234240 if (int (rd ) > 1 ) == True :
235241 durations .append (int (rd ))
236- #print durations
237242 if len (durations ) > 0 :
238243 duration = int (sum (durations )) / int (len (durations ))
239244 else :
@@ -243,9 +248,12 @@ def pulseIn_set(self,pin,val,numTrials=5):
243248 return float (duration )
244249 except :
245250 return - 1
251+
246252
247253 def close (self ):
254+ self .sr .flush ()
248255 self .sr .close ()
256+
249257
250258 def digitalRead (self ,pin ):
251259 """
@@ -268,6 +276,7 @@ def digitalRead(self,pin):
268276 except :
269277 return 0
270278
279+
271280 def Melody (self , pin , melody , durations ):
272281 """
273282 Plays a melody.
@@ -306,14 +315,12 @@ def Melody(self, pin, melody, durations):
306315 d = str (durations [duration ])
307316 cmd_str = cmd_str + d + "%"
308317 cmd_str = cmd_str [:- 1 ]+ "$!"
309- #print cmd_str
310318 try :
311319 self .sr .write (cmd_str )
312320 self .sr .flush ()
313321 except :
314322 pass
315323 cmd_str = '' .join (["@nto%" ,str (pin ),"$!" ])
316- #print cmd_str
317324 try :
318325 self .sr .write (cmd_str )
319326 self .sr .flush ()
@@ -324,6 +331,7 @@ def Melody(self, pin, melody, durations):
324331 else :
325332 return - 1
326333
334+
327335 def capacitivePin (self , pin ):
328336 '''
329337 Input:
@@ -341,15 +349,13 @@ def capacitivePin(self, pin):
341349 self .sr .write (cmd_str )
342350 rd = self .sr .readline ().replace ("\r \n " ,"" )
343351 if rd .isdigit () == True :
344- #print "rd: " + str(rd)
345352 return int (rd )
346353
347354
348- ##SDM
349355class Shrimp (Arduino ):
350356 def __init__ (self ):
351357 Arduino .__init__ (self )
352- ##SDM
358+
353359
354360class Wires (object ):
355361 """
@@ -358,6 +364,7 @@ class Wires(object):
358364 def __init__ (self , board ):
359365 self .board = board
360366 self .sr = board .sr
367+
361368
362369class Servos (object ):
363370 """
@@ -368,6 +375,7 @@ def __init__(self, board):
368375 self .board = board
369376 self .sr = board .sr
370377 self .servo_pos = {}
378+
371379
372380 def attach (self ,pin ,min = 544 , max = 2400 ):
373381 cmd_str = '' .join (["@sva%" ,str (pin ),"%" ,str (min ),"%" ,str (max ),"$!" ])
@@ -383,6 +391,7 @@ def attach(self,pin,min = 544, max = 2400):
383391 return 1
384392 except :
385393 return 0
394+
386395
387396 def detach (self ,pin ):
388397 cmd_str = '' .join (["@svd%" ,str (position ),"$!" ])
@@ -393,6 +402,7 @@ def detach(self,pin):
393402 pass
394403 del self .servo_pos [pin ]
395404
405+
396406 def write (self ,pin ,angle ):
397407 position = self .servo_pos [pin ]
398408 cmd_str = '' .join (["@svw%" ,str (position ),"%" ,str (angle ),"$!" ])
@@ -401,6 +411,7 @@ def write(self,pin,angle):
401411 self .sr .flush ()
402412 except :
403413 pass
414+
404415
405416 def writeMicroseconds (self ,pin ,uS ):
406417 cmd_str = '' .join (["@svw%" ,str (position ),"%" ,str (uS ),"$!" ])
@@ -409,6 +420,7 @@ def writeMicroseconds(self,pin,uS):
409420 self .sr .flush ()
410421 except :
411422 pass
423+
412424
413425 def read (self ,pin ):
414426 if pin not in self .servo_pos .keys ():
@@ -427,6 +439,7 @@ def read(self,pin):
427439 except :
428440 return None
429441
442+
430443class SoftwareSerial (object ):
431444 """
432445 Class for Arduino software serial functionality
@@ -436,6 +449,7 @@ def __init__(self,board):
436449 self .sr = board .sr
437450 self .connected = False
438451
452+
439453 def begin (self ,p1 ,p2 ,baud ):
440454 """
441455 Create software serial instance on
@@ -454,6 +468,7 @@ def begin(self,p1,p2,baud):
454468 else :
455469 self .connected = False
456470 return False
471+
457472
458473 def write (self ,data ):
459474 """
@@ -473,6 +488,7 @@ def write(self,data):
473488 else :
474489 return False
475490
491+
476492 def read (self ):
477493 """
478494 returns first character read from
@@ -487,18 +503,3 @@ def read(self):
487503 return response
488504 else :
489505 return False
490-
491- ##if __name__=="__main__":
492- # quick test
493- ## board=Arduino(9600, 'COM9')
494- ## board.Servos.attach(9)
495- ## board.Servos.write(9,90)
496- ## time.sleep(1)
497- ## print board.Servos.read(9)
498- ## t=time.time()
499- ## board.Servos.write(9,1)
500- ## while True:
501- ## a=board.Servos.read(9)
502- ## if a == 1:
503- ## print "time",time.time() - t
504- ## break
0 commit comments