chiark / gitweb /
Queued fan control. Issue #90
authorErik van der Zalm <erik@vdzalm.eu>
Sun, 4 Mar 2012 12:05:26 +0000 (13:05 +0100)
committerErik van der Zalm <erik@vdzalm.eu>
Sun, 4 Mar 2012 12:05:26 +0000 (13:05 +0100)
Marlin/Marlin.h
Marlin/Marlin.pde
Marlin/planner.cpp
Marlin/planner.h
Marlin/stepper.cpp
Marlin/ultralcd.pde

index e01d98ad500bae99d943108c91fc208f8ad99ddd..919287260b2c3822f8ac8c87c4c96d792fcb021a 100644 (file)
@@ -175,6 +175,7 @@ extern float homing_feedrate[];
 extern bool axis_relative_modes[];
 extern float current_position[NUM_AXIS] ;
 extern float add_homeing[3];
+extern unsigned char FanSpeed;
 
 // Handling multiple extruders pins
 extern uint8_t active_extruder;
index 4b9d06c6ad023a5071cf0ee0e7f58af849fbcc0e..e28acdec97230e6a6b168c6a356caba12eed970f 100644 (file)
@@ -131,6 +131,7 @@ volatile int extrudemultiply=100; //100->1 200->2
 float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
 float add_homeing[3]={0,0,0};
 uint8_t active_extruder = 0;
+unsigned char FanSpeed=0;
 
 //===========================================================================
 //=============================private variables=============================
@@ -145,8 +146,6 @@ static long gcode_N, gcode_LastN;
 static bool relative_mode = false;  //Determines Absolute or Relative Coordinates
 static bool relative_mode_e = false;  //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
 
-static uint8_t fanpwm=0;
-
 static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
 static bool fromsd[BUFSIZE];
 static int bufindr = 0;
@@ -938,19 +937,14 @@ void process_commands()
     #if FAN_PIN > -1
       case 106: //M106 Fan On
         if (code_seen('S')){
-            WRITE(FAN_PIN,HIGH);
-            fanpwm=constrain(code_value(),0,255);
-            analogWrite(FAN_PIN,  fanpwm);
+           FanSpeed=constrain(code_value(),0,255);
         }
         else {
-          WRITE(FAN_PIN,HIGH);
-          fanpwm=255;
-          analogWrite(FAN_PIN, fanpwm);                        
+          FanSpeed=255;                        
         }
         break;
       case 107: //M107 Fan Off
-        WRITE(FAN_PIN,LOW);
-        analogWrite(FAN_PIN, 0);
+        FanSpeed = 0;
         break;
     #endif //FAN_PIN
 
index 7b85f81459bc7f27140f873c4973284f60f493d9..5972dafea571f48c41be85c2cfcdbf57785ce7fb 100644 (file)
@@ -418,23 +418,34 @@ void check_axes_activity() {
   unsigned char y_active = 0;  
   unsigned char z_active = 0;
   unsigned char e_active = 0;
+  unsigned char fan_speed = 0;
+  unsigned char tail_fan_speed = 0;
   block_t *block;
 
   if(block_buffer_tail != block_buffer_head) {
     uint8_t block_index = block_buffer_tail;
+    tail_fan_speed = block_buffer[block_index].fan_speed;
     while(block_index != block_buffer_head) {
       block = &block_buffer[block_index];
       if(block->steps_x != 0) x_active++;
       if(block->steps_y != 0) y_active++;
       if(block->steps_z != 0) z_active++;
       if(block->steps_e != 0) e_active++;
+      if(block->fan_speed != 0) fan_speed++;
       block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
     }
   }
+  else {
+    if (FanSpeed != 0) analogWrite(FAN_PIN,FanSpeed); // If buffer is empty use current fan speed
+  }
   if((DISABLE_X) && (x_active == 0)) disable_x();
   if((DISABLE_Y) && (y_active == 0)) disable_y();
   if((DISABLE_Z) && (z_active == 0)) disable_z();
   if((DISABLE_E) && (e_active == 0)) { disable_e0();disable_e1();disable_e2(); }
+  if((FanSpeed == 0) && (fan_speed ==0)) analogWrite(FAN_PIN, 0);
+  if (FanSpeed != 0 && tail_fan_speed !=0) { 
+    analogWrite(FAN_PIN,tail_fan_speed);
+  }
 }
 
 
