4 import csv, time, argparse;
7 from random import randrange
8 from GoodFETMCPCAN import GoodFETMCPCAN;
9 from experiments import experiments
10 from GoodFETMCPCANCommunication import GoodFETMCPCANCommunication
11 from intelhex import IntelHex;
16 class FordExperiments(experiments):
18 def __init__(self, dataLocation = "../../contrib/ThayerData/"):
19 GoodFETMCPCANCommunication.__init__(self, dataLocation)
20 #super(FordExperiments,self).__init__(self) #initialize chip
23 def mimic1056(self,packetData,runTime):
26 self.spitSetup(self.freq)
27 #FIGURE out how to clear buffers
28 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
29 packet1 = self.client.rxpacket();
31 packetParsed = self.client.packet2parsed(packet1);
32 #keep sniffing till we read a packet
33 while( packet1 == None or packetParsed.get('sID') != 1056 ):
34 packet1 = self.client.rxpacket()
36 packetParsed = self.client.packet2parsed(packet1)
37 recieveTime = time.time()
38 packetParsed = self.client.packet2parsed(packet1)
39 if( packetParsed['sID'] != 1056):
40 print "Sniffed wrong packet"
42 countInitial = ord(packetParsed['db3']) #initial count value
44 #set data packet to match what was sniffed or at least what was input
47 if(packetData.get(idx) == None):
48 packet.append(ord(packetParsed.get(idx)))
50 packet.append(packetData.get(idx))
52 #### split SID into different regs
53 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
54 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
55 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
56 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
58 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
60 self.client.txpacket(packet);
62 while( (time.time()-recieveTime) < runTime):
63 #care about db3 or packet[8] that we want to count at the rate that it is
64 dT = time.time()-tpast
66 db3 = (countInitial + math.floor((time.time()-recieveTime)/0.2))%255
68 self.client.txpacket(packet)
72 self.client.MCPrts(TXB0=True)
73 tpast = time.time() #update our transmit time on the one before
77 def cycledb1_1056(self,runTime):
81 #FIGURE out how to clear buffers
82 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
83 packet1 = self.client.rxpacket();
85 packetParsed = self.client.packet2parsed(packet1);
86 #keep sniffing till we read a packet
87 while( packet1 == None or packetParsed.get('sID') != 1056 ):
89 packet1 = self.client.rxpacket()
91 packetParsed = self.client.packet2parsed(packet1)
92 recieveTime = time.time()
93 packetParsed = self.client.packet2parsed(packet1)
94 if( packetParsed['sID'] != 1056):
95 print "Sniffed wrong packet"
98 #set data packet to match what was sniffed or at least what was input
101 packet.append(ord(packetParsed.get(idx)))
103 packet[1] = packetValue;
106 #### split SID into different regs
107 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
108 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
109 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
110 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
111 # lower nibble is DLC
112 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
114 self.client.txpacket(packet);
116 while( (time.time()-recieveTime) < runTime):
117 #care about db3 or packet[8] that we want to count at the rate that it is
118 dT = time.time()-tpast
121 #temp = ((packetValue+1))%2
123 # pV = packetValue%255
129 self.client.txpacket(packet)
131 tpast = time.time() #update our transmit time on the one before
134 def getBackground(self,sId):
135 packet1 = self.client.rxpacket();
137 packetParsed = self.client.packet2parsed(packet1);
138 #keep sniffing till we read a packet
139 while( packet1 == None or packetParsed.get('sID') != sId ):
140 packet1 = self.client.rxpacket()
142 packetParsed = self.client.packet2parsed(packet1)
144 #recieveTime = time.time()
147 def cycle4packets1279(self):
148 self.client.serInit()
151 self.addFilter([1279, 1279, 1279, 1279, 1279, 1279], verbose = False)
152 packetParsed = self.getBackground(1279)
154 if (packetParsed[db0] == 16):
155 # if it's the first of the four packets, replace the value in db7 with 83
156 packetParsed[db7] = 83
157 # transmit new packet
158 self.client.txpacket(packetParsed)
160 # otherwise, leave it alone
161 # transmit same pakcet we read in
162 self.client.txpacket(packetParsed)
163 # print the packet we are transmitting
167 def oscillateTemperature(self,time):
169 self.client.serInit()
171 #FIGURE out how to clear buffers
172 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
173 packetParsed = self.getBackground(1056)
175 #set data packet to match what was sniffed or at least what was input
178 packet.append(ord(packetParsed.get(idx)))
180 packet[1] = packetValue;
183 #### split SID into different regs
184 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
185 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
186 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
187 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
188 # lower nibble is DLC
189 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
191 self.client.txpacket(packet);
192 startTime = tT.time()
193 while( (tT.time()-startTime) < runTime):
194 dt = tT.time()-startTime
195 inputValue = ((2.0*math.pi)/20.0)*dt
196 value = 30*math.sin(inputValue)+130
198 #packet[5] = int(value)
205 self.client.txpacket(packet)
207 #tpast = time.time() #update our transmit time on the one before
212 #reset eveything on the chip
213 self.client.serInit()
215 duration = 20; #seconds
218 listenPacket = [2, 9, 6, 153, 153, 153, 153, 153]
220 #actual response by the car
221 #r1 = [34, 88, 0, 0, 0, 0, 0, 0]
222 #r2 = [33, 75, 50, 78, 51, 46, 72, 69 ]
223 #r3 = [16, 19, 73, 4, 1, 70, 65, 66]
225 r1 = [34, 88, 0, 0, 0, 0, 0, 0]
226 r2 = [33, 75, 50, 78, 51, 46, 72, 69 ]
227 r3 = [16, 19, 73, 160, 159, 70, 65, 66]
230 SIDlow = (responseID & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
231 SIDhigh = (responseID >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
232 packet1 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
233 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
234 # lower nibble is DLC
235 r1[0],r1[1],r1[2],r1[3],r1[4],r1[5],r1[6],r1[7]]
236 packet2 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
237 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
238 # lower nibble is DLC
239 r2[0],r2[1],r2[2],r2[3],r2[4],r2[5],r2[6],r2[7]]
240 packet3 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
241 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
242 # lower nibble is DLC
243 r3[0],r3[1],r3[2],r3[3],r3[4],r3[5],r3[6],r3[7]]
245 self.multiPacketSpit(packet0 = r1, packet1 = r2, packet2 = r3, packet0rts = True, packet1rts = True, packet2rts = True)
247 #filter for the correct packet
248 self.filterForPacket(listenID, listenPacket[0],listenPacket[1], verbose = True)
249 self.client.rxpacket()
250 self.client.rxpacket() # flush buffers if there is anything
251 startTime = tT.time()
252 while( (tT.time() -startTime) < duration):
253 packet = self.client.rxpacket()
255 sid = ord(packet[0])<<3 | ord(packet[1])>>5
256 if( sid == listenID):
257 byte3 = ord(packet[6])
258 if( byte3 == listenPacket[3]):
259 print "SendingPackets!"
261 self.multpackSpit(packet0rts=True,packet1rts=True,packet2rts=True)
266 if __name__ == "__main__":
267 fe = FordExperiments("asdsf");
269 #packetData['db4'] = 4;
271 #fe.mimic1056(packetData, runTime)
272 #fe.cycledb1_1056(runTime)
273 #fe.oscillateTemperature(runTime)