Commit | Line | Data |
---|---|---|
0e8b102e | 1 | #include "CurrentControl.h" |
b0fc8a53 | 2 | #include "libs/Kernel.h" |
0e8b102e AW |
3 | #include "libs/nuts_bolts.h" |
4 | #include "libs/utils.h" | |
8d54c34c | 5 | #include "ConfigValue.h" |
fc7b9a7b | 6 | #include "libs/StreamOutput.h" |
13a520c1 MM |
7 | |
8 | #include "Gcode.h" | |
61134a65 | 9 | #include "Config.h" |
7af0714f | 10 | #include "checksumm.h" |
b0fc8a53 | 11 | #include "DigipotBase.h" |
13a520c1 | 12 | |
6de40109 JM |
13 | // add new digipot chips here |
14 | #include "mcp4451.h" | |
15 | #include "ad5206.h" | |
16 | ||
0e8b102e AW |
17 | #include <string> |
18 | using namespace std; | |
19 | ||
b0fc8a53 JM |
20 | #define alpha_current_checksum CHECKSUM("alpha_current") |
21 | #define beta_current_checksum CHECKSUM("beta_current") | |
22 | #define gamma_current_checksum CHECKSUM("gamma_current") | |
23 | #define delta_current_checksum CHECKSUM("delta_current") | |
24 | #define epsilon_current_checksum CHECKSUM("epsilon_current") | |
25 | #define zeta_current_checksum CHECKSUM("zeta_current") | |
26 | #define eta_current_checksum CHECKSUM("eta_current") | |
27 | #define theta_current_checksum CHECKSUM("theta_current") | |
28 | #define currentcontrol_module_enable_checksum CHECKSUM("currentcontrol_module_enable") | |
29 | #define digipotchip_checksum CHECKSUM("digipotchip") | |
30 | #define digipot_max_current CHECKSUM("digipot_max_current") | |
31 | #define digipot_factor CHECKSUM("digipot_factor") | |
32 | ||
33 | #define mcp4451_checksum CHECKSUM("mcp4451") | |
34 | #define ad5206_checksum CHECKSUM("ad5206") | |
35 | ||
2b08b195 JM |
36 | CurrentControl::CurrentControl() |
37 | { | |
38 | digipot = NULL; | |
f86783ab | 39 | } |
0e8b102e | 40 | |
2b08b195 JM |
41 | void CurrentControl::on_module_loaded() |
42 | { | |
43 | if( !THEKERNEL->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ) { | |
f1fb4fa4 JM |
44 | // as this module is not needed free up the resource |
45 | delete this; | |
46 | return; | |
47 | } | |
48 | ||
49 | // allocate digipot, if already allocated delete it first | |
50 | delete digipot; | |
51 | ||
52 | // see which chip to use | |
314ab8f7 | 53 | int chip_checksum = get_checksum(THEKERNEL->config->value(digipotchip_checksum)->by_default("mcp4451")->as_string()); |
f1fb4fa4 JM |
54 | if(chip_checksum == mcp4451_checksum) { |
55 | digipot = new MCP4451(); | |
2b08b195 | 56 | } else if(chip_checksum == ad5206_checksum) { |
f1fb4fa4 | 57 | digipot = new AD5206(); |
2b08b195 | 58 | } else { // need a default so use smoothie |
f1fb4fa4 JM |
59 | digipot = new MCP4451(); |
60 | } | |
0e8b102e | 61 | |
72fe24ef JM |
62 | digipot->set_max_current( THEKERNEL->config->value(digipot_max_current )->by_default(2.0f)->as_number()); |
63 | digipot->set_factor( THEKERNEL->config->value(digipot_factor )->by_default(113.33f)->as_number()); | |
64 | ||
0e8b102e | 65 | // Get configuration |
2b08b195 JM |
66 | this->digipot->set_current(0, THEKERNEL->config->value(alpha_current_checksum )->by_default(0.8f)->as_number()); |
67 | this->digipot->set_current(1, THEKERNEL->config->value(beta_current_checksum )->by_default(0.8f)->as_number()); | |
68 | this->digipot->set_current(2, THEKERNEL->config->value(gamma_current_checksum )->by_default(0.8f)->as_number()); | |
69 | this->digipot->set_current(3, THEKERNEL->config->value(delta_current_checksum )->by_default(0.8f)->as_number()); | |
70 | this->digipot->set_current(4, THEKERNEL->config->value(epsilon_current_checksum)->by_default(-1)->as_number()); | |
71 | this->digipot->set_current(5, THEKERNEL->config->value(zeta_current_checksum )->by_default(-1)->as_number()); | |
72 | this->digipot->set_current(6, THEKERNEL->config->value(eta_current_checksum )->by_default(-1)->as_number()); | |
73 | this->digipot->set_current(7, THEKERNEL->config->value(theta_current_checksum )->by_default(-1)->as_number()); | |
0e8b102e | 74 | |
d67c316f | 75 | |
13a520c1 | 76 | this->register_for_event(ON_GCODE_RECEIVED); |
0e8b102e AW |
77 | } |
78 | ||
79 | ||
13a520c1 MM |
80 | void CurrentControl::on_gcode_received(void *argument) |
81 | { | |
82 | Gcode *gcode = static_cast<Gcode*>(argument); | |
5110b07d | 83 | char alpha[8] = { 'X', 'Y', 'Z', 'E', 'A', 'B', 'C', 'D' }; |
2b08b195 JM |
84 | if (gcode->has_m) { |
85 | if (gcode->m == 907) { | |
86 | for (int i = 0; i < 8; i++) { | |
87 | if (gcode->has_letter(alpha[i])) { | |
88 | float c = gcode->get_value(alpha[i]); | |
d67c316f | 89 | this->digipot->set_current(i, c); |
d67c316f | 90 | } |
13a520c1 | 91 | } |
d67c316f | 92 | |
2b08b195 | 93 | } else if(gcode->m == 500 || gcode->m == 503) { |
5b0cf319 JM |
94 | float currents[8]; |
95 | bool has_setting= false; | |
2b08b195 | 96 | for (int i = 0; i < 8; i++) { |
5b0cf319 JM |
97 | currents[i]= this->digipot->get_current(i); |
98 | if(currents[i] >= 0) has_setting= true; | |
99 | } | |
100 | if(!has_setting) return; // don't oupuit anything if none are set using this current control | |
101 | ||
102 | gcode->stream->printf(";Digipot Motor currents:\nM907 "); | |
103 | for (int i = 0; i < 8; i++) { | |
104 | if(currents[i] >= 0) | |
105 | gcode->stream->printf("%c%1.5f ", alpha[i], currents[i]); | |
d67c316f | 106 | } |
2b08b195 | 107 | gcode->stream->printf("\n"); |
13a520c1 MM |
108 | } |
109 | } | |
110 | } |