2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 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.
4 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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
8 #include "libs/Kernel.h"
9 #include "libs/Module.h"
10 #include "libs/Config.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/SlowTicker.h"
14 #include "libs/StreamOutputPool.h"
16 #include "checksumm.h"
17 #include "ConfigValue.h"
19 #include "libs/StepTicker.h"
20 #include "libs/PublicData.h"
21 #include "modules/communication/SerialConsole.h"
22 #include "modules/communication/GcodeDispatch.h"
23 #include "modules/robot/Planner.h"
24 #include "modules/robot/Robot.h"
25 #include "modules/robot/Conveyor.h"
26 #include "StepperMotor.h"
27 #include "BaseSolution.h"
28 #include "EndstopsPublicAccess.h"
29 #include "Configurator.h"
30 #include "SimpleShell.h"
32 #include "platform_memory.h"
38 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
39 #define uart0_checksum CHECKSUM("uart0")
41 #define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency")
42 #define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse")
43 #define disable_leds_checksum CHECKSUM("leds_disable")
44 #define grbl_mode_checksum CHECKSUM("grbl_mode")
45 #define ok_per_line_checksum CHECKSUM("ok_per_line")
47 Kernel
* Kernel::instance
;
49 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
54 instance
= this; // setup the Singleton instance of the kernel
56 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
57 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
58 this->serial
= new SerialConsole(USBTX
, USBRX
, DEFAULT_SERIAL_BAUD_RATE
);
60 // Config next, but does not load cache yet
61 this->config
= new Config();
63 // Pre-load the config cache, do after setting up serial so we can report errors to serial
64 this->config
->config_cache_load();
66 // now config is loaded we can do normal setup for serial based on config
70 this->streams
= new StreamOutputPool();
72 this->current_path
= "/";
74 // Configure UART depending on MRI config
75 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
76 NVIC_SetPriorityGrouping(0);
79 switch( __mriPlatform_CommUartIndex() ) {
81 this->serial
= new(AHB0
) SerialConsole(USBTX
, USBRX
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
84 this->serial
= new(AHB0
) SerialConsole( p13
, p14
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
87 this->serial
= new(AHB0
) SerialConsole( p28
, p27
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
90 this->serial
= new(AHB0
) SerialConsole( p9
, p10
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
95 if(this->serial
== NULL
) {
96 this->serial
= new(AHB0
) SerialConsole(USBTX
, USBRX
, this->config
->value(uart0_checksum
,baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
99 //some boards don't have leds.. TOO BAD!
100 this->use_leds
= !this->config
->value( disable_leds_checksum
)->by_default(false)->as_bool();
103 this->grbl_mode
= this->config
->value( grbl_mode_checksum
)->by_default(true)->as_bool();
105 this->grbl_mode
= this->config
->value( grbl_mode_checksum
)->by_default(false)->as_bool();
108 // we exepct ok per line now not per G code, setting this to false will return to the old (incorrect) way of ok per G code
109 this->ok_per_line
= this->config
->value( ok_per_line_checksum
)->by_default(true)->as_bool();
111 this->add_module( this->serial
);
114 add_module( this->slow_ticker
= new SlowTicker());
116 this->step_ticker
= new StepTicker();
117 this->adc
= new Adc();
119 // TODO : These should go into platform-specific files
121 NVIC_SetPriorityGrouping(0);
122 NVIC_SetPriority(TIMER0_IRQn
, 2);
123 NVIC_SetPriority(TIMER1_IRQn
, 1);
124 NVIC_SetPriority(TIMER2_IRQn
, 4);
125 NVIC_SetPriority(PendSV_IRQn
, 3);
127 // Set other priorities lower than the timers
128 NVIC_SetPriority(ADC_IRQn
, 5);
129 NVIC_SetPriority(USB_IRQn
, 5);
133 if( NVIC_GetPriority(UART0_IRQn
) > 0 ){ NVIC_SetPriority(UART0_IRQn
, 5); }
134 if( NVIC_GetPriority(UART1_IRQn
) > 0 ){ NVIC_SetPriority(UART1_IRQn
, 5); }
135 if( NVIC_GetPriority(UART2_IRQn
) > 0 ){ NVIC_SetPriority(UART2_IRQn
, 5); }
136 if( NVIC_GetPriority(UART3_IRQn
) > 0 ){ NVIC_SetPriority(UART3_IRQn
, 5); }
138 NVIC_SetPriority(UART0_IRQn
, 5);
139 NVIC_SetPriority(UART1_IRQn
, 5);
140 NVIC_SetPriority(UART2_IRQn
, 5);
141 NVIC_SetPriority(UART3_IRQn
, 5);
144 // Configure the step ticker
145 this->base_stepping_frequency
= this->config
->value(base_stepping_frequency_checksum
)->by_default(100000)->as_number();
146 float microseconds_per_step_pulse
= this->config
->value(microseconds_per_step_pulse_checksum
)->by_default(1)->as_number();
148 // Configure the step ticker
149 this->step_ticker
->set_frequency( this->base_stepping_frequency
);
150 this->step_ticker
->set_unstep_time( microseconds_per_step_pulse
);
153 this->add_module( this->conveyor
= new Conveyor() );
154 this->add_module( this->gcode_dispatch
= new GcodeDispatch() );
155 this->add_module( this->robot
= new Robot() );
156 this->add_module( this->simpleshell
= new SimpleShell() );
158 this->planner
= new Planner();
159 this->configurator
= new Configurator();
162 // return a GRBL-like query string for serial ?
163 std::string
Kernel::get_query_string()
167 bool ok
= PublicData::get_value(endstops_checksum
, get_homing_status_checksum
, 0, &homing
);
168 if(!ok
) homing
= false;
173 str
.append("Alarm,");
176 }else if(feed_hold
) {
178 }else if(this->conveyor
->is_idle()) {
187 robot
->get_current_machine_position(mpos
);
188 // current_position/mpos includes the compensation transform so we need to get the inverse to get actual position
189 if(robot
->compensationTransform
) robot
->compensationTransform(mpos
, true); // get inverse compensation transform
193 size_t n
= snprintf(buf
, sizeof(buf
), "%1.4f,%1.4f,%1.4f,", robot
->from_millimeters(mpos
[0]), robot
->from_millimeters(mpos
[1]), robot
->from_millimeters(mpos
[2]));
194 str
.append("MPos:").append(buf
, n
);
196 // work space position
197 Robot::wcs_t pos
= robot
->mcs2wcs(mpos
);
198 n
= snprintf(buf
, sizeof(buf
), "%1.4f,%1.4f,%1.4f", robot
->from_millimeters(std::get
<X_AXIS
>(pos
)), robot
->from_millimeters(std::get
<Y_AXIS
>(pos
)), robot
->from_millimeters(std::get
<Z_AXIS
>(pos
)));
199 str
.append("WPos:").append(buf
, n
);
203 // return the last milestone if idle
206 Robot::wcs_t mpos
= robot
->get_axis_position();
207 size_t n
= snprintf(buf
, sizeof(buf
), "%1.4f,%1.4f,%1.4f,", robot
->from_millimeters(std::get
<X_AXIS
>(mpos
)), robot
->from_millimeters(std::get
<Y_AXIS
>(mpos
)), robot
->from_millimeters(std::get
<Z_AXIS
>(mpos
)));
208 str
.append("MPos:").append(buf
, n
);
210 // work space position
211 Robot::wcs_t pos
= robot
->mcs2wcs(mpos
);
212 n
= snprintf(buf
, sizeof(buf
), "%1.4f,%1.4f,%1.4f", robot
->from_millimeters(std::get
<X_AXIS
>(pos
)), robot
->from_millimeters(std::get
<Y_AXIS
>(pos
)), robot
->from_millimeters(std::get
<Z_AXIS
>(pos
)));
213 str
.append("WPos:").append(buf
, n
);
220 // Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
221 void Kernel::add_module(Module
* module
){
222 module
->on_module_loaded();
225 // Adds a hook for a given module and event
226 void Kernel::register_for_event(_EVENT_ENUM id_event
, Module
*mod
){
227 this->hooks
[id_event
].push_back(mod
);
230 // Call a specific event with an argument
231 void Kernel::call_event(_EVENT_ENUM id_event
, void * argument
){
233 if(id_event
== ON_HALT
) {
234 this->halted
= (argument
== nullptr);
235 was_idle
= conveyor
->is_idle(); // see if we were doing anything like printing
238 // send to all registered modules
239 for (auto m
: hooks
[id_event
]) {
240 (m
->*kernel_callback_functions
[id_event
])(argument
);
243 if(id_event
== ON_HALT
&& this->halted
&& !was_idle
) {
244 // we need to try to correct current positions if we were running
245 this->robot
->reset_position_from_current_actuator_position();
249 // These are used by tests to test for various things. basically mocks
250 bool Kernel::kernel_has_event(_EVENT_ENUM id_event
, Module
*mod
)
252 for (auto m
: hooks
[id_event
]) {
253 if(m
== mod
) return true;
258 void Kernel::unregister_for_event(_EVENT_ENUM id_event
, Module
*mod
)
260 for (auto i
= hooks
[id_event
].begin(); i
!= hooks
[id_event
].end(); ++i
) {
262 hooks
[id_event
].erase(i
);