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