Merge pull request #1270 from wolfmanjm/upstreamedge
[clinton/Smoothieware.git] / src / libs / Kernel.cpp
1 /*
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/>.
6 */
7
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"
13 #include "libs/Adc.h"
14 #include "libs/StreamOutputPool.h"
15 #include <mri.h>
16 #include "checksumm.h"
17 #include "ConfigValue.h"
18
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
32 #ifndef NO_TOOLS_LASER
33 #include "Laser.h"
34 #endif
35
36 #include "platform_memory.h"
37
38 #include <malloc.h>
39 #include <array>
40 #include <string>
41
42 #define laser_checksum CHECKSUM("laser")
43 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
44 #define uart0_checksum CHECKSUM("uart0")
45
46 #define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency")
47 #define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse")
48 #define disable_leds_checksum CHECKSUM("leds_disable")
49 #define grbl_mode_checksum CHECKSUM("grbl_mode")
50 #define feed_hold_enable_checksum CHECKSUM("enable_feed_hold")
51 #define ok_per_line_checksum CHECKSUM("ok_per_line")
52 #define new_status_format_checksum CHECKSUM("new_status_format")
53
54 Kernel* Kernel::instance;
55
56 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
57 Kernel::Kernel()
58 {
59 halted = false;
60 feed_hold = false;
61 enable_feed_hold = false;
62
63 instance = this; // setup the Singleton instance of the kernel
64
65 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
66 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
67 this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE);
68
69 // Config next, but does not load cache yet
70 this->config = new Config();
71
72 // Pre-load the config cache, do after setting up serial so we can report errors to serial
73 this->config->config_cache_load();
74
75 // now config is loaded we can do normal setup for serial based on config
76 delete this->serial;
77 this->serial = NULL;
78
79 this->streams = new StreamOutputPool();
80
81 this->current_path = "/";
82
83 // Configure UART depending on MRI config
84 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
85 NVIC_SetPriorityGrouping(0);
86
87 #if MRI_ENABLE != 0
88 switch( __mriPlatform_CommUartIndex() ) {
89 case 0:
90 this->serial = new(AHB0) SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum, baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
91 break;
92 case 1:
93 this->serial = new(AHB0) SerialConsole( p13, p14, this->config->value(uart0_checksum, baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
94 break;
95 case 2:
96 this->serial = new(AHB0) SerialConsole( p28, p27, this->config->value(uart0_checksum, baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
97 break;
98 case 3:
99 this->serial = new(AHB0) SerialConsole( p9, p10, this->config->value(uart0_checksum, baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
100 break;
101 }
102 #endif
103 // default
104 if(this->serial == NULL) {
105 this->serial = new(AHB0) SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum, baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
106 }
107
108 //some boards don't have leds.. TOO BAD!
109 this->use_leds = !this->config->value( disable_leds_checksum )->by_default(false)->as_bool();
110
111 #ifdef CNC
112 this->grbl_mode = this->config->value( grbl_mode_checksum )->by_default(true)->as_bool();
113 #else
114 this->grbl_mode = this->config->value( grbl_mode_checksum )->by_default(false)->as_bool();
115 #endif
116
117 this->enable_feed_hold = this->config->value( feed_hold_enable_checksum )->by_default(this->grbl_mode)->as_bool();
118
119 // 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
120 this->ok_per_line = this->config->value( ok_per_line_checksum )->by_default(true)->as_bool();
121
122 this->new_status_format = this->config->value( new_status_format_checksum )->by_default(true)->as_bool();
123
124 this->add_module( this->serial );
125
126 // HAL stuff
127 add_module( this->slow_ticker = new SlowTicker());
128
129 this->step_ticker = new StepTicker();
130 this->adc = new Adc();
131
132 // TODO : These should go into platform-specific files
133 // LPC17xx-specific
134 NVIC_SetPriorityGrouping(0);
135 NVIC_SetPriority(TIMER0_IRQn, 2);
136 NVIC_SetPriority(TIMER1_IRQn, 1);
137 NVIC_SetPriority(TIMER2_IRQn, 4);
138 NVIC_SetPriority(PendSV_IRQn, 3);
139
140 // Set other priorities lower than the timers
141 NVIC_SetPriority(ADC_IRQn, 5);
142 NVIC_SetPriority(USB_IRQn, 5);
143
144 // If MRI is enabled
145 if( MRI_ENABLE ) {
146 if( NVIC_GetPriority(UART0_IRQn) > 0 ) { NVIC_SetPriority(UART0_IRQn, 5); }
147 if( NVIC_GetPriority(UART1_IRQn) > 0 ) { NVIC_SetPriority(UART1_IRQn, 5); }
148 if( NVIC_GetPriority(UART2_IRQn) > 0 ) { NVIC_SetPriority(UART2_IRQn, 5); }
149 if( NVIC_GetPriority(UART3_IRQn) > 0 ) { NVIC_SetPriority(UART3_IRQn, 5); }
150 } else {
151 NVIC_SetPriority(UART0_IRQn, 5);
152 NVIC_SetPriority(UART1_IRQn, 5);
153 NVIC_SetPriority(UART2_IRQn, 5);
154 NVIC_SetPriority(UART3_IRQn, 5);
155 }
156
157 // Configure the step ticker
158 this->base_stepping_frequency = this->config->value(base_stepping_frequency_checksum)->by_default(100000)->as_number();
159 float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum)->by_default(1)->as_number();
160
161 // Configure the step ticker
162 this->step_ticker->set_frequency( this->base_stepping_frequency );
163 this->step_ticker->set_unstep_time( microseconds_per_step_pulse );
164
165 // Core modules
166 this->add_module( this->conveyor = new Conveyor() );
167 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
168 this->add_module( this->robot = new Robot() );
169 this->add_module( this->simpleshell = new SimpleShell() );
170
171 this->planner = new Planner();
172 this->configurator = new Configurator();
173 }
174
175 // return a GRBL-like query string for serial ?
176 std::string Kernel::get_query_string()
177 {
178 std::string str;
179 bool homing;
180 bool ok = PublicData::get_value(endstops_checksum, get_homing_status_checksum, 0, &homing);
181 if(!ok) homing = false;
182 bool running = false;
183
184 str.append("<");
185 if(halted) {
186 str.append("Alarm");
187 } else if(homing) {
188 running = true;
189 str.append("Home");
190 } else if(feed_hold) {
191 str.append("Hold");
192 } else if(this->conveyor->is_idle()) {
193 str.append("Idle");
194 } else {
195 running = true;
196 str.append("Run");
197 }
198
199 if(running) {
200 float mpos[3];
201 robot->get_current_machine_position(mpos);
202 // current_position/mpos includes the compensation transform so we need to get the inverse to get actual position
203 if(robot->compensationTransform) robot->compensationTransform(mpos, true); // get inverse compensation transform
204
205 char buf[128];
206 // machine position
207 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]));
208 if(n > sizeof(buf)) n= sizeof(buf);
209
210 if(new_status_format) {
211 str.append("|MPos:").append(buf, n);
212
213 #if MAX_ROBOT_ACTUATORS > 3
214 // deal with the ABC axis (E will be A)
215 for (int i = A_AXIS; i < robot->get_number_registered_motors(); ++i) {
216 // current actuator position
217 n = snprintf(buf, sizeof(buf), ",%1.4f", robot->from_millimeters(robot->actuators[i]->get_current_position()));
218 if(n > sizeof(buf)) n= sizeof(buf);
219 str.append(buf, n);
220 }
221 #endif
222
223 }else{
224 str.append(",MPos:").append(buf, n);
225 }
226
227 // work space position
228 Robot::wcs_t pos = robot->mcs2wcs(mpos);
229 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)));
230 if(n > sizeof(buf)) n= sizeof(buf);
231
232 if(new_status_format) {
233 str.append("|WPos:").append(buf, n);
234 // current feedrate
235 float fr= robot->from_millimeters(conveyor->get_current_feedrate()*60.0F);
236 n = snprintf(buf, sizeof(buf), "|F:%1.4f", fr);
237 if(n > sizeof(buf)) n= sizeof(buf);
238 str.append(buf, n);
239 float sr= robot->get_s_value();
240 n = snprintf(buf, sizeof(buf), "|S:%1.4f", sr);
241 if(n > sizeof(buf)) n= sizeof(buf);
242 str.append(buf, n);
243
244 // current Laser power
245 #ifndef NO_TOOLS_LASER
246 Laser *plaser= nullptr;
247 if(PublicData::get_value(laser_checksum, (void *)&plaser) && plaser != nullptr) {
248 float lp= plaser->get_current_power();
249 n = snprintf(buf, sizeof(buf), "|L:%1.4f", lp);
250 if(n > sizeof(buf)) n= sizeof(buf);
251 str.append(buf, n);
252 }
253 #endif
254
255 }else{
256 str.append(",WPos:").append(buf, n);
257 }
258
259 str.append(">\r\n");
260
261 } else {
262 // return the last milestone if idle
263 char buf[128];
264 // machine position
265 Robot::wcs_t mpos = robot->get_axis_position();
266 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)));
267 if(n > sizeof(buf)) n= sizeof(buf);
268 if(new_status_format) {
269 str.append("|MPos:").append(buf, n);
270 #if MAX_ROBOT_ACTUATORS > 3
271 // deal with the ABC axis (E will be A)
272 for (int i = A_AXIS; i < robot->get_number_registered_motors(); ++i) {
273 // current actuator position
274 n = snprintf(buf, sizeof(buf), ",%1.4f", robot->from_millimeters(robot->actuators[i]->get_current_position()));
275 if(n > sizeof(buf)) n= sizeof(buf);
276 str.append(buf, n);
277 }
278 #endif
279
280 }else{
281 str.append(",MPos:").append(buf, n);
282 }
283
284 // work space position
285 Robot::wcs_t pos = robot->mcs2wcs(mpos);
286 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)));
287 if(n > sizeof(buf)) n= sizeof(buf);
288 if(new_status_format) {
289 str.append("|WPos:").append(buf, n);
290 }else{
291 str.append(",WPos:").append(buf, n);
292 }
293
294 if(new_status_format) {
295 float fr= robot->from_millimeters(robot->get_feed_rate());
296 n = snprintf(buf, sizeof(buf), "|F:%1.4f", fr);
297 if(n > sizeof(buf)) n= sizeof(buf);
298 str.append(buf, n);
299 }
300
301 str.append(">\r\n");
302 }
303 return str;
304 }
305
306 // Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
307 void Kernel::add_module(Module* module)
308 {
309 module->on_module_loaded();
310 }
311
312 // Adds a hook for a given module and event
313 void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod)
314 {
315 this->hooks[id_event].push_back(mod);
316 }
317
318 // Call a specific event with an argument
319 void Kernel::call_event(_EVENT_ENUM id_event, void * argument)
320 {
321 bool was_idle = true;
322 if(id_event == ON_HALT) {
323 this->halted = (argument == nullptr);
324 was_idle = conveyor->is_idle(); // see if we were doing anything like printing
325 }
326
327 // send to all registered modules
328 for (auto m : hooks[id_event]) {
329 (m->*kernel_callback_functions[id_event])(argument);
330 }
331
332 if(id_event == ON_HALT) {
333 if(!this->halted || !was_idle) {
334 // if we were running and this is a HALT
335 // or if we are clearing the halt with $X or M999
336 // fix up the current positions in case they got out of sync due to backed up commands
337 this->robot->reset_position_from_current_actuator_position();
338 }
339 }
340 }
341
342 // These are used by tests to test for various things. basically mocks
343 bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod)
344 {
345 for (auto m : hooks[id_event]) {
346 if(m == mod) return true;
347 }
348 return false;
349 }
350
351 void Kernel::unregister_for_event(_EVENT_ENUM id_event, Module *mod)
352 {
353 for (auto i = hooks[id_event].begin(); i != hooks[id_event].end(); ++i) {
354 if(*i == mod) {
355 hooks[id_event].erase(i);
356 return;
357 }
358 }
359 }
360