refactor current control to save memory
authorJim Morris <morris@wolfman.com>
Thu, 24 Jul 2014 22:22:25 +0000 (15:22 -0700)
committerJim Morris <morris@wolfman.com>
Thu, 24 Jul 2014 22:22:25 +0000 (15:22 -0700)
M500 now saves all currents for all configured channels

src/modules/utils/currentcontrol/CurrentControl.cpp
src/modules/utils/currentcontrol/CurrentControl.h
src/modules/utils/currentcontrol/ad5206.h
src/modules/utils/currentcontrol/mcp4451.h

dissimilarity index 71%
index 34188ce..409632b 100644 (file)
-#include "libs/Kernel.h"
-#include "CurrentControl.h"
-#include "libs/nuts_bolts.h"
-#include "libs/utils.h"
-#include "ConfigValue.h"
-#include "libs/StreamOutput.h"
-
-#include "Gcode.h"
-#include "Config.h"
-#include "checksumm.h"
-
-// add new digipot chips here
-#include "mcp4451.h"
-#include "ad5206.h"
-
-#include <string>
-using namespace std;
-
-CurrentControl::CurrentControl(){
-    digipot= NULL;
-}
-
-void CurrentControl::on_module_loaded(){
-    if( !THEKERNEL->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ){
-        // as this module is not needed free up the resource
-        delete this;
-        return;
-    }
-
-    // allocate digipot, if already allocated delete it first
-    delete digipot;
-
-    // see which chip to use
-    int chip_checksum = get_checksum(THEKERNEL->config->value(digipotchip_checksum)->by_default("mcp4451")->as_string());
-    if(chip_checksum == mcp4451_checksum) {
-        digipot = new MCP4451();
-    }else if(chip_checksum == ad5206_checksum) {
-        digipot = new AD5206();
-    }else { // need a default so use smoothie
-        digipot = new MCP4451();
-    }
-
-    // Get configuration
-    this->alpha_current =           THEKERNEL->config->value(alpha_current_checksum  )->by_default(0.8f)->as_number();
-    this->beta_current  =           THEKERNEL->config->value(beta_current_checksum   )->by_default(0.8f)->as_number();
-    this->gamma_current =           THEKERNEL->config->value(gamma_current_checksum  )->by_default(0.8f)->as_number();
-    this->delta_current =           THEKERNEL->config->value(delta_current_checksum  )->by_default(0.8f)->as_number();
-    this->epsilon_current =         THEKERNEL->config->value(epsilon_current_checksum)->by_default(-1)->as_number();
-    this->zeta_current  =           THEKERNEL->config->value(zeta_current_checksum   )->by_default(-1)->as_number();
-    this->eta_current =             THEKERNEL->config->value(eta_current_checksum    )->by_default(-1)->as_number();
-    this->theta_current =           THEKERNEL->config->value(theta_current_checksum  )->by_default(-1)->as_number();
-
-    digipot->set_max_current(       THEKERNEL->config->value(digipot_max_current     )->by_default(2.0f)->as_number());
-    digipot->set_factor(            THEKERNEL->config->value(digipot_factor          )->by_default(113.33f)->as_number());
-
-    this->digipot->set_current(0, this->alpha_current);
-    this->digipot->set_current(1, this->beta_current );
-    this->digipot->set_current(2, this->gamma_current);
-    this->digipot->set_current(3, this->delta_current);
-    if(this->epsilon_current >= 0){
-        this->digipot->set_current(4, this->epsilon_current);
-        this->digipot->set_current(5, this->zeta_current );
-        this->digipot->set_current(6, this->eta_current);
-        this->digipot->set_current(7, this->theta_current);
-    }
-
-    this->original_delta_current= this->delta_current; // remember this to determine if we want to save on M500
-
-    this->register_for_event(ON_GCODE_RECEIVED);
-}
-
-
-void CurrentControl::on_gcode_received(void *argument)
-{
-    Gcode *gcode = static_cast<Gcode*>(argument);
-    char alpha[8] = { 'X', 'Y', 'Z', 'E', 'A', 'B', 'C', 'D' };
-    if (gcode->has_m)
-    {
-        if (gcode->m == 907)
-        {
-            int i;
-            for (i = 0; i < 8; i++)
-            {
-                if (gcode->has_letter(alpha[i])){
-                    float c= gcode->get_value(alpha[i]);
-                    this->digipot->set_current(i, c);
-                    switch(i) {
-                        case 0: this->alpha_current= c; break;
-                        case 1: this->beta_current= c; break;
-                        case 2: this->gamma_current= c; break;
-                        case 3: this->delta_current= c; break;
-                        case 4: this->epsilon_current= c; break;
-                        case 5: this->zeta_current= c; break;
-                        case 6: this->eta_current= c; break;
-                        case 7: this->theta_current= c; break;
-                    }
-                }
-                gcode->stream->printf("%c:%3.1fA%c", alpha[i], this->digipot->get_current(i), (i == 7)?'\n':' ');
-            }
-
-        }else if(gcode->m == 500 || gcode->m == 503) {
-            if(this->delta_current != this->original_delta_current) { // if not the same as loaded by config then save it
-                gcode->stream->printf(";Extruder current:\nM907 E%1.5f\n", this->delta_current);
-            }
-        }
-    }
-}
+#include "libs/Kernel.h"
+#include "CurrentControl.h"
+#include "libs/nuts_bolts.h"
+#include "libs/utils.h"
+#include "ConfigValue.h"
+#include "libs/StreamOutput.h"
+
+#include "Gcode.h"
+#include "Config.h"
+#include "checksumm.h"
+
+// add new digipot chips here
+#include "mcp4451.h"
+#include "ad5206.h"
+
+#include <string>
+using namespace std;
+
+CurrentControl::CurrentControl()
+{
+    digipot = NULL;
+}
+
+void CurrentControl::on_module_loaded()
+{
+    if( !THEKERNEL->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ) {
+        // as this module is not needed free up the resource
+        delete this;
+        return;
+    }
+
+    // allocate digipot, if already allocated delete it first
+    delete digipot;
+
+    // see which chip to use
+    int chip_checksum = get_checksum(THEKERNEL->config->value(digipotchip_checksum)->by_default("mcp4451")->as_string());
+    if(chip_checksum == mcp4451_checksum) {
+        digipot = new MCP4451();
+    } else if(chip_checksum == ad5206_checksum) {
+        digipot = new AD5206();
+    } else { // need a default so use smoothie
+        digipot = new MCP4451();
+    }
+
+    // Get configuration
+    this->digipot->set_current(0, THEKERNEL->config->value(alpha_current_checksum  )->by_default(0.8f)->as_number());
+    this->digipot->set_current(1, THEKERNEL->config->value(beta_current_checksum   )->by_default(0.8f)->as_number());
+    this->digipot->set_current(2, THEKERNEL->config->value(gamma_current_checksum  )->by_default(0.8f)->as_number());
+    this->digipot->set_current(3, THEKERNEL->config->value(delta_current_checksum  )->by_default(0.8f)->as_number());
+    this->digipot->set_current(4, THEKERNEL->config->value(epsilon_current_checksum)->by_default(-1)->as_number());
+    this->digipot->set_current(5, THEKERNEL->config->value(zeta_current_checksum   )->by_default(-1)->as_number());
+    this->digipot->set_current(6, THEKERNEL->config->value(eta_current_checksum    )->by_default(-1)->as_number());
+    this->digipot->set_current(7, THEKERNEL->config->value(theta_current_checksum  )->by_default(-1)->as_number());
+
+    digipot->set_max_current( THEKERNEL->config->value(digipot_max_current )->by_default(2.0f)->as_number());
+    digipot->set_factor( THEKERNEL->config->value(digipot_factor )->by_default(113.33f)->as_number());
+
+    this->register_for_event(ON_GCODE_RECEIVED);
+}
+
+
+void CurrentControl::on_gcode_received(void *argument)
+{
+    Gcode *gcode = static_cast<Gcode*>(argument);
+    char alpha[8] = { 'X', 'Y', 'Z', 'E', 'A', 'B', 'C', 'D' };
+    if (gcode->has_m) {
+        if (gcode->m == 907) {
+            for (int i = 0; i < 8; i++) {
+                if (gcode->has_letter(alpha[i])) {
+                    float c = gcode->get_value(alpha[i]);
+                    this->digipot->set_current(i, c);
+                }
+            }
+
+        } else if(gcode->m == 500 || gcode->m == 503) {
+            gcode->stream->printf(";Motor currents:\nM907 ");
+            for (int i = 0; i < 8; i++) {
+                float c = this->digipot->get_current(i);
+                if(c >= 0)
+                    gcode->stream->printf("%c%1.5f ", alpha[i], c);
+            }
+            gcode->stream->printf("\n");
+        }
+    }
+}
index 578897d..2b98121 100644 (file)
@@ -32,16 +32,6 @@ class CurrentControl : public Module {
         void on_gcode_received(void *);
 
     private:
-        float alpha_current;
-        float beta_current;
-        float gamma_current;
-        float delta_current;
-        float epsilon_current;
-        float zeta_current;
-        float eta_current;
-        float theta_current;
-        float original_delta_current;
-
         DigipotBase* digipot;
 
 };
