Weather Simulation for Dimming expansion module

Share you PDE file with our community
rufessor
Posts: 293
Joined: Tue Oct 25, 2011 7:39 am

Re: Weather Simulation for Dimming expansion module

Post by rufessor »

Got it significantly cleaned up... will be testing soon
rufessor
Posts: 293
Joined: Tue Oct 25, 2011 7:39 am

Re: Weather Simulation for Dimming expansion module

Post by rufessor »

Question-

Can I use structures or is this going to cause problems in porting this into the library?

Thinking of creating a structure (basically a class) to hold the diverse globals I use then I can pass that around to modify individual components in subroutines. But... it requires a .h file to be linked which contains the structure declaration and I am not sure if this will munge things up later.

I should also admit I am not certain I am going to do this... but it looks to be a nice compact way to hold globals allowing almost a hash like database... enabling me to rapidly add a "global" variable when I need by simply assigning a new member to the structure. In some ways it seems like this would in fact be better for incorporation into a code library as it "isolates" my stuff from everything else. But I will have to change a lot more of the code so I am not going here until I can figure out if its supported or advised.
Kurt? or Roberto? others who know?? thoughts?

I am likely exposing my ignorance here... cause its probably obvious- but only if you know it :)
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Weather Simulation for Dimming expansion module

Post by lnevo »

My moon simulation uses a struct but a related code was stuck in the .h file also the sunlocation and tide classes that you have to add to the libraries. So anyway you choose to go.
rufessor
Posts: 293
Joined: Tue Oct 25, 2011 7:39 am

Re: Weather Simulation for Dimming expansion module

Post by rufessor »

Sent you a PM... but to include others.

If I make a big structure to hold the necessary globals we now have a new .h file - seemingly not a big deal just one include line in the main. But, then any edits to the program for say, inclusion of a new variable or something require the .h file also be edited or (seemingly not a good choice) a new struct be created that inherits the old and includes the new elements. This gets nasty fast so I am not proposing that but am I missing anything with the idea of using a structure to hold all the globals.

Thinking I can then just initialize them as type and bounds within the structure and then in the code simply fill them as they are created. Seems like a good way of avoiding collision between variable names and its no big deal to simply edit the .h file containing the structure if we need a new variable. But I am not seeing this as saving many lines of code... just not sure I should through to the effort.

Proposing something like this


new .h file...

typedef struct{
long rise;
long set;
byte ChMAX[8];
etc...
}Weather


Then in code...
include Weather.h

Weather.rise=sunriseFunc(timenow);
and another example...
Weather.ChMAX[0] = calculation result for that channel intensity;

But this of course means that I need to go through every line of code and for all variables switch to the structure format ..

e.g. rise now becomes Weather.rise etc etc so is it worth the effort for name space preservation?
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Weather Simulation for Dimming expansion module

Post by lnevo »

I think you can just define the struct in the main ino file. You only need a separate declaration file if your going to share between other c files. I believe...

Are you planning to have seperate c files or put all the code into the .h file?
rufessor
Posts: 293
Joined: Tue Oct 25, 2011 7:39 am

Re: Weather Simulation for Dimming expansion module

Post by rufessor »

