in-file config of the TemperatureControl, and simple adjustments to make
authorArthur Wolf <wolf.arthur@gmail.com>
Wed, 4 Jan 2012 13:59:19 +0000 (14:59 +0100)
committerArthur Wolf <wolf.arthur@gmail.com>
Wed, 4 Jan 2012 13:59:19 +0000 (14:59 +0100)
porting easier

src/libs/Config.cpp
src/libs/Config.h
src/libs/SlowTicker.cpp
src/modules/robot/Planner.cpp
src/modules/robot/Stepper.cpp
src/modules/tools/temperaturecontrol/TemperatureControl.cpp
src/modules/tools/temperaturecontrol/TemperatureControl.h

index 4ef3b1f..1d72cc5 100644 (file)
@@ -98,6 +98,9 @@ void Config::set_string( uint16_t check_sum, string value ){
 ConfigValue* Config::value(uint16_t check_sum){
     ConfigValue* result = new ConfigValue;
     result->check_sum = check_sum; 
+    if( this->has_config_file() == false ){
+       return result;
+    } 
     // Open the config file ( find it if we haven't already found it ) 
     FILE *lp = fopen(this->get_config_file().c_str(), "r");
     string buffer;
@@ -127,12 +130,22 @@ ConfigValue* Config::value(uint16_t check_sum){
     return result;
 }
 
-// Get the filename for the config file
-string Config::get_config_file(){
-    if( this->config_file_found ){ return this->config_file; }
+bool Config::has_config_file(){
+    if( this->config_file_found ){ return true; }
     this->try_config_file("/local/config");
     this->try_config_file("/sd/config");
     if( this->config_file_found ){
+        return true; 
+    }else{
+        return false; 
+    }
+
+}
+
+// Get the filename for the config file
+string Config::get_config_file(){
+    if( this->config_file_found ){ return this->config_file; }
+    if( this->has_config_file() ){
         return this->config_file;
     }else{
         printf("ERROR: no config file found\r\n"); 
index a4cdce2..e00d73f 100644 (file)
@@ -93,6 +93,7 @@ class Config : public Module {
         ConfigValue* value(uint16_t check_sum);
         bool   has_characters(uint16_t check_sum, string str );
         string get_config_file();
+        bool has_config_file();
         void try_config_file(string candidate);
 
         string config_file;         // Path to the config file
index 437133c..dad4579 100644 (file)
@@ -11,17 +11,17 @@ SlowTicker* global_slow_ticker;
 
 SlowTicker::SlowTicker(){
     global_slow_ticker = this;
-    LPC_SC->PCONP |= (1 << 22);
-    LPC_TIM2->MR0 = 1000000; 
-    LPC_TIM2->MCR = 3;
-    LPC_TIM2->TCR = 1; 
-    NVIC_EnableIRQ(TIMER2_IRQn);
+    LPC_SC->PCONP |= (1 << 22);     // Power Ticker ON
+    LPC_TIM2->MR0 = 1000000;        // Initial dummy value for Match Register
+    LPC_TIM2->MCR = 3;              // Match on MR0, reset on MR0
+    LPC_TIM2->TCR = 1;              // Enable interrupt
+    NVIC_EnableIRQ(TIMER2_IRQn);    // Enable interrupt handler
 }
 
 void SlowTicker::set_frequency( int frequency ){
-    LPC_TIM2->MR0 = int(floor((SystemCoreClock/4)/frequency));
-    LPC_TIM2->TCR = 3; 
-    LPC_TIM2->TCR = 1; 
+    LPC_TIM2->MR0 = int(floor((SystemCoreClock/4)/frequency));  // SystemCoreClock/4 = Timer increments in a second
+    LPC_TIM2->TCR = 3;  // Reset
+    LPC_TIM2->TCR = 1;  // Reset
 }
 
 void SlowTicker::tick(){
@@ -31,8 +31,8 @@ void SlowTicker::tick(){
 }
 
 extern "C" void TIMER2_IRQHandler (void){
-    if((LPC_TIM2->IR >> 0) & 1){
-        LPC_TIM2->IR |= 1 << 0;
+    if((LPC_TIM2->IR >> 0) & 1){  // If interrupt register set for MR0
+        LPC_TIM2->IR |= 1 << 0;   // Reset it 
         global_slow_ticker->tick(); 
     }
 }
index 47d5d0b..6b0f00d 100644 (file)
@@ -30,8 +30,8 @@ void Planner::on_module_loaded(){
 }
 
 void Planner::on_config_reload(void* argument){
-    this->acceleration =       this->kernel->config->value(acceleration_checksum       )->required()->as_number();
-    this->max_jerk =           this->kernel->config->value(max_jerk_checksum           )->required(      )->as_number();
+    this->acceleration =       this->kernel->config->value(acceleration_checksum       )->by_default(100 )->as_number();
+    this->max_jerk =           this->kernel->config->value(max_jerk_checksum           )->by_default(100 )->as_number();
     this->junction_deviation = this->kernel->config->value(junction_deviation_checksum )->by_default(0.05)->as_number(); 
 }
 
index 0184acf..90b79ac 100644 (file)
@@ -64,12 +64,12 @@ void Stepper::on_config_reload(void* argument){
     this->base_stepping_frequency       =  this->kernel->config->value(base_stepping_frequency_checksum      )->by_default(100000)->as_number();
     this->step_gpio_port      = gpios[(int)this->kernel->config->value(step_gpio_port_checksum               )->by_default(0     )->as_number()];
     this->dir_gpio_port       = gpios[(int)this->kernel->config->value(dir_gpio_port_checksum                )->by_default(0     )->as_number()];
-    this->alpha_step_pin                =  this->kernel->config->value(alpha_step_pin_checksum               )->required(        )->as_number();
-    this->beta_step_pin                 =  this->kernel->config->value(beta_step_pin_checksum                )->required(        )->as_number();
-    this->gamma_step_pin                =  this->kernel->config->value(gamma_step_pin_checksum               )->required(        )->as_number();
-    this->alpha_dir_pin                 =  this->kernel->config->value(alpha_dir_pin_checksum                )->required(        )->as_number();
-    this->beta_dir_pin                  =  this->kernel->config->value(beta_dir_pin_checksum                 )->required(        )->as_number();
-    this->gamma_dir_pin                 =  this->kernel->config->value(gamma_dir_pin_checksum                )->required(        )->as_number();
+    this->alpha_step_pin                =  this->kernel->config->value(alpha_step_pin_checksum               )->by_default(1     )->as_number();
+    this->beta_step_pin                 =  this->kernel->config->value(beta_step_pin_checksum                )->by_default(2     )->as_number();
+    this->gamma_step_pin                =  this->kernel->config->value(gamma_step_pin_checksum               )->by_default(3     )->as_number();
+    this->alpha_dir_pin                 =  this->kernel->config->value(alpha_dir_pin_checksum                )->by_default(4     )->as_number();
+    this->beta_dir_pin                  =  this->kernel->config->value(beta_dir_pin_checksum                 )->by_default(5     )->as_number();
+    this->gamma_dir_pin                 =  this->kernel->config->value(gamma_dir_pin_checksum                )->by_default(6     )->as_number();
     this->step_mask                     = ( 1 << this->alpha_step_pin ) + ( 1 << this->beta_step_pin ) + ( 1 << this->gamma_step_pin );
     this->dir_mask                      = ( 1 << this->alpha_dir_pin  ) + ( 1 << this->beta_dir_pin  ) + ( 1 << this->gamma_dir_pin  );
     this->step_bits[ALPHA_STEPPER ]     = this->alpha_step_pin;
@@ -147,7 +147,8 @@ extern "C" void TIMER0_IRQHandler (void){
 // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Smoothie. It is executed at the rate set with
 // config_step_timer. It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
 inline void Stepper::main_interrupt(){
-    
+   
+    // TODO: Explain this magic 
     // Step dir pins first, then step pinse, stepper drivers like to know the direction before the step signal comes in
     // Clear dir   pins 
     this->dir_gpio_port->FIOSET  =                          this->dir_invert_mask          & this->dir_mask; 
index 96b999e..88d13b8 100644 (file)
@@ -14,24 +14,7 @@ void TemperatureControl::on_module_loaded(){
     this->desired_adc_value = UNDEFINED;
 
     // Settings
-    this->readings_per_second = 5;
-
-    this->r0 = 100000;               // Stated resistance eg. 100K
-    this->t0 = 25 + 273.15;          // Temperature at stated resistance, eg. 25C
-    this->beta = 4066;               // Thermistor beta rating. See http://reprap.org/bin/view/Main/MeasuringThermistorBeta
-    this->vadc = 3.3;                // ADC Reference
-    this->vcc  = 3.3;                // Supply voltage to potential divider
-    this->k = this->r0 * exp( -this->beta / this->t0 );
-    double r1 = 0;
-    double r2 = 4700;
-
-    if( r1 > 0 ){
-        this->vs = r1 * this->vcc / ( r1 + r2 );
-        this->rs = r1 * r2 / ( r1 + r2 );
-    }else{
-        this->vs = this->vcc;
-        this->rs = r2;
-    } 
+    this->on_config_reload(this);
 
     this->acceleration_factor = 10;
 
@@ -47,6 +30,34 @@ void TemperatureControl::on_module_loaded(){
 
 }
 
+
+// Get configuration from the config file
+void TemperatureControl::on_config_reload(void* argument){
+
+    this->readings_per_second = this->kernel->config->value(readings_per_second_ckeckusm     )->by_default(5     )->as_number();
+
+    // Values are here : http://reprap.org/wiki/Thermistor
+    // TODO: WARNING : THIS WILL CHANGE and backward compatibility will be broken for config files that use this
+    this->r0 =                  this->kernel->config->value(temperature_control_r0_ckeckusm  )->by_default(100000)->as_number();               // Stated resistance eg. 100K
+    this->t0 =                  this->kernel->config->value(temperature_control_t0_ckeckusm  )->by_default(25    )->as_number() + 273.15;      // Temperature at stated resistance, eg. 25C
+    this->beta =                this->kernel->config->value(temperature_control_beta_ckeckusm)->by_default(4066  )->as_number();               // Thermistor beta rating. See http://reprap.org/bin/view/Main/MeasuringThermistorBeta
+    this->vadc =                this->kernel->config->value(temperature_control_vadc_ckeckusm)->by_default(3.3   )->as_number();               // ADC Reference
+    this->vcc  =                this->kernel->config->value(temperature_control_vcc_ckeckusm )->by_default(3.3   )->as_number();               // Supply voltage to potential divider
+    this->r1 =                  this->kernel->config->value(temperature_control_r1_ckeckusm  )->by_default(0     )->as_number();
+    this->r2 =                  this->kernel->config->value(temperature_control_r2_ckeckusm  )->by_default(4700  )->as_number();
+
+    this->k = this->r0 * exp( -this->beta / this->t0 );
+    
+    if( r1 > 0 ){
+        this->vs = r1 * this->vcc / ( r1 + r2 );
+        this->rs = r1 * r2 / ( r1 + r2 );
+    }else{
+        this->vs = this->vcc;
+        this->rs = r2;
+    } 
+
+}
+
 void TemperatureControl::on_gcode_execute(void* argument){
     Gcode* gcode = static_cast<Gcode*>(argument);
     
index 0136d8a..e538074 100644 (file)
@@ -8,12 +8,26 @@
 
 #define UNDEFINED -1
 
+
+#define temperature_control_r0_ckeckusm    8728
+#define readings_per_second_ckeckusm       18645 
+#define temperature_control_t0_ckeckusm    9754
+#define temperature_control_beta_ckeckusm  64275 
+#define temperature_control_vadc_ckeckusm  8725
+#define temperature_control_vcc_ckeckusm   4274
+#define temperature_control_r1_ckeckusm    8985
+#define temperature_control_r2_ckeckusm    9242
+
+
+
+
 class TemperatureControl : public Module {
     public:
         TemperatureControl();
         
         void on_module_loaded();
         void on_gcode_execute(void* argument);
+        void on_config_reload(void* argument);
         void set_desired_temperature(double desired_temperature);
         double get_temperature();
         double adc_value_to_temperature(double adc_value);
@@ -33,6 +47,8 @@ class TemperatureControl : public Module {
         // Thermistor computation settings
         double r0;
         double t0;
+        double r1;
+        double r2;
         double beta;
         double vadc;
         double vcc;