Michael Lloyd

Sine Wave Generator

1 post in this topic

This is strictly for fun so don't burn out any brain cells on it. Since this is a programmers forum I know someone has already wondered "use this for what??". I'd use the tag to see how fast the server tag  is updating. The trend on the server will be different than the trend in the PLC by the amount of lag time in the system (duh). I might use some version of this to animate a graphic but with PHMSA we don't do much of that these days, 

The idea is to generate a fairly smooth sine wave with a period of 60 seconds by using only the PLC clock and some trigonometry. My trig skills have waned considerably over the last "few" (40... ugh) years.

I can generate a fairly ugly sine wave with a period that's a little too short with this:

// Generate a Sine Wave from Clock.Second

/* If CLOCK.Second > 0 AND CLOCK.Second <= 30 THEN
t := 2 * CLOCK.Second;
Else
t := (2 * CLOCK.Second) + 1;
End_if; Testing, this can be deleted */

t := 2.75 * CLOCK.Second; 

Y:=4.0*SIN(((3.14159/4)*(t/10.0))+(3.14159/2));

This is what generated the cleanest sine wave without any clock reset weirdness, sort of.

t/10.0 sets the period but the result is more like 40s  than the 60s I was looking for.

2.75 is a P.E.M.F. (precision equation modification factor) aka fudge factor that I came up with to get me close to what I wanted to see on the trend in the CLX software. It generates a sine wave with a slightly flat peak at the time the clock.seconds value resets to 0. I could probably tweak it a little and get rid of some of the slop.

The first bit that's commented out was me chasing a rabbit. It can be deleted.

The equation for Y is just the equation of a sine wave except that the real equation only uses t not t/10.0

t can be calculated with wavelength / velocity but I didn't chase that rabbit...

Why doesn't AB have a constant for ╥. It's probably the most universal constant there is and there's no ╥? 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now