summaryrefslogtreecommitdiff
path: root/controller.c
diff options
context:
space:
mode:
Diffstat (limited to 'controller.c')
-rw-r--r--controller.c47
1 files changed, 41 insertions, 6 deletions
diff --git a/controller.c b/controller.c
index bde2cac..6dee4da 100644
--- a/controller.c
+++ b/controller.c
@@ -1,9 +1,13 @@
#include <avr/io.h>
#include <avr/interrupt.h>
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
#include "controller.h"
#include "bridge.h"
+#include "serial.h"
static void adc_init(void)
{
@@ -40,20 +44,43 @@ void controller_init(void)
timer_init();
}
-int controller_set(long target)
+static long celsius_to_adc(double target_temp)
{
- if (target < 50 || target > 974)
+ long rv;
+
+ rv = 1203.59290 * exp(-0.0686201496 * target_temp) + 413.59437;
+ if (rv > 1017)
+ return 1017;
+ if (rv < 450)
+ return 450;
+ return rv;
+}
+
+static double adc_to_celsius(long adc)
+{
+ double rv;
+
+ rv = adc - 413.59437;
+ rv /= 1203.59290;
+ rv = log(rv);
+ rv /= -0.0686201496;
+ return rv;
+}
+
+int controller_set(double target)
+{
+ if (target < 8.0 || target > 42.0)
return 1;
- controller_target_temp = target;
+ controller_target_temp = celsius_to_adc(target);
controller_num_iterations = 1;
controller_active = 1;
return 0;
}
-long controller_get(void)
+double controller_get(void)
{
- return controller_measured_temp;
+ return adc_to_celsius(controller_measured_temp);
}
void controller_off(void)
@@ -82,8 +109,16 @@ ISR(ADC_vect)
return;
controller_num_iterations--;
- if (controller_num_iterations)
+ if (controller_num_iterations) {
+#if 0
+ char buffer[128];
+ snprintf(buffer, sizeof(buffer), "DEBUG adc==%ld adc_target=%ld\r\n",
+ (long int)controller_measured_temp,
+ controller_target_temp);
+ serial_send(buffer, strlen(buffer), 0);
+#endif
return;
+ }
controller_num_iterations = 60;