Having not yet done this, I can only say that there are numerous posts on Arduino forums stating that the compiler has issues with package creation occurring in what amounts to an inappropriate order for it to be included in with the code so the recommendations there for simplest fix is just to use an external file and include it. Thus that was going to be my method of choice (its not like I can't then test this by copy and paste so whichever works I guess). In terms of going to a full object oriented implementation, that would probably in this case be more work than required and do little other than split it up over a bunch of files and objects. If it was going to be a core implementation that would spit out stuff like rise and set and cloud times and storm stuff across a widely dispersed set of implementations it might be worth it but since its running on the controller and is basically designed to be a compact package its kinda not worth it IMHO.
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Weather Simulation for Dimming expansion module

Post by binder »

Just to chime in. Structures and classes are virtually the same. A structure is simply a class without functions (yes, I'm paraphrasing it).

If you plan on only using the variables in one file (Weather.cpp), then you won't need to have a header file. You can simply put all the variables at the top of the file (like you are already doing).
Ideally, you would have a header file and an implementation file for all the functions and you would turn it into a class to keep everything grouped together nicely. Then the class would only use the variables that are associated with it. This would make things simpler to deal with.

If you were to put all of your functions into a separate file and then create the variables in the sketch, you would run into problems like you were discussing. The compiler will not be able to locate the variables for use with your functions because they are located elsewhere. This is why a header file is needed. (which I'm sure you already knew that).

You mentioned that it could get "nasty fast" by having to include lots of variables in the header file. I would think that if you created the functions properly, there really wouldn't be a need to continually edit the header file with the variables. If you have to modify values, the best route would be to create a class and then have functions to initialize the variables and update them as needed for use in the code.
If you have to create more variables and functions in the code, then that's just how it goes. Having them located in 1 or 2 files and separate from the other code is much easier to deal with. Then you just have a class to included and start using.

With a class, you could simply have 1 function call to calculate all the values and store them in the proper variables and then have a function call to return the values.

IMO, have the class would be the best solution because of consolidation and isolation of the code and maintaining simplicity.

Going off of your example, here's a way it could potentially be used:

Code: Select all

class Weather {

public:
	Weather();

	// updates the values
	// calls CalculateSunrise, CalculateSunset, CalculateIntensity
	void Update();
	long GetSunrise();
	long GetSunset();
	byte GetIntensity(int channel);

private:
	// calculates sunrise and stores it in rise
	void CalculateSunrise();
	// calculates sunset and stores it in set
	void CalculateSunset();
	// calculates the intensity for channels and stores in ChMax
	void CalculateIntensity();

private:
	long rise;
	long set;
	byte ChMax[8];
};
Inside your INO file, you include "Weather.h"
Then have a global Weather variable:
Weather w;

Inside your loop you would have something like this:

Code: Select all

void loop() {
	// code
	// or have code that only updates 1 time per day
	w.Update();
	// this is not the proper format, just an example
	PWMSlope(w.GetSunrise(), w.GetSunset());
	// code
}
You could even use it inside the DrawCustomMain() function.
This way you don't have to worry about creating all the variables you need, you can simply keep them isolated and have a common interface to be used just like the rest of the libraries.

Just throwing out some ideas for you. Personally, I think the class is more readable, but that's just my opinion.
rufessor
Posts: 293
Joined: Tue Oct 25, 2011 7:39 am

Re: Weather Simulation for Dimming expansion module

Post by rufessor »

Thanks Kurt-

Class structured (OO whatever) C++ is not something I have done before but I am using Object Oriented structure in Perl for work so its not really foreign. Let me make something up that I think will mostly work out and have you take a look at it via PM. This is going to take a little longer cause the coding part of my brain is currently being completely fried by projects at work... so I need to finish a few things to free up some time to play!

Thanks- PM will eventually show up
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Weather Simulation for Dimming expansion module

Post by binder »

rufessor wrote:Thanks Kurt-

Class structured (OO whatever) C++ is not something I have done before but I am using Object Oriented structure in Perl for work so its not really foreign. Let me make something up that I think will mostly work out and have you take a look at it via PM. This is going to take a little longer cause the coding part of my brain is currently being completely fried by projects at work... so I need to finish a few things to free up some time to play!

Thanks- PM will eventually show up
haha. Yeah, I can COMPLETELY understand about your brain being fried. Mine gets that way too from work and play.
rufessor
Posts: 293
Joined: Tue Oct 25, 2011 7:39 am

Re: Weather Simulation for Dimming expansion module

Post by rufessor »

ALmost there at least in terms of a first try at this... probably much will be obviously a bit wrong in the implementation...


Lenovo and Kurt- I will PM you with some code- if you do not mind can you just LOOK at it. I have not tested it I just want to know if I have the structure correct.

I took your advice and tried this as a class... it will be much more portable that way. But this is my first and only attempt at this in C... its different than Perl so its likely I have things just a bit stupid..


Don't worry about doing any debugging or compile just need to know if it looks right for this. Your help will greatly speed my getting this done. Working off examples only takes you so far...
Last edited by rufessor on Fri May 10, 2013 8:58 am, edited 3 times in total.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Weather Simulation for Dimming expansion module

Post by lnevo »

Sure.
rufessor
Posts: 293
Joined: Tue Oct 25, 2011 7:39 am

Re: Weather Simulation for Dimming expansion module

Post by rufessor »

So I was looking at the sunlocation library- I was originally thinking about trying to stream line this and use just what Kurt did in pulling the rise set calculations I did on top of the SWFLTEK library so that the weather package (thats what I am calling it) would use that pre existing library, but your running your offsets all in one, where as I have it set up to allow individual strings to accept different offsets. I think this will not resolve easily... so I think I will probably move to using the new moon library but otherwise were going to have to implement another instance of the rise and set calculations with my offset calculation in this new class. No big deal since the underlying code base from SWFLTEK is compact and people will not be running both weather and sunlocation- Maybe take a look at if its possible to condense this into a single universal class but its going to be probably beyond me to deal with this level of abstraction- I am confused enough dealing with my own code :mrgreen:

This will make more sense when you guys get code... but don't look for it today..
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Weather Simulation for Dimming expansion module

Post by lnevo »

I Think there would be overlap between users using both weather and sun location. Which offsets are you referring? We can easily break it up as needed, i can definitely help with that. We can also abstract offset stuff without having to make one big class also. I've been thinking now that my git is working to submit a pull request with sun/moon/tide. Weather would be a great one to have inside. So after you send the code we can go over what might be required! Sounds like you've made some good progress :)
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Weather Simulation for Dimming expansion module

Post by lnevo »

Another option would be to abstract out the swfltk libraries so both classes use them independently. Again, willing to help however I can :)
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Weather Simulation for Dimming expansion module

Post by binder »

yeah, all of that stuff can be easily abstracted. we just need a good solid foundation of how things will work and we can go from there. piece of cake. that was one of my specialties when i was a software engineer. :)
avionixx
Posts: 20
Joined: Thu Apr 25, 2013 10:12 pm

Re: Weather Simulation for Dimming expansion module

