I'd probably just grab the one in the Oscat library.
18.9. GEN_SIN
Type Function module Input PT: TIME (period time)
AM: REAL (signal amplitude)
OS: REAL (signal ofset)
DL: REAL (signal delay 0..1 * PT )
Output Q: BOOL (binary output) OUT: REAL (analog output)
GEN_SIN is a sine wave generator with programmable period, adjustable amplitude and signal ofset. A special feature is a adjustable delay so that with multiple generators overlapping signals can be generated. A Binary Output Q passes a logical signal, which is generated phase equal to the sine signal. The input DL is a delay for the output signal. The Delay is spe- cifed with DL * PT. A DL of 0.5 delays the signal by half a period.
Our controller is used for testing purposes. One can change amplitude, frequency, phase and offset on-the-fly without any discontinuity in the position, velocity and acceleration because we ramp values using 5th order polynomials.
BTW, my example above is flawed too. One should never add small floating point numbers to a bigger sum especially of the small floating point numbers can't be represented exactly as a REAL. This is much better.
Code:
K:=2*PI/1000; (* DO ONCE *)
...
...
N:=N+1; (* REPEAT THIS *)
N:=MOD(N,1000);
RESULT:=SIN(K*N);
In CapinWinky's example setting x to 0 is a major flaw.
Very true, a standard 32 bit float can only go up to about 8.4 million (2^24 signed) in the mantissa before it loses resolution. For instance, 8388607 + 0.5 will result in 8388607 on most platforms.
How so? It resets the mantissa; the only problem I see is my check should be >= instead of > and that would only matter if 2*PI was 6.383 and SIN(6.383) returned 0 (resulting in two 0 outputs in a row), and I would think most systems would not incorrectly define PI as 3.1415.
How so? It resets the mantissa; the only problem I see is my check should be >= instead of > and that would only matter if 2*PI was 6.383 and SIN(6.383) returned 0 (resulting in two 0 outputs in a row), and I would think most systems would not incorrectly define PI as 3.1415.
2*PI=6.283185307179586476925286766559
if the angle is incremented by 0.001 then the first time it will exceed 2*PI is 6.284. When you reset x to 0 you are effectively subtracting 6.284 not 2*PI.
dlweber said:
Were you wanting a quadrature output? I seen your title said QUAD sine wave
karsten get yourself the oscat txt version, so you can see what happens.
it is working correct.
if needing quadrature there is also a cos function.
btw it is not very fast as every step costs 1 msecond.