Topic: USB POV/Accelerometer Toy (Read 64948 times)

## Re: USB POV/Accelerometer Toy

##### Reply #45 – February 16, 2012, 03:51:36 pm
[quote author="viswesr"]This requires floating point arithmetic. The value of k was chosen by trying different values like 0.1, 0.2, 0.01, 0.02 etc.. . At 0.04 we got a smooth curve.[/quote]
If you are working with a processor that runs very slow when using floating point, then you can used fixed point instead of float or int. In fixed point, you typically have a sign bit and all the remaining bits are fractional. Your k = 0.04 constant would be 0x05 for 8-bit fixed point, 0x014 for 10-bit fixed point, 0x052 for 12-bit fixed point, or 0x051F for 16-bit fixed point. These values are not precise, and end up being 0.039, 0.0400, 0.03999 or 0.03998, but floating point is also not always precise for certain fractional values. Since you were hunting around until you found 0.04, then I suppose it doesn't really matter how precise the number is so long as it works.

It's actually quite common in the DSP world to calculate a 1st-order IIR low-pass using fixed-point math. It can sometimes be a pain to deal with the multiplications, but as I said, if things are running slow in floating point then fixed point can save you.

Also, depending upon your available processing power, fixed point might give you snappier response than floating point.

## Re: USB POV/Accelerometer Toy

##### Reply #46 – February 17, 2012, 07:33:58 am
Thanks for your suggestion. At the moment, the MCU is configured to run fast. It supports USB. Hence, floating point arithmetic works fine. In future, we could try to run the CPU at two speeds -- one for USB (fast) and another for display (slow) to reduce power. In this case, we can decide if fixed or floating point arithmetic is useful based on the precision and performance.

In the above plots, the data captured by PC via USB-UART has some duplicate values (which is avoided now).  At 250Hz sample rate, even k=0.1 gives a good result. I am fine tuning this value for the application.

Regards,
Viswa

## Re: Re: USB POV/Accelerometer Toy

##### Reply #47 – February 17, 2012, 01:54:15 pm
"Fast" is relative. I use so many different CPUs that I cannot remember which ones support floating point natively. My suggestion was intended for processors without native floating point support. The PIC18F14K50 must be using a floating point emulation library, unless I am mistaken.

Another thing to think about is whether the MCU speed is independent of the USB speed. I'm working with a DSP that has four different clocks. Besides the Real Time Clock and the CPU clock, there is both a digital and an analog PLL for USB (the analog has less jitter). Anyway, on that processor it is possible to slow down the CPU while still running USB at Full Speed. These sorts of gimmicks actually save on battery power. I believe that Microchip allows the CPU to be slowed down without ruining the USB support.

In other words, you might find that your battery will last a lot longer if you rewrite your floating point code as fixed point, and then slow the CPU clock until it just works.

## Re: Re: USB POV/Accelerometer Toy

##### Reply #48 – February 17, 2012, 05:47:14 pm
The PIC18F24J50 is connected to a 16Mhz crystal and PLLDIV is set to  4 and  CPDIV = 0. Yes, this MCU does not have a native floating point support. Fixed point arithmetic will be used for this filter.

MCU clock frequency has to be atleast 8 Mhz for USB to work. The USB interface is used for uploading the bitmap and after which we can reduce the clock speed further.

Thanks.

## Re: USB POV/Accelerometer Toy

##### Reply #49 – March 02, 2012, 11:46:41 am
contd.. of Part 1 (preprocession acccelerometer data):

The accelerometer plot posted in Part 1 (post) has some duplicate data. A proper data sampling produced good results for k = 0.1 in filter expression.

The new filter expression is AccelXFiltered = 0.9 * AccelXFiltered + (0.1) * AccelX; . In code, a fixed point arithmetic was used instead of floting point arithmetic.

## Re: USB POV/Accelerometer Toy

##### Reply #50 – March 02, 2012, 11:58:28 am
Part 2 : Accelerometer Zero G Offset Calibration

For proper functioning of POV Toy accelerometer has be calibrated for 0g Offset. The following application note gives details about offset calibration : AN3728, How to Calibrate 0g Offset for the MMA745xL Family http://http://cache.freescale.com/files/sensors/doc/app_note/AN3745.pdf

## Re: Re: USB POV/Accelerometer Toy

##### Reply #51 – April 19, 2012, 04:20:37 am
[quote author="viswa"]The PIC18F24J50 is connected to a 16Mhz crystal and PLLDIV is set to  4 and  CPDIV = 0. Yes, this MCU does not have a native floating point support. Fixed point arithmetic will be used for this filter.

MCU clock frequency has to be atleast 8 Mhz for USB to work. The USB interface is used for uploading the bitmap and after which we can reduce the clock speed further.[/quote]
I think that you could set the Internal Oscillator Block to generate 125 kHz with OSCCON[6:4]=001 and FOSC=00. That would allow the USB to continue at 48 MHz while the other peripherals and CPU run slower. Then again, I've never tried this. I only looked at the data sheet. There could easily be a limitation on various clock speed interactions that is not shown on that chart.

## Re: Re: USB POV/Accelerometer Toy

##### Reply #53 – July 10, 2012, 01:13:51 pm
Nice build and great photo, thanks Pietja. Is this the board that needed high-current therapy? We'll add this to the wiki.
Got a question? Please ask in the forum for the fastest answers.

## Re: USB POV/Accelerometer Toy

##### Reply #54 – July 12, 2012, 08:54:45 am
Yes this is the board that needed some therapy and a little surgery but its working fine :)

## Re: USB POV/Accelerometer Toy

##### Reply #55 – March 26, 2013, 03:32:03 am
Good post! I have learned a lot. People here are so kind and warm-hearted.