4 import csv, time, argparse;
7 from random import randrange
8 from GoodFETMCPCAN import GoodFETMCPCAN;
9 from GoodFETMCPCANCommunication import GoodFETMCPCANCommunication
10 from intelhex import IntelHex;
15 class FordExperiments(GoodFETMCPCANCommunication):
18 GoodFETMCPCANCommunication.__init__(self)
19 #super(FordExperiments,self).__init__(self) #initialize chip
22 def mimic1056(self,packetData,runTime):
25 self.spitSetup(self.freq)
26 #FIGURE out how to clear buffers
27 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
28 packet1 = self.client.rxpacket();
30 packetParsed = self.client.packet2parsed(packet1);
31 #keep sniffing till we read a packet
32 while( packet1 == None or packetParsed.get('sID') != 1056 ):
33 packet1 = self.client.rxpacket()
35 packetParsed = self.client.packet2parsed(packet1)
36 recieveTime = time.time()
37 packetParsed = self.client.packet2parsed(packet1)
38 if( packetParsed['sID'] != 1056):
39 print "Sniffed wrong packet"
41 countInitial = ord(packetParsed['db3']) #initial count value
43 #set data packet to match what was sniffed or at least what was input
46 if(packetData.get(idx) == None):
47 packet.append(ord(packetParsed.get(idx)))
49 packet.append(packetData.get(idx))
51 #### split SID into different regs
52 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
53 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
54 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
55 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
57 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
59 self.client.txpacket(packet);
61 while( (time.time()-recieveTime) < runTime):
62 #care about db3 or packet[8] that we want to count at the rate that it is
63 dT = time.time()-tpast
65 db3 = (countInitial + math.floor((time.time()-recieveTime)/0.2))%255
67 self.client.txpacket(packet)
71 self.client.MCPrts(TXB0=True)
72 tpast = time.time() #update our transmit time on the one before
76 def cycledb1_1056(self,runTime):
80 #FIGURE out how to clear buffers
81 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
82 packet1 = self.client.rxpacket();
84 packetParsed = self.client.packet2parsed(packet1);
85 #keep sniffing till we read a packet
86 while( packet1 == None or packetParsed.get('sID') != 1056 ):
88 packet1 = self.client.rxpacket()
90 packetParsed = self.client.packet2parsed(packet1)
91 recieveTime = time.time()
92 packetParsed = self.client.packet2parsed(packet1)
93 if( packetParsed['sID'] != 1056):
94 print "Sniffed wrong packet"
97 #set data packet to match what was sniffed or at least what was input
100 packet.append(ord(packetParsed.get(idx)))
102 packet[1] = packetValue;
105 #### split SID into different regs
106 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
107 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
108 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
109 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
110 # lower nibble is DLC
111 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
113 self.client.txpacket(packet);
115 while( (time.time()-recieveTime) < runTime):
116 #care about db3 or packet[8] that we want to count at the rate that it is
117 dT = time.time()-tpast
120 #temp = ((packetValue+1))%2
122 # pV = packetValue%255
128 self.client.txpacket(packet)
130 tpast = time.time() #update our transmit time on the one before
133 def getBackground(self,sId):
134 packet1 = self.client.rxpacket();
136 packetParsed = self.client.packet2parsed(packet1);
137 #keep sniffing till we read a packet
138 while( packet1 == None or packetParsed.get('sID') != sId ):
139 packet1 = self.client.rxpacket()
141 packetParsed = self.client.packet2parsed(packet1)
143 #recieveTime = time.time()
146 def cycle4packets1279(self):
147 self.client.serInit()
150 self.addFilter([1279, 1279, 1279, 1279, 1279, 1279], verbose = False)
151 packetParsed = self.getBackground(1279)
153 if (packetParsed[db0] == 16):
154 # if it's the first of the four packets, replace the value in db7 with 83
155 packetParsed[db7] = 83
156 # transmit new packet
157 self.client.txpacket(packetParsed)
159 # otherwise, leave it alone
160 # transmit same pakcet we read in
161 self.client.txpacket(packetParsed)
162 # print the packet we are transmitting
166 def oscillateTemperature(self,time):
168 self.client.serInit()
170 #FIGURE out how to clear buffers
171 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
172 packetParsed = self.getBackground(1056)
174 #set data packet to match what was sniffed or at least what was input
177 packet.append(ord(packetParsed.get(idx)))
179 packet[1] = packetValue;
182 #### split SID into different regs
183 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
184 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
185 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
186 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
187 # lower nibble is DLC
188 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
190 self.client.txpacket(packet);
191 startTime = tT.time()
192 while( (tT.time()-startTime) < runTime):
193 dt = tT.time()-startTime
194 inputValue = ((2.0*math.pi)/20.0)*dt
195 value = 30*math.sin(inputValue)+130
197 #packet[5] = int(value)
204 self.client.txpacket(packet)
206 #tpast = time.time() #update our transmit time on the one before
210 if __name__ == "__main__":
211 fe = FordExperiments();
213 packetData['db4'] = 4;
215 #fe.mimic1056(packetData, runTime)
216 #fe.cycledb1_1056(runTime)
217 fe.oscillateTemperature(runTime)