Allow TABS in config
[clinton/Smoothieware.git] / src / modules / utils / currentcontrol / CurrentControl.cpp
1 #include "libs/Kernel.h"
2 #include "CurrentControl.h"
3 #include "libs/nuts_bolts.h"
4 #include "libs/utils.h"
5
6 #include "Gcode.h"
7 #include "Config.h"
8
9 // add new digipot chips here
10 #include "mcp4451.h"
11 #include "ad5206.h"
12
13 #include <string>
14 using namespace std;
15
16 CurrentControl::CurrentControl(){
17 digipot= NULL;
18 }
19
20 void CurrentControl::on_module_loaded(){
21 if( !THEKERNEL->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ){
22 // as this module is not needed free up the resource
23 delete this;
24 return;
25 }
26
27 // allocate digipot, if already allocated delete it first
28 delete digipot;
29
30 // see which chip to use
31 int chip_checksum = get_checksum(THEKERNEL->config->value(digipotchip_checksum)->by_default("mcp4451")->as_string());
32 if(chip_checksum == mcp4451_checksum) {
33 digipot = new MCP4451();
34 }else if(chip_checksum == ad5206_checksum) {
35 digipot = new AD5206();
36 }else { // need a default so use smoothie
37 digipot = new MCP4451();
38 }
39
40 // Get configuration
41 this->alpha_current = THEKERNEL->config->value(alpha_current_checksum )->by_default(0.8f)->as_number();
42 this->beta_current = THEKERNEL->config->value(beta_current_checksum )->by_default(0.8f)->as_number();
43 this->gamma_current = THEKERNEL->config->value(gamma_current_checksum )->by_default(0.8f)->as_number();
44 this->delta_current = THEKERNEL->config->value(delta_current_checksum )->by_default(0.8f)->as_number();
45 this->epsilon_current = THEKERNEL->config->value(epsilon_current_checksum)->by_default(-1)->as_number();
46 this->zeta_current = THEKERNEL->config->value(zeta_current_checksum )->by_default(-1)->as_number();
47 this->eta_current = THEKERNEL->config->value(eta_current_checksum )->by_default(-1)->as_number();
48 this->theta_current = THEKERNEL->config->value(theta_current_checksum )->by_default(-1)->as_number();
49
50 digipot->set_max_current( THEKERNEL->config->value(digipot_max_current )->by_default(2.0f)->as_number());
51 digipot->set_factor( THEKERNEL->config->value(digipot_factor )->by_default(113.33f)->as_number());
52
53 this->digipot->set_current(0, this->alpha_current);
54 this->digipot->set_current(1, this->beta_current );
55 this->digipot->set_current(2, this->gamma_current);
56 this->digipot->set_current(3, this->delta_current);
57 if(this->epsilon_current >= 0){
58 this->digipot->set_current(4, this->epsilon_current);
59 this->digipot->set_current(5, this->zeta_current );
60 this->digipot->set_current(6, this->eta_current);
61 this->digipot->set_current(7, this->theta_current);
62 }
63
64 this->original_delta_current= this->delta_current; // remember this to determine if we want to save on M500
65
66 this->register_for_event(ON_GCODE_RECEIVED);
67 }
68
69
70 void CurrentControl::on_gcode_received(void *argument)
71 {
72 Gcode *gcode = static_cast<Gcode*>(argument);
73 char alpha[8] = { 'X', 'Y', 'Z', 'E', 'A', 'B', 'C', 'D' };
74 if (gcode->has_m)
75 {
76 if (gcode->m == 907)
77 {
78 int i;
79 for (i = 0; i < 8; i++)
80 {
81 if (gcode->has_letter(alpha[i])){
82 float c= gcode->get_value(alpha[i]);
83 this->digipot->set_current(i, c);
84 switch(i) {
85 case 0: this->alpha_current= c; break;
86 case 1: this->beta_current= c; break;
87 case 2: this->gamma_current= c; break;
88 case 3: this->delta_current= c; break;
89 case 4: this->epsilon_current= c; break;
90 case 5: this->zeta_current= c; break;
91 case 6: this->eta_current= c; break;
92 case 7: this->theta_current= c; break;
93 }
94 }
95 gcode->stream->printf("%c:%3.1fA%c", alpha[i], this->digipot->get_current(i), (i == 7)?'\n':' ');
96 }
97
98 }else if(gcode->m == 500 || gcode->m == 503) {
99 if(this->delta_current != this->original_delta_current) { // if not the same as loaded by config then save it
100 gcode->stream->printf(";Extruder current:\nM907 E%1.5f\n", this->delta_current);
101 }
102 }
103 }
104 }