Author Topic: Energia - Float to string?  (Read 14167 times)

0 Members and 1 Guest are viewing this topic.

Offline IanB

  • Super Contributor
  • ***
  • Posts: 11859
  • Country: us
Re: Energia - Float to string?
« Reply #25 on: November 09, 2013, 08:14:40 pm »
Indeed.

If you sample a value from an A-D converter you will get an 8 bit or 12 bit scaled quantity. That is your raw data and it also represents the inherent precision of your sampled value. If you are going to do processing like filtering or averaging you may as well do it in the scaled integer space corresponding to your input device and convert it for display at the last moment. (This is how many control systems and PLCs work internally. They don't work with floating point as it is unnecessarily expensive.)

For instance, suppose you want to take an average of eight samples. You simply sum each of your samples into an accumulator until you have eight of them stored. To get the average you need to divide the result by 8 which is a simple matter of shifting right by 3 bits. Shifting and adding are very fast operations on all processors.

As a matter of interest I did this in 1984 during my final year lab project for my engineering degree. I had a laboratory scale glass distillation column and an 8 bit microcomputer running a version of tiny BASIC that only had integer variables. I could measure the temperature at the top of the column using an 8 bit analog input from a temperature sensor and I could control the reflux flow with a digital output that would open and close a solenoid valve. A single reading from the temperature sensor only gave me a temperature precise to 1 degree with a lot of random noise. So I took several readings in succession and averaged them, giving me a much more stable reading precise to about 0.1 degrees. I then implemented a PI control algorithm and used it to adjust the reflux flow by PWM on the reflux valve. All was done in 16 bit integer arithmetic with no floating point available. It worked fine and I could could regulate the temperature at the desired set point.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf