Go Down

Topic: Measuring phase angle and power (Read 9255 times) previous topic - next topic

draythomp

Just a quick interjection that might (maybe) help you avoid a problem.  I tried solid state relays for a high current project, a water heater controller.  It was a 240V thing that was optoisolated.  Worked great, got hot.  It had to have a heat sink and airflow to keep cool enough to touch and couldn't be mounted in a box outside where I live (desert) because the ambient temperature and afternoon sun would destroy it.  I switched to an old fashioned contactor controlled by a little bitty relay off a 5V digital pin.  This was because an A/C contactor was easily available and ran off 24VAC.  It runs nice and cool and has been cycling now for over two years.  At first the noise was offensive, now its comforting because I know it's working.

Now, I know it may have been the particular device I got stuck with and your mileage may vary, but keep this in mind when choosing the device.  In my case, old school worked best.
Trying to keep my house under control http://www.desert-home.com/

pauldreed


Just a quick interjection that might (maybe) help you avoid a problem.  I tried solid state relays for a high current project, a water heater controller.  It was a 240V thing that was optoisolated.  Worked great, got hot.  It had to have a heat sink and airflow to keep cool enough to touch and couldn't be mounted in a box outside where I live (desert) because the ambient temperature and afternoon sun would destroy it.  I switched to an old fashioned contactor controlled by a little bitty relay off a 5V digital pin.  This was because an A/C contactor was easily available and ran off 24VAC.  It runs nice and cool and has been cycling now for over two years.  At first the noise was offensive, now its comforting because I know it's working.

Now, I know it may have been the particular device I got stuck with and your mileage may vary, but keep this in mind when choosing the device.  In my case, old school worked best.

Thanks Dave, if it was simply switching the immersion heater on or off then I too would prefer a A/C contactor in preference to a SSR, but we are trying to match the power usage to coincide with the spare power available by using a phase controller. So if the immersion load is 2kw, and I only have 1kw spare solar power, then the phase controller would fire at 90 degrees, reducing the immersion load to 1kw.
Your blog makes interesting reading, perhaps I can swop you some Yorkshire UK cold and rain for a little Arizona heat and sunshine!

Perfo

The first question is, do you need it to run at an accurate time frame? And if so what kind of tolerance do you need?
One thing to remember about the delay() function is it is a little unproductive in that all it does is stop the processor so nothing gets done or calculated for the period you've set, well nothing gets done except for interrupts. So in essence you can't set a cycle time by using a delay function as every time you add a println or take one out your timings will change and unless all the functions you use in a program always take exactly the same amount of time to do their thing then the timings will change every loop of the code. So back to the original question, how accurate do you need it? If the answer is very accurate and quick then you may have to do a lot more analysis on what your function do and how they do it. For example that clumsy anti chatter stuff would be ditched. If the answer is pretty accurate and fairly fast then we have a bit of wriggle room.
I'll go with the latter for now, if everything is happening fast enough for your liking when you minus off the 2 second delay you put in then we don't need to analyse the code too deeply but we will need to get an idea of how long it takes to perform a single loop as obviously you can't have a refresh quicker than the bare loop time. You could put a println(millis()) statement at the start of the program and do 10000 loops then do another println(millis()) at the end and calculate the average time for one loop add a bit of time (say 50%) to get a bit of comfort and then take this as you max speed. You could then set up an interrupt to fire off the required output routine on a timed basis knowing all the other calculations have had time to do their thing. However there is an even easier answer if you don't mind a little bit of variation and that is to take a snapshot of the time (millis()) then keep looping until the
now time = the start time + 2000
and then fire off the output routine the actual time the output routine takes will still be a variable but should be fairly constant. So after all my waffling try this.

Add a couple of unsigned integer variables called StartTime and NowTime
Take out the delay() code on line 95
And instead add the following code.
do
{
    // Whizz around here until the 2 seconds have been used up
   NowTime = millis();
  If (NowTime < StartTime) {
    break;
  }
} while (NowTime < (StartTime+2000));
StartTime = millis();

