Welcome! Log In Create A New Profile

Advanced

Temperature management code

Posted by dazed.dnc 
Temperature management code
September 26, 2010 10:32PM
I was getting 10-20 degree temperature swings from my PID controler. I spent all weekend trying to adjust it and wasn't able to do much better than a plain bang-bang control does. I also tried making my own temperature lookup table as well as swapping in the published ultimachine thermistor table. This got my temperature readings a little closer to the actual temperature, but I still had big swings.

As I watched how the temperature bounced back and fourth, I noted that if the heater would kick on earlier in the down swing and switch off shortly after the up swing started, this would reduce the oscillation amplitude. I also noted that if the heater could slowly switch instead of instantaneously cutting off, this seemed to slow down the rate of change and make it easier for the controler to pick up. Based on these observations, I hacked together the following code:

    PID4_Input = 1023 - analogRead(THERMOCOUPLE4_PIN);
    error = (PID4_Input - PID4_Setpoint) / PID4_Setpoint;
    PID4_Input += PID4_Input*error; //exaggerate the change by the error, should cause our swing to get detected sooner
    if( PID4_Input < PID4_Setpoint){    
      PID4_Output = abs((int)1023*error) ; //the "on" intensity  is proportional to our error
    }else{
      PID4_Output = 0;
    }
    analogWrite(HEATER4_PIN,PID4_Output); //bang bang better than PID?

I have tested this code at 100C and 60C set points. In both cases, the actual temperature stayed within -0/+3C of the set point. However, the output almost never turns off. The voltage output from my MOSFET is almost always at 1-2V (the heater is actually operating on 120V and is controlled by a solid state relay which is driven by the MOSFET on the extruder controler. This was the easiest way I found of getting the ~240 or so watts needed to heat the reprap build plate to 100C without getting a large power supply.)

For my build plate setup, it seems to work great. I fear that it will not work for the extruder heater though, since it draws more amperage. The MOSFET and relay seem to be staying very cool, but I suspect that this is because the power dissipation is very low. If I try to drive a 6 ohm load with a MOSFET that is only 8% on for several hours, what effect is that likely to have in the long run? Can MOSFETs run like this without cutting their life shorter?
Re: Temperature management code
September 27, 2010 12:09AM
mosfets are fine as long as you don't exceed their voltage ratings and don't heat the die above 175c.

If you're seeing 1-2v across your mosfet when it's switched on, your Vgs isn't high enough for that mosfet. I used IRL3803 in my electronics, it can carry 16A at Vgs=4.5v and no heatsink.

Most power mosfets are only partially turned on at Vgs=4.5v, you need to look for logic level power mosfets which actually quote Rds(on)@Vgs=4.5v in the datasheet.


-----------------------------------------------
Wooden Mendel
Teacup Firmware
Re: Temperature management code
September 27, 2010 07:07AM
The mosfet does switch all the way on if I write 1023 to the output pin, but if I use the above code, the output seems to settle at a much lower value - which only partially switches the mosfet. It reaches a relatively steady state immediately after the first overshoot, which is how a good PID loop is supposed to look. I just wasn't sure what this would do to the circuits since this intentionally drives them at a voltage bellow Vgs for extended periods of time.

I'm assuming this causes power loss in the form of waste heat at the solid state relay which drives the load too. It doesn't feel like it is heating up, so maybe my assumption is wrong or maybe I just haven't run it long enough to feel the heat on my heat sink. Am I loosing a significant amount of energy efficiency by running my heater like this?

Edited 2 time(s). Last edit at 09/27/2010 05:59PM by dazed.dnc.
Re: Temperature management code
September 27, 2010 05:11PM
hm, on closer look you're using PWM. that switches the mosfet on and off /really/ fast to simulate an analog voltage. It's no sweat for the mosfet itself, although it can stress your microcontroller's output pin if its connected directly to the gate.

Also, SSRs are usually a standard optotriac/triac pair so it won't switch off until mains zero-crossing.

If your PWM frequency is much faster than mains frequency, it'll be turning on quite early in the cycle and delivering 90% power almost regardless of PWM setting.

If you want to properly PWM a mains device, you need to detect zero crossings and synchronise your PWM clock to them, otherwise you get too much power regardless of PWM value, or beat frequencies, or both. Optocouplers are your friend here, allowing you to bring the information across your necessary isolation gap.

As for the heat, SSR forward drop is probably only a volt or so, and 240w/120v is 2 amps. 2A * 1V is 2 watts, an amount of waste power that a TO-220 package can just barely dissipate all by itself and which larger packages should have no problems with.

