Dual CANbus monitor and instrumentation cluster
Eagle Schematic and Board design
/media/uploads/TickTock/canaryr5.zip
Parts List
Assembly
1) LCD Displays
I found ribbon cable is a nice way to organize the wires to the displays. You'll need 12 conductors and I connected them in the following order:
1 | LED+ |
2 | LED- |
3 | RSTB |
4 | MOSI |
5 | SCLK |
6 | CSB |
7 | X+ |
8 | X- |
9 | Y+ |
10 | Y- |
11 | VDD |
12 | GND |
First I connected all the GND connections (IM0, IM1, IM3, & 2 GND). Do not connect the bottom GND until you have the ribbon cable connected. After making all the ribbon cable connections (connecting the GND of the ribbon cable to the bottom GND pad), solder the GND bar from the previous step to the back of the bottom GND connection. Finally, make a connection from the back side 3.3V pin to IM2. Take a break and repeat for the second display. Once the two displays are complete combine all wires except CS0, CS1, X+, X-, Y+, and Y-. Connect X- of the left display to X+ of the right. Similarly connect Y- of the left display to Y+ of the right. Insulate any exposed wires.
2) PCB
Refer to the schematics to place all the components on the board. If you plan to install into the CANary 3D enclosure, DO NOT install the battery holder or the socket for the mbed and, instead, connect two wires to the VB and GND pads nearby. You will have to install the battery holder against the back wall to avoid interfering with the right-hand display and the mbed will have to be directly soldered. I have not found a socket with a low enough profile to fit in the space provided (depth of enclosure is limited by the space behind the center console). Also, I recommend keeping as much lead as possible on the Zener diode (bending it as shown to clear the back wall). Although it is operating well within parameters, the Zener gets quite hot during extended operation and the leads help dissipate the heat and keep it away from the PCB and other components.
Once the PCB is populated, solder the LCDs to the PCB. CS0 connects to the right display and CS1 connects to the left.
3) Testing
1) | First step is to buzz out all connections from the LCDs to the pins in the main board |
2) | Next check the touch screen connections. On the main board, place an Ohm meter across X+ and X-. You should read 700 Ohms. Repeat for Y+ and Y-. Then test the resistance from X+ to Y+. With nothing touching the screens, it should read >100K Ohms and <1K when touching either screen. |
3) | When all connections are checked, solder in the mbed. Download and install the touch2 program http://mbed.org/users/TickTock/code/touch2/ to test the basic operation of the mbed and touch screens. |
tips: | |
Touch screen is sensitive - excess flux on X+,X-,Y+,Y- connection on mbed can result in flakey operation | |
If touch is not working, double-check the LCD0_CS and LCD1_CS are not swapped. LCD0_CS must connect to the CS of the LCD that has X- & Y- connected to the mbed. LCD1_CS must connect to the CS of the LCD that has X+ & Y+ connected to the mbed. | |
4) | Once touch2 works, it is time to connect to the OBD connector. I highly recommend double checking all connections from the OBD to the PCB with the cable in place before connecting to the Leaf. Buzz out all the pins in the OBS to make sure none are shorting to each other, Check that the 12V goes to the Zener (and nothing else) and the switched 12V to the resistor divider (and nothing else). Test the ground connection properly connects to ground and nothing else. |
5) | Once you are confident there are no shorts or wrong connections from the OBD connector, take a deep breath and plug it into your leaf. Touch2 program should come up and function. Unplug and install the latest CANary firmware. Test all features before installing into the enclosure (gids, cellpair, menu system, logging) since installing and removing from the enclosure is a PITA. |
4) Enclosure
The 3D printer leaves a lot of powder behind - I used a strong spray of water to get it out of all the cracks. The enclosure comes with a rather rough finish. I recommend convincing yourself you like it, then simply lightly sand then paint before assembly. Sanding is very difficult - the nylon is very nicely fused and doesn't want to sand. I tried sandblasting and that didn't work either. I had some limited success with filler and then sanding, but only on the outside - it is too difficult to sand the face.
5) Final Assembly
utility.cpp@85:5c27e88b3fbe, 2013-04-17 (annotated)
- Committer:
- TickTock
- Date:
- Wed Apr 17 12:45:13 2013 +0000
- Revision:
- 85:5c27e88b3fbe
- Parent:
- 83:52b1f330a62d
- Child:
- 86:d1c9e8ac1c4b
Merged with Gary's lost message counter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TickTock | 13:62e0f7f39ff5 | 1 | // utility.cpp |
TickTock | 13:62e0f7f39ff5 | 2 | #include "utility.h" |
TickTock | 13:62e0f7f39ff5 | 3 | |
TickTock | 13:62e0f7f39ff5 | 4 | void mbed_reset(); |
TickTock | 22:a43df3905863 | 5 | |
TickTock | 13:62e0f7f39ff5 | 6 | void RTC_IRQHandler() { |
TickTock | 13:62e0f7f39ff5 | 7 | timer.reset(); // zero ms at the-seconds-tic |
TickTock | 20:3bf176d14b14 | 8 | canIdle=(++secsNoMsg>canTimeout)?true:false; |
TickTock | 20:3bf176d14b14 | 9 | userIdle=(++secsNoTouch>userTimeout)?true:false; |
TickTock | 13:62e0f7f39ff5 | 10 | LPC_RTC->ILR |= (1<<0); // clear interrupt to prepare for next |
TickTock | 34:4751a8259b18 | 11 | tick=true; |
garygid | 69:6bfdfc002036 | 12 | // will use this to generate a logTP() just before the next Message received. |
garygid | 69:6bfdfc002036 | 13 | if( (time(NULL) % 60) == 0) ZeroSecTick = true; // gg - at 0-second of each minute |
TickTock | 13:62e0f7f39ff5 | 14 | } |
TickTock | 13:62e0f7f39ff5 | 15 | |
TickTock | 37:fea2c1d52c5f | 16 | void RTC_Init (void) { |
TickTock | 13:62e0f7f39ff5 | 17 | LPC_RTC->ILR=0x00; // set up the RTC interrupts |
TickTock | 13:62e0f7f39ff5 | 18 | LPC_RTC->CIIR=0x01; // interrupts each second |
TickTock | 13:62e0f7f39ff5 | 19 | LPC_RTC->CCR = 0x01; // Clock enable |
TickTock | 13:62e0f7f39ff5 | 20 | //NVIC_SetPriority( RTC_IRQn, 10 ); |
TickTock | 13:62e0f7f39ff5 | 21 | NVIC_EnableIRQ( RTC_IRQn ); |
TickTock | 13:62e0f7f39ff5 | 22 | } |
TickTock | 13:62e0f7f39ff5 | 23 | |
TickTock | 13:62e0f7f39ff5 | 24 | void logMsg (char *msg) { |
TickTock | 13:62e0f7f39ff5 | 25 | strcpy(displayLog[displayLoc],msg); |
TickTock | 13:62e0f7f39ff5 | 26 | displayLoc=displayLoc>17?0:displayLoc+1; |
TickTock | 13:62e0f7f39ff5 | 27 | } |
TickTock | 13:62e0f7f39ff5 | 28 | |
TickTock | 13:62e0f7f39ff5 | 29 | void touch_ISR(){ |
TickTock | 37:fea2c1d52c5f | 30 | //LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF); // seems to work without so maybe not necessary (performed inInterruptIn handler?) |
TickTock | 35:5acbd8a64a89 | 31 | touched=true; // just set flag - touch screen algorythm is long and we don't want to block other interrupts |
TickTock | 13:62e0f7f39ff5 | 32 | } |
TickTock | 13:62e0f7f39ff5 | 33 | |
TickTock | 13:62e0f7f39ff5 | 34 | unsigned short getTimeStamp() { |
TickTock | 13:62e0f7f39ff5 | 35 | unsigned short msec = timer.read_ms() ; // read ms from the timer |
TickTock | 13:62e0f7f39ff5 | 36 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 13:62e0f7f39ff5 | 37 | unsigned short isecs = secs%60 ; // modulo 60 for 0-59 seconds from RTC |
TickTock | 13:62e0f7f39ff5 | 38 | return ((isecs<<10)+msec) ; // return the two byte time stamp |
TickTock | 13:62e0f7f39ff5 | 39 | } |
TickTock | 13:62e0f7f39ff5 | 40 | |
TickTock | 13:62e0f7f39ff5 | 41 | void logCan (char mType, CANMessage canRXmsg) { |
TickTock | 85:5c27e88b3fbe | 42 | |
TickTock | 85:5c27e88b3fbe | 43 | // re-arranged to put static first |
TickTock | 85:5c27e88b3fbe | 44 | static unsigned char ii = 0; |
TickTock | 85:5c27e88b3fbe | 45 | static unsigned char lasti = 0; // indexindex |
TickTock | 85:5c27e88b3fbe | 46 | static unsigned char bdi=0; |
TickTock | 85:5c27e88b3fbe | 47 | static signed short imotorRPM = 0; |
TickTock | 85:5c27e88b3fbe | 48 | static unsigned short nLost = 0; // gg - overrun |
TickTock | 85:5c27e88b3fbe | 49 | |
TickTock | 83:52b1f330a62d | 50 | char sTemp[40]; |
TickTock | 83:52b1f330a62d | 51 | unsigned char changed,i; |
TickTock | 40:0e6e71a7323f | 52 | signed short packV; |
TickTock | 37:fea2c1d52c5f | 53 | signed short packA; |
TickTock | 40:0e6e71a7323f | 54 | signed long imWs_x4; |
TickTock | 85:5c27e88b3fbe | 55 | unsigned short ts=0; //moved getTimeStamp call to inside debug loop for speed |
TickTock | 85:5c27e88b3fbe | 56 | |
TickTock | 36:dbd39c315258 | 57 | secsNoMsg=0; // reset deadman switch |
TickTock | 85:5c27e88b3fbe | 58 | if(debugMode){ // code to insert actual number of dropped frames for overrun debug - skiped in normal mode to keep logcan short |
TickTock | 85:5c27e88b3fbe | 59 | ts = getTimeStamp(); |
TickTock | 85:5c27e88b3fbe | 60 | if(logOpen){ |
TickTock | 85:5c27e88b3fbe | 61 | if(canRXmsg.id>0) { |
TickTock | 85:5c27e88b3fbe | 62 | // check to see if buffer is already full (read - write) = 1 |
TickTock | 85:5c27e88b3fbe | 63 | // actually the last buffer location cannot be used because then |
TickTock | 85:5c27e88b3fbe | 64 | // the buffer would look empty after writePointer++ |
TickTock | 85:5c27e88b3fbe | 65 | |
TickTock | 85:5c27e88b3fbe | 66 | //if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/16)) // modulo is slow? |
TickTock | 85:5c27e88b3fbe | 67 | |
TickTock | 85:5c27e88b3fbe | 68 | // maxBufLen = 512, so pointers are 0 through 511 |
TickTock | 85:5c27e88b3fbe | 69 | if( (readPointer - writePointer) == 1 || (writePointer - readPointer) == (maxBufLen - 1)) { |
TickTock | 85:5c27e88b3fbe | 70 | // the buffer is "full", so Lose this message |
TickTock | 85:5c27e88b3fbe | 71 | |
TickTock | 85:5c27e88b3fbe | 72 | // point to the last-stored message |
TickTock | 85:5c27e88b3fbe | 73 | int tempWritePointer = writePointer - 1 ; |
TickTock | 85:5c27e88b3fbe | 74 | if( tempWritePointer == -1 ) tempWritePointer = maxBufLen - 1; |
TickTock | 85:5c27e88b3fbe | 75 | char strLost[9] ; |
TickTock | 85:5c27e88b3fbe | 76 | |
TickTock | 85:5c27e88b3fbe | 77 | if( nLost == 0 ) { |
TickTock | 85:5c27e88b3fbe | 78 | // this is the first message lost |
TickTock | 85:5c27e88b3fbe | 79 | // and we must overwrite the last message with an FFE comment message |
TickTock | 85:5c27e88b3fbe | 80 | // So, there will be two messages lost as the comment message is laid in. |
TickTock | 85:5c27e88b3fbe | 81 | nLost = 2; |
TickTock | 85:5c27e88b3fbe | 82 | sprintf(strLost,"%s","Lost0002"); // indicate two messages lost |
TickTock | 85:5c27e88b3fbe | 83 | |
TickTock | 85:5c27e88b3fbe | 84 | // overlay the last message with a "Lost0002" comment |
TickTock | 85:5c27e88b3fbe | 85 | writeBuffer[tempWritePointer][0]=0; |
TickTock | 85:5c27e88b3fbe | 86 | writeBuffer[tempWritePointer][1]=(ts&0xff00)>>8; // Time Stamp (2 bytes_ |
TickTock | 85:5c27e88b3fbe | 87 | writeBuffer[tempWritePointer][2]=(ts&0x00ff); |
TickTock | 85:5c27e88b3fbe | 88 | writeBuffer[tempWritePointer][3]=0xfe; // MsgID, low byte |
TickTock | 85:5c27e88b3fbe | 89 | writeBuffer[tempWritePointer][4]=0xff; // Len nibble, and MsgID high nibble |
TickTock | 85:5c27e88b3fbe | 90 | |
TickTock | 85:5c27e88b3fbe | 91 | for(i=5;i<13;i++){ |
TickTock | 85:5c27e88b3fbe | 92 | writeBuffer[tempWritePointer][i]= strLost[i-5]; |
TickTock | 85:5c27e88b3fbe | 93 | } |
TickTock | 85:5c27e88b3fbe | 94 | } else { |
TickTock | 85:5c27e88b3fbe | 95 | // increment the loat counter |
TickTock | 85:5c27e88b3fbe | 96 | nLost += 1; |
TickTock | 85:5c27e88b3fbe | 97 | |
TickTock | 85:5c27e88b3fbe | 98 | // lay the new count into the comment |
TickTock | 85:5c27e88b3fbe | 99 | sprintf(strLost,"%04d",nLost); |
TickTock | 85:5c27e88b3fbe | 100 | for(i=9;i<13;i++){ |
TickTock | 85:5c27e88b3fbe | 101 | writeBuffer[tempWritePointer][i]= strLost[i-9]; |
TickTock | 85:5c27e88b3fbe | 102 | } |
TickTock | 85:5c27e88b3fbe | 103 | } |
TickTock | 85:5c27e88b3fbe | 104 | } else { |
TickTock | 85:5c27e88b3fbe | 105 | // is room to insert the message |
TickTock | 85:5c27e88b3fbe | 106 | // get it inserted quickly |
TickTock | 85:5c27e88b3fbe | 107 | writeBuffer[writePointer][0]=mType; |
TickTock | 85:5c27e88b3fbe | 108 | writeBuffer[writePointer][1]=(ts&0xff00)>>8; // Time Stamp (2 bytes_ |
TickTock | 85:5c27e88b3fbe | 109 | writeBuffer[writePointer][2]=(ts&0x00ff); |
TickTock | 85:5c27e88b3fbe | 110 | writeBuffer[writePointer][3]=canRXmsg.id&0xff; // MsgID, low byte |
TickTock | 85:5c27e88b3fbe | 111 | writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4); // Len nibble, and MsgID high nibble |
TickTock | 85:5c27e88b3fbe | 112 | for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?) |
TickTock | 85:5c27e88b3fbe | 113 | writeBuffer[writePointer][i]=canRXmsg.data[i-5]; |
TickTock | 85:5c27e88b3fbe | 114 | } |
TickTock | 85:5c27e88b3fbe | 115 | //-------------- |
TickTock | 85:5c27e88b3fbe | 116 | // force unused data bytes to FF for CAN-Do compatibility - gg - force FF |
TickTock | 85:5c27e88b3fbe | 117 | if(canRXmsg.len < 8){ |
TickTock | 85:5c27e88b3fbe | 118 | for(i=canRXmsg.len; i<8; i++) { |
TickTock | 85:5c27e88b3fbe | 119 | writeBuffer[writePointer][i+5]=0xFF; |
TickTock | 85:5c27e88b3fbe | 120 | } |
TickTock | 85:5c27e88b3fbe | 121 | } |
TickTock | 85:5c27e88b3fbe | 122 | //-------------- |
TickTock | 85:5c27e88b3fbe | 123 | // note, this is not protected from the interrupt |
TickTock | 85:5c27e88b3fbe | 124 | // due to the nLost code above, this no longer |
TickTock | 85:5c27e88b3fbe | 125 | // overflows to writePointer = readPointer |
TickTock | 85:5c27e88b3fbe | 126 | // which would make the buffer look empty |
TickTock | 85:5c27e88b3fbe | 127 | if (++writePointer >= maxBufLen) { |
TickTock | 85:5c27e88b3fbe | 128 | writePointer = 0; |
TickTock | 85:5c27e88b3fbe | 129 | led3 = !led3; |
TickTock | 85:5c27e88b3fbe | 130 | } |
TickTock | 85:5c27e88b3fbe | 131 | //-------------- |
TickTock | 85:5c27e88b3fbe | 132 | // log a local message if we had lost messages. gg - logcan |
TickTock | 85:5c27e88b3fbe | 133 | if( nLost > 0 ) { |
TickTock | 85:5c27e88b3fbe | 134 | // We previously lost messages that did not get into the buffer |
TickTock | 85:5c27e88b3fbe | 135 | sprintf(sTemp,"-- Write Buffer Lost [%d]\n", nLost); |
TickTock | 85:5c27e88b3fbe | 136 | logMsg(sTemp); // write buffer overrun |
TickTock | 85:5c27e88b3fbe | 137 | spkr.beep(500,0.25); |
TickTock | 85:5c27e88b3fbe | 138 | |
TickTock | 85:5c27e88b3fbe | 139 | nLost = 0 ; |
TickTock | 85:5c27e88b3fbe | 140 | } |
TickTock | 85:5c27e88b3fbe | 141 | //-------------- |
TickTock | 85:5c27e88b3fbe | 142 | } |
TickTock | 83:52b1f330a62d | 143 | } |
TickTock | 85:5c27e88b3fbe | 144 | } |
TickTock | 85:5c27e88b3fbe | 145 | }else{ // not debugMode - keep code short |
TickTock | 85:5c27e88b3fbe | 146 | if(logOpen){ |
TickTock | 85:5c27e88b3fbe | 147 | if(canRXmsg.id>0) { |
TickTock | 85:5c27e88b3fbe | 148 | writeBuffer[writePointer][0]=mType; |
TickTock | 85:5c27e88b3fbe | 149 | writeBuffer[writePointer][1]=(ts&0xff00)>>8; |
TickTock | 85:5c27e88b3fbe | 150 | writeBuffer[writePointer][2]=(ts&0x00ff); |
TickTock | 85:5c27e88b3fbe | 151 | writeBuffer[writePointer][3]=canRXmsg.id&0xff; |
TickTock | 85:5c27e88b3fbe | 152 | writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4); |
TickTock | 85:5c27e88b3fbe | 153 | for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?) |
TickTock | 85:5c27e88b3fbe | 154 | writeBuffer[writePointer][i]=canRXmsg.data[i-5]; |
TickTock | 85:5c27e88b3fbe | 155 | } |
TickTock | 85:5c27e88b3fbe | 156 | if (++writePointer >= maxBufLen) { |
TickTock | 85:5c27e88b3fbe | 157 | writePointer = 0; |
TickTock | 85:5c27e88b3fbe | 158 | led3 = !led3; |
TickTock | 85:5c27e88b3fbe | 159 | } |
TickTock | 85:5c27e88b3fbe | 160 | if (writePointer==readPointer) { |
TickTock | 85:5c27e88b3fbe | 161 | // Just overwrote an entry that hasn't been sent to thumbdrive |
TickTock | 85:5c27e88b3fbe | 162 | sprintf(sTemp,"Write buffer overrun.\n"); |
TickTock | 85:5c27e88b3fbe | 163 | logMsg(sTemp); // write buffer overrun |
TickTock | 85:5c27e88b3fbe | 164 | spkr.beep(500,0.25); |
TickTock | 85:5c27e88b3fbe | 165 | } |
TickTock | 13:62e0f7f39ff5 | 166 | } |
TickTock | 13:62e0f7f39ff5 | 167 | } |
TickTock | 37:fea2c1d52c5f | 168 | } |
TickTock | 40:0e6e71a7323f | 169 | |
TickTock | 83:52b1f330a62d | 170 | if(canRXmsg.id<0x800){ // Block FFE and FFF messages |
TickTock | 83:52b1f330a62d | 171 | if(indexLastMsg[canRXmsg.id]==0) { //Check if no entry |
TickTock | 83:52b1f330a62d | 172 | ii=ii<99?ii+1:0; // Should never wrap - less than 100 different messages ever used |
TickTock | 83:52b1f330a62d | 173 | indexLastMsg[canRXmsg.id]=ii; //Create entry if first message |
TickTock | 83:52b1f330a62d | 174 | } |
TickTock | 85:5c27e88b3fbe | 175 | if(dMode[0]==changedScreen||dMode[1]==changedScreen){// Skip if not using (for execution speed) |
TickTock | 83:52b1f330a62d | 176 | changed=msgChanged[indexLastMsg[canRXmsg.id]]; |
TickTock | 85:5c27e88b3fbe | 177 | // This is cleared in the main loop when reset button is touched |
TickTock | 83:52b1f330a62d | 178 | for(i=0;i<8;i++){ |
TickTock | 83:52b1f330a62d | 179 | if(lastMsg[indexLastMsg[canRXmsg.id]].data[i]!=canRXmsg.data[i]){ |
TickTock | 83:52b1f330a62d | 180 | changed |= 1<<i; |
garygid | 79:24f1793171e7 | 181 | } |
TickTock | 13:62e0f7f39ff5 | 182 | } |
TickTock | 83:52b1f330a62d | 183 | msgChanged[indexLastMsg[canRXmsg.id]]=changed; |
TickTock | 13:62e0f7f39ff5 | 184 | } |
garygid | 79:24f1793171e7 | 185 | |
TickTock | 83:52b1f330a62d | 186 | lastMsg[indexLastMsg[canRXmsg.id]]=canRXmsg; //Store in table |
TickTock | 83:52b1f330a62d | 187 | |
TickTock | 83:52b1f330a62d | 188 | //Miscellaneous on-recieve operations below |
TickTock | 83:52b1f330a62d | 189 | if((mType==2)&&(canRXmsg.id==0x358)){ // headlight/turn signal indicator |
TickTock | 83:52b1f330a62d | 190 | headlights = (canRXmsg.data[1]&0x80)?true:false; |
TickTock | 83:52b1f330a62d | 191 | }else if((mType==1)&&(canRXmsg.id==0x7bb)){ // is battery data? Need to store all responses |
TickTock | 83:52b1f330a62d | 192 | if(canRXmsg.data[0]<0x20){ |
TickTock | 83:52b1f330a62d | 193 | if(canRXmsg.data[3]==2){//cellpair data |
TickTock | 83:52b1f330a62d | 194 | bdi=0; |
TickTock | 83:52b1f330a62d | 195 | sprintf(sTemp,"Getting cell pair data\n"); |
TickTock | 83:52b1f330a62d | 196 | logMsg(sTemp); |
TickTock | 83:52b1f330a62d | 197 | }else if(canRXmsg.data[3]==4){//temperature data |
TickTock | 83:52b1f330a62d | 198 | bdi=0x20; |
TickTock | 83:52b1f330a62d | 199 | sprintf(sTemp,"Getting temperature data\n"); |
TickTock | 83:52b1f330a62d | 200 | logMsg(sTemp); |
TickTock | 83:52b1f330a62d | 201 | }else bdi=0xff; // ignore other messages (for now) |
TickTock | 83:52b1f330a62d | 202 | lasti=0; |
TickTock | 83:52b1f330a62d | 203 | } |
TickTock | 83:52b1f330a62d | 204 | if(bdi<0xff){ |
TickTock | 83:52b1f330a62d | 205 | i=canRXmsg.data[0]&0x0f; //lower nibble of D0 is index |
TickTock | 83:52b1f330a62d | 206 | if(lasti>i){ //detect rollover and offset index appropriately |
TickTock | 83:52b1f330a62d | 207 | bdi=0x10; |
TickTock | 83:52b1f330a62d | 208 | } |
TickTock | 83:52b1f330a62d | 209 | lasti=i; //remember the msb to detect rollover next time around |
TickTock | 83:52b1f330a62d | 210 | i+=bdi; |
TickTock | 83:52b1f330a62d | 211 | if(i==22){ |
TickTock | 83:52b1f330a62d | 212 | logCP=yesBattLog; // Only log if logging enabled |
TickTock | 83:52b1f330a62d | 213 | showCP=true; // Always show |
TickTock | 83:52b1f330a62d | 214 | } |
TickTock | 83:52b1f330a62d | 215 | i*=7; |
TickTock | 83:52b1f330a62d | 216 | if(i<0xfa){ // Is there a better way to do this? |
TickTock | 83:52b1f330a62d | 217 | battData[i+0]=canRXmsg.data[1]; |
TickTock | 83:52b1f330a62d | 218 | battData[i+1]=canRXmsg.data[2]; |
TickTock | 83:52b1f330a62d | 219 | battData[i+2]=canRXmsg.data[3]; |
TickTock | 83:52b1f330a62d | 220 | battData[i+3]=canRXmsg.data[4]; |
TickTock | 83:52b1f330a62d | 221 | battData[i+4]=canRXmsg.data[5]; |
TickTock | 83:52b1f330a62d | 222 | battData[i+5]=canRXmsg.data[6]; |
TickTock | 83:52b1f330a62d | 223 | battData[i+6]=canRXmsg.data[7]; |
TickTock | 83:52b1f330a62d | 224 | } |
TickTock | 83:52b1f330a62d | 225 | } |
TickTock | 83:52b1f330a62d | 226 | }else if((mType==1)&&(canRXmsg.id==0x1db)){ //Battery Volts and Amps |
TickTock | 83:52b1f330a62d | 227 | packV=((canRXmsg.data[2]<<2)|(canRXmsg.data[3]>>6)); // 1 LSB = 0.5V |
TickTock | 83:52b1f330a62d | 228 | packA=((canRXmsg.data[0]<<3)|(canRXmsg.data[1]>>5)); // 1 LSB = 0.5A |
TickTock | 83:52b1f330a62d | 229 | if(packA>0x03ff){ |
TickTock | 83:52b1f330a62d | 230 | packA|=0xf800;//extend sign; |
TickTock | 83:52b1f330a62d | 231 | } |
TickTock | 83:52b1f330a62d | 232 | packA -= 1; //Slight correction to value required (unique to my Leaf?) |
TickTock | 83:52b1f330a62d | 233 | imWs_x4 = packV; // Volts*milliSeconds*2 |
TickTock | 83:52b1f330a62d | 234 | imWs_x4 *= -packA; // milliWattseconds*4 |
TickTock | 83:52b1f330a62d | 235 | if (!((imotorRPM<2)&&(imWs_x4<0))){ //Ignore if charging from wall |
TickTock | 83:52b1f330a62d | 236 | mWs_x4 += imWs_x4; // total mWs_x4 |
TickTock | 83:52b1f330a62d | 237 | numWsamples++; |
TickTock | 83:52b1f330a62d | 238 | } |
TickTock | 83:52b1f330a62d | 239 | }else if((mType==1)&&(canRXmsg.id==0x1da)){ //Motor Speed |
TickTock | 83:52b1f330a62d | 240 | imotorRPM=((canRXmsg.data[4]<<8)|(canRXmsg.data[5])); |
TickTock | 83:52b1f330a62d | 241 | if(imotorRPM<0){ // take absolute value |
TickTock | 83:52b1f330a62d | 242 | imotorRPM=-imotorRPM; |
TickTock | 83:52b1f330a62d | 243 | } |
TickTock | 83:52b1f330a62d | 244 | motorRPM+=imotorRPM; |
TickTock | 83:52b1f330a62d | 245 | numSsamples++; |
TickTock | 37:fea2c1d52c5f | 246 | } |
TickTock | 37:fea2c1d52c5f | 247 | } |
TickTock | 13:62e0f7f39ff5 | 248 | } |
TickTock | 13:62e0f7f39ff5 | 249 | |
TickTock | 13:62e0f7f39ff5 | 250 | void logTS () { |
TickTock | 13:62e0f7f39ff5 | 251 | CANMessage tsMsg; |
TickTock | 13:62e0f7f39ff5 | 252 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
garygid | 69:6bfdfc002036 | 253 | // NOTE: In Mbed, I believe that this is seconds past start of 1970, not 1900 |
garygid | 69:6bfdfc002036 | 254 | // but this is good, since seconds past 1970 is what CAN-Do expects. GG - Date Time |
TickTock | 13:62e0f7f39ff5 | 255 | tsMsg.id=0xfff; |
TickTock | 13:62e0f7f39ff5 | 256 | tsMsg.len=0xf; |
garygid | 69:6bfdfc002036 | 257 | tsMsg.data[0]=secs&0xff; |
TickTock | 13:62e0f7f39ff5 | 258 | tsMsg.data[1]=(secs>>8)&0xff; |
TickTock | 13:62e0f7f39ff5 | 259 | tsMsg.data[2]=(secs>>16)&0xff; |
garygid | 69:6bfdfc002036 | 260 | tsMsg.data[3]=(secs>>24)&0xff; |
garygid | 69:6bfdfc002036 | 261 | tsMsg.data[4]=0; // 0xff; gg - Date Time |
garygid | 69:6bfdfc002036 | 262 | tsMsg.data[5]=0; // 0xff; for CAN-Do |
garygid | 69:6bfdfc002036 | 263 | tsMsg.data[6]=0; // 0xff; |
TickTock | 13:62e0f7f39ff5 | 264 | tsMsg.data[7]=0xff; |
garygid | 69:6bfdfc002036 | 265 | logCan(0,tsMsg); // Date-Time |
garygid | 69:6bfdfc002036 | 266 | } |
garygid | 69:6bfdfc002036 | 267 | |
TickTock | 83:52b1f330a62d | 268 | void logErrMsg (char * errMsg) { |
garygid | 69:6bfdfc002036 | 269 | // log CAN-Do 8-character Pseudo Message |
garygid | 69:6bfdfc002036 | 270 | CANMessage tsMsg; |
garygid | 69:6bfdfc002036 | 271 | tsMsg.id=0xffe; // pseudo Message to CAN-Do log |
garygid | 69:6bfdfc002036 | 272 | tsMsg.len=0xf; |
garygid | 69:6bfdfc002036 | 273 | int iMsgLen = strlen(errMsg); |
garygid | 69:6bfdfc002036 | 274 | // 8 character message compatible with CAN-Do |
garygid | 69:6bfdfc002036 | 275 | for(int i=0; i<8; i++){ |
garygid | 69:6bfdfc002036 | 276 | tsMsg.data[i]=' '; |
garygid | 69:6bfdfc002036 | 277 | if( i < iMsgLen ) tsMsg.data[i]=errMsg[i]; |
garygid | 69:6bfdfc002036 | 278 | } |
garygid | 69:6bfdfc002036 | 279 | logCan(0,tsMsg); // FFE Comment Message |
TickTock | 13:62e0f7f39ff5 | 280 | } |
TickTock | 13:62e0f7f39ff5 | 281 | |
TickTock | 78:a383971fe02f | 282 | void sendReq() { |
TickTock | 78:a383971fe02f | 283 | static char data[8] = {0x02, 0x21, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff}; |
TickTock | 78:a383971fe02f | 284 | if(reqMsgCnt<99){ |
TickTock | 78:a383971fe02f | 285 | switch (reqMsgCnt){ |
TickTock | 78:a383971fe02f | 286 | case 0: |
TickTock | 78:a383971fe02f | 287 | can1.monitor(false); // set to active mode |
TickTock | 78:a383971fe02f | 288 | can1SleepMode = 0; // enable TX |
TickTock | 83:52b1f330a62d | 289 | data[0]=0x02; //change to request group 1 |
TickTock | 78:a383971fe02f | 290 | data[1]=0x21; |
TickTock | 78:a383971fe02f | 291 | data[2]=0x01; |
TickTock | 78:a383971fe02f | 292 | break; |
TickTock | 83:52b1f330a62d | 293 | case 6: // group 1 has 6 frames |
TickTock | 78:a383971fe02f | 294 | can1.monitor(false); // set to active mode |
TickTock | 78:a383971fe02f | 295 | can1SleepMode = 0; // enable TX |
TickTock | 83:52b1f330a62d | 296 | data[0]=0x02; //change to request group 2 (cp data) |
TickTock | 78:a383971fe02f | 297 | data[1]=0x21; |
TickTock | 78:a383971fe02f | 298 | data[2]=0x02; |
TickTock | 78:a383971fe02f | 299 | break; |
TickTock | 83:52b1f330a62d | 300 | case 35: // group 2 has 29 frames |
TickTock | 83:52b1f330a62d | 301 | data[0]=0x02; //change to request group 3 |
TickTock | 78:a383971fe02f | 302 | data[1]=0x21; |
TickTock | 78:a383971fe02f | 303 | data[2]=0x03; |
TickTock | 78:a383971fe02f | 304 | break; |
TickTock | 83:52b1f330a62d | 305 | case 40: // group 3 has 5 frames |
TickTock | 83:52b1f330a62d | 306 | data[0]=0x02; //change to request group 4 (temperature) |
TickTock | 78:a383971fe02f | 307 | data[1]=0x21; |
TickTock | 78:a383971fe02f | 308 | data[2]=0x04; |
TickTock | 78:a383971fe02f | 309 | break; |
TickTock | 83:52b1f330a62d | 310 | case 43: // group 4 has 3 frames |
TickTock | 83:52b1f330a62d | 311 | data[0]=0x02; //change to request group 5 |
TickTock | 78:a383971fe02f | 312 | data[1]=0x21; |
TickTock | 78:a383971fe02f | 313 | data[2]=0x05; |
TickTock | 78:a383971fe02f | 314 | break; |
TickTock | 83:52b1f330a62d | 315 | case 54: // group 5 has 11 frames |
TickTock | 78:a383971fe02f | 316 | reqMsgCnt = 99; |
TickTock | 78:a383971fe02f | 317 | can1SleepMode = 1; // disable TX |
TickTock | 78:a383971fe02f | 318 | can1.monitor(true); // set to snoop mode |
TickTock | 83:52b1f330a62d | 319 | msgReq.detach(); // stop ticker |
TickTock | 78:a383971fe02f | 320 | default: |
TickTock | 78:a383971fe02f | 321 | data[0]=0x30; //change to request next line message |
TickTock | 78:a383971fe02f | 322 | data[1]=0x01; |
TickTock | 78:a383971fe02f | 323 | data[2]=0x00; |
TickTock | 78:a383971fe02f | 324 | } |
TickTock | 78:a383971fe02f | 325 | can1.write(CANMessage(0x79b, data, 8)); |
TickTock | 78:a383971fe02f | 326 | reqMsgCnt++; |
TickTock | 78:a383971fe02f | 327 | } |
TickTock | 78:a383971fe02f | 328 | } |
TickTock | 78:a383971fe02f | 329 | |
TickTock | 78:a383971fe02f | 330 | /*void sendCPreq() { |
TickTock | 13:62e0f7f39ff5 | 331 | char i; |
TickTock | 13:62e0f7f39ff5 | 332 | char data[8] = {0x02, 0x21, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff}; |
TickTock | 13:62e0f7f39ff5 | 333 | can1.monitor(false); // set to active mode |
TickTock | 13:62e0f7f39ff5 | 334 | can1SleepMode = 0; // enable TX |
TickTock | 13:62e0f7f39ff5 | 335 | can1.write(CANMessage(0x79b, data, 8)); |
garygid | 69:6bfdfc002036 | 336 | |
garygid | 69:6bfdfc002036 | 337 | if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus |
garygid | 69:6bfdfc002036 | 338 | |
garygid | 69:6bfdfc002036 | 339 | logCan(1,CANMessage(0x79b, data, 8)); // Group 2 Request on EV |
TickTock | 13:62e0f7f39ff5 | 340 | data[0]=0x30; //change to request next line message |
TickTock | 13:62e0f7f39ff5 | 341 | data[1]=0x01; |
TickTock | 13:62e0f7f39ff5 | 342 | data[2]=0x00; |
TickTock | 23:cd03f9c3395e | 343 | for(i=0;i<29;i++){ |
TickTock | 13:62e0f7f39ff5 | 344 | wait_ms(16); //wait 16ms |
TickTock | 13:62e0f7f39ff5 | 345 | can1.write(CANMessage(0x79b, data, 8)); |
TickTock | 13:62e0f7f39ff5 | 346 | } |
TickTock | 13:62e0f7f39ff5 | 347 | can1SleepMode = 1; // disable TX |
TickTock | 13:62e0f7f39ff5 | 348 | can1.monitor(true); // set to snoop mode |
TickTock | 13:62e0f7f39ff5 | 349 | } |
TickTock | 13:62e0f7f39ff5 | 350 | |
TickTock | 13:62e0f7f39ff5 | 351 | void sendTreq() { |
TickTock | 13:62e0f7f39ff5 | 352 | char i; |
TickTock | 13:62e0f7f39ff5 | 353 | char data[8] = {0x02, 0x21, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff}; |
TickTock | 13:62e0f7f39ff5 | 354 | can1.monitor(false); // set to active mode |
TickTock | 13:62e0f7f39ff5 | 355 | can1SleepMode = 0; // enable TX |
TickTock | 13:62e0f7f39ff5 | 356 | can1.write(CANMessage(0x79b, data, 8)); |
garygid | 69:6bfdfc002036 | 357 | |
garygid | 69:6bfdfc002036 | 358 | if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus |
garygid | 69:6bfdfc002036 | 359 | |
garygid | 69:6bfdfc002036 | 360 | logCan(1,CANMessage(0x79b, data, 8)); // Group 4 request on EV |
TickTock | 13:62e0f7f39ff5 | 361 | data[0]=0x30; //change to request next line message |
TickTock | 13:62e0f7f39ff5 | 362 | data[1]=0x01; |
TickTock | 13:62e0f7f39ff5 | 363 | data[2]=0x00; |
TickTock | 23:cd03f9c3395e | 364 | for(i=0;i<3;i++){ |
TickTock | 13:62e0f7f39ff5 | 365 | wait_ms(16); //wait 16ms |
TickTock | 13:62e0f7f39ff5 | 366 | can1.write(CANMessage(0x79b, data, 8)); |
TickTock | 13:62e0f7f39ff5 | 367 | } |
TickTock | 13:62e0f7f39ff5 | 368 | can1SleepMode = 1; // disable TX |
TickTock | 13:62e0f7f39ff5 | 369 | can1.monitor(true); // set to snoop mode |
TickTock | 31:082372c83f68 | 370 | } |
TickTock | 31:082372c83f68 | 371 | |
TickTock | 36:dbd39c315258 | 372 | void autoPollISR() { //This is the ticker ISR for auto-polling |
TickTock | 33:a277743ebdeb | 373 | pollCP=true; //Set a flag to do in main loop instead of here |
TickTock | 78:a383971fe02f | 374 | } //since ticker blocks other interrupts*/ |
TickTock | 13:62e0f7f39ff5 | 375 | |
TickTock | 78:a383971fe02f | 376 | void autoPollISR(){ |
TickTock | 78:a383971fe02f | 377 | reqMsgCnt = 0; //reset message counter |
TickTock | 83:52b1f330a62d | 378 | msgReq.attach(&sendReq,0.015); |
TickTock | 78:a383971fe02f | 379 | } |
TickTock | 36:dbd39c315258 | 380 | void playbackISR() { //Used for autoplayback |
TickTock | 36:dbd39c315258 | 381 | step=true; |
TickTock | 36:dbd39c315258 | 382 | } |
TickTock | 36:dbd39c315258 | 383 | |
TickTock | 36:dbd39c315258 | 384 | void doNothing(){ //CAN deattach work-around |
TickTock | 36:dbd39c315258 | 385 | } |
TickTock | 36:dbd39c315258 | 386 | |
TickTock | 13:62e0f7f39ff5 | 387 | void recieve1() { |
TickTock | 13:62e0f7f39ff5 | 388 | CANMessage msg1; |
TickTock | 13:62e0f7f39ff5 | 389 | can1.read(msg1); |
garygid | 69:6bfdfc002036 | 390 | |
garygid | 69:6bfdfc002036 | 391 | if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus |
garygid | 69:6bfdfc002036 | 392 | |
garygid | 69:6bfdfc002036 | 393 | logCan(1, msg1); // EVcan Message Received |
TickTock | 13:62e0f7f39ff5 | 394 | led1 = !led1; |
TickTock | 13:62e0f7f39ff5 | 395 | } |
TickTock | 13:62e0f7f39ff5 | 396 | |
TickTock | 13:62e0f7f39ff5 | 397 | void recieve2() { |
TickTock | 13:62e0f7f39ff5 | 398 | CANMessage msg2; |
TickTock | 13:62e0f7f39ff5 | 399 | can2.read(msg2); |
garygid | 69:6bfdfc002036 | 400 | |
garygid | 69:6bfdfc002036 | 401 | if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus |
garygid | 69:6bfdfc002036 | 402 | |
garygid | 69:6bfdfc002036 | 403 | logCan(2, msg2); // CARcan Message Received |
TickTock | 13:62e0f7f39ff5 | 404 | led2 = !led2; |
TickTock | 13:62e0f7f39ff5 | 405 | } |
TickTock | 13:62e0f7f39ff5 | 406 | |
TickTock | 22:a43df3905863 | 407 | unsigned char buttonX(unsigned short X, unsigned char columns) { |
TickTock | 22:a43df3905863 | 408 | unsigned char val = X*columns/320; |
TickTock | 22:a43df3905863 | 409 | return val; |
TickTock | 22:a43df3905863 | 410 | } |
TickTock | 22:a43df3905863 | 411 | |
TickTock | 22:a43df3905863 | 412 | unsigned char buttonY(unsigned short Y, unsigned char rows) { |
TickTock | 22:a43df3905863 | 413 | unsigned short val = Y*rows/240; |
TickTock | 22:a43df3905863 | 414 | return val; |
TickTock | 22:a43df3905863 | 415 | } |
TickTock | 23:cd03f9c3395e | 416 | |
TickTock | 23:cd03f9c3395e | 417 | void saveConfig(){ |
TickTock | 23:cd03f9c3395e | 418 | FILE *cfile; |
TickTock | 23:cd03f9c3395e | 419 | cfile = fopen("/local/config.txt", "w"); |
TickTock | 48:d1ce92104a1f | 420 | fprintf(cfile,"format 3\r\n"); |
TickTock | 23:cd03f9c3395e | 421 | fprintf(cfile,"x0_off %d\r\n",tt.x0_off); |
TickTock | 23:cd03f9c3395e | 422 | fprintf(cfile,"y0_off %d\r\n",tt.y0_off); |
TickTock | 23:cd03f9c3395e | 423 | fprintf(cfile,"x0_pp %d\r\n",tt.x0_pp); |
TickTock | 23:cd03f9c3395e | 424 | fprintf(cfile,"y0_pp %d\r\n",tt.y0_pp); |
TickTock | 23:cd03f9c3395e | 425 | fprintf(cfile,"x1_off %d\r\n",tt.x1_off); |
TickTock | 23:cd03f9c3395e | 426 | fprintf(cfile,"y1_off %d\r\n",tt.y1_off); |
TickTock | 23:cd03f9c3395e | 427 | fprintf(cfile,"x1_pp %d\r\n",tt.x1_pp); |
TickTock | 23:cd03f9c3395e | 428 | fprintf(cfile,"y1_pp %d\r\n",tt.y1_pp); |
TickTock | 23:cd03f9c3395e | 429 | fprintf(cfile,"x_mid %d\r\n",tt.x_mid); |
TickTock | 26:462ccb580472 | 430 | if (dMode[0]==config1Screen) |
TickTock | 41:8d4609ea7259 | 431 | fprintf(cfile,"dMode0 %d\r\n",mainScreen); |
TickTock | 26:462ccb580472 | 432 | else |
TickTock | 26:462ccb580472 | 433 | fprintf(cfile,"dMode0 %d\r\n",dMode[0]); |
TickTock | 26:462ccb580472 | 434 | if (dMode[1]==config1Screen) |
TickTock | 41:8d4609ea7259 | 435 | fprintf(cfile,"dMode1 %d\r\n",mainScreen); |
TickTock | 26:462ccb580472 | 436 | else |
TickTock | 26:462ccb580472 | 437 | fprintf(cfile,"dMode1 %d\r\n",dMode[1]); |
TickTock | 35:5acbd8a64a89 | 438 | fprintf(cfile,"ledHi %4.3f\r\n",ledHi); |
TickTock | 35:5acbd8a64a89 | 439 | fprintf(cfile,"ledLo %4.3f\r\n",ledLo); |
TickTock | 35:5acbd8a64a89 | 440 | fprintf(cfile,"pollInt %d\r\n",pollInt); |
TickTock | 35:5acbd8a64a89 | 441 | fprintf(cfile,"scale12V %4.2f\r\n",scale12V); |
TickTock | 48:d1ce92104a1f | 442 | fprintf(cfile,"skin %d\r\n",skin); |
TickTock | 50:83d5864c64a0 | 443 | fprintf(cfile,"dtePeriod %d\r\n",dtePeriod); |
TickTock | 23:cd03f9c3395e | 444 | fclose(cfile); |
TickTock | 23:cd03f9c3395e | 445 | } |
TickTock | 23:cd03f9c3395e | 446 | |
TickTock | 23:cd03f9c3395e | 447 | void readConfig(){ |
TickTock | 23:cd03f9c3395e | 448 | FILE *cfile; |
TickTock | 23:cd03f9c3395e | 449 | int ff; |
TickTock | 23:cd03f9c3395e | 450 | char sTemp[40]; |
TickTock | 23:cd03f9c3395e | 451 | cfile = fopen("/local/config.txt", "r"); |
TickTock | 23:cd03f9c3395e | 452 | if (cfile==NULL){ // if doesn't exist --> create |
TickTock | 23:cd03f9c3395e | 453 | sprintf(sTemp,"No config file found.\n"); |
garygid | 69:6bfdfc002036 | 454 | logMsg(sTemp); // no config file |
TickTock | 23:cd03f9c3395e | 455 | sprintf(sTemp,"Calibrating touch screen.\n"); |
garygid | 69:6bfdfc002036 | 456 | logMsg(sTemp); // calibrating |
TickTock | 23:cd03f9c3395e | 457 | //tt.setcal(5570, 34030, 80, 108, 33700, 5780, 82, 108, 32500);// bypass calibration using my values |
TickTock | 23:cd03f9c3395e | 458 | tt.calibrate(); // run touchscreen calibration routine |
garygid | 58:4d06288d75a2 | 459 | // NOTE: calibrates screen 1 first, then screen 0. |
TickTock | 23:cd03f9c3395e | 460 | saveConfig(); |
TickTock | 23:cd03f9c3395e | 461 | } else { |
TickTock | 48:d1ce92104a1f | 462 | ledHi = 0.823; |
TickTock | 48:d1ce92104a1f | 463 | ledLo = 0.1; |
TickTock | 48:d1ce92104a1f | 464 | pollInt = 300; |
TickTock | 48:d1ce92104a1f | 465 | scale12V = 16.2; |
TickTock | 48:d1ce92104a1f | 466 | skin = ttSkin; |
TickTock | 48:d1ce92104a1f | 467 | fscanf(cfile, "format %d\r\n", &ff ) ; |
TickTock | 23:cd03f9c3395e | 468 | fscanf(cfile, "x0_off %d\r\n", &tt.x0_off ) ; |
TickTock | 23:cd03f9c3395e | 469 | fscanf(cfile, "y0_off %d\r\n", &tt.y0_off ) ; |
TickTock | 23:cd03f9c3395e | 470 | fscanf(cfile, "x0_pp %d\r\n", &tt.x0_pp ) ; |
TickTock | 23:cd03f9c3395e | 471 | fscanf(cfile, "y0_pp %d\r\n", &tt.y0_pp ) ; |
TickTock | 23:cd03f9c3395e | 472 | fscanf(cfile, "x1_off %d\r\n", &tt.x1_off ) ; |
TickTock | 23:cd03f9c3395e | 473 | fscanf(cfile, "y1_off %d\r\n", &tt.y1_off ) ; |
TickTock | 23:cd03f9c3395e | 474 | fscanf(cfile, "x1_pp %d\r\n", &tt.x1_pp ) ; |
TickTock | 23:cd03f9c3395e | 475 | fscanf(cfile, "y1_pp %d\r\n", &tt.y1_pp ) ; |
TickTock | 23:cd03f9c3395e | 476 | fscanf(cfile, "x_mid %d\r\n", &tt.x_mid ) ; |
TickTock | 26:462ccb580472 | 477 | fscanf(cfile, "dMode0 %d\r\n", &dMode[0] ) ; |
TickTock | 26:462ccb580472 | 478 | fscanf(cfile, "dMode1 %d\r\n", &dMode[1] ) ; |
TickTock | 35:5acbd8a64a89 | 479 | if(ff>1){ |
TickTock | 48:d1ce92104a1f | 480 | fscanf(cfile, "ledHi %f\r\n", &ledHi ) ; |
TickTock | 48:d1ce92104a1f | 481 | fscanf(cfile, "ledLo %f\r\n", &ledLo ) ; |
TickTock | 35:5acbd8a64a89 | 482 | fscanf(cfile, "pollInt %d\r\n", &pollInt ) ; |
TickTock | 48:d1ce92104a1f | 483 | fscanf(cfile, "scale12V %f\r\n", &scale12V ) ; |
TickTock | 48:d1ce92104a1f | 484 | } |
TickTock | 48:d1ce92104a1f | 485 | if(ff>2){ |
TickTock | 48:d1ce92104a1f | 486 | fscanf(cfile, "skin %d\r\n", &skin ) ; |
TickTock | 50:83d5864c64a0 | 487 | fscanf(cfile, "dtePeriod %d\r\n", &dtePeriod ) ; |
TickTock | 35:5acbd8a64a89 | 488 | } |
TickTock | 23:cd03f9c3395e | 489 | fclose(cfile); |
TickTock | 48:d1ce92104a1f | 490 | if(ff<3){//If not latest format, save as latest format |
TickTock | 35:5acbd8a64a89 | 491 | saveConfig(); |
TickTock | 48:d1ce92104a1f | 492 | sprintf(sTemp,"Config file format updated.\n"); |
garygid | 69:6bfdfc002036 | 493 | logMsg(sTemp); // config forat updates |
TickTock | 48:d1ce92104a1f | 494 | } |
TickTock | 43:e7f6f80590e3 | 495 | sprintf(sTemp,"Config file loaded.\n"); |
garygid | 69:6bfdfc002036 | 496 | logMsg(sTemp); // config file loaded |
TickTock | 23:cd03f9c3395e | 497 | } |
TickTock | 23:cd03f9c3395e | 498 | } |
TickTock | 25:ddf0ec209f03 | 499 | |
TickTock | 25:ddf0ec209f03 | 500 | void upDate(unsigned char field, bool upDownBar){ |
TickTock | 25:ddf0ec209f03 | 501 | struct tm t; // pointer to a static tm structure |
TickTock | 25:ddf0ec209f03 | 502 | time_t seconds ; |
TickTock | 25:ddf0ec209f03 | 503 | seconds = time(NULL); |
TickTock | 25:ddf0ec209f03 | 504 | t = *localtime(&seconds) ; |
TickTock | 25:ddf0ec209f03 | 505 | switch(field){ |
TickTock | 25:ddf0ec209f03 | 506 | case 0: // year |
TickTock | 25:ddf0ec209f03 | 507 | if (upDownBar) { |
TickTock | 25:ddf0ec209f03 | 508 | t.tm_year = t.tm_year+1; |
TickTock | 25:ddf0ec209f03 | 509 | } else { |
TickTock | 25:ddf0ec209f03 | 510 | t.tm_year = t.tm_year-1; |
TickTock | 25:ddf0ec209f03 | 511 | } |
TickTock | 25:ddf0ec209f03 | 512 | break; |
TickTock | 25:ddf0ec209f03 | 513 | case 1: // month |
TickTock | 25:ddf0ec209f03 | 514 | if (upDownBar) { |
TickTock | 25:ddf0ec209f03 | 515 | t.tm_mon = (t.tm_mon<12)?t.tm_mon+1:1; |
TickTock | 25:ddf0ec209f03 | 516 | } else { |
TickTock | 25:ddf0ec209f03 | 517 | t.tm_mon = (t.tm_mon>2)?t.tm_mon-1:12; |
TickTock | 25:ddf0ec209f03 | 518 | } |
TickTock | 25:ddf0ec209f03 | 519 | break; |
TickTock | 25:ddf0ec209f03 | 520 | case 2: // day |
TickTock | 25:ddf0ec209f03 | 521 | if (upDownBar) { |
TickTock | 25:ddf0ec209f03 | 522 | t.tm_mday = (t.tm_mday<31)?t.tm_mday+1:1; |
TickTock | 25:ddf0ec209f03 | 523 | } else { |
TickTock | 25:ddf0ec209f03 | 524 | t.tm_mday = (t.tm_mday>2)?t.tm_mday-1:31; |
TickTock | 25:ddf0ec209f03 | 525 | } |
TickTock | 25:ddf0ec209f03 | 526 | break; |
TickTock | 25:ddf0ec209f03 | 527 | case 3: // hour |
TickTock | 25:ddf0ec209f03 | 528 | if (upDownBar) { |
TickTock | 25:ddf0ec209f03 | 529 | t.tm_hour = (t.tm_hour<23)?t.tm_hour+1:0; |
TickTock | 25:ddf0ec209f03 | 530 | } else { |
TickTock | 25:ddf0ec209f03 | 531 | t.tm_hour = (t.tm_hour>1)?t.tm_hour-1:23; |
TickTock | 25:ddf0ec209f03 | 532 | } |
TickTock | 25:ddf0ec209f03 | 533 | break; |
TickTock | 25:ddf0ec209f03 | 534 | case 4: // minute |
TickTock | 25:ddf0ec209f03 | 535 | if (upDownBar) { |
TickTock | 25:ddf0ec209f03 | 536 | t.tm_min = (t.tm_min<59)?t.tm_min+1:0; |
TickTock | 25:ddf0ec209f03 | 537 | } else { |
TickTock | 25:ddf0ec209f03 | 538 | t.tm_min = (t.tm_min>1)?t.tm_min-1:59; |
TickTock | 25:ddf0ec209f03 | 539 | } |
TickTock | 25:ddf0ec209f03 | 540 | break; |
TickTock | 25:ddf0ec209f03 | 541 | case 5: // second |
TickTock | 25:ddf0ec209f03 | 542 | if (upDownBar) { |
TickTock | 25:ddf0ec209f03 | 543 | t.tm_sec = (t.tm_sec<59)?t.tm_sec+1:0; |
TickTock | 25:ddf0ec209f03 | 544 | } else { |
TickTock | 25:ddf0ec209f03 | 545 | t.tm_sec = (t.tm_sec>1)?t.tm_sec-1:59; |
TickTock | 25:ddf0ec209f03 | 546 | } |
TickTock | 25:ddf0ec209f03 | 547 | break; |
TickTock | 25:ddf0ec209f03 | 548 | default: |
TickTock | 25:ddf0ec209f03 | 549 | break; |
TickTock | 25:ddf0ec209f03 | 550 | } |
TickTock | 25:ddf0ec209f03 | 551 | set_time(mktime(&t)); |
TickTock | 25:ddf0ec209f03 | 552 | } |
TickTock | 25:ddf0ec209f03 | 553 | |
TickTock | 39:eef8beac7411 | 554 | void logPackVoltages() { // Turbo3 - routine to dump CP values to text file |
TickTock | 39:eef8beac7411 | 555 | char sTemp[40]; |
TickTock | 39:eef8beac7411 | 556 | struct tm t; // pointer to a static tm structure |
TickTock | 39:eef8beac7411 | 557 | short unsigned max, min, jv, i, bd; |
TickTock | 39:eef8beac7411 | 558 | unsigned avg; |
TickTock | 39:eef8beac7411 | 559 | unsigned short gids, SOC, packV; |
TickTock | 39:eef8beac7411 | 560 | signed short packA; |
TickTock | 39:eef8beac7411 | 561 | time_t seconds ; |
TickTock | 39:eef8beac7411 | 562 | |
TickTock | 39:eef8beac7411 | 563 | CANMessage msg; |
TickTock | 39:eef8beac7411 | 564 | |
TickTock | 39:eef8beac7411 | 565 | seconds = time(NULL); // Turbo3 |
TickTock | 39:eef8beac7411 | 566 | t = *localtime(&seconds) ; // Turbo3 |
TickTock | 39:eef8beac7411 | 567 | |
TickTock | 39:eef8beac7411 | 568 | msg = lastMsg[indexLastMsg[0x5bc]]; //Get gids |
TickTock | 39:eef8beac7411 | 569 | gids = (msg.data[0]<<2)+(msg.data[1]>>6); |
TickTock | 39:eef8beac7411 | 570 | msg = lastMsg[indexLastMsg[0x55b]]; //Get SOC |
TickTock | 39:eef8beac7411 | 571 | SOC = (msg.data[0]<<2)+(msg.data[1]>>6); |
TickTock | 39:eef8beac7411 | 572 | msg = lastMsg[indexLastMsg[0x1db]]; //Get pack volts |
TickTock | 39:eef8beac7411 | 573 | packV = (msg.data[2]<<2)+(msg.data[3]>>6); |
TickTock | 39:eef8beac7411 | 574 | packA = (msg.data[0]<<3)+(msg.data[1]>>5); |
TickTock | 39:eef8beac7411 | 575 | if (packA & 0x400) packA |= 0xf800; |
TickTock | 39:eef8beac7411 | 576 | |
TickTock | 39:eef8beac7411 | 577 | max=0; |
TickTock | 39:eef8beac7411 | 578 | min=9999; |
TickTock | 39:eef8beac7411 | 579 | avg=0; |
TickTock | 39:eef8beac7411 | 580 | for(i=0; i<96; i++) { |
TickTock | 39:eef8beac7411 | 581 | bd=(battData[i*2+3]<<8)+battData[i*2+4]; |
TickTock | 39:eef8beac7411 | 582 | avg+=bd; |
TickTock | 39:eef8beac7411 | 583 | if(bd>max) max=bd; |
TickTock | 39:eef8beac7411 | 584 | if(bd<min) min=bd; |
TickTock | 39:eef8beac7411 | 585 | } |
TickTock | 39:eef8beac7411 | 586 | avg /= 96; |
TickTock | 39:eef8beac7411 | 587 | if(min<3713) { |
TickTock | 39:eef8beac7411 | 588 | jv=avg-(max-avg)*1.5; |
TickTock | 39:eef8beac7411 | 589 | } else { // Only compute judgement value if min cellpair meets <= 3712mV requirement |
TickTock | 39:eef8beac7411 | 590 | jv=0; |
TickTock | 39:eef8beac7411 | 591 | } |
TickTock | 39:eef8beac7411 | 592 | |
TickTock | 39:eef8beac7411 | 593 | FILE *bfile; |
TickTock | 39:eef8beac7411 | 594 | //bfile = fopen("/local/batvolt.txt", "a"); |
TickTock | 39:eef8beac7411 | 595 | bfile = fopen("/usb/batvolt.txt", "a"); |
TickTock | 39:eef8beac7411 | 596 | if(bfile!=NULL) { |
TickTock | 39:eef8beac7411 | 597 | strftime(sTemp, 40, "%a %m/%d/%Y %X", &t); |
TickTock | 39:eef8beac7411 | 598 | fprintf(bfile,"%s,%d,%5.1f%%,%5.1f,%5.1f,%d,%d,%d,%d,%d",sTemp,gids,(float)SOC/10,(float)packV/2,(float)packA/2,max,min,avg,max-min,jv); |
TickTock | 39:eef8beac7411 | 599 | fprintf(bfile,"%d,%d,%d,%d,",(battData[224+ 3]<<8)+battData[224+ 4],battData[224+ 5],(battData[224+ 6]<<8)+battData[224+ 7],battData[224+ 8]); |
TickTock | 39:eef8beac7411 | 600 | fprintf(bfile,"%d,%d,%d,%d", (battData[224+ 9]<<8)+battData[224+10],battData[224+11],(battData[224+12]<<8)+battData[224+13],battData[224+14]); |
TickTock | 39:eef8beac7411 | 601 | for(i=0; i<96; i++) { |
TickTock | 39:eef8beac7411 | 602 | bd=(battData[i*2+3]<<8)+battData[i*2+4]; |
TickTock | 39:eef8beac7411 | 603 | fprintf(bfile,",%d",bd); |
TickTock | 39:eef8beac7411 | 604 | } |
TickTock | 39:eef8beac7411 | 605 | fprintf(bfile,"\r\n"); |
TickTock | 39:eef8beac7411 | 606 | fclose(bfile); |
TickTock | 39:eef8beac7411 | 607 | } |
TickTock | 39:eef8beac7411 | 608 | logCP=false; |
TickTock | 40:0e6e71a7323f | 609 | showCP=true; |
TickTock | 39:eef8beac7411 | 610 | } |
TickTock | 25:ddf0ec209f03 | 611 |