Tidal Effect Simulation

Share you PDE file with our community
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

Was afraid of that...guess I'll be writing my own night mode...
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

Code is working so far. I'll have some data after it runs for a while. In the meantime, here's the latest class all packaged up. I would love to get some testers :)
Attachments
Tide.zip
(1.42 KiB) Downloaded 472 times
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Tidal Effect Simulation

Post by DrewPalmer04 »

What's the size this takes up. Poor me and my standard RA. :( sad
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

It shouldn't take up much space... it's only one large function and a few helper variables. Give it a try. Be advised I just updated it... had an error with my Ebb/Flood calculation. I need a better way, but at least it should work correctly now.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Tidal Effect Simulation

Post by DrewPalmer04 »

Consider me a tester lol
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

Well isIncoming does not seem to be working right... not sure why :(
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Tidal Effect Simulation

Post by DrewPalmer04 »

What's it doing? Can you post the code here. I don't have access to arduino software to open .ino
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

Ino files are txt...open in any editor.

Basically comparing the amplitude of the sine wave. If its higher than the previous i return true else false...

i think i know what the problem is...the code probably runs too frequently and there may only be a blip when the amplitude is different.. It should change each second but I'm probably getting 5 or 6 checks per second...

I'll have to add a check to limit the test...
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Tidal Effect Simulation

Post by DrewPalmer04 »

I mean I'm on an iPhone and it's zipped too. Lol if you figured it out. Cool.
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

Yeah I know what I need to do. I need to keep the status in a variable. If it changes then we can switch the variable. If it stays the same, no change. Then the function can just return the status variable. That should work. Sound right?
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Tidal Effect Simulation

Post by DrewPalmer04 »

Yep. Will flow smoothly.
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

Ok. New code put in place. Looks like it's working properly... we'll just have to see :)

The tidal curve is looking good. Combined with Roberto's ReefCrest function, I'm quite happy with the way my flow is changing and moving around. The PWMSlope worked great going in and out of "Night" mode.

Here's the latest version.
Attachments
Tide.zip
(1.46 KiB) Downloaded 528 times
User avatar
jsclownfish
Posts: 378
Joined: Mon Oct 24, 2011 7:52 pm
Location: Saint Louis

Re: Tidal Effect Simulation

Post by jsclownfish »

nice work! I've been holding off on flow control for awhile to save a few bucks, but the tidal code work and the jebo prices with the RA connection seems to good to pass up,
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

The gap between high and low tide doesn't seem to be as big as it should based on current MoonPhase.. I need to look into that. I'm currently seeing a difference of 11, but it should be closer to 20 since we're close to a new moon. Oops....just realized why! :)
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

Yup. The issue was the offsets need to be split in half since we only need half to affect the amplitude. But I was dividing it before I calculated the range which we need as max-min. So my range was half what it should have been. Good to know this was close to my minimum range at quarter moons. I'll post the new code later tonight.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

This post has good info, which probably should have been posted here, but since the question was asked in another thread...

http://forum.reefangel.com/viewtopic.php?p=22021#p22021
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Tidal Effect Simulation

Post by DrewPalmer04 »

How would us control freaks implement this without sunlocation.h? I want to define my times really :)
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

Has no dependency...it only relies on MoonPhase...

The only thing I use sun location for is to set my night mode timing...you can just as easy hard code that.
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Tidal Effect Simulation

Post by DrewPalmer04 »

Just had the same thought. Thanks. Keep us updated. I'm about to "find" time to test this on the tank for you.
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

Ok. Didn't have time to package it, so I'll just post the code..

Tide.h

Code: Select all

#ifndef __TIDE_H__
#define __TIDE_H__
#define PI 3.141593 
class Tide
{
public:
  Tide();
  int  CalcTide();
  void CalcFlow();
  void Init(int Speed, int minOffset, int maxOffset);

  void SetOffset(int minOffset, int maxOffset);
  void SetWaveLength(double WaveLength);

  void SetSpeed(int Speed);
   
  inline int GetSpeed() { return m_Speed; }
  inline boolean isIncoming() { return m_Flood; } 
  inline boolean isOutgoing() { return m_Ebb; } 
  
private:
  int m_Speed;
  int m_CurrSpeed;
  int m_MinOffset;
  int m_MaxOffset;
  double m_Amp, m_PrevAmp;
  double m_WaveLength;
  boolean m_Ebb,m_Flood;
};


#endif  // __TIDE_H__
Tide.cpp

Code: Select all

#include <stdlib.h>
#include <math.h>
#include <Time.h>
#include "Tide.h"
#include "Globals.h"

Tide::Tide()
{
	m_WaveLength=12*SECS_PER_HOUR;
	m_MaxOffset = 50;
	m_MinOffset = 10;
	m_Speed = 50;
	m_Amp = 1;
	m_CurrSpeed=m_Speed;
}

void Tide::Init(int Speed, int minOffset, int maxOffset)
{
	SetSpeed(Speed);
	SetOffset(minOffset,maxOffset);
}

