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"
31 #include "TemperatureControlPublicAccess.h"
33 #ifndef NO_TOOLS_LASER
37 #include "platform_memory.h"
43 #define laser_checksum CHECKSUM("laser")
44 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
45 #define uart0_checksum CHECKSUM("uart0")
47 #define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency")
48 #define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse")
49 #define disable_leds_checksum CHECKSUM("leds_disable")
50 #define grbl_mode_checksum CHECKSUM("grbl_mode")
51 #define feed_hold_enable_checksum CHECKSUM("enable_feed_hold")
52 #define ok_per_line_checksum CHECKSUM("ok_per_line")
53 #define new_status_format_checksum CHECKSUM("new_status_format")
55 Kernel
* Kernel::instance
;
57 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
62 enable_feed_hold
= false;
64 instance
= this; // setup the Singleton instance of the kernel
66 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
67 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
68 this->serial
= new SerialConsole(USBTX
, USBRX
, DEFAULT_SERIAL_BAUD_RATE
);
70 // Config next, but does not load cache yet
71 this->config
= new Config();
73 // Pre-load the config cache, do after setting up serial so we can report errors to serial
74 this->config
->config_cache_load();
76 // now config is loaded we can do normal setup for serial based on config
80 this->streams
= new StreamOutputPool();
82 this->current_path
= "/";
84 // Configure UART depending on MRI config
85 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
86 NVIC_SetPriorityGrouping(0);
89 switch( __mriPlatform_CommUartIndex() ) {
91 this->serial
= new(AHB0
) SerialConsole(USBTX
, USBRX
, this->config
->value(uart0_checksum
, baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
94 this->serial
= new(AHB0
) SerialConsole( p13
, p14
, this->config
->value(uart0_checksum
, baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
97 this->serial
= new(AHB0
) SerialConsole( p28
, p27
, this->config
->value(uart0_checksum
, baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
100 this->serial
= new(AHB0
) SerialConsole( p9
, p10
, this->config
->value(uart0_checksum
, baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
105 if(this->serial
== NULL
) {
106 this->serial
= new(AHB0
) SerialConsole(USBTX
, USBRX
, this->config
->value(uart0_checksum
, baud_rate_setting_checksum
)->by_default(DEFAULT_SERIAL_BAUD_RATE
)->as_number());
109 //some boards don't have leds.. TOO BAD!
110 this->use_leds
= !this->config
->value( disable_leds_checksum
)->by_default(false)->as_bool();
113 this->grbl_mode
= this->config
->value( grbl_mode_checksum
)->by_default(true)->as_bool();
115 this->grbl_mode
= this->config
->value( grbl_mode_checksum
)->by_default(false)->as_bool();
118 this->enable_feed_hold
= this->config
->value( feed_hold_enable_checksum
)->by_default(this->grbl_mode
)->as_bool();
120 // we expect ok per line now not per G code, setting this to false will return to the old (incorrect) way of ok per G code
121 this->ok_per_line
= this->config
->value( ok_per_line_checksum
)->by_default(true)->as_bool();
123 this->new_status_format
= this->config
->value( new_status_format_checksum
)->by_default(true)->as_bool();
125 this->add_module( this->serial
);
128 add_module( this->slow_ticker
= new SlowTicker());
130 this->step_ticker
= new StepTicker();
131 this->adc
= new Adc();
133 // TODO : These should go into platform-specific files
135 NVIC_SetPriorityGrouping(0);
136 NVIC_SetPriority(TIMER0_IRQn
, 2);
137 NVIC_SetPriority(TIMER1_IRQn
, 1);
138 NVIC_SetPriority(TIMER2_IRQn
, 4);
139 NVIC_SetPriority(PendSV_IRQn
, 3);
141 // Set other priorities lower than the timers
142 NVIC_SetPriority(ADC_IRQn
, 5);
143 NVIC_SetPriority(USB_IRQn
, 5);
147 if( NVIC_GetPriority(UART0_IRQn
) > 0 ) { NVIC_SetPriority(UART0_IRQn
, 5); }
148 if( NVIC_GetPriority(UART1_IRQn
) > 0 ) { NVIC_SetPriority(UART1_IRQn
, 5); }
149 if( NVIC_GetPriority(UART2_IRQn
) > 0 ) { NVIC_SetPriority(UART2_IRQn
, 5); }
150 if( NVIC_GetPriority(UART3_IRQn
) > 0 ) { NVIC_SetPriority(UART3_IRQn
, 5); }
152 NVIC_SetPriority(UART0_IRQn
, 5);
153 NVIC_SetPriority(UART1_IRQn
, 5);
154 NVIC_SetPriority(UART2_IRQn
, 5);
155 NVIC_SetPriority(UART3_IRQn
, 5);
158 // Configure the step ticker
159 this->base_stepping_frequency
= this->config
->value(base_stepping_frequency_checksum
)->by_default(100000)->as_number();
160 float microseconds_per_step_pulse
= this->config
->value(microseconds_per_step_pulse_checksum
)->by_default(1)->as_number();
162 // Configure the step ticker
163 this->step_ticker
->set_frequency( this->base_stepping_frequency
);
164 this->step_ticker
->set_unstep_time( microseconds_per_step_pulse
);
167 this->add_module( this->conveyor
= new Conveyor() );
168 this->add_module( this->gcode_dispatch
= new GcodeDispatch() );
169 this->add_module( this->robot
= new Robot() );
170 this->add_module( this->simpleshell
= new SimpleShell() );
172 this->planner
= new Planner();
173 this->configurator
= new Configurator();
176 // return a GRBL-like query string for serial ?
177 std::string
Kernel::get_query_string()
181 bool ok
= PublicData::get_value(endstops_checksum
, get_homing_status_checksum
, 0, &homing
);
182 if(!ok
) homing
= false;
183 bool running
= false;
191 } else if(feed_hold
) {
193 } else if(this->conveyor
->is_idle()) {
202 robot
->get_current_machine_position(mpos
);
203 // current_position/mpos includes the compensation transform so we need to get the inverse to get actual position
204 if(robot
->compensationTransform
) robot
->compensationTransform(mpos
, true); // get inverse compensation transform
208 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]));
209 if(n
> sizeof(buf
)) n
= sizeof(buf
);
211 if(new_status_format
) {
212 str
.append("|MPos:").append(buf
, n
);
214 #if MAX_ROBOT_ACTUATORS > 3
215 // deal with the ABC axis (E will be A)
216 for (int i
= A_AXIS
; i
< robot
->get_number_registered_motors(); ++i
) {
217 // current actuator position
218 n
= snprintf(buf
, sizeof(buf
), ",%1.4f", robot
->from_millimeters(robot
->actuators
[i
]->get_current_position()));
219 if(n
> sizeof(buf
)) n
= sizeof(buf
);
225 str
.append(",MPos:").append(buf
, n
);
228 // work space position
229 Robot::wcs_t pos
= robot
->mcs2wcs(mpos
);
230 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
)));
231 if(n
> sizeof(buf
)) n
= sizeof(buf
);
233 if(new_status_format
) {
234 str
.append("|WPos:").append(buf
, n
);
236 float fr
= robot
->from_millimeters(conveyor
->get_current_feedrate()*60.0F
);
237 n
= snprintf(buf
, sizeof(buf
), "|F:%1.4f", fr
);
238 if(n
> sizeof(buf
)) n
= sizeof(buf
);
240 float sr
= robot
->get_s_value();
241 n
= snprintf(buf
, sizeof(buf
), "|S:%1.4f", sr
);
242 if(n
> sizeof(buf
)) n
= sizeof(buf
);
245 // current Laser power
246 #ifndef NO_TOOLS_LASER
247 Laser
*plaser
= nullptr;
248 if(PublicData::get_value(laser_checksum
, (void *)&plaser
) && plaser
!= nullptr) {
249 float lp
= plaser
->get_current_power();
250 n
= snprintf(buf
, sizeof(buf
), "|L:%1.4f", lp
);
251 if(n
> sizeof(buf
)) n
= sizeof(buf
);
257 str
.append(",WPos:").append(buf
, n
);
261 // return the last milestone if idle
264 Robot::wcs_t mpos
= robot
->get_axis_position();
265 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
)));
266 if(n
> sizeof(buf
)) n
= sizeof(buf
);
267 if(new_status_format
) {
268 str
.append("|MPos:").append(buf
, n
);
269 #if MAX_ROBOT_ACTUATORS > 3
270 // deal with the ABC axis (E will be A)
271 for (int i
= A_AXIS
; i
< robot
->get_number_registered_motors(); ++i
) {
272 // current actuator position
273 n
= snprintf(buf
, sizeof(buf
), ",%1.4f", robot
->from_millimeters(robot
->actuators
[i
]->get_current_position()));
274 if(n
> sizeof(buf
)) n
= sizeof(buf
);
280 str
.append(",MPos:").append(buf
, n
);
283 // work space position
284 Robot::wcs_t pos
= robot
->mcs2wcs(mpos
);
285 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
)));
286 if(n
> sizeof(buf
)) n
= sizeof(buf
);
287 if(new_status_format
) {
288 str
.append("|WPos:").append(buf
, n
);
290 str
.append(",WPos:").append(buf
, n
);
293 if(new_status_format
) {
294 float fr
= robot
->from_millimeters(robot
->get_feed_rate());
295 n
= snprintf(buf
, sizeof(buf
), "|F:%1.4f", fr
);
296 if(n
> sizeof(buf
)) n
= sizeof(buf
);
302 // if not grbl mode get temperatures
303 if(new_status_format
&& !is_grbl_mode()) {
304 struct pad_temperature temp
;
305 // scan all temperature controls
306 std::vector
<struct pad_temperature
> controllers
;
307 bool ok
= PublicData::get_value(temperature_control_checksum
, poll_controls_checksum
, &controllers
);
310 for (auto &c
: controllers
) {
311 size_t n
= snprintf(buf
, sizeof(buf
), "|%s:%1.1f,%1.1f", c
.designator
.c_str(), c
.current_temperature
, c
.target_temperature
);
312 if(n
> sizeof(buf
)) n
= sizeof(buf
);
322 // Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
323 void Kernel::add_module(Module
* module
)
325 module
->on_module_loaded();
328 // Adds a hook for a given module and event
329 void Kernel::register_for_event(_EVENT_ENUM id_event
, Module
*mod
)
331 this->hooks
[id_event
].push_back(mod
);
334 // Call a specific event with an argument
335 void Kernel::call_event(_EVENT_ENUM id_event
, void * argument
)
337 bool was_idle
= true;
338 if(id_event
== ON_HALT
) {
339 this->halted
= (argument
== nullptr);
340 was_idle
= conveyor
->is_idle(); // see if we were doing anything like printing
343 // send to all registered modules
344 for (auto m
: hooks
[id_event
]) {
345 (m
->*kernel_callback_functions
[id_event
])(argument
);
348 if(id_event
== ON_HALT
) {
349 if(!this->halted
|| !was_idle
) {
350 // if we were running and this is a HALT
351 // or if we are clearing the halt with $X or M999
352 // fix up the current positions in case they got out of sync due to backed up commands
353 this->robot
->reset_position_from_current_actuator_position();
358 // These are used by tests to test for various things. basically mocks
359 bool Kernel::kernel_has_event(_EVENT_ENUM id_event
, Module
*mod
)
361 for (auto m
: hooks
[id_event
]) {
362 if(m
== mod
) return true;
367 void Kernel::unregister_for_event(_EVENT_ENUM id_event
, Module
*mod
)
369 for (auto i
= hooks
[id_event
].begin(); i
!= hooks
[id_event
].end(); ++i
) {
371 hooks
[id_event
].erase(i
);