Post by avionixx »

I'm trying to upload this to my PWM expansion but for some reason the upload is always timing out. Do I need to make any changes to my upload settings to get it to finish?

I'm plugged in directly to the PWM module using the RA upload cable
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Weather Simulation for Dimming expansion module

Post by rimai »

Make sure you choose Reef Angel w/ optiboot as board
Roberto.
avionixx
Posts: 20
Joined: Thu Apr 25, 2013 10:12 pm

Re: Weather Simulation for Dimming expansion module

Post by avionixx »

Haha, that was the only option I didn't try. Thanks Roberto!
avionixx
Posts: 20
Joined: Thu Apr 25, 2013 10:12 pm

Re: Weather Simulation for Dimming expansion module

Post by avionixx »

Ok, so I got it up and running on my PWM module and everything looks like it's working except I notice that when clouds pass over, the dimming is choppy; it almost takes on a flickering type of quality. I'm not sure if this is normal or not, I was expecting it to be a bit smoother than what I'm experiencing. Any thoughts?
jjdezek
Posts: 329
Joined: Fri May 17, 2013 1:35 pm

Re: Weather Simulation for Dimming expansion module

Post by jjdezek »

ok im interesting in doing this code but im new to the coding and dont know what the channel max and flicker is? i know i my lights dont kick on until they hit 10 and they peak at 35 for whites and 40 for blue. where do you get the 31 number and the 200's from?
Image
avionixx
Posts: 20
Joined: Thu Apr 25, 2013 10:12 pm

Re: Weather Simulation for Dimming expansion module

Post by avionixx »

jjdezek wrote:ok im interesting in doing this code but im new to the coding and dont know what the channel max and flicker is? i know i my lights dont kick on until they hit 10 and they peak at 35 for whites and 40 for blue. where do you get the 31 number and the 200's from?

Channel max is basically the max intensity you want your lights to be (from 0-255) and channel flicker is how low your lights can be dimmed before they start to flicker. Keep in mind that both values must add up to 255 or less, any more and the lights won't turn on.

What I ended up doing was using percentages to calculate the values; I keep all of my lights at 60% max so I multiplied 255 x .6 and got 153 for channel max. As for the channel flicker, my drivers can dim to 0 without flickering so I put it as 1. If you have drivers that can't dim to 0, you'll have to do some trial and error to see how low they can go without flickering.
jjdezek
Posts: 329
Joined: Fri May 17, 2013 1:35 pm

Re: Weather Simulation for Dimming expansion module

Post by jjdezek »

so my max is 40% on blues so i take 255 x .4=102 so i would put 102 in the channels that are for blue? whites peak at 35% so i would put 89?
Image
avionixx
Posts: 20
Joined: Thu Apr 25, 2013 10:12 pm

Re: Weather Simulation for Dimming expansion module

Post by avionixx »

jjdezek wrote:so my max is 40% on blues so i take 255 x .4=102 so i would put 102 in the channels that are for blue? whites peak at 35% so i would put 89?
Yup, you got it!
jjdezek
Posts: 329
Joined: Fri May 17, 2013 1:35 pm

Re: Weather Simulation for Dimming expansion module

Post by jjdezek »

ok i went through the code and changed things to my liking and what not. i tried loading it and it came back with error invalid digit "9" in octal contstrant
Image
jjdezek
Posts: 329
Joined: Fri May 17, 2013 1:35 pm

Re: Weather Simulation for Dimming expansion module

Post by jjdezek »

do you paste the code into the regular sketch or is there someplace else you put it? i tried the standard code without modifying it and it gave me the same error? what are the steps to load this code?
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Weather Simulation for Dimming expansion module

Post by lnevo »

Sounds like a syntax error, can you post the full code and the compiler error msg.
jjdezek
Posts: 329
Joined: Fri May 17, 2013 1:35 pm

Re: Weather Simulation for Dimming expansion module

Post by jjdezek »

i tried but it said im only allowed 60000 files too big.....im a noob and not sure how to do all this posting codes on forum
Image
jjdezek
Posts: 329
Joined: Fri May 17, 2013 1:35 pm

Re: Weather Simulation for Dimming expansion module

Post by jjdezek »

The following features were automatically added:
Watchdog Timer
Version Menu

The following features were detected:
Simple Menu
sketch_jun02b.cpp:41:19: error: invalid digit "9" in octal constant
sketch_jun02b.cpp:41:27: error: invalid digit "9" in octal constant
sketch_jun02b.cpp:41:35: error: invalid digit "9" in octal constant
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Weather Simulation for Dimming expansion module

Post by lnevo »

Awesome so those errors are telling you which lines have errors in them. Sometimes its above or below depending on the errors. Can you post the full code?
jjdezek
Posts: 329
Joined: Fri May 17, 2013 1:35 pm

Re: Weather Simulation for Dimming expansion module

Post by jjdezek »

i tried it said the code was too big to upload...i went back to original code and redid it now its scanning fine. before i load this into my dimmer expansion i have one question. i put the lat and long for hawaii but im in ga if i understand it correctly it will still come on in the morning here not there right?
Image
Post Reply