ps: PWMing mains stuff like this can generate some pretty horrific EM noise. make sure you have a snubber in place, even if the SSR has one already


-----------------------------------------------
Wooden Mendel
Teacup Firmware
Re: Temperature management code
September 27, 2010 05:58PM
I was just going to edit my previous reply, but I see Triffid_Hunter posted, so I'll start a new one.

I measure that the heater draws an average of 0.5A to maintain a steady state. At 120V, this means I am drawing about 60W to keep the build plate at a steady 100C. It takes 264W (120V at 2.2A) for about two minutes to get it to 100C. Considering a typical filament bulb is rated at 60W, that doesn't sound bad to me. Since this code powers the heater to some level almost 100% of the time, I would still be interested in knowing if a real PID loop that drives it with an actual duty cycle could yield higher efficiency, but I'm not sure how to calculate or measure that - nor could I get my PID settings adjusted to a point where it reached a steady state. I suspect that it averages out to about the same over time, but I would like to do the math and see if it were true.

Triffid_Hunter: I knew about the zero-crossing thing. Nophead told me about that in another forum. I thought maybe PID would be able to compensate for the on/off delays though since the point of PID is to slowly figure out a duty cycle that maintains the set point. That probably explains why I was having such a hard time tuning it though. Could a larger D term or a larger I term make this less problematic, or is it just too chaotic to compensate for?

I checked the schematic and the mosfet gate is directly connected to the Arduino. How do you suggest connecting to the mosfet gatge instead? Also, what is a snubber? Is it just one of those iron rings placed over the power wire, or is it a specialized circuit?

Edited 1 time(s). Last edit at 09/27/2010 06:01PM by dazed.dnc.
Re: Temperature management code
September 27, 2010 08:04PM
Your issue is that your PID is probably running a couple hundred times faster than the mains. At 60hz, mains zero-crosses 120 times per second, and it's the time between zero cross and SSR switch-on that determines your duty cycle. If the PWM is running at some number of kilohertz, then the SSR will /always/ be turned on quite shortly after the zero crossing, rendering your PWM output equally as effective as bang-bang. You can still use PID and bang-bang control, simply turn output on if PID result above some value (eg 64), and off if below.

As for the mosfet gate, I have mine directly connected too, but a 10r to 22r resistor in series is preferred. The issue here is that the mosfet gate looks like a capacitor (I think about 2nF) so it takes tons of current in short bursts to turn on and off quickly. The resistor drops the peak current while still allowing the mosfet to turn on and off quickly. Apparently the gates can also oscillate or ring without the resistor, but I haven't personally encountered this just yet.

The mosfet I chose (IRL3803) can carry 16A with no heatsink. 16A*12V=192W, so I will just use 12v for my heated bed when I make one.

The snubber is basically a combination of resistors and capacitors and maybe diodes to smooth out the current surge when the SSR turns off. In AC circuits, the voltage and current can get out of synchronisation (google "power factor") so there can still be current flowing at zero crossing. Since the SSR turns off at this point, there can be an inductive kick which may exceed the voltage rating. The snubber "grabs" this pulse and keeps it below the voltage rating of the SSR while not altering normal operation. This is rarely a problem with resistive loads like heaters, but adding a snubber is a good habit to get into.


-----------------------------------------------
Wooden Mendel
Teacup Firmware
Re: Temperature management code
September 28, 2010 05:30PM
Thanks for the advise. I really need to get this machine running since I have a couple of time-sensitive projects going on. I think what I'll do is stick with the current set up just to keep things moving forward. It doesn't sound like there is an immediate threat to the circuits and it looks to be decently efficient energy wise, so I think it is acceptable for now. Even if it does wear out the arduino output, there are still 5+ others to choose from. If it burns out in a matter of weeks, I'll feel a little differently about it though.

I did try a 12V nicrome heater first. I had to wrap too much nichrome around the plate, which made me concerned about short circuits. I was also calculating that it would need a minimum of 12A. I wasn't comfortable pushing my PSU that close to the maximum rating. I briefly looked at using resistors, but it wasn't clear to me what wattage/type of resistor to get and I don't have high temperature solder on hand. It seemed simpler/safer to just mount a doughnut shaped 120V coffee heater and be done with it. Even though the voltage is ten times higher, I don't have to worry about wires shifting around and creating super hot spots that set fire to the plywood. If I hadn't smelled the smoldering wood, that is exactly what would have happened to the makerbot.

