LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   RTLinux does not support sine functions...How do i make it support the same (https://www.linuxquestions.org/questions/programming-9/rtlinux-does-not-support-sine-functions-how-do-i-make-it-support-the-same-696460/)

navderm 01-11-2009 05:00 AM

RTLinux does not support sine functions...How do i make it support the same
 
I have been working on RTLinux for some time.
I wanted to generate a sine curve... but the RTLinux does not take the sin command from math.h. how do I make sin curve from RT.

I mean is there some changes in the RTLinux Makefile that can help me access it?

Plz help its urgent ..

wje_lq 01-11-2009 06:23 AM

Quote:

Plz help its urgent ..
Here ya go. If it's really that urgent, this is the quickest way.

navderm 01-11-2009 07:05 AM

Seems like a cool thing but it aint...
Coz i guess I have already done that and posted something here coz I did not find the solution I was looking for...

Well been said that, I have a code which works with AD/DA converter and has the capability to use the sine function in RT. so much for GOOGLING.

The make file that I use and the make file that this converters code uses is pretty different.

I tried using it but many variables clashed and system did not work finally.

So i thought someone must've worked on the RTLinux and used this system or may be used sine fn. (as is being used by the code).

Thx..

ErV 01-11-2009 07:14 AM

comment removed

jlinkels 01-11-2009 09:48 AM

math.h is not available in kernel space whwre RT is running in.

To get a sine function, you could implement it yourself using a Taylor series. If you use a polynomial of degree 7 (3 terms) your error is < 0.000003.

If that is too much processor time to calculate, create a table during startup and use that one. It goes without saying that you only have to create a table from 0 to pi/2 and other angles can be found using simple arithmetic, same for cosine and tangent.

jlinkels

navderm 01-11-2009 10:27 AM

Hey..
Thank you so much for the same..

I was thinking of using Taylor series...
But the problem is the frequency of work that i need..(around 5 KHz) from RTLinux.

Is there a way in which I could find out how much time this computation would take?

If this is not possible then its better choice to go ahead with the table format.

thx a lot once again

jlinkels 01-11-2009 11:16 AM

I don't think it is very CPU intensive.

Sin(x) = x - x^3/3! + x^5/5! - x^7/7! for a 7th degree polynome. If you calculate smart, you only have to calculate x^2, x*x^2, this *x^2 and this *x^2, which are 4 floating point multiplications, 3 divisions by a constant (3!, 5! are constants) and 3 additions. That is really not much.

Since you'd have to write the function anyway, just write it, and if it takes to much time use the function to generate the lookup-table.


jlinkels

jlinkels 01-11-2009 01:53 PM

In case you are writing this for an embedded system without math processor, you could implement this fully in integer arithmetic.

If you do that, you have to use smart scaling in order to have all terms fit into 32-bits integers, but it can be done.

You could scale like this:
for x: x -> x * 2^30
x^3/3!: x -> x * 2^10 (you'll end up with a term which is scaled 2^30)
x^5/5!: x: -> x * 2^5 (term is scaled 2^25)
x^7/7!: x: -> x * 2^3 (term is scaled 2^21)

Adding up the terms can be done as:

(x(1) - x(3) + x(5) * 2^5 - x(7) * 2^7) / 2^30

This keeps the eventual error < 10e-2.

Acuuracy might be improved if you write x^5/5! as (x^2/10) * (x^3/12) in which case you might be able to scale x to a higher power of 2 before you get an overflow.

jlinkels

navderm 01-14-2009 07:13 PM

Hie All

Here is how I got out of the problem.(it wasn't by GOOGLING alone)

I took an angle and converted it to the range between 0<=theta<=180
then i used four conditions to get the theta from 0 to 90 deg. (trigonometric relations)

i. theta
ii. 2PI - theta
iii. ...
iv. ...

then i used the taylor series.

if taylor series is used in the vicinity of 180deg htere is a lot of error.
till 90 deg there is not much error (0.001 types). so the result work fine for me.

In case any one gets the problem again.
I would recommend it as its very easy and implements well.

Thanx
===============

ALSO: I AM 100% CONFIDENT THERE IS A WAY YOU CAN USE SINE FUNCTIONS VIA MAKE FILE COMMANDS..
I HAVE NOT BEEN ABLE TO FIGURE IT OUT AS YET. BUT AS SOON AS I DO I'LL POST IT HERE SO THAT PEOPLE WHO WANT IT MAY GET IT.

IF ANYONE DOES KNOW ABOUT IT...PLEASE DO THE SAME

THX A LOT

~navderm


All times are GMT -5. The time now is 11:22 PM.