Simpler definition of temp sensors and heaters.

This commit is contained in:
Stephan Walter 2011-01-30 13:40:36 +01:00 committed by Michael Moon
parent 14809c4a7c
commit fcaa76d3c0
6 changed files with 131 additions and 95 deletions

View File

@ -1,6 +1,3 @@
#ifndef _CONFIG_H
#define _CONFIG_H
/*
CONTENTS
@ -231,12 +228,9 @@
// ANALOG_MASK is a bitmask of all analog channels used- if you use more than one analog input (more than one temp sensor?), bitwise-or them all together
#define ANALOG_MASK MASK(TEMP_PIN_CHANNEL)
// how many temperature sensors do you have?
#define NUM_TEMP_SENSORS 1
/***************************************************************************\
* *
* Fill in the following struct according to your hardware *
* Define your temperature sensors here *
* *
* If your temperature sensor has no associated heater, enter '255' as the *
* heater index. Unassociated temperature sensors are still read, but they *
@ -250,19 +244,12 @@
* *
\***************************************************************************/
#ifdef TEMP_C
struct {
uint8_t temp_type;
uint8_t temp_pin;
uint8_t heater_index;
} temp_sensors[NUM_TEMP_SENSORS] =
{
// type pin heater
{ TT_INTERCOM, 0, 255 }
};
#ifndef DEFINE_TEMP_SENSOR
#define DEFINE_TEMP_SENSOR(...)
#endif
// name type pin heater index
DEFINE_TEMP_SENSOR(extruder, TT_INTERCOM, 0, 255)
/***************************************************************************\
@ -271,9 +258,6 @@ struct {
* *
\***************************************************************************/
// number of heaters- for GEN3, set to zero as extruder manages the heater by itself
#define NUM_HEATERS 0
// index of bed heater for M105 and M140 commands. set to 255 if no bed heater
#define BED_HEATER 1
@ -282,7 +266,7 @@ struct {
/***************************************************************************\
* *
* Fill in the following struct according to your hardware *
* Define your heaters here *
* *
* If your heater isn't on a PWM-able pin, set heater_pwm to zero and we'll *
* use bang-bang output. Note that PID will still be used *
@ -294,19 +278,13 @@ struct {
* *
\***************************************************************************/
#ifdef HEATER_C
struct {
volatile uint8_t *heater_port;
uint8_t heater_pin;
volatile uint8_t *heater_pwm;
} heaters[NUM_HEATERS]/* =
{
// port pin pwm
{ &PORTD, PIND6, &OCR0A },
{ &PORTD, PIND5, &OCR0B }
}*/;
#ifndef DEFINE_HEATER
#define DEFINE_HEATER(...)
#endif
// name port pin pwm
// DEFINE_HEATER(extruder, PORTD, PIND6, OCR0A)
// DEFINE_HEATER(bed, PORTD, PIND5, OCR0B)
/***************************************************************************\
@ -436,5 +414,3 @@ struct {
* OCR5CL - PL5 - DIO44 *
* *
\***************************************************************************/
#endif /* _CONFIG_H */

View File

@ -273,17 +273,17 @@ void process_gcode_command() {
break;
// M7/M106- fan on
#if NUM_HEATERS > 1
case 7:
case 106:
heater_set(1, 255);
if (NUM_HEATERS > 1)
heater_set(1, 255);
break;
// M107- fan off
case 9:
case 107:
heater_set(1, 0);
if (NUM_HEATERS > 1)
heater_set(1, 0);
break;
#endif
// M109- set temp and wait
case 109:
@ -326,7 +326,6 @@ void process_gcode_command() {
// newline is sent from gcode_parse after we return
break;
#if NUM_HEATERS > 0
// M130- heater P factor
case 130:
if (next_target.seen_S)
@ -362,7 +361,6 @@ void process_gcode_command() {
if (next_target.S)
power_on();
break;
#endif /* NUM_HEATERS > 0 */
// M190- power on
case 190:

View File

@ -9,9 +9,21 @@
#ifndef EXTRUDER
#include "sersendf.h"
#endif
#include "temp.h"
#define HEATER_C
#include "config.h"
typedef struct {
volatile uint8_t *heater_port;
uint8_t heater_pin;
volatile uint8_t *heater_pwm;
} heater_definition_t;
#undef DEFINE_HEATER
#define DEFINE_HEATER(name, port, pin, pwm) { &(port), (pin), &(pwm) },
static const heater_definition_t heaters[NUM_HEATERS] =
{
#include "config.h"
};
#undef DEFINE_HEATER
// this struct holds the heater PID factors that are stored in the EEPROM during poweroff
struct {
@ -50,8 +62,7 @@ typedef struct {
EE_factor EEMEM EE_factors[NUM_HEATERS];
void heater_init() {
#if NUM_HEATERS > 0
uint8_t i;
heater_t i;
// setup pins
for (i = 0; i < NUM_HEATERS; i++) {
*(heaters[i].heater_port) &= ~MASK(heaters[i].heater_pin);
@ -94,28 +105,27 @@ void heater_init() {
heaters_pid[i].i_limit = DEFAULT_I_LIMIT;
}
}
#endif /* NUM_HEATERS > 0 */
}
void heater_save_settings() {
#if NUM_HEATERS > 0
uint8_t i;
heater_t i;
for (i = 0; i < NUM_HEATERS; i++) {
eeprom_write_dword((uint32_t *) &EE_factors[i].EE_p_factor, heaters_pid[i].p_factor);
eeprom_write_dword((uint32_t *) &EE_factors[i].EE_i_factor, heaters_pid[i].i_factor);
eeprom_write_dword((uint32_t *) &EE_factors[i].EE_d_factor, heaters_pid[i].d_factor);
eeprom_write_word((uint16_t *) &EE_factors[i].EE_i_limit, heaters_pid[i].i_limit);
}
#endif /* NUM_HEATERS > 0 */
}
void heater_tick(uint8_t h, uint8_t t, uint16_t current_temp, uint16_t target_temp) {
#if (NUM_HEATERS > 0) && (NUM_TEMP_SENSORS > 0)
void heater_tick(heater_t h, temp_index_t t, uint16_t current_temp, uint16_t target_temp) {
int16_t heater_p;
int16_t heater_d;
uint8_t pid_output;
int16_t t_error = target_temp - current_temp;
if (h >= NUM_HEATERS || t >= NUM_TEMP_SENSORS)
return;
heaters_runtime[h].temp_history[heaters_runtime[h].temp_history_pointer++] = current_temp;
heaters_runtime[h].temp_history_pointer &= (TH_COUNT - 1);
@ -215,11 +225,12 @@ void heater_tick(uint8_t h, uint8_t t, uint16_t current_temp, uint16_t target_te
#endif /* HEATER_SANITY_CHECK */
heater_set(h, pid_output);
#endif /* if NUM_HEATERS > 0 && NUM_TEMP_SENSORS > 0 */
}
void heater_set(uint8_t index, uint8_t value) {
#if NUM_HEATERS > 0
void heater_set(heater_t index, uint8_t value) {
if (index >= NUM_HEATERS)
return;
if (heaters[index].heater_pwm) {
*(heaters[index].heater_pwm) = value;
#ifdef DEBUG
@ -233,21 +244,32 @@ void heater_set(uint8_t index, uint8_t value) {
else
*(heaters[index].heater_port) &= ~MASK(heaters[index].heater_pin);
}
#endif /* if NUM_HEATERS > 0 */
}
void pid_set_p(uint8_t index, int32_t p) {
void pid_set_p(heater_t index, int32_t p) {
if (index >= NUM_HEATERS)
return;
heaters_pid[index].p_factor = p;
}
void pid_set_i(uint8_t index, int32_t i) {
void pid_set_i(heater_t index, int32_t i) {
if (index >= NUM_HEATERS)
return;
heaters_pid[index].i_factor = i;
}
void pid_set_d(uint8_t index, int32_t d) {
void pid_set_d(heater_t index, int32_t d) {
if (index >= NUM_HEATERS)
return;
heaters_pid[index].d_factor = d;
}
void pid_set_i_limit(uint8_t index, int32_t i_limit) {
void pid_set_i_limit(heater_t index, int32_t i_limit) {
if (index >= NUM_HEATERS)
return;
heaters_pid[index].i_limit = i_limit;
}

View File

@ -1,20 +1,31 @@
#ifndef _HEATER_H
#define _HEATER_H
#include "config.h"
#include <stdint.h>
#include "temp.h"
#define enable_heater() heater_set(0, 64)
#define disable_heater() heater_set(0, 0)
#undef DEFINE_HEATER
#define DEFINE_HEATER(name, port, pin, pwm) HEATER_##name,
typedef enum
{
#include "config.h"
NUM_HEATERS
} heater_t;
#undef DEFINE_HEATER
void heater_init(void);
void heater_save_settings(void);
void heater_set(uint8_t index, uint8_t value);
void heater_tick(uint8_t h, uint8_t t, uint16_t current_temp, uint16_t target_temp);
void heater_set(heater_t index, uint8_t value);
void heater_tick(heater_t h, temp_sensor_t t, uint16_t current_temp, uint16_t target_temp);
void pid_set_p(uint8_t index, int32_t p);
void pid_set_i(uint8_t index, int32_t i);
void pid_set_d(uint8_t index, int32_t d);
void pid_set_i_limit(uint8_t index, int32_t i_limit);
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);
#endif /* _HEATER_H */

72
temp.c
View File

@ -4,6 +4,17 @@
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include "arduino.h"
#include "delay.h"
#include "debug.h"
#ifndef EXTRUDER
#include "sersendf.h"
#endif
#include "heater.h"
#ifdef GEN3
#include "intercom.h"
#endif
typedef enum {
TT_THERMISTOR,
TT_MAX6675,
@ -18,19 +29,19 @@ typedef enum {
TCOPEN
} temp_flags_enum;
#define TEMP_C
#include "config.h"
typedef struct {
uint8_t temp_type;
uint8_t temp_pin;
uint8_t heater_index;
} temp_sensor_definition_t;
#include "arduino.h"
#include "delay.h"
#include "debug.h"
#ifndef EXTRUDER
#include "sersendf.h"
#endif
#include "heater.h"
#ifdef GEN3
#include "intercom.h"
#endif
#undef DEFINE_TEMP_SENSOR
#define DEFINE_TEMP_SENSOR(name, type, pin, heater) { (type), (pin), (heater) },
static const temp_sensor_definition_t temp_sensors[NUM_TEMP_SENSORS] =
{
#include "config.h"
};
#undef DEFINE_TEMP_SENSOR
// this struct holds the runtime sensor data- read temperatures, targets, etc
struct {
@ -80,7 +91,7 @@ uint16_t temptable[NUMTEMPS][2] PROGMEM = {
#endif
void temp_init() {
uint8_t i;
temp_sensor_t i;
for (i = 0; i < NUM_TEMP_SENSORS; i++) {
switch(temp_sensors[i].temp_type) {
#ifdef TEMP_MAX6675
@ -112,7 +123,7 @@ void temp_init() {
}
void temp_sensor_tick() {
uint8_t i = 0;
temp_sensor_t i = 0;
for (; i < NUM_TEMP_SENSORS; i++) {
if (temp_sensors_runtime[i].next_read_time) {
temp_sensors_runtime[i].next_read_time--;
@ -257,7 +268,9 @@ void temp_sensor_tick() {
}
uint8_t temp_achieved() {
uint8_t i, all_ok = 255;
temp_sensor_t i;
uint8_t all_ok = 255;
for (i = 0; i < NUM_TEMP_SENSORS; i++) {
if (temp_sensors_runtime[i].temp_residency < TEMP_RESIDENCY_TIME)
all_ok = 0;
@ -265,7 +278,10 @@ uint8_t temp_achieved() {
return all_ok;
}
void temp_set(uint8_t index, uint16_t temperature) {
void temp_set(temp_sensor_t index, uint16_t temperature) {
if (index >= NUM_TEMP_SENSORS)
return;
temp_sensors_runtime[index].target_temp = temperature;
temp_sensors_runtime[index].temp_residency = 0;
#ifdef GEN3
@ -274,25 +290,31 @@ void temp_set(uint8_t index, uint16_t temperature) {
#endif
}
uint16_t temp_get(uint8_t index) {
uint16_t temp_get(temp_sensor_t index) {
if (index >= NUM_TEMP_SENSORS)
return 0;
return temp_sensors_runtime[index].last_read_temp;
}
// extruder doesn't have sersendf_P
#ifndef EXTRUDER
void temp_print(uint8_t index) {
void temp_print(temp_sensor_t index) {
uint8_t c = 0;
if (index >= NUM_TEMP_SENSORS)
return;
c = (temp_sensors_runtime[index].last_read_temp & 3) * 25;
#if BED_HEATER < NUM_HEATERS
uint8_t b = 0;
b = (temp_sensors_runtime[BED_HEATER].last_read_temp & 3) * 25;
if (BED_HEATER < NUM_HEATERS) {
uint8_t b = 0;
b = (temp_sensors_runtime[BED_HEATER].last_read_temp & 3) * 25;
sersendf_P(PSTR("T:%u.%u B:%u.%u\n"), temp_sensors_runtime[index].last_read_temp >> 2, c, temp_sensors_runtime[BED_HEATER].last_read_temp >> 2 , b);
#else
sersendf_P(PSTR("T:%u.%u B:%u.%u\n"), temp_sensors_runtime[index].last_read_temp >> 2, c, temp_sensors_runtime[BED_HEATER].last_read_temp >> 2 , b);
} else {
sersendf_P(PSTR("T:%u.%u"), temp_sensors_runtime[index].last_read_temp >> 2, c);
#endif
}
}
#endif

17
temp.h
View File

@ -1,6 +1,7 @@
#ifndef _TEMP_H
#define _TEMP_H
#include "config.h"
#include <stdint.h>
/*
@ -11,6 +12,14 @@ no point in specifying a port- all the different temp sensors we have must be on
we still need to specify which analog pins we use in machine.h for the analog sensors however, otherwise the analog subsystem won't read them.
*/
#undef DEFINE_TEMP_SENSOR
#define DEFINE_TEMP_SENSOR(name, type, pin, heater) TEMP_SENSOR_##name,
typedef enum {
#include "config.h"
NUM_TEMP_SENSORS
} temp_sensor_t;
#undef DEFINE_TEMP_SENSOR
#define temp_tick temp_sensor_tick
void temp_init(void);
@ -19,11 +28,9 @@ void temp_sensor_tick(void);
uint8_t temp_achieved(void);
void temp_set(uint8_t index, uint16_t temperature);
uint16_t temp_get(uint8_t index);
void temp_set(temp_sensor_t index, uint16_t temperature);
uint16_t temp_get(temp_sensor_t index);
void temp_print(uint8_t index);
uint16_t temp_read(uint8_t index);
void temp_print(temp_sensor_t index);
#endif /* _TIMER_H */