Merge branch 'fix/when-slow-ticker-is-started' into feature/correct-rotarydelta-homing
[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/Stepper.h"
26 #include "modules/robot/Conveyor.h"
27 #include "StepperMotor.h"
28 #include "BaseSolution.h"
29 #include "EndstopsPublicAccess.h"
30 #include "Configurator.h"
31 #include "SimpleShell.h"
32
33 #include "platform_memory.h"
34
35 #include <malloc.h>
36 #include <array>
37 #include <string>
38
39 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
40 #define uart0_checksum CHECKSUM("uart0")
41
42 #define base_stepping_frequency_checksum CHECKSUM("base_stepping_frequency")
43 #define microseconds_per_step_pulse_checksum CHECKSUM("microseconds_per_step_pulse")
44 #define acceleration_ticks_per_second_checksum CHECKSUM("acceleration_ticks_per_second")
45 #define disable_leds_checksum CHECKSUM("leds_disable")
46 #define grbl_mode_checksum CHECKSUM("grbl_mode")
47 #define ok_per_line_checksum CHECKSUM("ok_per_line")
48
49 Kernel* Kernel::instance;
50
51 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
52 Kernel::Kernel(){
53 halted= false;
54 feed_hold= false;
55
56 instance= this; // setup the Singleton instance of the kernel
57
58 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
59 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
60 this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE);
61
62 // Config next, but does not load cache yet
63 this->config = new Config();
64
65 // Pre-load the config cache, do after setting up serial so we can report errors to serial
66 this->config->config_cache_load();
67
68 // now config is loaded we can do normal setup for serial based on config
69 delete this->serial;
70 this->serial= NULL;
71
72 this->streams = new StreamOutputPool();
73
74 this->current_path = "/";
75
76 // Configure UART depending on MRI config
77 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
78 NVIC_SetPriorityGrouping(0);
79
80 #if MRI_ENABLE != 0
81 switch( __mriPlatform_CommUartIndex() ) {
82 case 0:
83 this->serial = new(AHB0) SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
84 break;
85 case 1:
86 this->serial = new(AHB0) SerialConsole( p13, p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
87 break;
88 case 2:
89 this->serial = new(AHB0) SerialConsole( p28, p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
90 break;
91 case 3:
92 this->serial = new(AHB0) SerialConsole( p9, p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
93 break;
94 }
95 #endif
96 // default
97 if(this->serial == NULL) {
98 this->serial = new(AHB0) SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
99 }
100
101 //some boards don't have leds.. TOO BAD!
102 this->use_leds= !this->config->value( disable_leds_checksum )->by_default(false)->as_bool();
103 this->grbl_mode= this->config->value( grbl_mode_checksum )->by_default(false)->as_bool();
104 this->ok_per_line= this->config->value( ok_per_line_checksum )->by_default(true)->as_bool();
105
106 this->add_module( this->serial );
107
108 // HAL stuff
109 add_module( this->slow_ticker = new SlowTicker());
110
111 this->step_ticker = new StepTicker();
112 this->adc = new Adc();
113
114 // TODO : These should go into platform-specific files
115 // LPC17xx-specific
116 NVIC_SetPriorityGrouping(0);
117 NVIC_SetPriority(TIMER0_IRQn, 2);
118 NVIC_SetPriority(TIMER1_IRQn, 1);
119 NVIC_SetPriority(TIMER2_IRQn, 4);
120 NVIC_SetPriority(PendSV_IRQn, 3);
121 NVIC_SetPriority(RIT_IRQn, 3); // we make acceleration tick the same prio as pendsv so it can't be pre-empted by end of block
122
123 // Set other priorities lower than the timers
124 NVIC_SetPriority(ADC_IRQn, 5);
125 NVIC_SetPriority(USB_IRQn, 5);
126
127 // If MRI is enabled
128 if( MRI_ENABLE ){
129 if( NVIC_GetPriority(UART0_IRQn) > 0 ){ NVIC_SetPriority(UART0_IRQn, 5); }
130 if( NVIC_GetPriority(UART1_IRQn) > 0 ){ NVIC_SetPriority(UART1_IRQn, 5); }
131 if( NVIC_GetPriority(UART2_IRQn) > 0 ){ NVIC_SetPriority(UART2_IRQn, 5); }
132 if( NVIC_GetPriority(UART3_IRQn) > 0 ){ NVIC_SetPriority(UART3_IRQn, 5); }
133 }else{
134 NVIC_SetPriority(UART0_IRQn, 5);
135 NVIC_SetPriority(UART1_IRQn, 5);
136 NVIC_SetPriority(UART2_IRQn, 5);
137 NVIC_SetPriority(UART3_IRQn, 5);
138 }
139
140 // Configure the step ticker
141 this->base_stepping_frequency = this->config->value(base_stepping_frequency_checksum)->by_default(100000)->as_number();
142 float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum)->by_default(5)->as_number();
143 this->acceleration_ticks_per_second = THEKERNEL->config->value(acceleration_ticks_per_second_checksum)->by_default(1000)->as_number();
144
145 // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
146 this->step_ticker->set_reset_delay( microseconds_per_step_pulse );
147 this->step_ticker->set_frequency( this->base_stepping_frequency );
148 this->step_ticker->set_acceleration_ticks_per_second(acceleration_ticks_per_second); // must be set after set_frequency
149
150 // Core modules
151 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
152 this->add_module( this->robot = new Robot() );
153 this->add_module( this->stepper = new Stepper() );
154 this->add_module( this->conveyor = new Conveyor() );
155 this->add_module( this->simpleshell = new SimpleShell() );
156
157 this->planner = new Planner();
158 this->configurator = new Configurator();
159 }
160
161 // return a GRBL-like query string for serial ?
162 std::string Kernel::get_query_string()
163 {
164 std::string str;
165 bool homing;
166 bool ok = PublicData::get_value(endstops_checksum, get_homing_status_checksum, 0, &homing);
167 if(!ok) homing= false;
168
169 str.append("<");
170 if(halted) {
171 str.append("Alarm,");
172 }else if(homing) {
173 str.append("Home,");
174 }else if(feed_hold) {
175 str.append("Hold,");
176 }else if(this->conveyor->is_queue_empty()) {
177 str.append("Idle,");
178 }else{
179 str.append("Run,");
180 }
181
182 // get real time current actuator position in mm
183 ActuatorCoordinates current_position{
184 robot->actuators[X_AXIS]->get_current_position(),
185 robot->actuators[Y_AXIS]->get_current_position(),
186 robot->actuators[Z_AXIS]->get_current_position()
187 };
188
189 // get machine position from the actuator position using FK
190 float mpos[3];
191 robot->arm_solution->actuator_to_cartesian(current_position, mpos);
192
193 char buf[64];
194 // machine position
195 size_t n= snprintf(buf, sizeof(buf), "%f,%f,%f,", mpos[0], mpos[1], mpos[2]);
196 str.append("MPos:").append(buf, n);
197
198 // work space position
199 Robot::wcs_t pos= robot->mcs2wcs(mpos);
200 n= snprintf(buf, sizeof(buf), "%f,%f,%f", robot->from_millimeters(std::get<X_AXIS>(pos)), robot->from_millimeters(std::get<Y_AXIS>(pos)), robot->from_millimeters(std::get<Z_AXIS>(pos)));
201 str.append("WPos:").append(buf, n);
202 str.append(">\r\n");
203 return str;
204 }
205
206 // Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
207 void Kernel::add_module(Module* module){
208 module->on_module_loaded();
209 }
210
211 // Adds a hook for a given module and event
212 void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){
213 this->hooks[id_event].push_back(mod);
214 }
215
216 // Call a specific event with an argument
217 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
218 if(id_event == ON_HALT) {
219 this->halted= (argument == nullptr);
220 }
221 for (auto m : hooks[id_event]) {
222 (m->*kernel_callback_functions[id_event])(argument);
223 }
224 }
225
226 // These are used by tests to test for various things. basically mocks
227 bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod)
228 {
229 for (auto m : hooks[id_event]) {
230 if(m == mod) return true;
231 }
232 return false;
233 }
234
235 void Kernel::unregister_for_event(_EVENT_ENUM id_event, Module *mod)
236 {
237 for (auto i = hooks[id_event].begin(); i != hooks[id_event].end(); ++i) {
238 if(*i == mod) {
239 hooks[id_event].erase(i);
240 return;
241 }
242 }
243 }
244