That said, there are obvious reasons to stick with DC heaters. I'll keep this thread bookmarked and re-examine the heated build plate when I have more time.
Re: Temperature management code
October 17, 2010 11:23PM
A quirk of PID is that the entire thing is heavily influenced by how fast you PID versus what the time constants of your heater circuit and feedback are.

I found this building a Duck Egg incubator that I wanted to be fairly accurately controled.

It was DC control so the loop versus supply frequency was'nt an issue.

If the loop is running too fast then it over compensates followed by a correcting under compensation and oscilates around the desired set point (temp you want). It is all just too quick and therefore unstable.

You also get a different kind of instability if it is too slow. But it is better than if your pid loop is too fast.

The time constants of your heater/feedback are influenced by:-

A. How close your sensor is to your heater.
B. How quickly your sensor reacts to a change of temperature and accurately registers it (Some are surprisingly slow)
C. How much heat energy your heater can dump into what you are heating (wattage), this is related to how quickly the heat will travel across your heated pate. And also how much you can over drive it.

You need to get all of this in the right ball park before you attempt to tune your PID terms if you want success.

It took me a couple of weeks of patient fiddling to learn this.

So if your heater is over powered (heats things up quicker, could be an advantage, can also be a disadvatage when coupled with too fast a PID loop) but is positioned a long way on the heat plate from the thermistor and the quoted response time of the thermistor is slow. Your fast pid will accumulate positive error (integration) over heat the heater plate at the heater and much too late will notice what it has done. It now compensates by turning the heater way too low and keeps on going (again integrating way too much error) the plate cools and eventualy the thermistor ntices. Pid then starts again with too uch heat.

So onwards it goes and just oscilates for ever. Adjust the PID parameters all you like it will make no difference.

Under these circumstances Bang/Bang will give a much better result.

So the fix is slow your pid loop right down.

ways to do this.

A. Filter some noise out of your thermistor pickup by oversampling and dividing the value (add the next value to the total and divide by two by doing a right shift, very quick) Every X running average samples do a PID calculation and set your heater drive.

B. Add in a delay to the loop. (what we actualy did above but used the delay to do something very useful)

PID is a bit of a pain to start with, once you have got the feel for it though it is great.

My incubator works realy well. Pity my Drake is firing blanks.

Cheers

aka47

Edited 1 time(s). Last edit at 10/17/2010 11:29PM by aka47.


Necessity hopefully becomes the absentee parent of successfully invented children.
Re: Temperature management code
October 27, 2010 10:28PM
I think my heater is too overpowered or the thermal mass is not enough for bang/bang to stabilize. I'm getting temperature swings just as big as with PID. I have been reading Nophead's blogs and it sounds like 3 degrees can make a surprising difference in the quality of the parts, so I would like to do better than a 10 degree temperature swing.

I have been trying to determine how to measure an appropriate time constant. So far all the information I'm finding just describes the influence of Tc in PID controllers or using it in formulas to calculate other PID parameters. Is there a way to graph out the system and pull a Tc value out of that data? I have a thermocouple temperature meter, so I can graph out the registered and actual temperature at different points in time, but is that at all useful?

I dug up some information on makerbot PID tuning, but the suggested T and L values are for an extruder. They do not seem to apply to my heated build plate.

I will also try moving my thermistor closer to the heater. I thought it would be better to measure temperature closer to the outline of the build plate, since the outside edges will take longer to heat up than the area immediately over the heater. Sounds like this adds too much delay in the system response though. Maybe I would just be better off increasing the delay between reaching the set point and starting a build.
Re: Temperature management code
October 28, 2010 04:35AM
Certainly moving your temp sensor close to or on your heater is a very good move.


Necessity hopefully becomes the absentee parent of successfully invented children.
Re: Temperature management code
October 30, 2010 01:52AM
if you move the sensor and your readings become too high, you can simply compensate by sending a higher target temperature smiling smiley


-----------------------------------------------
Wooden Mendel
Teacup Firmware
Re: Temperature management code
December 05, 2010 01:57PM
I almost forgot about this thread. I found the problem.

I'm running a RAMPS kit, which has one arduino performing all the control logic. There is a setting in the firmware for it that controls how many cycles it takes before the temperature management functions are run.

