Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5b07bdd

Browse files
committedMay 28, 2019
Merge branch 'serial' of https://github.com/PetoiCamp/OpenCat
2 parents 61258d6 + 1d99740 commit 5b07bdd

File tree

7 files changed

+173
-13
lines changed

7 files changed

+173
-13
lines changed
 
Binary file not shown.

‎SerialMasterPython/ardSerial.py

+25-12
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,53 @@
1313
bytesize=serial.EIGHTBITS,
1414
timeout=1
1515
)
16-
17-
def serialWriteNumToByte(token, var=[]): # Only to be used for c m i l within Python
18-
if token =='c' or token =='m':
19-
instrStr = token + str(var[0])+" "+str(var[1])+'\n'
16+
def wrapper(task): #Structure is [token, var=[], time]
17+
print task
18+
if len(task)==2:
19+
serialWriteByte([task[0]])
20+
elif isinstance(task[1][0],int):
21+
serialWriteNumToByte(task[0],task[1])
22+
else:
23+
serialWriteByte(task[1])
24+
time.sleep(task[-1])
2025

21-
elif token == 'l' or token=='i':
26+
def serialWriteNumToByte(token, var=[]): # Only to be used for c m u b i l o within Python
27+
#print("Num Token "); print(token);print(" var ");print(var);print("\n\n");
28+
if token == 'l' or token=='i':
2229
var=list(map(lambda x:int(x), var))
2330
instrStr=token+struct.pack('b' * len(var), *var)+'~'
24-
ser.write(instrStr)
31+
elif token =='c' or token =='m' or token =='u' or token =='b':
32+
instrStr = token + str(var[0])+" "+str(var[1])+'\n'
33+
print "!!!!"+ instrStr
34+
ser.write(instrStr)
2535

2636
def serialWriteByte(var=[]):
2737
token = var[0][0]
28-
if (token == 'c' or token == 'm') and len(var)>=2:
38+
#print var
39+
if (token == 'c' or token == 'm' or token=='b' or token=='u') and len(var)>=2:
2940
instrStr=""
3041
for element in var:
3142
instrStr=instrStr +element+" "
32-
elif token == 'l' or token=='i':
43+
elif token == 'l' or token=='i' :
3344
if(len(var[0])>1):
3445
var.insert(1,var[0][1:])
3546
var[1:]=list(map(lambda x:int(x), var[1:]))
3647
instrStr = token+struct.pack('b' * len(var[1:]), *var[1:])+'~'
37-
3848
elif token == 'w' or token == 'k':
3949
instrStr = var[0] + '\n'
4050
else:
4151
instrStr = token
52+
print "!!!!!!! "+instrStr
4253
ser.write(instrStr)
4354

4455
if __name__ == '__main__':
4556
counter=0
4657
if len(sys.argv) >= 2:
47-
# print(sys.argv[1][0], sys.argv[1:]) #remove later
48-
serialWriteByte(sys.argv[1:])
49-
58+
if len(sys.argv)==2:
59+
wrapper([sys.argv[1],0])
60+
else:
61+
# print [sys.argv[1][0],sys.argv[1:],0]
62+
wrapper([sys.argv[1][0],sys.argv[1:],0])
5063
else:
5164
while True:
5265
for a in np.arange(0, 2 * math.pi, 0.2):

‎SerialMasterPython/ardSerial.pyc

459 Bytes
Binary file not shown.

‎SerialMasterPython/debuglog.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
Testing command-line inputs
4+
5+
./ardserial.py c->works
6+
7+
./ardserial.py c15-> gives c1 5..which might be a problem
8+
9+
./ardserial.py m1 75, m 1 75 also work
10+
11+
./ardserial.py u-> works
12+
13+
./ardSerial.py i 1 9 9 13 13 1-> works
14+
15+
16+
./ardserial.py u 0 7->works
17+
18+
Testing wrapper() with schedule
19+
20+
The cases with ints arrays work
21+
22+
Single letter cases seem to work (although things 'ktr' and 'c8' might have issues)
23+
24+
string arrays might be an issues
25+
26+
Even though the last process is a d, the light doesnt turn off.
27+
28+
Beep works
29+
30+
There might be a bug in 'k'

‎SerialMasterPython/example.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,30 @@
99
import numpy as np
1010

1111

12+
schedule = [['m',[1,45],1],\
13+
['d',5],\
14+
['m',[1,-30],1],\
15+
['d',5],\
16+
17+
['d',5],\
18+
19+
['d',5],\
20+
21+
['d',5],\
22+
23+
['d',5],\
24+
['b','10','255',1],\
25+
['ktr',10]]
26+
27+
for task in schedule:
28+
wrapper(task)
1229
for i in range(1000):
1330
#serialWriteByte(["i","0",str(int(15*math.sin(2*math.pi*i/100))),"1",str(int(15*math.sin(4*math.pi*i/100))),"2",str(int(15*math.sin(2*math.pi*i/100)))])
1431
#serialWriteNumToByte("i",[0,int(15*math.sin(2*math.pi*i/100)),1,int(15*math.sin(4*math.pi*i/100)),2,int(15*math.sin(2*math.pi*i/100))])
1532
#serialWriteByte(["m","1","10"])
1633
#ser.write("m1 10\n")
1734

