Go Down

### Topic: SoftwareSerial magic numbers (Read 22887 times)previous topic - next topic

#### robtillaart

#15
##### Jan 04, 2013, 09:02 pm
@Dr John,
Yep a changing baudrate communication would certainly be slower than a fixed speed, but calculating the values take micro-seconds, no FP math involved.

I did the test with 2 Arduinos - UNO + DUemillanove - so one with a crystal and one with resonator (?) and used for both SW serial (you could have seen this in the code

I did not try a HW serial against the SW serial yet although I did test it with a (19200) HW serial LCD - see earlier post.

This analysis is not final yet as I expect the formulas can be improved a bit for the higher speeds. This can be done by non-linear polynomes at the cost of extra footprint or maybe by slighty tuning the constants in the formulas. Need some time to test (a lot more)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#16
##### Jan 04, 2013, 09:03 pm
@Mikal,
stuff to think through, thanks
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### drjiohnsmith

#17
##### Jan 05, 2013, 01:09 pm
well done

testing this sort of thing is a real pain I know,

#### robtillaart

#18
##### Jan 05, 2013, 04:23 pm

Tweaked the numbers in the spreadsheet to minimize the cumulative relative error.
There was a large relative error in the higher baud rates, now the relative error is minimized, while keeping the functions linear

(not extensively tested yet)
Code: [Select]
`// 16MHZrxstop = 16000000L/(7 * baudrate) - 2;rxintra = rxstop;  tx = rxstop - 4; rxcenter = rxstop/2 - 7; // 8MHZrxstop = 8000000L/(7 * baudrate) - 4;rxintra = rxstop;  tx = rxstop - 2; rxcenter = rxstop/2 - 10; // 20MHZrxstop = 20000000L/(7 * baudrate) - 3;rxintra = rxstop;  tx = rxstop - 3; rxcenter = rxstop/2 - 7; `

to be continued...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#19
##### Jan 05, 2013, 05:11 pm

Run with the previous formulas
Code: [Select]
`start...BAUD BYTE70660 D5 FAIL81950 AD FAIL88870 AF FAIL89570 BD FAIL94410 D5 FAIL95340 AA FAIL96590 D5 FAIL98980 AA FAIL100750 AB FAIL103590 BD FAIL105740 D5 FAIL110600 AA FAIL113260 AF FAIL120200 AA FAIL...`

Now a run with the new offsets
Code: [Select]
`start...BAUD BYTE90440 D5 FAIL97150 AD FAIL101140 AA FAIL101210 D5 FAIL103180 D5 FAIL105430 AA FAIL106130 D5 FAIL108400 A9 FAIL108990 AA FAIL109440 D5 FAIL111270 AA FAIL111320 D5 FAIL117300 D5 FAIL118480 AA FAIL...`

The first fail with new parameters lies about 20 K higher, but other runs started to fail at ~79/80K .

Conclusion for now:
The new offsets are definitely better than the previous, but still not good enough to get a fail free software serial up to 115200.
TODO: test @8Mhz and @20Mhz (don't have such duinos)

A deep dive in the code might be needed.
TBC...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#20
##### Jan 05, 2013, 08:07 pm
Next test - longer string "the quick brown fox jumps over the lazy dog" (42 chars)  sent from A-> B at different speeds starting at 100 baud step size 100.
B sends back the number of chars correctly received from start of the string. so when receiving "theXquick brown fox jumps over the lazy dog" the answer would be 3.

Again we see baud rates up to 70K perform 100%, above failing starts...

Code: [Select]
`BAUD CHARS74800 34 FAIL79900 11 FAIL81000 3 FAIL84900 31 FAIL85200 23 FAIL85800 21 FAIL86200 15 FAIL86300 20 FAIL86600 38 FAIL86900 41 FAIL87100 38 FAIL87300 15 FAIL87400 20 FAIL87700 38 FAIL88300 20 FAIL88400 170 FAIL  <<<< a very strange one ???88500 37 FAIL88700 18 FAIL88800 15 FAIL89000 38 FAIL89200 38 FAIL89500 15 FAIL89600 12 FAIL89800 18 FAIL90000 31 FAIL..`

For the statistics: highest successful transfer rate was 103800 baud,

Conclusion:
Up to 70K the formula based SoftSerial does work as expected, that is about 20% faster than 57600 from the fixed tables.

115200:
As the values are identical to the table based SS for 115200, I do not expect the (exisiting) table version to work at least for receiving data.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#21
##### Jan 05, 2013, 08:32 pm
Another test; steps of 1000 start @ 1000 (so not as fine grained but much faster).

The string length is 42, the value returned is now the number of same characters.
So 41 means that of the characters received 41 matched "the quick brown fox ...dog".

Missing chars start now at 83K and we see the quality gradually drop, missing 8 chars at 115K => that is 20%!)

(baudrates without extra info are OK)
Code: [Select]
`7000071000720007300074000750007600077000780007900080000810008200083000 41 FAIL8400085000 41 FAIL8600087000 40 FAIL88000 41 FAIL890009000091000 41 FAIL92000 41 FAIL93000 41 FAIL9400095000 39 FAIL96000 41 FAIL97000 41 FAIL98000 41 FAIL99000 41 FAIL100000 40 FAIL101000 40 FAIL102000 39 FAIL103000 41 FAIL104000 40 FAIL105000 39 FAIL106000 39 FAIL107000 39 FAIL108000 38 FAIL109000 39 FAIL110000 37 FAIL111000 38 FAIL112000 39 FAIL113000 39 FAIL114000 37 FAIL115000 34 FAIL116000 34 FAIL117000 39 FAIL118000 36 FAIL119000 36 FAIL120000 38 FAIL`

No new conclusions from this test.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#22
##### Jan 11, 2013, 04:24 pm

I like what you are doing robtillaart!

Would it be possible to write a code that auto calibrates the "magic numbers".

I would imagine using two serial communication links. One link could be the hardware serial port that would send to the slave the baud rate to be tested. Next, at the determined baud rate, the master would send a byte or string that is predetermined. The slave would then adjust the timing variables (within an allowed range) until the string is captured successfully "x" number of times. Lastly, the slave would perhaps save the variable results to EPROM or send to the serial monitor.
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#### robtillaart

#23
##### Jan 11, 2013, 09:56 pm
Quote
Would it be possible to write a code that auto calibrates the "magic numbers".

Definitely, but because the tables existed It was easier to let Excel find them.

In fact you can derive the magic numbers from the protocol. You know that the a data bit has 1/baudrate seconds between the edges. As you can see how much time the machine code takes to read and store a bit you can derive the magic numbers even quite exact for a given baud rate.

The real problem is that you need to find a protocol that works for all given baud rates, so an auto calibrating mode needs a number of known patterns to learn. Best to start with the highest baud rate as this is the most critical one. If you have the basic formula  rxbit = CLOCKSPEED/(alpha * baudrate) - beta;  you can try all possible combinations for alpha and beta between 1..20 so after 400 bytes you get the ranges for alpha and beta that work. You try the next baud rate and the ranges will decrease until all baudrates done. Then take the middle of the ranges and you're done.

A better faster approach is first find the optimal alpha, the search the optimal beta then alpha again then beta again until same values appear.
That will bring you to the optimal value within 50 or so bytes  (~10x faster).

Instead of linear search through the ranges you can do a binary search,...

For me the strange thing is the value SEVEN where I expected EIGHT in the formula - 16000000L/(7 * baudrate) - 3;
but I did not really investigate 16000000L/(8 * baudrate) + BETA ... => todo list

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#24
##### Jan 11, 2013, 10:07 pm

I'm glad to hear it is possible. You have helped my understanding of the process a lot although, it is a bit over my head to try to program an auto-calibration program.

Thanks,
Mark
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#### robtillaart

#25
##### Jan 11, 2013, 11:20 pm
There are 4 levels of acting -> goal -> strategy -> tactics -> operations

You did define your goal, but you were thinking tactics (how to program) and operations (details) . You skipped one level.

You must think of the strategies how such thing can be done and do "thought experiments" to find the tactics that belong to the strategy to make the strategy choice.

strategies can be : - brute force (test all), random search (aka darting;), hill climbing (change any param that improves the result), analytics etc

imho it is not over your head, it's not easy but you can do it, give it a try

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### synfinatic

#26
##### Jan 29, 2013, 07:32 pm
Hey Rob, I just wanted to say thank you for your research on this.  As it turns out I have a need for SoftwareSerial to do 7800 baud to read from a motorcycle ECU->Dash communication link and it sounds like you've solved my problem.

#### robtillaart

#27
##### Jan 29, 2013, 08:41 pm
@synfinatic

Let me hear the results of your tests, I'm interested. I can imagine that a motorcycle can generate quite some noise which may be disruptive to the signal.
Did you think shielding/grounding etc?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### synfinatic

#28
##### Jan 29, 2013, 09:02 pm
I haven't even begun to think/worry about that yet.  Hopefully it just works.  Ha!

In all seriousness, I'm literally waiting for FedEx to deliver my 'scope (hopefully Friday) so I'll have a better idea then for what I'm dealing with.  I do know that at 9600 baud it seems to kinda-sorta work, but I do seem to be getting some corruption, but that mostly seems to be due to the timing differences from what I can tell/guess.  So it appears the line is pretty clean.

That being said, I haven't tried it with the bike actually running yet, so things could get a whole lot noisier once those coils start firing.

#### synfinatic

#29
##### Jan 30, 2013, 06:33 am
So far so good.  7800 seems to be decoding messages without any errors and without my timing hack which was necessary at 9600.

Go Up

Please enter a valid email to subscribe