forked from Traumflug/Teacup_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heater.h
106 lines (85 loc) · 2.73 KB
/
heater.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef _HEATER_H
#define _HEATER_H
#include "config_wrapper.h"
#include <stdint.h>
#include "temp.h"
/// Default scaled P factor, equivalent to 8.0 counts/qC or 32 counts/C.
#define DEFAULT_P 8192
/// Default scaled I factor, equivalent to 0.5 counts/(qC*qs) or 8 counts/C*s.
#define DEFAULT_I 512
/// Default scaled D factor, equivalent to 24 counts/(qc/(TH_COUNT*qs)) or
/// 192 counts/(C/s).
#define DEFAULT_D 24576
/// Default scaled I limit, equivalent to 384 qC*qs, or 24 C*s.
#define DEFAULT_I_LIMIT 384
/** \def HEATER_THRESHOLD
Defines the threshold when to turn a non-PWM heater on and when to turn it
off. Applies only to heaters which have only two states, on and off.
Opposed to those heaters which allow to turn them on gradually as needed,
usually by using PWM.
*/
#ifdef BANG_BANG
#define HEATER_THRESHOLD ((BANG_BANG_ON + BANG_BANG_OFF) / 2)
#else
#define HEATER_THRESHOLD 8
#endif
#undef DEFINE_HEATER_ACTUAL
#define DEFINE_HEATER_ACTUAL(name, ...) HEATER_ ## name,
typedef enum
{
#include "config_wrapper.h"
NUM_HEATERS,
HEATER_noheater
} heater_t;
#undef DEFINE_HEATER_ACTUAL
/** This struct holds the runtime heater data.
PID integrator history, temperature history, sanity checker.
*/
typedef struct {
/// Integrator, \f$-i_{limit} < \sum{4*eC*\Delta t} < i_{limit}\f$
int16_t heater_i;
/// Store last TH_COUNT readings in a ring, so we can smooth out our
/// differentiator.
uint16_t temp_history[TH_COUNT];
/// Pointer to last entry in ring.
uint8_t temp_history_pointer;
#ifdef HEATER_SANITY_CHECK
/// How long things haven't seemed sane.
uint16_t sanity_counter;
/// A temperature we consider sane given the heater settings.
uint16_t sane_temperature;
#endif
/// This is the PID value we eventually send to the heater.
uint8_t heater_output;
} heater_runtime_t;
typedef struct {
/// sigma delta values for software pwm
int16_t sd_accu;
int16_t sd_dir;
} soft_pwm_runtime_t;
typedef enum {
NO_PWM = 0,
SOFTWARE_PWM = 1,
HARDWARE_PWM = 2
} pwm_type_t;
#ifndef FORCE_SOFTWARE_PWM
// Force hardware PWM if available.
#define HARDWARE_PWM_START SOFTWARE_PWM
#else
#define HARDWARE_PWM_START HARDWARE_PWM
#endif
void heater_init(void);
void pid_init(void);
void heater_set(heater_t index, uint8_t value);
void heater_tick(heater_t h, temp_type_t type, uint16_t current_temp, uint16_t target_temp);
void soft_pwm_tick(void);
uint8_t heaters_all_zero(void);
#ifdef EECONFIG
void pid_set_p(heater_t index, int32_t p);
void pid_set_i(heater_t index, int32_t i);
void pid_set_d(heater_t index, int32_t d);
void pid_set_i_limit(heater_t index, int32_t i_limit);
void heater_save_settings(void);
#endif /* EECONFIG */
void heater_print(uint16_t i);
#endif /* _HEATER_H */