Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Kernel.cpp Source File

Kernel.cpp

00001 /*
00002       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003       Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
00004       Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00005       You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
00006 */
00007 
00008 #include "libs/Kernel.h"
00009 #include "libs/Module.h"
00010 #include "libs/Config.h"
00011 #include "libs/nuts_bolts.h"
00012 #include "libs/SlowTicker.h"
00013 #include "libs/Pauser.h"
00014 #include "libs/StreamOutputPool.h"
00015 
00016 #include "modules/communication/SerialConsole.h"
00017 #include "modules/communication/GcodeDispatch.h"
00018 #include "modules/robot/Planner.h"
00019 #include "modules/robot/Robot.h"
00020 #include "modules/robot/Stepper.h"
00021 #include "modules/robot/Conveyor.h"
00022 #include "modules/tools/endstops/Endstops.h"
00023 
00024 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
00025 #define uart0_checksum             CHECKSUM("uart0")
00026 
00027 Kernel* Kernel::instance;
00028 
00029 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
00030 Kernel::Kernel(){
00031     instance= this; // setup the Singleton instance of the kernel
00032 
00033     // Config first, because we need the baud_rate setting before we start serial
00034     this->config         = new Config();
00035 
00036     // Serial second, because the other modules might want to say something
00037     this->streams        = new StreamOutputPool();
00038 
00039     this->current_path   = "/";
00040 
00041     this->add_module( this->config );
00042     this->add_module( this->serial );
00043 
00044     // HAL stuff
00045     add_module( this->slow_ticker          = new SlowTicker());
00046     this->step_ticker          = new StepTicker();
00047 //    this->adc                  = new Adc();
00048 
00049     // Configure the step ticker
00050     int base_stepping_frequency          =  this->config->value(base_stepping_frequency_checksum      )->by_default(100000)->as_number();
00051     float microseconds_per_step_pulse   =  this->config->value(microseconds_per_step_pulse_checksum  )->by_default(5     )->as_number();
00052 
00053     // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
00054     this->step_ticker->set_reset_delay( microseconds_per_step_pulse / 1000000L );
00055     this->step_ticker->set_frequency(   base_stepping_frequency );
00056 
00057     // Core modules
00058     this->add_module( this->gcode_dispatch = new GcodeDispatch() );
00059     this->add_module( this->robot          = new Robot()         );
00060     this->add_module( this->stepper        = new Stepper()       );
00061     this->add_module( this->planner        = new Planner()       );
00062     this->add_module( this->conveyor       = new Conveyor()      );
00063     this->add_module( this->pauser         = new Pauser()        );
00064     this->add_module( this->public_data    = new PublicData()    );
00065     this->add_module( this->toolsmanager   = new ToolsManager()    );
00066 
00067 }
00068 
00069 // Add a module to Kernel. We don't actually hold a list of modules, we just tell it where Kernel is
00070 void Kernel::add_module(Module* module){
00071     module->on_module_loaded();
00072 }
00073 
00074 // Adds a hook for a given module and event
00075 void Kernel::register_for_event(_EVENT_ENUM id_event, Module* module){
00076     this->hooks[id_event].push_back(module);
00077 }
00078 
00079 // Call a specific event without arguments
00080 void Kernel::call_event(_EVENT_ENUM id_event){
00081     for (Module* current : hooks[id_event]) {
00082         (current->*kernel_callback_functions[id_event])(this);
00083     }
00084 }
00085 
00086 // Call a specific event with an argument
00087 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
00088     for (Module* current : hooks[id_event]) {
00089         (current->*kernel_callback_functions[id_event])(argument);
00090     }
00091 }