Added a section for viewing arbitration IDs information. This will be stored in a...
[goodfet] / client / FordExperiments.py
1 import sys;
2 import binascii;
3 import array;
4 import csv, time, argparse;
5 import datetime
6 import os
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;
12 import Queue
13 import math
14
15 tT = time
16 class FordExperiments(experiments):
17     
18     def __init__(self, dataLocation = "../../contrib/ThayerData/"):
19         GoodFETMCPCANCommunication.__init__(self, dataLocation)
20         #super(FordExperiments,self).__init__(self) #initialize chip
21         self.freq = 500;
22
23     def mimic1056(self,packetData,runTime):
24         #setup chip
25         self.client.serInit()
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();
30         if(packet1 != None):
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()
35             if(packet1 != None):
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"
41             return
42         countInitial = ord(packetParsed['db3']) #initial count value
43         packet = []
44         #set data packet to match what was sniffed or at least what was input
45         for i in range(0,8):
46             idx = "db%d"%i
47             if(packetData.get(idx) == None):
48                 packet.append(ord(packetParsed.get(idx)))
49             else:
50                 packet.append(packetData.get(idx))
51         print packet
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) 
57                   # lower nibble is DLC                   
58                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
59         packetCount = 1;
60         self.client.txpacket(packet);
61         tpast = time.time()
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
65             if( dT/0.2 >= 1):
66                 db3 = (countInitial + math.floor((time.time()-recieveTime)/0.2))%255
67                 packet[8] = db3
68                 self.client.txpacket(packet)
69                 packetCount += 1
70             else:
71                 packetCount += 1
72                 self.client.MCPrts(TXB0=True)
73             tpast = time.time()  #update our transmit time on the one before   
74             
75                 
76          
77     def cycledb1_1056(self,runTime):
78         #setup chip
79         self.client.serInit()
80         self.spitSetup(500)
81         #FIGURE out how to clear buffers
82         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
83         packet1 = self.client.rxpacket();
84         if(packet1 != None):
85             packetParsed = self.client.packet2parsed(packet1);
86         #keep sniffing till we read a packet
87         while( packet1 == None or packetParsed.get('sID') != 1056 ):
88             time.sleep(.1)
89             packet1 = self.client.rxpacket()
90             if(packet1 != None):
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"
96             return
97         packet = []
98         #set data packet to match what was sniffed or at least what was input
99         for i in range(0,8):
100             idx = "db%d"%i
101             packet.append(ord(packetParsed.get(idx)))
102         packetValue = 0
103         packet[1] = packetValue;
104         
105         print packet
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]]
113         packetCount = 1;
114         self.client.txpacket(packet);
115         tpast = time.time()
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
119             packetValue += 10
120             pV = packetValue%255
121             #temp = ((packetValue+1))%2
122             #if( temp == 1):
123             #    pV = packetValue%255
124             #else:
125             #    pV = 0
126             packet[6] = pV
127             #packet[6] = 1
128             print packet
129             self.client.txpacket(packet)
130             packetCount += 1
131             tpast = time.time()  #update our transmit time on the one before   
132         print packetCount;
133         
134     def getBackground(self,sId):
135         packet1 = self.client.rxpacket();
136         if(packet1 != None):
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()
141             if(packet1 != None):
142                 packetParsed = self.client.packet2parsed(packet1)
143             
144         #recieveTime = time.time()
145         return packetParsed
146
147     def cycle4packets1279(self):
148         self.client.serInit()
149         self.spitSetup(500)
150         # filter on 1279
151         self.addFilter([1279, 1279, 1279, 1279, 1279, 1279], verbose = False)
152         packetParsed = self.getBackground(1279)
153         packet = []
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)
159         else:
160         # otherwise, leave it alone
161             # transmit same pakcet we read in
162             self.client.txpacket(packetParsed)
163         # print the packet we are transmitting
164         print packetParsed
165         
166         
167     def oscillateTemperature(self,time):
168         #setup chip
169         self.client.serInit()
170         self.spitSetup(500)
171         #FIGURE out how to clear buffers
172         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
173         packetParsed = self.getBackground(1056)
174         packet = []
175         #set data packet to match what was sniffed or at least what was input
176         for i in range(0,8):
177             idx = "db%d"%i
178             packet.append(ord(packetParsed.get(idx)))
179         packetValue = 0
180         packet[1] = packetValue;
181         
182         print packet
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]]
190         packetCount = 1;
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
197             print value
198             #packet[5] = int(value)
199             if( value > 130 ):
200                 packet[5] = 160
201             else:
202                 packet[5] = 100
203             #packet[6] = 1
204             print packet
205             self.client.txpacket(packet)
206             packetCount += 1
207             #tpast = time.time()  #update our transmit time on the one before   
208         print packetCount;
209         
210         
211     def fakeVIN(self):
212        #reset eveything on the chip
213        self.client.serInit() 
214        self.reset()
215        duration = 20; #seconds 
216        
217        listenID = 2015
218        listenPacket = [2, 9, 6, 153, 153, 153, 153, 153]
219        responseID = 2024
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]
224        
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]
228        
229        #format
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]]
244
245        self.multiPacketSpit(packet0 = r1, packet1 = r2, packet2 = r3, packet0rts = True, packet1rts = True, packet2rts = True)
246
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()
254            if( packet != None):
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!"
260                        #send packets
261                        self.multpackSpit(packet0rts=True,packet1rts=True,packet2rts=True)
262                        
263        
264        
265         
266 if __name__ == "__main__":
267     fe = FordExperiments("asdsf");
268     #packetData = {}
269     #packetData['db4'] = 4;
270     #runTime = 100;
271     #fe.mimic1056(packetData, runTime)
272     #fe.cycledb1_1056(runTime)
273     #fe.oscillateTemperature(runTime)
274     fe.fakeVIN()