MichaelMeissner:
I do wonder about whether you really want to do this <...>
**Agreed. **
I wrote this sketch for two reasons, one to learn more about using Streams and the many functions available which help to minimize code that I need to write and second because I wanted to experiment with off loading floating point from an Integer Only uC and not bother with dealing with integer arithmetic and then scaling to decimal.
- I feel point #1 is obvious and was well served
- Point #2 probably is obvious if I explain that the Interger Only uC is a PICAXE
Plus, while certainly not rocket science, the little sketch provides the foundation to play around and try other approaches to the ones I used. Having a working sketch as a foundation to investigate changes is helpful (I feel) to learning... and keeps 'trash questions' off the forum (or provide a convenient link to point lazy minds that cannot read online documentation.)
As to the reference to
lose a lot of the performance when you have to transfer data to/from the co-processor
, my experience is that "performance" is a relative term. The biggest obstacle to working with an UNO is the 2K of RAM and this RAM is significantly impacted by linking in the IEEE float library. Further, every transcendental function takes a hit on the flash storage. There 'may' be cases where off-loading is proper and performance of the main uC is not impacted because it is busy doing other chores and will accept the coprocessor answer in 40mS or so over a port that is already implemented... moving a few ASCII bytes over serial requires no additional hardware or software in most cases. I'm expecting that the speed could be increased to 38.4K or 57.6K without any issues whatsoever - but I have not tried it outside of using the Arduino terminal.
The other, maybe less obvious, use is that the code can be extended to include new commands. For example, perhaps I need to routinely calculate the Hypotenuse of a Rt triangle. Pick a command name, say RTT, increment the variable "operations", add "RTT" to sStack[], implement the code needed in a new "case 21:" , snatch "a" and "b" from the serial stream, do the calculations, ... then pass the results back over the serial port. This is no different than defining the macro to the hardware coprocessor except that only a small serial stub is used in the primary uC... not a bunch of setup stuff.
Performance to some would be just to keep the main Arduino free of unnecessary libraries and RAM impact and to off-load the calculations of infrequent calculations and suffer the serial transfer penalties. If a hardware handshake were implemented between the two chips, then the main uC can use an interrupt to snag the results and be busy with other things during the wait.
But, I agree with everything you stated.
Still, a $2 ATmega328P, 16MHz xtal, and a couple of load caps would total less than $3... the performance may not be there but the cost is not there, either. And, you are programming in a standard, already understood, environment.
...
#define operations 22
...
char* sStack[ ] = {
"DIV","MUL","ADD","SUB","Y^X","LOG","NLG","10X","1/X","e^X",
"SQR","X^2","SIN","COS","TAN","ASN","ACS","ATN","DEG","RAD",
"DEC", "RTT"};
...
case 21: // RTT calculate the Hypotenuse of a right triangle from sides a and b
if (verbose) {Serial.print(F("Prompting for rt triangle side a and side b: ")); }
a = Serial.parseFloat(); if (verbose) { Serial.print(a); Serial.print(F(" ")); }
b = Serial.parseFloat(); if (verbose) { Serial.print(b); }
if (verbose) {Serial.print(F(" Hypotenuse = ")); }
Serial << _FLOAT(sqrt(a*a + b*b), decimals);
if (verbose) {Serial.println(); }
break;
Example
Input: rtt 10 10
Output:
Enter Instruction: RTT Found at location 21
Prompting for rt triangle side a and side b: 10.00 10.00 Hypotenuse = 14.1421356
Milliseconds = 46 Free RAM = 1436
Example
**Input:**dec 3 rtt 10 10
Output:
Enter Instruction: DEC Found at location 20
Prompting for Decimal places 0-10: Decimal Places = 3.00
Milliseconds = 21 Free RAM = 1436
Enter Instruction: RTT Found at location 21
Prompting for rt triangle side a and side b: 10.00 10.00 Hypotenuse = 14.142
Milliseconds = 83 Free RAM = 1436
Calculator.ino (10.9 KB)