Commit f57913cc58292ec2e5e6e3f4477d95650fa7e3cf
authorMichel Pollet <buserror@gmail.com>
Sun, 3 Jun 2012 14:07:56 +0000 (15:07 +0100)
committerMichel Pollet <buserror@gmail.com>
Sun, 3 Jun 2012 14:07:56 +0000 (15:07 +0100)
Somehow raise a temperature using a score, reduces it automatically etc

Signed-off-by: Michel Pollet <buserror@gmail.com>
2 files changed:
examples/board_reprap/src/heatpot.c
examples/board_reprap/src/reprap.c

index dddf7de9a1d174b6f454c914d3523126b4623157..a866a908658f1c446ec1c141c5f78f08dfcf22f7 100644 (file)
@@ -20,7 +20,9 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <math.h>
 #include "sim_avr.h"
 #include "sim_time.h"
 
@@ -33,6 +35,29 @@ heatpot_evaluate_timer(
         void * param)
 {
        heatpot_p  p = (heatpot_p) param;
+
+       float weight = 0.0;
+
+       for (int si = 0; si < 32; si++)
+               if (p->tally[si].sid)
+                       weight += p->tally[si].cost;
+
+       float delta = p->current - p->ambiant;
+       float noise = (float)((random() % 32) - 16) / 32.0;
+       delta += noise;
+
+       float cost = ((delta/2) + noise) * -weight;
+
+       if (delta < 0) {
+               cost = (-delta / 10);
+       } else {
+               cost = weight > 0 ? 0.1 : -0.1;
+       }
+       p->current += cost;
+//     printf("%s w=%.3f d=%.3f c=%.3f = %.3f\n", p->name, weight, delta, cost, p->current);
+
+       avr_raise_irq(p->irq + IRQ_HEATPOT_TEMP_OUT, p->current * 256);
+
        return when + p->cycle;
 }
 
@@ -59,14 +84,16 @@ heatpot_init(
                const char * name,
                float ambiant )
 {
+       memset(p, 0, sizeof(*p));
        p->avr = avr;
        strcpy(p->name, (char*)name);
        p->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_HEATPOT_COUNT, irq_names);
        avr_irq_register_notify(p->irq + IRQ_HEATPOT_TALLY, heatpot_tally_in_hook, p);
 
-       p->cycle = avr_usec_to_cycles(avr, 100000 / 1000);
+       p->cycle = avr_usec_to_cycles(avr, 100000 / 1 /*1000 */);
        avr_cycle_timer_register_usec(avr, p->cycle, heatpot_evaluate_timer, p);
 
+       p->ambiant = p->current = ambiant;
 }
 
 void
@@ -78,7 +105,7 @@ heatpot_tally(
        int f = -1, ei = -1;
        for (int si = 0; si < 32 && f == -1; si++)
                if (p->tally[si].sid == 0)
-                       ei = si;
+                       ei = ei == -1 ? si : ei;
                else if (p->tally[si].sid == sid)
                        f = si;
        if (f == -1) {
index fad480710a566ee8b101f30ea4323a3907afff34..92d67b6ca8cce3f823ffb57b2aa1dcca0ccf76c0 100644 (file)
@@ -50,7 +50,8 @@
  * these are the sources of heat and cold to register to the heatpots
  */
 enum {
-       TALLY_HOTEND_PWM        = 1,
+       TALLY_AMBIANT = 1,
+       TALLY_HOTEND_PWM,
        TALLY_HOTBED,
        TALLY_HOTEND_FAN,
 };
@@ -130,14 +131,44 @@ get_ardu_irq(
  * called when the AVR change any of the pins on port B
  * so lets update our buffer
  */
-void
+static void
 hotbed_change_hook(
                struct avr_irq_t * irq,
                uint32_t value,
                void * param)
+{
+//     printf("%s %d\n", __func__, value);
+//     pin_state = (pin_state & ~(1 << irq->irq)) | (value << irq->irq);
+       heatpot_tally(
+                       &reprap.hotbed,
+                       TALLY_HOTEND_PWM,
+                       value ? 1.0f : 0 );
+}
+static void
+hotend_change_hook(
+               struct avr_irq_t * irq,
+               uint32_t value,
+               void * param)
+{
+//     printf("%s %d\n", __func__, value);
+//     pin_state = (pin_state & ~(1 << irq->irq)) | (value << irq->irq);
+       heatpot_tally(
+                       &reprap.hotend,
+                       TALLY_HOTBED,
+                       value ? 1.0f : 0 );
+}
+static void
+hotend_fan_change_hook(
+               struct avr_irq_t * irq,
+               uint32_t value,
+               void * param)
 {
        printf("%s %d\n", __func__, value);
 //     pin_state = (pin_state & ~(1 << irq->irq)) | (value << irq->irq);
+       heatpot_tally(
+                       &reprap.hotend,
+                       TALLY_HOTEND_FAN,
+                       value ? -0.05 : 0 );
 }
 
 
@@ -229,12 +260,26 @@ reprap_init(
        heatpot_init(avr, &r->hotend, "hotend", 28.0f);
        heatpot_init(avr, &r->hotbed, "hotbed", 25.0f);
 
+       heatpot_tally(&r->hotend, TALLY_AMBIANT, -0.5f);
+       heatpot_tally(&r->hotbed, TALLY_AMBIANT, -0.3f);
+
        /* connect heatpot temp output to thermistors */
        avr_connect_irq(r->hotend.irq + IRQ_HEATPOT_TEMP_OUT,
                        r->therm_hotend.irq + IRQ_TERM_TEMP_VALUE_IN);
        avr_connect_irq(r->hotbed.irq + IRQ_HEATPOT_TEMP_OUT,
                        r->therm_hotbed.irq + IRQ_TERM_TEMP_VALUE_IN);
 
+       avr_irq_register_notify(
+                       get_ardu_irq(avr, HEATER_0_PIN, arduidiot_644),
+                       hotend_change_hook, NULL);
+       avr_irq_register_notify(
+                       get_ardu_irq(avr, FAN_PIN, arduidiot_644),
+                       hotend_fan_change_hook, NULL);
+       avr_irq_register_notify(
+                       get_ardu_irq(avr, HEATER_BED_PIN, arduidiot_644),
+                       hotbed_change_hook, NULL);
+
+       //avr_irq_register_notify()
        float axis_pp_per_mm[4] = DEFAULT_AXIS_STEPS_PER_UNIT;  // from Marlin!
        {
                avr_irq_t * e = get_ardu_irq(avr, X_ENABLE_PIN, arduidiot_644);