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