2baf55133cd6751e49fb8deaba3afbb7eba930fc
[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 GoodFETMCPCANCommunication import GoodFETMCPCANCommunication
10 from intelhex import IntelHex;
11 import Queue
12 import math
13
14 tT = time
15 class FordExperiments(GoodFETMCPCANCommunication):
16     
17     def init(self):
18         super(FordExperimetns,self).__init__(self) #initialize chip
19         self.freq = 500;
20
21     def mimic1056(self,packetData,runTime):
22         #setup chip
23         self.client.serInit()
24         self.spitSetup(500)
25         #FIGURE out how to clear buffers
26         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
27         packet1 = self.client.rxpacket();
28         if(packet1 != None):
29             packetParsed = self.client.packet2parsed(packet1);
30         #keep sniffing till we read a packet
31         while( packet1 == None or packetParsed.get('sID') != 1056 ):
32             packet1 = self.client.rxpacket()
33             if(packet1 != None):
34                 packetParsed = self.client.packet2parsed(packet1)
35         recieveTime = time.time()
36         packetParsed = self.client.packet2parsed(packet1)
37         if( packetParsed['sID'] != 1056):
38             print "Sniffed wrong packet"
39             return
40         countInitial = ord(packetParsed['db3']) #initial count value
41         packet = []
42         #set data packet to match what was sniffed or at least what was input
43         for i in range(0,8):
44             idx = "db%d"%i
45             if(packetData.get(idx) == None):
46                 packet.append(ord(packetParsed.get(idx)))
47             else:
48                 packet.append(packetData.get(idx))
49         print packet
50         #### split SID into different regs
51         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
52         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
53         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
54                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
55                   # lower nibble is DLC                   
56                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
57         packetCount = 1;
58         self.client.txpacket(packet);
59         tpast = time.time()
60         while( (time.time()-recieveTime) < runTime):
61             #care about db3 or packet[8] that we want to count at the rate that it is
62             dT = time.time()-tpast
63             if( dT/0.2 >= 1):
64                 db3 = (countInitial + math.floor((time.time()-recieveTime)/0.2))%255
65                 packet[8] = db3
66                 self.client.txpacket(packet)
67                 packetCount += 1
68             else:
69                 packetCount += 1
70                 self.client.MCPrts(TXB0=True)
71             tpast = time.time()  #update our transmit time on the one before   
72             
73                 
74          
75     def cycledb1_1056(self,runTime):
76         #setup chip
77         self.client.serInit()
78         self.spitSetup(500)
79         #FIGURE out how to clear buffers
80         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
81         packet1 = self.client.rxpacket();
82         if(packet1 != None):
83             packetParsed = self.client.packet2parsed(packet1);
84         #keep sniffing till we read a packet
85         while( packet1 == None or packetParsed.get('sID') != 1056 ):
86             time.sleep(.1)
87             packet1 = self.client.rxpacket()
88             if(packet1 != None):
89                 packetParsed = self.client.packet2parsed(packet1)
90         recieveTime = time.time()
91         packetParsed = self.client.packet2parsed(packet1)
92         if( packetParsed['sID'] != 1056):
93             print "Sniffed wrong packet"
94             return
95         packet = []
96         #set data packet to match what was sniffed or at least what was input
97         for i in range(0,8):
98             idx = "db%d"%i
99             packet.append(ord(packetParsed.get(idx)))
100         packetValue = 0
101         packet[1] = packetValue;
102         
103         print packet
104         #### split SID into different regs
105         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
106         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
107         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
108                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
109                   # lower nibble is DLC                   
110                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
111         packetCount = 1;
112         self.client.txpacket(packet);
113         tpast = time.time()
114         while( (time.time()-recieveTime) < runTime):
115             #care about db3 or packet[8] that we want to count at the rate that it is
116             dT = time.time()-tpast
117             packetValue += 1
118             pV = packetValue%255
119             #temp = ((packetValue+1))%2
120             #if( temp == 1):
121             #    pV = packetValue%255
122             #else:
123             #    pV = 0
124             packet[6] = pV
125             #packet[6] = 1
126             print packet
127             self.client.txpacket(packet)
128             packetCount += 1
129             tpast = time.time()  #update our transmit time on the one before   
130         print packetCount;
131         
132     def getBackground(self,sId):
133         packet1 = self.client.rxpacket();
134         if(packet1 != None):
135             packetParsed = self.client.packet2parsed(packet1);
136         #keep sniffing till we read a packet
137         while( packet1 == None or packetParsed.get('sID') != sId ):
138             packet1 = self.client.rxpacket()
139             if(packet1 != None):
140                 packetParsed = self.client.packet2parsed(packet1)
141             
142         #recieveTime = time.time()
143         return packetParsed
144         
145         
146     def oscillateTemperature(self,time):
147         #setup chip
148         self.client.serInit()
149         self.spitSetup(500)
150         #FIGURE out how to clear buffers
151         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
152         packetParsed = self.getBackground(1056)
153         packet = []
154         #set data packet to match what was sniffed or at least what was input
155         for i in range(0,8):
156             idx = "db%d"%i
157             packet.append(ord(packetParsed.get(idx)))
158         packetValue = 0
159         packet[1] = packetValue;
160         
161         print packet
162         #### split SID into different regs
163         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
164         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
165         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
166                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
167                   # lower nibble is DLC                   
168                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
169         packetCount = 1;
170         self.client.txpacket(packet);
171         startTime = tT.time()
172         while( (tT.time()-startTime) < runTime):
173             dt = tT.time()-startTime
174             inputValue = ((2.0*math.pi)/20.0)*dt
175             value = 30*math.sin(inputValue)+130
176             print value
177             #packet[5] = int(value)
178             if( value > 130 ):
179                 packet[5] = 160
180             else:
181                 packet[5] = 100
182             #packet[6] = 1
183             print packet
184             self.client.txpacket(packet)
185             packetCount += 1
186             #tpast = time.time()  #update our transmit time on the one before   
187         print packetCount;
188         
189         
190 if __name__ == "__main__":
191     fe = FordExperiments();
192     packetData = {}
193     packetData['db4'] = 4;
194     runTime = 100;
195     #fe.mimic1056(packetData, runTime)
196     #fe.cycledb1_1056(runTime)
197     fe.oscillateTemperature(runTime)