// Get config void Switch::on_config_reload(void* argument){ this->on_m_code = this->kernel->config->value(switch_checksum, this->name_checksum, on_m_code_checksum )->required()->as_number(); this->off_m_code = this->kernel->config->value(switch_checksum, this->name_checksum, off_m_code_checksum )->required()->as_number(); this->output_pin.from_string(this->kernel->config->value(switch_checksum, this->name_checksum, output_pin_checksum )->required()->as_string())->as_output(); this->output_pin.set(this->kernel->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(0)->as_number() ); set_low_on_debug(output_pin.port_number, output_pin.pin); }
// Get config void Switch::on_config_reload(void *argument) { this->input_pin.from_string( THEKERNEL->config->value(switch_checksum, this->name_checksum, input_pin_checksum )->by_default("nc")->as_string())->as_input(); this->input_pin_behavior = THEKERNEL->config->value(switch_checksum, this->name_checksum, input_pin_behavior_checksum )->by_default(momentary_checksum)->as_number(); std::string input_on_command = THEKERNEL->config->value(switch_checksum, this->name_checksum, input_on_command_checksum )->by_default("")->as_string(); std::string input_off_command = THEKERNEL->config->value(switch_checksum, this->name_checksum, input_off_command_checksum )->by_default("")->as_string(); this->output_pin.from_string(THEKERNEL->config->value(switch_checksum, this->name_checksum, output_pin_checksum )->by_default("nc")->as_string())->as_output(); this->output_on_command = THEKERNEL->config->value(switch_checksum, this->name_checksum, output_on_command_checksum )->by_default("")->as_string(); this->output_off_command = THEKERNEL->config->value(switch_checksum, this->name_checksum, output_off_command_checksum )->by_default("")->as_string(); this->switch_state = THEKERNEL->config->value(switch_checksum, this->name_checksum, startup_state_checksum )->by_default(false)->as_bool(); string type = THEKERNEL->config->value(switch_checksum, this->name_checksum, output_type_checksum )->by_default("pwm")->as_string(); if(type == "pwm") this->output_type= PWM; else if(type == "digital") this->output_type= DIGITAL; else this->output_type= PWM; // unkown type default to pwm if(this->output_pin.connected()) { if(this->output_type == PWM) { this->output_pin.max_pwm(THEKERNEL->config->value(switch_checksum, this->name_checksum, max_pwm_checksum )->by_default(255)->as_number()); this->switch_value = THEKERNEL->config->value(switch_checksum, this->name_checksum, startup_value_checksum )->by_default(this->output_pin.max_pwm())->as_number(); if(this->switch_state) { this->output_pin.pwm(this->switch_value); // will be truncated to max_pwm } else { this->output_pin.set(false); } } else { this->output_pin.set(this->switch_state); } } set_low_on_debug(output_pin.port_number, output_pin.pin); // Set the on/off command codes, Use GCode to do the parsing input_on_command_letter = 0; input_off_command_letter = 0; if(!input_on_command.empty()) { Gcode gc(input_on_command, NULL); if(gc.has_g) { input_on_command_letter = 'G'; input_on_command_code = gc.g; } else if(gc.has_m) { input_on_command_letter = 'M'; input_on_command_code = gc.m; } } if(!input_off_command.empty()) { Gcode gc(input_off_command, NULL); if(gc.has_g) { input_off_command_letter = 'G'; input_off_command_code = gc.g; } else if(gc.has_m) { input_off_command_letter = 'M'; input_off_command_code = gc.m; } } if(input_pin.connected()) { // set to initial state this->input_pin_state = this->input_pin.get(); // input pin polling THEKERNEL->slow_ticker->attach( 100, this, &Switch::pinpoll_tick); } if(this->output_type == PWM && this->output_pin.connected()) { // PWM THEKERNEL->slow_ticker->attach(1000, &this->output_pin, &Pwm::on_tick); } }
// Get configuration from the config file void TemperatureControl::load_config() { // General config this->set_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, set_m_code_checksum)->by_default(104)->as_number(); this->set_and_wait_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, set_and_wait_m_code_checksum)->by_default(109)->as_number(); this->get_m_code = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, get_m_code_checksum)->by_default(105)->as_number(); this->readings_per_second = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, readings_per_second_checksum)->by_default(20)->as_number(); this->designator = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, designator_checksum)->by_default(string("T"))->as_string(); // Max and min temperatures we are not allowed to get over (Safety) this->max_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_temp_checksum)->by_default(300)->as_number(); this->min_temp = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, min_temp_checksum)->by_default(0)->as_number(); // Heater pin this->heater_pin.from_string( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, heater_pin_checksum)->by_default("nc")->as_string()); if(this->heater_pin.connected()){ this->readonly= false; this->heater_pin.as_output(); } else { this->readonly= true; } // For backward compatibility, default to a thermistor sensor. std::string sensor_type = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, sensor_checksum)->by_default("thermistor")->as_string(); // Instantiate correct sensor (TBD: TempSensor factory?) delete sensor; sensor = nullptr; // In case we fail to create a new sensor. if(sensor_type.compare("thermistor") == 0) { sensor = new Thermistor(); } else if(sensor_type.compare("max31855") == 0) { sensor = new Max31855(); } else if(sensor_type.compare("ad8495") == 0) { sensor = new AD8495(); } else { sensor = new TempSensor(); // A dummy implementation } sensor->UpdateConfig(temperature_control_checksum, this->name_checksum); this->preset1 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset1_checksum)->by_default(0)->as_number(); this->preset2 = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, preset2_checksum)->by_default(0)->as_number(); // sigma-delta output modulation this->o = 0; if(!this->readonly) { // used to enable bang bang control of heater this->use_bangbang = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, bang_bang_checksum)->by_default(false)->as_bool(); this->hysteresis = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, hysteresis_checksum)->by_default(2)->as_number(); this->windup = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, windup_checksum)->by_default(false)->as_bool(); this->heater_pin.max_pwm( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, max_pwm_checksum)->by_default(255)->as_number() ); this->heater_pin.set(0); set_low_on_debug(heater_pin.port_number, heater_pin.pin); // activate SD-DAC timer THEKERNEL->slow_ticker->attach( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, pwm_frequency_checksum)->by_default(2000)->as_number(), &heater_pin, &Pwm::on_tick); } // reading tick THEKERNEL->slow_ticker->attach( this->readings_per_second, this, &TemperatureControl::thermistor_read_tick ); this->PIDdt = 1.0 / this->readings_per_second; // PID setPIDp( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, p_factor_checksum)->by_default(10 )->as_number() ); setPIDi( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_factor_checksum)->by_default(0.3f)->as_number() ); setPIDd( THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, d_factor_checksum)->by_default(200)->as_number() ); if(!this->readonly) { // set to the same as max_pwm by default this->i_max = THEKERNEL->config->value(temperature_control_checksum, this->name_checksum, i_max_checksum )->by_default(this->heater_pin.max_pwm())->as_number(); } this->iTerm = 0.0; this->lastInput = -1.0; this->last_reading = 0.0; }