1 #include "CurrentControl.h"
2 #include "libs/Kernel.h"
3 #include "libs/nuts_bolts.h"
4 #include "libs/utils.h"
5 #include "ConfigValue.h"
6 #include "libs/StreamOutput.h"
10 #include "checksumm.h"
11 #include "DigipotBase.h"
13 // add new digipot chips here
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")
33 #define mcp4451_checksum CHECKSUM("mcp4451")
34 #define ad5206_checksum CHECKSUM("ad5206")
36 CurrentControl::CurrentControl()
41 void CurrentControl::on_module_loaded()
43 if( !THEKERNEL
->config
->value( currentcontrol_module_enable_checksum
)->by_default(false)->as_bool() ) {
44 // as this module is not needed free up the resource
49 // allocate digipot, if already allocated delete it first
52 // see which chip to use
53 int chip_checksum
= get_checksum(THEKERNEL
->config
->value(digipotchip_checksum
)->by_default("mcp4451")->as_string());
54 if(chip_checksum
== mcp4451_checksum
) {
55 digipot
= new MCP4451();
56 } else if(chip_checksum
== ad5206_checksum
) {
57 digipot
= new AD5206();
58 } else { // need a default so use smoothie
59 digipot
= new MCP4451();
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());
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());
76 this->register_for_event(ON_GCODE_RECEIVED
);
80 void CurrentControl::on_gcode_received(void *argument
)
82 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
83 char alpha
[8] = { 'X', 'Y', 'Z', 'E', 'A', 'B', 'C', 'D' };
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
]);
89 this->digipot
->set_current(i
, c
);
93 } else if(gcode
->m
== 500 || gcode
->m
== 503) {
94 gcode
->stream
->printf(";Motor currents:\nM907 ");
95 for (int i
= 0; i
< 8; i
++) {
96 float c
= this->digipot
->get_current(i
);
98 gcode
->stream
->printf("%c%1.5f ", alpha
[i
], c
);
100 gcode
->stream
->printf("\n");