void Tide::SetSpeed(int Speed)
{
	m_Speed = Speed;
}

void Tide::SetWaveLength(double WaveLength)
{ 
	m_WaveLength = WaveLength;
}

void Tide::SetOffset(int minOffset, int maxOffset)
{
	// We want these in half since they will be multiplied by amplitude (+1/-1)
	m_MinOffset = minOffset/2;
	m_MaxOffset = maxOffset/2;
}

/*
Spring tides occur during the full moon and the new moon.
At these times, the high tides are very high and the low tides are very low.

Neap tides occur during quarter moons.
The result is a smaller difference between high and low tides

MoonPhase 0-25 = Spring
MoonPhase 26-74 = Neap
MoonPhase 75-100 = Spring

So, the effect of the Moon will be a cosine wave.
*/

int Tide::CalcTide() {
  double moonOffset; // gap between high and low
  double amplitude;  // tide curve

  // Calculate the gap between high and low tide based on MoonPhase()
  moonOffset=cos(((2*PI)/100)*MoonPhase());
  moonOffset=((moonOffset+1)/2)*100; // Comvert to percentage
  moonOffset=map(moonOffset,0,100,m_MinOffset,m_MaxOffset);

  // Find out the current tidal height
  amplitude=sin(((2*PI)/m_WaveLength)*now()); 
  amplitude=amplitude*moonOffset; 
  
  // Update Ebb/Flood
  m_PrevAmp=m_Amp;
  m_Amp=amplitude;
  CalcFlow();

  // Adjust the calculate speed to be in our adjusted range
  m_CurrSpeed=constrain(m_Speed+amplitude,0,100);

  return m_CurrSpeed;
}

void Tide::CalcFlow() {
  if (m_Amp>m_PrevAmp) {
    m_Flood=true;
    m_Ebb=false;
  } else if (m_Amp<m_PrevAmp) {
    m_Flood=false;
    m_Ebb=true;
  }
}
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

Hmmm...maybe I did want the range cut in half...probably...not sure now...oh well, play with it later :)

Edit: reverted the change in the code above...after re-looking at the math...
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

I think I can use the map() function to shift the range... this way I can stop banging my head and questioning myself... :)
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Tidal Effect Simulation

Post by DrewPalmer04 »

lnevo wrote:I think I can use the map() function to shift the range... this way I can stop banging my head and questioning myself... :)
HATE when that happens ;)
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

Modified the above code with the map function which looks to be working good in my test executable. Should be good. We'll see how test goes.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

Doing the constrain badly...need to reload later tonight. Thank god for graphs! ;)
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

Yep... was actually doing the constrain twice and amplitude starts out -1 to 1. So... didn't need it.

Code modified in http://forum.reefangel.com/viewtopic.php?p=22049#p22049
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Tidal Effect Simulation

Post by lnevo »

I think the map function is not working as written. Hard to tell since based on current MoonPhase we should be close to minimum which I am, but I've seen no change... In any event, I plan on doing some debug tonight to confirm the values Im getting.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

Yeah, map did not like a number between 0 and 1. I multiplied by 100 and then used that in the map function :)
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Tidal Effect Simulation

Post by lnevo »

So, I wanted to post some charts of that show the usage of the simulation.

Here is the graph of the default curve coming out of CalcTide() function. You can see that the high/low marks before Mar 25 are just slightly less than today's. This will continue to grow till we hit full moon.
Image

Here you can see the change of incoming/outgoing tide. I use the following code to address either channel 0 or channel 1 of my vortech pumps. I do the slower pump first so that RFS shows the higher value :)

Code: Select all

  ReefAngel.RF.SetMode(Custom,rcSpeedAS,tide.isOutgoing());
  ReefAngel.RF.SetMode(Custom,rcSpeed,tide.isIncoming());
Image

Here's the speed that my tide class is set to. You can see my transition to night mode around the 9pm and 10am marks.

Code: Select all

  tide.SetSpeed(PWMSlope-1(sl.GetRiseHour(),sl.GetRiseMinute(),
    sl.GetSetHour(),sl.GetSetMinute(),vtNightSpeed,vtSpeed,120,vtNightSpeed));
Image

And finally, the moment we've been building up to. This is the end result of the speed my vortech's are being set to. This is with the ReefCrest function applied to it. You could use whichever mode you wanted and feed the result similarly. The anti-sync output is applied to the second pump. That is the second graph.

Code: Select all

  int rcSpeed=ReefCrestMode(tide.CalcTide(),vtDuration,true);
Image
Currently the anti-sync pump is running at 70% of the main pump. The pumps switch roles during the switch from high toward low tide and vice-versa.

Code: Select all

  int rcSpeedAS=ReefCrestMode(tide.CalcTide(),vtDuration,false)*pumpOffset;
Image
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Tidal Effect Simulation

Post by rimai »

Looks pretty cool :)
Roberto.
Post Reply