Data Structures | Defines | Functions | Variables

heater.c File Reference

Manage heaters. More...

#include "heater.h"
#include <stdlib.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include "arduino.h"
#include "debug.h"
#include "temp.h"
#include "crc.h"
#include "sersendf.h"
#include "config.h"

Data Structures

struct  heater_definition_t
 simply holds pinout data- port, pin, pwm channel if used More...
struct  EE_factor
 this lives in the eeprom so we can save our PID settings for each heater More...

Defines

#define DEFINE_HEATER(name, port, pin, pwm)   { &(port), (pin), &(pwm) },
 helper macro to fill heater definition struct from config.h
#define DEFAULT_P   8192
 default scaled P factor, equivalent to 8.0
#define DEFAULT_I   512
 default scaled I factor, equivalent to 0.5
#define DEFAULT_D   24576
 default scaled D factor, equivalent to 24
#define DEFAULT_I_LIMIT   384
 default scaled I limit

Functions

void heater_init ()
 initialise heater subsystem Set directions, initialise PWM timers, read PID factors from eeprom, etc
void heater_save_settings ()
 Write PID factors to eeprom.
void heater_tick (heater_t h, temp_sensor_t t, uint16_t current_temp, uint16_t target_temp)
 run heater PID algorithm
void heater_set (heater_t index, uint8_t value)
 manually set PWM output
uint8_t heaters_all_off ()
 turn off all heaters
void pid_set_p (heater_t index, int32_t p)
 set heater P factor
void pid_set_i (heater_t index, int32_t i)
 set heater I factor
void pid_set_d (heater_t index, int32_t d)
 set heater D factor
void pid_set_i_limit (heater_t index, int32_t i_limit)
 set heater I limit
void heater_print (uint16_t i)
 send heater debug info to host

Variables

struct {
   int32_t   p_factor
 scaled P factor
   int32_t   i_factor
 scaled I factor
   int32_t   d_factor
 scaled D factor
   int16_t   i_limit
 scaled I limit, such that $-i_{limit} < i_{factor} < i_{limit}$
heaters_pid [NUM_HEATERS]
 this struct holds the heater PID factors
struct {
   int16_t   heater_i
 integrator, $-i_{limit} < \sum{\Delta t} < i_{limit}$
   uint16_t   temp_history [TH_COUNT]
 store last TH_COUNT readings in a ring, so we can smooth out our differentiator
   uint8_t   temp_history_pointer
 pointer to last entry in ring
   uint8_t   heater_output
 this is the PID value we eventually send to the heater
heaters_runtime [NUM_HEATERS]
EE_factor EEMEM EE_factors [NUM_HEATERS]

Detailed Description

Manage heaters.


Function Documentation

void heater_print ( uint16_t  i )

send heater debug info to host

Parameters:
iindex of heater to send info for

Referenced by process_gcode_command().

void heater_set ( heater_t  index,
uint8_t  value 
)

manually set PWM output

Parameters:
indexthe heater we're setting the output for
valuethe PWM value to write

anything done by this function is overwritten by heater_tick above if the heater has an associated temp sensor

Referenced by dda_start(), dda_step(), heater_tick(), and process_gcode_command().

void heater_tick ( heater_t  h,
temp_sensor_t  t,
uint16_t  current_temp,
uint16_t  target_temp 
)

run heater PID algorithm

Parameters:
hwhich heater we're running the loop for
twhich temp sensor this heater is attached to
current_tempthe temperature that the associated temp sensor is reporting
target_tempthe temperature we're trying to achieve

Referenced by temp_sensor_tick().

uint8_t heaters_all_off ( void   )

turn off all heaters

for emergency stop

Referenced by clock_250ms().

void pid_set_d ( heater_t  index,
int32_t  d 
)

set heater D factor

Parameters:
indexheater to change D factor for
dscaled D factor

Referenced by process_gcode_command().

void pid_set_i ( heater_t  index,
int32_t  i 
)

set heater I factor

Parameters:
indexheater to change I factor for
iscaled I factor

Referenced by process_gcode_command().

void pid_set_i_limit ( heater_t  index,
int32_t  i_limit 
)

set heater I limit

Parameters:
indexheater to set I limit for
i_limitscaled I limit

Referenced by process_gcode_command().

void pid_set_p ( heater_t  index,
int32_t  p 
)

set heater P factor

Parameters:
indexheater to change factor for
pscaled P factor

Referenced by process_gcode_command().


Variable Documentation

this struct holds the heater PID factors

PID is a fascinating way to control any closed loop control, combining the error (P), cumulative error (I) and rate at which we're approacing the setpoint (D) in such a way that when correctly tuned, the system will achieve target temperature quickly and with little to no overshoot

At every sample, we calculate $OUT = k_P (S - T) + k_I \int (S - T) + k_D \frac{dT}{dt}$ where S is setpoint and T is temperature.

The three factors kP, kI, kD are chosen to give the desired behaviour given the dynamics of the system.

See http://www.eetimes.com/design/embedded/4211211/PID-without-a-PhD for the full story

Referenced by heater_init(), heater_print(), heater_save_settings(), heater_tick(), pid_set_d(), pid_set_i(), pid_set_i_limit(), and pid_set_p().

 All Data Structures Files Functions Variables Defines