18-
serialWriteNumToByte('i',[0,30*math.sin(4*math.pi*i/500),1,30*math.sin(4*math.pi*i/500+math.pi/2),2,30*math.cos(4*math.pi*i/500)]) #serialWriteNumToByte(['c'])
35+
# wrapper('i',[0,30*math.sin(4*math.pi*i/500),1,30*math.sin(4*math.pi*i/500+math.pi/2),2,30*math.cos(4*math.pi*i/500)]) #serialWriteNumToByte(['c'])
1936
if i%250==0:
2037
print("should stop")
2138
time.sleep(1)

‎SerialMasterPython/test.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
from ardSerial import wrapper
3+
4+
5+
schedule = [['ktr',10],\
6+
['d',2],\
7+
['c',5],\
8+
['d',2],\
9+
['c0',5],\
10+
['d',2],\
11+
['c2',5],\
12+
['d',2],\
13+
['c15',5],\
14+
['d',2],\
15+
['m',[1,-30],1],\
16+
['d',2],\
17+
['i',[5,50,7,100,1,0],1],\
18+
['d',2],\
19+
['l',[1,2,3,4,5,6,7,8,9,1,2,3,4,5,6],5],\
20+
['d',2],\
21+
['m',['m','5','55'],1],\
22+
['d',2],\
23+
['b',['b','10','255'],1],['d',2]]
24+
25+
26+
for task in schedule:
27+
wrapper(task)

‎SerialMasterPython/test_File.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import unittest
2+
from ardSerial import wrapper
3+
4+
class TestSum(unittest.TestCase):
5+
def test_scheduler(self):
6+
schedule = [['ktr',1000],\
7+
['d',1500],\
8+
['c',500],\
9+
['d',150],\
10+
['c0',5],\
11+
['d',15],\
12+
['c2',5],\
13+
['d',15],\
14+
['c15',5],\
15+
['d',15],\
16+
['m',[1,-30],1],\
17+
['d',15],\
18+
['i',[5,50,7,100,1,0],1],\
19+
['d',15],\
20+
['l',[1,2,3,4,5,6,7,8,9,1,2,3,4,5,6],5],\
21+
['d',15],\
22+
['m',['m','5','55'],1],\
23+
['d',15],\
24+
['b',['b','10','255'],1],['d',15]]
25+
26+
27+
for task in schedule:
28+
wrapper(task)
29+
30+
'''
31+
sol_str=['ktr','d','c','c0','c2','c15',\
32+
['m',[1,-30]],\
33+
['i',[5,50,7,100,1,0]],\
34+
['l',[1,2,3,4,5,6,7,8,9,1,2,3,4,5,6]],\
35+
['m','5','55'],\
36+
['b','10','255'],'d']
37+
38+
#A for loop here would have made it diffcult to trace the errors
39+
40+
self.assertEqual(wrapper(schedule[0]),sol_str[0])
41+
self.assertEqual(wrapper(schedule[1]),sol_str[1])
42+
self.assertEqual(wrapper(schedule[2]),sol_str[2])
43+
self.assertEqual(wrapper(schedule[3]),sol_str[3])
44+
self.assertEqual(wrapper(schedule[4]),sol_str[4])
45+
self.assertEqual(wrapper(schedule[5]),sol_str[5])
46+
self.assertEqual(wrapper(schedule[6]),sol_str[6])
47+
self.assertEqual(wrapper(schedule[7]),sol_str[7])
48+
self.assertEqual(wrapper(schedule[8]),sol_str[8])
49+
self.assertEqual(wrapper(schedule[9]),sol_str[9])
50+
self.assertEqual(wrapper(schedule[10]),sol_str[10])
51+
self.assertEqual(wrapper(schedule[11]),sol_str[11])
52+
53+
schedule = [['m',[1,45],1],\
54+
['d',5],\
55+
['m',[1,-30],1],\
56+
['b',['b','10','255'],1],\
57+
['ktr',10]]
58+
sol_str=[['m',[1,45]],\
59+
'd',\
60+
['m',[1,-30]],\
61+
['b','10','255'],\
62+
'ktr']
63+
#A for loop here would have made it diffcult to trace the errors
64+
self.assertEqual(wrapper(schedule[0]),sol_str[0])
65+
self.assertEqual(wrapper(schedule[1]),sol_str[1])
66+
self.assertEqual(wrapper(schedule[2]),sol_str[2])
67+
self.assertEqual(wrapper(schedule[3]),sol_str[3])
68+
self.assertEqual(wrapper(schedule[4]),sol_str[4])
69+
# assert pro.wrapper(schedule[5])==
70+
'''
71+
72+
if __name__ == '__main__':
73+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.