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