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