Merge branch 'feature/e-endstop' into merge-abc-with-homing
[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"
56ce2b5a 31
2317ea09
JM
32#include "platform_memory.h"
33
a39e1557 34#include <malloc.h>
56ce2b5a 35#include <array>
dcc91612 36#include <string>
ded56b35 37
d4f93cf4
AG
38#define baud_rate_setting_checksum CHECKSUM("baud_rate")
39#define uart0_checksum CHECKSUM("uart0")
4cff3ded 40
38bf9a1c
JM
41#define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency")
42#define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse")
73706276 43#define disable_leds_checksum CHECKSUM("leds_disable")
6c0193b3 44#define grbl_mode_checksum CHECKSUM("grbl_mode")
7d04f42d 45#define ok_per_line_checksum CHECKSUM("ok_per_line")
38bf9a1c 46
879341be
JM
47Kernel* Kernel::instance;
48
7b49793d 49// The kernel is the central point in Smoothie : it stores modules, and handles event calls
4cff3ded 50Kernel::Kernel(){
73706276 51 halted= false;
17aca3f2 52 feed_hold= false;
73706276 53
879341be 54 instance= this; // setup the Singleton instance of the kernel
d4ee6ee2 55
2ee2ad19 56 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
36463641 57 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
2ee2ad19 58 this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE);
577414f6
JM
59
60 // Config next, but does not load cache yet
a157d099 61 this->config = new Config();
a39e1557 62
bcb96295 63 // Pre-load the config cache, do after setting up serial so we can report errors to serial
577414f6
JM
64 this->config->config_cache_load();
65
2ee2ad19 66 // now config is loaded we can do normal setup for serial based on config
577414f6 67 delete this->serial;
9326040b 68 this->serial= NULL;
a39e1557 69
a157d099 70 this->streams = new StreamOutputPool();
d4ee6ee2 71
75f4581c
JM
72 this->current_path = "/";
73
b2b0b56d 74 // Configure UART depending on MRI config
bb3b7f09 75 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
b2b0b56d 76 NVIC_SetPriorityGrouping(0);
f5f88509 77
2ee2ad19 78#if MRI_ENABLE != 0
bb3b7f09
SK
79 switch( __mriPlatform_CommUartIndex() ) {
80 case 0:
b07867dd 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());
bb3b7f09
SK
82 break;
83 case 1:
b07867dd 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());
bb3b7f09
SK
85 break;
86 case 2:
b07867dd 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());
bb3b7f09
SK
88 break;
89 case 3:
b07867dd 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());
bb3b7f09 91 break;
4c4a372a 92 }
2ee2ad19
JM
93#endif
94 // default
9326040b 95 if(this->serial == NULL) {
b07867dd 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());
9326040b 97 }
f5f88509 98
73706276
JM
99 //some boards don't have leds.. TOO BAD!
100 this->use_leds= !this->config->value( disable_leds_checksum )->by_default(false)->as_bool();
12e6e7b3
JM
101
102 #ifdef CNC
103 this->grbl_mode= this->config->value( grbl_mode_checksum )->by_default(true)->as_bool();
104 #else
6c0193b3 105 this->grbl_mode= this->config->value( grbl_mode_checksum )->by_default(false)->as_bool();
12e6e7b3
JM
106 #endif
107
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
7d04f42d 109 this->ok_per_line= this->config->value( ok_per_line_checksum )->by_default(true)->as_bool();
73706276 110
4cff3ded 111 this->add_module( this->serial );
38d375e7 112
df27a6a3 113 // HAL stuff
a157d099 114 add_module( this->slow_ticker = new SlowTicker());
3eadcfee 115
a157d099 116 this->step_ticker = new StepTicker();
8a9f9313 117 this->adc = new Adc();
650ed0a8 118
d337942a 119 // TODO : These should go into platform-specific files
df27a6a3 120 // LPC17xx-specific
813727fb 121 NVIC_SetPriorityGrouping(0);
df27a6a3
MM
122 NVIC_SetPriority(TIMER0_IRQn, 2);
123 NVIC_SetPriority(TIMER1_IRQn, 1);
16220afe
JM
124 NVIC_SetPriority(TIMER2_IRQn, 4);
125 NVIC_SetPriority(PendSV_IRQn, 3);
feb204be 126
b2b0b56d 127 // Set other priorities lower than the timers
16220afe
JM
128 NVIC_SetPriority(ADC_IRQn, 5);
129 NVIC_SetPriority(USB_IRQn, 5);
f5f88509 130
df27a6a3 131 // If MRI is enabled
b2b0b56d 132 if( MRI_ENABLE ){
16220afe
JM
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); }
b2b0b56d 137 }else{
16220afe
JM
138 NVIC_SetPriority(UART0_IRQn, 5);
139 NVIC_SetPriority(UART1_IRQn, 5);
140 NVIC_SetPriority(UART2_IRQn, 5);
141 NVIC_SetPriority(UART3_IRQn, 5);
b2b0b56d
AW
142 }
143
feb204be 144 // Configure the step ticker
a157d099 145 this->base_stepping_frequency = this->config->value(base_stepping_frequency_checksum)->by_default(100000)->as_number();
12ae3902 146 float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum)->by_default(1)->as_number();
f5f88509 147
12ae3902 148 // Configure the step ticker
dd0a7cfa 149 this->step_ticker->set_frequency( this->base_stepping_frequency );
12ae3902 150 this->step_ticker->set_unstep_time( microseconds_per_step_pulse );
3c132bd0 151
df27a6a3 152 // Core modules
6c0d8cf7 153 this->add_module( this->conveyor = new Conveyor() );
40843ebc 154 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
81b547a1 155 this->add_module( this->robot = new Robot() );
3e54c9fc 156 this->add_module( this->simpleshell = new SimpleShell() );
558e170c 157
8a9f9313
JM
158 this->planner = new Planner();
159 this->configurator = new Configurator();
4cff3ded
AW
160}
161
dcc91612
JM
162// return a GRBL-like query string for serial ?
163std::string Kernel::get_query_string()
164{
165 std::string str;
07186543
JM
166 bool homing;
167 bool ok = PublicData::get_value(endstops_checksum, get_homing_status_checksum, 0, &homing);
168 if(!ok) homing= false;
31c6c2c2 169 bool running= false;
07186543 170
dcc91612
JM
171 str.append("<");
172 if(halted) {
173 str.append("Alarm,");
07186543
JM
174 }else if(homing) {
175 str.append("Home,");
17aca3f2
JM
176 }else if(feed_hold) {
177 str.append("Hold,");
b5708347 178 }else if(this->conveyor->is_idle()) {
dcc91612
JM
179 str.append("Idle,");
180 }else{
31c6c2c2 181 running= true;
dcc91612
JM
182 str.append("Run,");
183 }
184
31c6c2c2 185 if(running) {
31c6c2c2 186 float mpos[3];
fdfa00d2 187 robot->get_current_machine_position(mpos);
98728d3f
JM
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
31c6c2c2
JM
190
191 char buf[128];
192 // machine position
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);
195
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);
200 str.append(">\r\n");
201
202 }else{
203 // return the last milestone if idle
204 char buf[128];
205 // machine position
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);
209
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);
214 str.append(">\r\n");
215
216 }
dcc91612
JM
217 return str;
218}
219
cb2e6bc6 220// Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
4cff3ded 221void Kernel::add_module(Module* module){
4cff3ded
AW
222 module->on_module_loaded();
223}
224
93694d6b 225// Adds a hook for a given module and event
96f67b65
JM
226void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){
227 this->hooks[id_event].push_back(mod);
4cff3ded
AW
228}
229
93ea6adb
JM
230// Call a specific event with an argument
231void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
785da581 232 bool was_idle= true;
73706276
JM
233 if(id_event == ON_HALT) {
234 this->halted= (argument == nullptr);
7baa50df 235 was_idle= conveyor->is_idle(); // see if we were doing anything like printing
73706276 236 }
785da581
JM
237
238 // send to all registered modules
96f67b65 239 for (auto m : hooks[id_event]) {
93ea6adb 240 (m->*kernel_callback_functions[id_event])(argument);
4cff3ded 241 }
785da581 242
c7f4902c 243 if(id_event == ON_HALT && this->halted && !was_idle) {
785da581
JM
244 // we need to try to correct current positions if we were running
245 this->robot->reset_position_from_current_actuator_position();
246 }
4cff3ded
AW
247}
248
93ea6adb
JM
249// These are used by tests to test for various things. basically mocks
250bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod)
251{
96f67b65 252 for (auto m : hooks[id_event]) {
93ea6adb 253 if(m == mod) return true;
4cff3ded 254 }
93ea6adb 255 return false;
4cff3ded 256}
93ea6adb
JM
257
258void Kernel::unregister_for_event(_EVENT_ENUM id_event, Module *mod)
259{
260 for (auto i = hooks[id_event].begin(); i != hooks[id_event].end(); ++i) {
261 if(*i == mod) {
262 hooks[id_event].erase(i);
263 return;
264 }
265 }
266}
267