<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_1_1435829902428_14324"><span>Thank you all for the help with this. </span></div><div id="yui_3_16_0_1_1435829902428_14324" dir="ltr"><span id="yui_3_16_0_1_1435829902428_14927">It was difficult for me to bend my mind around this.</span></div><div id="yui_3_16_0_1_1435829902428_14324" dir="ltr">I was not able to test Brian's suggestion, but after many tests (printing bytes values and intermediate results) I ended up with something very similar.</div><div id="yui_3_16_0_1_1435829902428_14324" dir="ltr">Thanks again,</div><div id="yui_3_16_0_1_1435829902428_14324" dir="ltr">Nikolay</div> <br><div class="qtdSeparateBR"><br><br></div><div class="yahoo_quoted" style="display: block;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div dir="ltr"> <font size="2" face="Arial"> On Thursday, June 25, 2015 8:52 AM, Mark Webb-Johnson <mark@webb-johnson.net> wrote:<br> </font> </div> <br><br> <div class="y_msg_container">^this.<br clear="none"><br clear="none">Nice ;-)<br clear="none"><br clear="none">Mark.<br clear="none"><div class="yqt7688278307" id="yqtfd43871"><br clear="none">> On 25 Jun, 2015, at 2:47 pm, HONDA S-2000 <<a shape="rect" ymailto="mailto:s2000@audiobanshee.com" href="mailto:s2000@audiobanshee.com">s2000@audiobanshee.com</a>> wrote:<br clear="none">> <br clear="none">> First, 0x07DCC0 is supposed to be 515264, not 520264. Looks like you added 5000 in your spreadsheet.<br clear="none">> <br clear="none">> Second, you don't show your declarations for sv1 & sv2, which could be part of the problem.<br clear="none">> <br clear="none">> <br clear="none">> Personally, I would start with the following code (on a 32-bit machine):<br clear="none">> <br clear="none">> unsigned long sv = (unsigned)can_databuffer[4];<br clear="none">> sv <<= 8;<br clear="none">> sv |= (unsigned)can_databuffer[5];<br clear="none">> sv <<= 8;<br clear="none">> sv |= (unsigned)can_databuffer[6];<br clear="none">> car_SOC = (sv + 5000) / 10000UL;<br clear="none">> <br clear="none">> or, if you want to follow Mark's general advice, the last line would be:<br clear="none">> <br clear="none">> car_SOC = ((sv + 5000) * 3355) >> 25;<br clear="none">> <br clear="none">> The 25 bit shift is exactly the same as dividing by 33554432, so multiplying by 3355 before shifting is the same as multiplying by 0.00009998679161 or dividing by 10001.321 (with only 0.0132% error).<br clear="none">> <br clear="none">> <br clear="none">> I'm just not sure whether 32-bit math is efficient on this platform. Since you're converting a 20-bit number to 7-bit (0%..100%), then I assume you could leave off the 3rd byte, can_databuffer[6], and still be accurate enough.<br clear="none">> <br clear="none">> unsigned short sv = (unsigned)can_databuffer[4];<br clear="none">> sv <<= 8;<br clear="none">> sv |= (unsigned)can_databuffer[5];<br clear="none">> car_SOC = ((sv + 33) * 13) >> 9;<br clear="none">> <br clear="none">> Note that the 16-bit value jump by about 40 for every 1% SOC, so I was adding 20 to round off. The actual value would be 19.53125 if float were efficient. That doesn't quite reach 100%, so I fudged by adding 33 instead. That's not enough to round up the lowest values too much, because 1% will still be 1%.<br clear="none">> <br clear="none">> The second code example above was a bit tricky, because lopping off the last byte divides the input by 256, but combining that with the original 1/10000 scaling means you need to divide by 39.0625 (I just did some calculations on an envelope to come up with the code above, but tested it with values of 100%, 99%, 2%, and 1% to make sure it's close enough). I can't quite calculate the percent error of dropping a byte and then using multiply/shift instead of divide. Maybe someone else can review this.<br clear="none">> <br clear="none">> Brian<br clear="none">> <br clear="none">> <br clear="none">> On Jun 24, 2015, at 4:35 PM, Nikolay Shishkov <<a shape="rect" ymailto="mailto:nshishkov@yahoo.com" href="mailto:nshishkov@yahoo.com">nshishkov@yahoo.com</a>> wrote:<br clear="none">>> I seem to not been able to grasp the idea and would appreciate a code example.<br clear="none">>> For example byte1=0x07, byte2=0xDC, byte3=0xC0<br clear="none">>> If I put these in excel and calculate 7*256*256 + 220*256 + 192 I get 520264.<br clear="none">>> Then when I divide this by 10000, I get 52.0264. And this matches the 52% shown by the car.<br clear="none">>> <br clear="none">>> Currently my code for this conversion is:<br clear="none">>> sv1 = (unsigned long)can_databuffer[4];<br clear="none">>> sv1 = sv1*65536UL;<br clear="none">>> sv2 = (unsigned long)can_databuffer[5];<br clear="none">>> sv2 = sv2*256UL;<br clear="none">>> sv3 = (unsigned long)can_databuffer[6];<br clear="none">>> sv3 = sv3;<br clear="none">>> <br clear="none">>> car_SOC = (char)((unsigned long)(sv1 + sv2 + sv3 + 5000UL)/10000UL);<br clear="none">>> <br clear="none">>> And the result for car_SOC is 49. <br clear="none">>> How is this possible?<br clear="none">>> And what would be the correct way to put these 3 bytes together and divide them by 10000 to get to the final value in char?<br clear="none">>> <br clear="none">>> Thanks in advance,<br clear="none">>> Nikolay</div><br clear="none">>> <br clear="none">> _______________________________________________<br clear="none">> OvmsDev mailing list<br clear="none">> <a shape="rect" ymailto="mailto:OvmsDev@lists.teslaclub.hk" href="mailto:OvmsDev@lists.teslaclub.hk">OvmsDev@lists.teslaclub.hk</a><br clear="none">> <a shape="rect" href="http://lists.teslaclub.hk/mailman/listinfo/ovmsdev" target="_blank">http://lists.teslaclub.hk/mailman/listinfo/ovmsdev</a><div class="yqt7688278307" id="yqtfd47301"><br clear="none"></div><br><br></div> </div> </div> </div></div></body></html>