This should act as a buffer to get the timings right as long as all the functions and print statement etc take less than 2 seconds to operate then it should fire off the outputs every two seconds. The output functions also take a finite time thus the start time is reset just before the outputs get called then the time it takes for them is also included in the overall timings.
Two slight problems with the quick fix and that is the first loop maybe out slightly as the millis function starts when the program starts to run and this will be ever so slightly different to the value of StartTime (which is zero) when it first gets compared. The second problem is every 50 days or so the millis() function will over flow which give us two scenarios
1, It happens just after the StartTime gets assigned and thus the NowTime will be a lot smaller than the StartTime
2, It happens after the NowTime gets set and thus the StartTime will be a lot smaller.
Scenario 2 doesn't really matter as it just means a short loop for that one but scenario 1 would result in a verrrry long delay and thus I've put a check for it in the While loop.
See how you get one with it I'm writing it in word in my tea break so as per normal it's not tested and thus maybe tosh..

Perfo


Just a quick interjection that might (maybe) help you avoid a problem.  I tried solid state relays for a high current project, a water heater controller.  It was a 240V thing that was optoisolated.  Worked great, got hot.  It had to have a heat sink and airflow to keep cool enough to touch and couldn't be mounted in a box outside where I live (desert) because the ambient temperature and afternoon sun would destroy it.  I switched to an old fashioned contactor controlled by a little bitty relay off a 5V digital pin.  This was because an A/C contactor was easily available and ran off 24VAC.  It runs nice and cool and has been cycling now for over two years.  At first the noise was offensive, now its comforting because I know it's working.

Now, I know it may have been the particular device I got stuck with and your mileage may vary, but keep this in mind when choosing the device.  In my case, old school worked best.

Yep some of them can get warm but heat sinks on the bay are fairly cheap, better still use the hot water tank as the heat sink and don't waste any preciosu watts. We don't have the same problem over here dray with things overheading if left outside, we do lose things due to being floated off in the rain or wind though :)

draythomp

Some folks don't realize those things actually get hot.  I guess it isn't intuitive because it came as a surprise to me.  To give you some idea, just two days ago I was helping my neighbor spread some asphalt for a new driveway.  We had a lot of trouble because it melted and was sticking to everything, the tires on my tractor were covered with about an inch of the stuff.  We finally gave up and waited until the next morning.  During this process one of the pins from the scraper blade on the tractor fell off and I had to find it.  When I picked it up, it burned my hand and I just popped the blister it raised on my left index finger.  High temperature today, 116F, but it's expected to be cooler tomorrow, around 113F.  By 0900 it was over 100F.  However, you do get used to it, I keep my swimming pool at 94F during the summer so I don't freeze when I get in it.

So, you can see why something that gets hot on its own isn't something that we want to put in a closed metal box outside.  Good old contactors.   In your neck of the woods though, it could have worked fine, even as suggested, mounted to the hot water heater.

But, I don't get much snow, and the girls don't wear much. :smiley-red:
Trying to keep my house under control http://www.desert-home.com/

pauldreed

#35
Jul 03, 2011, 11:53 am Last Edit: Jul 03, 2011, 11:09 pm by pauldreed Reason: 1
AAAAH, problem solved!
Spent quite a lot of time today trying to get my head around the programming language, and how the authors of the original project have written it. Eventually it became clear that it functions by sampling a defined number of cycles of the mains and when it has done that, it runs various calculations, eventually outputting the results to print, then repeats the process. So no need to have the delay (2000), all I needed to do was to increase/decrease the number of cycles analysed to alter the display update speed.
By introducing the delay (2000), there was in fact 2 timers running, and sometimes they were in sync (updating the display every 2 seconds) and sometimes not in sync (updating 3 or 4 seconds).

Thanks for the help Perfo.

Paul

Perfo

I like snow but then the girl thing maybe more tempting. :)

Perfo

It's not generally a good plan to rely on how long a function takes to run for the basis of doing something at a particular time. The most obvious problem would be if you transported the code to a faster or slower compatible processor or one with slightly different architecture. Even if you don't change the system speed a complex function that gets inputs and gives outputs will involve various hardware handshakes etc and thus will be different every time they run. In your case I don't think the differences are going to matter so if you're happy then job well done. However if you want to protect against variations and make the code multi platform then you would need the do while loop to add on however many milliseconds are needed to hit the 2000 mark. Of course this would still require the slowest processor to be able to do the calcs and samples in a fraction of the 2000 milliseconds. Delay() has its place but should be treated with caution.

Go Up