1 #include "libs/Kernel.h"
2 #include "CurrentControl.h"
3 #include "libs/nuts_bolts.h"
4 #include "libs/utils.h"
8 // add new digipot chips here
15 CurrentControl::CurrentControl(){
19 void CurrentControl::on_module_loaded(){
20 if( !THEKERNEL
->config
->value( currentcontrol_module_enable_checksum
)->by_default(false)->as_bool() ){
21 // as this module is not needed free up the resource
26 // allocate digipot, if already allocated delete it first
29 // see which chip to use
30 int chip_checksum
= get_checksum(THEKERNEL
->config
->value(digipotchip_checksum
)->by_default("mcp4451")->as_string());
31 if(chip_checksum
== mcp4451_checksum
) {
32 digipot
= new MCP4451();
33 }else if(chip_checksum
== ad5206_checksum
) {
34 digipot
= new AD5206();
35 }else { // need a default so use smoothie
36 digipot
= new MCP4451();
40 this->alpha_current
= THEKERNEL
->config
->value(alpha_current_checksum
)->by_default(0.8)->as_number();
41 this->beta_current
= THEKERNEL
->config
->value(beta_current_checksum
)->by_default(0.8)->as_number();
42 this->gamma_current
= THEKERNEL
->config
->value(gamma_current_checksum
)->by_default(0.8)->as_number();
43 this->delta_current
= THEKERNEL
->config
->value(delta_current_checksum
)->by_default(0.8)->as_number();
44 this->epsilon_current
= THEKERNEL
->config
->value(epsilon_current_checksum
)->by_default(-1)->as_number();
45 this->zeta_current
= THEKERNEL
->config
->value(zeta_current_checksum
)->by_default(-1)->as_number();
46 this->eta_current
= THEKERNEL
->config
->value(eta_current_checksum
)->by_default(-1)->as_number();
47 this->theta_current
= THEKERNEL
->config
->value(theta_current_checksum
)->by_default(-1)->as_number();
49 digipot
->set_max_current( THEKERNEL
->config
->value(digipot_max_current
)->by_default(2.0)->as_number());
50 digipot
->set_factor( THEKERNEL
->config
->value(digipot_factor
)->by_default(113.33)->as_number());
52 this->digipot
->set_current(0, this->alpha_current
);
53 this->digipot
->set_current(1, this->beta_current
);
54 this->digipot
->set_current(2, this->gamma_current
);
55 this->digipot
->set_current(3, this->delta_current
);
56 if(this->epsilon_current
>= 0){
57 this->digipot
->set_current(4, this->epsilon_current
);
58 this->digipot
->set_current(5, this->zeta_current
);
59 this->digipot
->set_current(6, this->eta_current
);
60 this->digipot
->set_current(7, this->theta_current
);
63 this->register_for_event(ON_GCODE_RECEIVED
);
67 void CurrentControl::on_gcode_received(void *argument
)
69 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
70 char alpha
[8] = { 'X', 'Y', 'Z', 'E', 'A', 'B', 'C', 'D' };
76 for (i
= 0; i
< 8; i
++)
78 if (gcode
->has_letter(alpha
[i
]))
79 this->digipot
->set_current(i
, gcode
->get_value(alpha
[i
]));
80 gcode
->stream
->printf("%c:%3.1fA%c", alpha
[i
], this->digipot
->get_current(i
), (i
== 7)?'\n':' ');