Back then, I think it was set to run every 1000 or 2000 cycles. I found that setting it to 750 helped substantially. Setting it lower would probably give even better temperature control, but I think there will be a point of diminishing returns where you start experiencing motor control issues. If there is something similar for gen3 electronics, the motherboard only has to deal with communications and motor control, so you can probably sample the temperature much more frequently and further improve the temperature control.
Re: Temperature management code
December 12, 2010 04:14PM
Been kicking around some ideas for power heater control using a variant of Burst firing.

I got to thinking that it is fine to DC supply everything but probably most of us are using large ish power supplys fed from AC mains to rung heating anciliarys (Beds etc) and extruders. Sometimes too we want to experiment with say hot air flow or other stuff. Loading up our existing DC supply to much is a bad idea. buying ever bigger supplies is an equaly poor idea.

For a little while I was looking at using electronic transformers used for halogen lighting as a cheap dc source.

Then got round to thinking that if we used 12v AC that was rectified it is plenty good enough for powering heaters.

Controlling AC looks on the face of it to be more troublesome than DC except when we stop to consider that the average heating circuit has such a long time constant that (As nophead observes) bang bang control can be good enough.

OK we can tap a little bit of that rectified 12v AC to smooth, regulate and power out controller. The rest can be left as Unregulated, recitfied DC.

This gives us (in the UK) 100 half cycles of 12v DC per second (in the states 120). we can control this very cheaply using thyristors (pence for killowats) and turn the power on selectively with a resolution of whole half cycles. It will also be reasonably noise free as it turns on/off around the zero point.

So our heaters probably have time constants around the 1 to 2 second mark meaning this is very doable.

What we do is assume a time period of 100 cycles and turn on the power at the zero points for the number of half cycles we want to give a percentage of heating. For 50% we trigger the thyristor 50 cycles out of the periods 100. 25 for 25% and so on.

we don't want the power to be all lumped up at one end of the 100 cycle period we want it spread out as evenly as possible across the full period. so either a look up-table or a bit of math will allow us to turn on the half cycles with relatively even spacing at the power level we want.

The technique is a variant on "Burst Firing"

It is as easy to control multiple channels as one and is very easy to do with an Arduino. (I was originally interested in the multi channel thing for controlling heat zones on experimental chip to filament extruders)

You would still run PID but your heater control would be heating power in % steps instead of PWM. Note that your PID should also run at a time period that is more appropriate to your heaters time constant.

High current 12v mains transformers and bridge rectifiers are readily scavenged from old Battery chargers, CB Power Packs etc etc.

A project for a rainy weekend perhaps.


Necessity hopefully becomes the absentee parent of successfully invented children.
Re: Temperature management code
December 12, 2010 11:53PM
Maybe I'm just not up to speed enough to understand the benefit of all that, but on the surface it sounds overly complex.

I use a solid state relay to control the AC. I use an output on my RAMPS shield to control the relay. Once I figured out the "SLOW_CLOCK" thing, I was able to control it without any special code or PWM. It has a temperature swing of maybe +/- 5C. I figure this is probably good enough because the build plate itself is going to have a slight temperature gradient from corner to corner even if the heater does maintain a perfectly steady temperature. I have not insulated the underside either, so I'm sure I could optimize it more than I have. I'm just fighting bigger problems at the moment.

What you are saying sounds like it could work, but that is a lot of customizing for what may be very little gain.
Re: Temperature management code
December 13, 2010 12:22AM
Using a transformer has it's own issues.

Transformers draw a fair percentage of their current, even when the output current is not being drawn upon. This is a major issue if you're trying to reduce the energy footprint (ie: more efficient).

You gain a bit of efficiency by using AC over DC (no regulation losses, no rectification losses), but not enough to offset the constant current draw.

Switch-mode supplies can reduce their input current draw when there is no output current draw (some do, some don't), plus they're much more efficient. They also use a lot less metal in their manufacture.

While the Burst firing method you describe is useful, it may not make any sense in a setup like this.
Re: Temperature management code
December 13, 2010 02:09PM
I think you will find an un-rectified transformer is more efficient than most switch mode PSUs. Cheap SM PSUs are rarely better than 80% efficient and need a fan to keep them cool.

Transformers on the other hand generally run a lot cooler even without a fan show far less energy is being wasted. True they need a magnetising current when there is no load, but SM PSUs also need some quiescent power and are a lot less efficient at low loads.

I have seen some cheap transformers where the laminations have been welded together instead of being clamped. I don't think they will be very efficient as it defeats the purpose of the laminations!


[www.hydraraptor.blogspot.com]
Sorry, only registered users may post in this forum.

Click here to login