Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CurrentControl.cpp Source File

CurrentControl.cpp

00001 #include "libs/Kernel.h"
00002 #include "CurrentControl.h"
00003 #include "libs/nuts_bolts.h"
00004 #include "libs/utils.h"
00005 
00006 #include "Gcode.h"
00007 
00008 // add new digipot chips here
00009 #include "mcp4451.h"
00010 #include "ad5206.h"
00011 
00012 #include <string>
00013 using namespace std;
00014 
00015 CurrentControl::CurrentControl(){
00016     digipot= NULL;
00017 }
00018 
00019 void CurrentControl::on_module_loaded(){
00020     if( !THEKERNEL->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ){
00021         // as this module is not needed free up the resource
00022         delete this;
00023         return;
00024     }
00025 
00026     // allocate digipot, if already allocated delete it first
00027     delete digipot;
00028 
00029     // see which chip to use
00030     int chip_checksum = get_checksum(THEKERNEL->config->value(digipotchip_checksum)->by_default("mcp4451")->as_string());
00031     if(chip_checksum == mcp4451_checksum) {
00032         digipot = new MCP4451();
00033     }else if(chip_checksum == ad5206_checksum) {
00034         digipot = new AD5206();
00035     }else { // need a default so use smoothie
00036         digipot = new MCP4451();
00037     }
00038 
00039     // Get configuration
00040     this->alpha_current =           THEKERNEL->config->value(alpha_current_checksum  )->by_default(0.8f)->as_number();
00041     this->beta_current  =           THEKERNEL->config->value(beta_current_checksum   )->by_default(0.8f)->as_number();
00042     this->gamma_current =           THEKERNEL->config->value(gamma_current_checksum  )->by_default(0.8f)->as_number();
00043     this->delta_current =           THEKERNEL->config->value(delta_current_checksum  )->by_default(0.8f)->as_number();
00044     this->epsilon_current =         THEKERNEL->config->value(epsilon_current_checksum)->by_default(-1)->as_number();
00045     this->zeta_current  =           THEKERNEL->config->value(zeta_current_checksum   )->by_default(-1)->as_number();
00046     this->eta_current =             THEKERNEL->config->value(eta_current_checksum    )->by_default(-1)->as_number();
00047     this->theta_current =           THEKERNEL->config->value(theta_current_checksum  )->by_default(-1)->as_number();
00048 
00049     digipot->set_max_current(       THEKERNEL->config->value(digipot_max_current     )->by_default(2.0f)->as_number());
00050     digipot->set_factor(            THEKERNEL->config->value(digipot_factor          )->by_default(113.33f)->as_number());
00051 
00052     this->digipot->set_current(0, this->alpha_current);
00053     this->digipot->set_current(1, this->beta_current );
00054     this->digipot->set_current(2, this->gamma_current);
00055     this->digipot->set_current(3, this->delta_current);
00056     if(this->epsilon_current >= 0){
00057         this->digipot->set_current(4, this->epsilon_current);
00058         this->digipot->set_current(5, this->zeta_current );
00059         this->digipot->set_current(6, this->eta_current);
00060         this->digipot->set_current(7, this->theta_current);
00061     }
00062 
00063     this->original_delta_current= this->delta_current; // remember this to determine if we want to save on M500
00064 
00065     this->register_for_event(ON_GCODE_RECEIVED);
00066 }
00067 
00068 
00069 void CurrentControl::on_gcode_received(void *argument)
00070 {
00071     Gcode *gcode = static_cast<Gcode*>(argument);
00072     char alpha[8] = { 'X', 'Y', 'Z', 'E', 'A', 'B', 'C', 'D' };
00073     if (gcode->has_m)
00074     {
00075         if (gcode->m == 907)
00076         {
00077             int i;
00078             for (i = 0; i < 8; i++)
00079             {
00080                 if (gcode->has_letter(alpha[i])){
00081                     float c= gcode->get_value(alpha[i]);
00082                     this->digipot->set_current(i, c);
00083                     switch(i) {
00084                         case 0: this->alpha_current= c; break;
00085                         case 1: this->beta_current= c; break;
00086                         case 2: this->gamma_current= c; break;
00087                         case 3: this->delta_current= c; break;
00088                         case 4: this->epsilon_current= c; break;
00089                         case 5: this->zeta_current= c; break;
00090                         case 6: this->eta_current= c; break;
00091                         case 7: this->theta_current= c; break;
00092                     }
00093                 }
00094                 gcode->stream->printf("%c:%3.1fA%c", alpha[i], this->digipot->get_current(i), (i == 7)?'\n':' ');
00095             }
00096 
00097         }else if(gcode->m == 500 || gcode->m == 503) {
00098             if(this->delta_current != this->original_delta_current) { // if not the same as loaded by config then save it
00099                 gcode->stream->printf(";Extruder current:\nM907 E%1.5f\n", this->delta_current);
00100             }
00101         }
00102     }
00103 }