index bf9d7d9..ec9cd30 100644 (file)
@@ -16,11 +16,16 @@ class AD5206 : public DigipotBase {
             this->spi= new mbed::SPI(P0_9,P0_8,P0_7); //should be able to set those pins in config
             cs.from_string("4.29")->as_output(); //this also should be configurable
             cs.set(1);
+            for (int i = 0; i < 6; i++) currents[i] = -1;
         }
 
         void set_current( int channel, float current )
         {
                        if(channel<6){
+                if(current < 0) {
+                    currents[channel]= -1;
+                    return;
+                }
                                current = min( max( current, 0.0L ), 2.0L );
                                char adresses[6] = { 0x05, 0x03, 0x01, 0x00, 0x02, 0x04 };
                                currents[channel] = current;
@@ -41,7 +46,9 @@ class AD5206 : public DigipotBase {
 
         float get_current(int channel)
         {
-            return currents[channel];
+            if(channel < 6)
+                return currents[channel];
+            return -1;
         }
 
     private:
index cdd8de2..c9471a7 100644 (file)
@@ -14,8 +14,7 @@ class MCP4451 : public DigipotBase {
             // I2C com
             this->i2c = new mbed::I2C(p9, p10);
             this->i2c->frequency(20000);
-            for (int i = 0; i < 8; i++)
-                currents[i] = 0.0;
+            for (int i = 0; i < 8; i++) currents[i] = -1;
         }
 
         ~MCP4451(){
@@ -24,6 +23,10 @@ class MCP4451 : public DigipotBase {
 
         void set_current( int channel, float current )
         {
+            if(current < 0) {
+                currents[channel]= -1;
+                return;
+            }
             current = min( (float) max( current, 0.0f ), this->max_current );
             currents[channel] = current;
             char addr = 0x58;