@@ -498,6 +509,8 @@ void plan_buffer_line(const float &x, const float &y, const float &z, const floa
   // Bail if this is a zero-length block
   if (block->step_event_count <=dropsegments) { return; };
 
+  block->fan_speed = FanSpeed;
+  
   // Compute direction bits for this block 
   block->direction_bits = 0;
   if (target[X_AXIS] < position[X_AXIS]) { block->direction_bits |= (1<<X_AXIS); }
index ac9719fbcf5ab9fade5c94f4f6695afcdf6263b9..8fa4d4ec3404d43980a3d9e67d7efd63e3bd92e3 100644 (file)
@@ -59,6 +59,7 @@ typedef struct {
   unsigned long initial_rate;                        // The jerk-adjusted step rate at start of block  
   unsigned long final_rate;                          // The minimal rate at exit
   unsigned long acceleration_st;                     // acceleration steps/sec^2
+  unsigned long fan_speed;
   volatile char busy;
 } block_t;
 
index 5ad2b1ee51c64b8950cf5b234b1f9cc6b4db5291..55c09902bc3a96ea56f839e6f96868c0377f1a5e 100644 (file)
@@ -21,9 +21,6 @@
 /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
    and Philipp Tiefenbacher. */
 
-
-
-
 #include "Marlin.h"
 #include "stepper.h"
 #include "planner.h"
@@ -303,7 +300,8 @@ ISR(TIMER1_COMPA_vect)
       counter_y = counter_x;
       counter_z = counter_x;
       counter_e = counter_x;
-      step_events_completed = 0;
+      step_events_completed = 0; 
+      
       #ifdef Z_LATE_ENABLE 
         if(current_block->steps_z > 0) {
           enable_z();
index 27b867497261b8b225f361dd33f301ff042363c2..afb6908a1655cd4de109a9068c2c3b257fda5e98 100644 (file)
@@ -812,7 +812,7 @@ void MainMenu::showTune()
         if(force_lcd_update)
         {
           lcd.setCursor(0,line);lcdprintPGM(MSG_FAN_SPEED);
-          lcd.setCursor(13,line);lcd.print(ftostr3(fanpwm));
+          lcd.setCursor(13,line);lcd.print(ftostr3(FanSpeed));
         }
         
         if((activeline!=line) )
@@ -823,7 +823,7 @@ void MainMenu::showTune()
           linechanging=!linechanging;
           if(linechanging)
           {
-              encoderpos=fanpwm;
+              encoderpos=FanSpeed;
           }
           else
           {
@@ -836,8 +836,8 @@ void MainMenu::showTune()
         {
           if(encoderpos<0) encoderpos=0;
           if(encoderpos>255) encoderpos=255;
-          fanpwm=encoderpos;
-            analogWrite(FAN_PIN,  fanpwm);
+          FanSpeed=encoderpos;
+            analogWrite(FAN_PIN,  FanSpeed);
           lcd.setCursor(13,line);lcd.print(itostr3(encoderpos));
         }
         
@@ -1134,7 +1134,7 @@ void MainMenu::showControlTemp()
         if(force_lcd_update)
         {
           lcd.setCursor(0,line);lcdprintPGM(MSG_FAN_SPEED);
-          lcd.setCursor(13,line);lcd.print(ftostr3(fanpwm));
+          lcd.setCursor(13,line);lcd.print(ftostr3(FanSpeed));
         }
         
         if((activeline!=line) )
@@ -1145,7 +1145,7 @@ void MainMenu::showControlTemp()
           linechanging=!linechanging;
           if(linechanging)
           {
-              encoderpos=fanpwm;
+              encoderpos=FanSpeed;
           }
           else
           {
@@ -1158,8 +1158,8 @@ void MainMenu::showControlTemp()
         {
           if(encoderpos<0) encoderpos=0;
           if(encoderpos>255) encoderpos=255;
-          fanpwm=encoderpos;
-            analogWrite(FAN_PIN,  fanpwm);
+          FanSpeed=encoderpos;
+            analogWrite(FAN_PIN,  FanSpeed);
           lcd.setCursor(13,line);lcd.print(itostr3(encoderpos));
         }