Merge pull request #1307 from wolfmanjm/upstreamedge
[clinton/Smoothieware.git] / src / libs / Kernel.cpp
CommitLineData
df27a6a3 1/*
4cff3ded
AW
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
4cff3ded
AW
6*/
7
4cff3ded
AW
8#include "libs/Kernel.h"
9#include "libs/Module.h"
10#include "libs/Config.h"
4cff3ded 11#include "libs/nuts_bolts.h"
ded56b35 12#include "libs/SlowTicker.h"
3c132bd0 13#include "libs/Adc.h"
38d375e7 14#include "libs/StreamOutputPool.h"
4c4a372a 15#include <mri.h>
7af0714f 16#include "checksumm.h"
8d54c34c 17#include "ConfigValue.h"
4cff3ded 18
61134a65
JM
19#include "libs/StepTicker.h"
20#include "libs/PublicData.h"
4cff3ded
AW
21#include "modules/communication/SerialConsole.h"
22#include "modules/communication/GcodeDispatch.h"
23#include "modules/robot/Planner.h"
24#include "modules/robot/Robot.h"
3fceb8eb 25#include "modules/robot/Conveyor.h"
dcc91612 26#include "StepperMotor.h"
a395f085 27#include "BaseSolution.h"
07186543 28#include "EndstopsPublicAccess.h"
3e54c9fc
JM
29#include "Configurator.h"
30#include "SimpleShell.h"
14557969 31#include "TemperatureControlPublicAccess.h"
56ce2b5a 32
ee1da330
JM
33#ifndef NO_TOOLS_LASER
34#include "Laser.h"
35#endif
36
2317ea09
JM
37#include "platform_memory.h"
38
a39e1557 39#include <malloc.h>
56ce2b5a 40#include <array>
dcc91612 41#include <string>
ded56b35 42
ee1da330 43#define laser_checksum CHECKSUM("laser")
d4f93cf4
AG
44#define baud_rate_setting_checksum CHECKSUM("baud_rate")
45#define uart0_checksum CHECKSUM("uart0")
4cff3ded 46
38bf9a1c
JM
47#define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency")
48#define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse")
73706276 49#define disable_leds_checksum CHECKSUM("leds_disable")
6c0193b3 50#define grbl_mode_checksum CHECKSUM("grbl_mode")
4b7cd602 51#define feed_hold_enable_checksum CHECKSUM("enable_feed_hold")
7d04f42d 52#define ok_per_line_checksum CHECKSUM("ok_per_line")
ee1da330 53#define new_status_format_checksum CHECKSUM("new_status_format")
38bf9a1c 54
879341be
JM
55Kernel* Kernel::instance;
56
7b49793d 57// The kernel is the central point in Smoothie : it stores modules, and handles event calls
7e0ca38f
JM
58Kernel::Kernel()
59{
60 halted = false;
61 feed_hold = false;
62 enable_feed_hold = false;
73706276 63
7e0ca38f 64 instance = this; // setup the Singleton instance of the kernel
d4ee6ee2 65
2ee2ad19 66 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
7e0ca38f 67 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
2ee2ad19 68 this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE);
577414f6
JM
69
70 // Config next, but does not load cache yet
a157d099 71 this->config = new Config();
a39e1557 72
bcb96295 73 // Pre-load the config cache, do after setting up serial so we can report errors to serial
577414f6
JM
74 this->config->config_cache_load();
75
2ee2ad19 76 // now config is loaded we can do normal setup for serial based on config
577414f6 77 delete this->serial;
7e0ca38f 78 this->serial = NULL;
a39e1557 79
a157d099 80 this->streams = new StreamOutputPool();
d4ee6ee2 81
75f4581c
JM
82 this->current_path = "/";
83
b2b0b56d 84 // Configure UART depending on MRI config
bb3b7f09 85 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
b2b0b56d 86 NVIC_SetPriorityGrouping(0);
f5f88509 87
2ee2ad19 88#if MRI_ENABLE != 0
bb3b7f09
SK
89 switch( __mriPlatform_CommUartIndex() ) {
90 case 0:
7e0ca38f 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());
bb3b7f09
SK
92 break;
93 case 1:
7e0ca38f 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());
bb3b7f09
SK
95 break;
96 case 2:
7e0ca38f 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());
bb3b7f09
SK
98 break;
99 case 3:
7e0ca38f 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());
bb3b7f09 101 break;
4c4a372a 102 }
2ee2ad19
JM
103#endif
104 // default
9326040b 105 if(this->serial == NULL) {
7e0ca38f 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());
9326040b 107 }
f5f88509 108
73706276 109 //some boards don't have leds.. TOO BAD!
7e0ca38f 110 this->use_leds = !this->config->value( disable_leds_checksum )->by_default(false)->as_bool();
12e6e7b3 111
7e0ca38f
JM
112#ifdef CNC
113 this->grbl_mode = this->config->value( grbl_mode_checksum )->by_default(true)->as_bool();
114#else
115 this->grbl_mode = this->config->value( grbl_mode_checksum )->by_default(false)->as_bool();
116#endif
12e6e7b3 117
7e0ca38f 118 this->enable_feed_hold = this->config->value( feed_hold_enable_checksum )->by_default(this->grbl_mode)->as_bool();
4b7cd602 119
c9d966bc 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
7e0ca38f 121 this->ok_per_line = this->config->value( ok_per_line_checksum )->by_default(true)->as_bool();
73706276 122
a7520bd5 123 this->new_status_format = this->config->value( new_status_format_checksum )->by_default(true)->as_bool();
ee1da330 124
4cff3ded 125 this->add_module( this->serial );
38d375e7 126
df27a6a3 127 // HAL stuff
a157d099 128 add_module( this->slow_ticker = new SlowTicker());
3eadcfee 129
a157d099 130 this->step_ticker = new StepTicker();
8a9f9313 131 this->adc = new Adc();
650ed0a8 132
d337942a 133 // TODO : These should go into platform-specific files
df27a6a3 134 // LPC17xx-specific
813727fb 135 NVIC_SetPriorityGrouping(0);
df27a6a3
MM
136 NVIC_SetPriority(TIMER0_IRQn, 2);
137 NVIC_SetPriority(TIMER1_IRQn, 1);
16220afe
JM
138 NVIC_SetPriority(TIMER2_IRQn, 4);
139 NVIC_SetPriority(PendSV_IRQn, 3);
feb204be 140
b2b0b56d 141 // Set other priorities lower than the timers
16220afe
JM
142 NVIC_SetPriority(ADC_IRQn, 5);
143 NVIC_SetPriority(USB_IRQn, 5);
f5f88509 144
df27a6a3 145 // If MRI is enabled
7e0ca38f
JM
146 if( MRI_ENABLE ) {
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); }
151 } else {
16220afe
JM
152 NVIC_SetPriority(UART0_IRQn, 5);
153 NVIC_SetPriority(UART1_IRQn, 5);
154 NVIC_SetPriority(UART2_IRQn, 5);
155 NVIC_SetPriority(UART3_IRQn, 5);
b2b0b56d
AW
156 }
157
feb204be 158 // Configure the step ticker
a157d099 159 this->base_stepping_frequency = this->config->value(base_stepping_frequency_checksum)->by_default(100000)->as_number();
12ae3902 160 float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum)->by_default(1)->as_number();
f5f88509 161
12ae3902 162 // Configure the step ticker
dd0a7cfa 163 this->step_ticker->set_frequency( this->base_stepping_frequency );
12ae3902 164 this->step_ticker->set_unstep_time( microseconds_per_step_pulse );
3c132bd0 165
df27a6a3 166 // Core modules
6c0d8cf7 167 this->add_module( this->conveyor = new Conveyor() );
40843ebc 168 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
81b547a1 169 this->add_module( this->robot = new Robot() );
3e54c9fc 170 this->add_module( this->simpleshell = new SimpleShell() );
558e170c 171
8a9f9313
JM
172 this->planner = new Planner();
173 this->configurator = new Configurator();
4cff3ded
AW
174}
175
dcc91612
JM
176// return a GRBL-like query string for serial ?
177std::string Kernel::get_query_string()
178{
179 std::string str;
07186543
JM
180 bool homing;
181 bool ok = PublicData::get_value(endstops_checksum, get_homing_status_checksum, 0, &homing);
7e0ca38f
JM
182 if(!ok) homing = false;
183 bool running = false;
07186543 184
dcc91612
JM
185 str.append("<");
186 if(halted) {
ee1da330 187 str.append("Alarm");
7e0ca38f
JM
188 } else if(homing) {
189 running = true;
ee1da330 190 str.append("Home");
7e0ca38f 191 } else if(feed_hold) {
ee1da330 192 str.append("Hold");
7e0ca38f 193 } else if(this->conveyor->is_idle()) {
ee1da330 194 str.append("Idle");
7e0ca38f
JM
195 } else {
196 running = true;
ee1da330 197 str.append("Run");
dcc91612
JM
198 }
199
31c6c2c2 200 if(running) {
31c6c2c2 201 float mpos[3];
fdfa00d2 202 robot->get_current_machine_position(mpos);
98728d3f
JM
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
31c6c2c2
JM
205
206 char buf[128];
207 // machine position
255219dc 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]));
5fe7262c 209 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
210
211 if(new_status_format) {
212 str.append("|MPos:").append(buf, n);
31c6c2c2 213
7e0ca38f 214#if MAX_ROBOT_ACTUATORS > 3
ee1da330
JM
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
255219dc 218 n = snprintf(buf, sizeof(buf), ",%1.4f", robot->from_millimeters(robot->actuators[i]->get_current_position()));
5fe7262c 219 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
220 str.append(buf, n);
221 }
7e0ca38f
JM
222#endif
223
ee1da330
JM
224 }else{
225 str.append(",MPos:").append(buf, n);
226 }
227
31c6c2c2 228 // work space position
7e0ca38f 229 Robot::wcs_t pos = robot->mcs2wcs(mpos);
255219dc 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)));
5fe7262c 231 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
232
233 if(new_status_format) {
234 str.append("|WPos:").append(buf, n);
235 // current feedrate
236 float fr= robot->from_millimeters(conveyor->get_current_feedrate()*60.0F);
255219dc 237 n = snprintf(buf, sizeof(buf), "|F:%1.4f", fr);
5fe7262c 238 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
239 str.append(buf, n);
240 float sr= robot->get_s_value();
255219dc 241 n = snprintf(buf, sizeof(buf), "|S:%1.4f", sr);
5fe7262c 242 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
243 str.append(buf, n);
244
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();
255219dc 250 n = snprintf(buf, sizeof(buf), "|L:%1.4f", lp);
5fe7262c 251 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
252 str.append(buf, n);
253 }
254 #endif
255
256 }else{
257 str.append(",WPos:").append(buf, n);
258 }
259
7e0ca38f 260 } else {
31c6c2c2
JM
261 // return the last milestone if idle
262 char buf[128];
263 // machine position
7e0ca38f 264 Robot::wcs_t mpos = robot->get_axis_position();
255219dc 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)));
5fe7262c 266 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
267 if(new_status_format) {
268 str.append("|MPos:").append(buf, n);
7e0ca38f 269#if MAX_ROBOT_ACTUATORS > 3
ee1da330
JM
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
255219dc 273 n = snprintf(buf, sizeof(buf), ",%1.4f", robot->from_millimeters(robot->actuators[i]->get_current_position()));
5fe7262c 274 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
275 str.append(buf, n);
276 }
7e0ca38f 277#endif
31c6c2c2 278
ee1da330
JM
279 }else{
280 str.append(",MPos:").append(buf, n);
281 }
282
31c6c2c2 283 // work space position
7e0ca38f 284 Robot::wcs_t pos = robot->mcs2wcs(mpos);
255219dc 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)));
5fe7262c 286 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
287 if(new_status_format) {
288 str.append("|WPos:").append(buf, n);
289 }else{
290 str.append(",WPos:").append(buf, n);
291 }
292
293 if(new_status_format) {
294 float fr= robot->from_millimeters(robot->get_feed_rate());
255219dc 295 n = snprintf(buf, sizeof(buf), "|F:%1.4f", fr);
5fe7262c 296 if(n > sizeof(buf)) n= sizeof(buf);
ee1da330
JM
297 str.append(buf, n);
298 }
31c6c2c2
JM
299
300 }
14557969
JM
301
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);
308 if (ok) {
309 char buf[32];
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);
313 str.append(buf, n);
314 }
315 }
316 }
317
318 str.append(">\n");
dcc91612
JM
319 return str;
320}
321
cb2e6bc6 322// Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
7e0ca38f
JM
323void Kernel::add_module(Module* module)
324{
4cff3ded
AW
325 module->on_module_loaded();
326}
327
93694d6b 328// Adds a hook for a given module and event
7e0ca38f
JM
329void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod)
330{
96f67b65 331 this->hooks[id_event].push_back(mod);
4cff3ded
AW
332}
333
93ea6adb 334// Call a specific event with an argument
7e0ca38f
JM
335void Kernel::call_event(_EVENT_ENUM id_event, void * argument)
336{
337 bool was_idle = true;
73706276 338 if(id_event == ON_HALT) {
7e0ca38f
JM
339 this->halted = (argument == nullptr);
340 was_idle = conveyor->is_idle(); // see if we were doing anything like printing
73706276 341 }
785da581
JM
342
343 // send to all registered modules
96f67b65 344 for (auto m : hooks[id_event]) {
93ea6adb 345 (m->*kernel_callback_functions[id_event])(argument);
4cff3ded 346 }
785da581 347
92485600
JM
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();
354 }
785da581 355 }
4cff3ded
AW
356}
357
93ea6adb
JM
358// These are used by tests to test for various things. basically mocks
359bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod)
360{
96f67b65 361 for (auto m : hooks[id_event]) {
93ea6adb 362 if(m == mod) return true;
4cff3ded 363 }
93ea6adb 364 return false;
4cff3ded 365}
93ea6adb
JM
366
367void Kernel::unregister_for_event(_EVENT_ENUM id_event, Module *mod)
368{
369 for (auto i = hooks[id_event].begin(); i != hooks[id_event].end(); ++i) {
370 if(*i == mod) {
371 hooks[id_event].erase(i);
372 return;
373 }
374 }
375}
376