fix using is queue empty when it should be is_idle
[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 acceleration_ticks_per_second_checksum CHECKSUM("acceleration_ticks_per_second")
44 #define disable_leds_checksum CHECKSUM("leds_disable")
45 #define grbl_mode_checksum CHECKSUM("grbl_mode")
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 halted= false;
53 feed_hold= false;
54
55 instance= this; // setup the Singleton instance of the kernel
56
57 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
58 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
59 this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE);
60
61 // Config next, but does not load cache yet
62 this->config = new Config();
63
64 // Pre-load the config cache, do after setting up serial so we can report errors to serial
65 this->config->config_cache_load();
66
67 // now config is loaded we can do normal setup for serial based on config
68 delete this->serial;
69 this->serial= NULL;
70
71 this->streams = new StreamOutputPool();
72
73 this->current_path = "/";
74
75 // Configure UART depending on MRI config
76 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
77 NVIC_SetPriorityGrouping(0);
78
79 #if MRI_ENABLE != 0
80 switch( __mriPlatform_CommUartIndex() ) {
81 case 0:
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());
83 break;
84 case 1:
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());
86 break;
87 case 2:
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());
89 break;
90 case 3:
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());
92 break;
93 }
94 #endif
95 // default
96 if(this->serial == NULL) {
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());
98 }
99
100 //some boards don't have leds.. TOO BAD!
101 this->use_leds= !this->config->value( disable_leds_checksum )->by_default(false)->as_bool();
102 this->grbl_mode= this->config->value( grbl_mode_checksum )->by_default(false)->as_bool();
103 this->ok_per_line= this->config->value( ok_per_line_checksum )->by_default(true)->as_bool();
104
105 this->add_module( this->serial );
106
107 // HAL stuff
108 add_module( this->slow_ticker = new SlowTicker());
109
110 this->step_ticker = new StepTicker();
111 this->adc = new Adc();
112
113 // TODO : These should go into platform-specific files
114 // LPC17xx-specific
115 NVIC_SetPriorityGrouping(0);
116 NVIC_SetPriority(TIMER0_IRQn, 2);
117 NVIC_SetPriority(TIMER1_IRQn, 1);
118 NVIC_SetPriority(TIMER2_IRQn, 4);
119 NVIC_SetPriority(PendSV_IRQn, 3);
120
121 // Set other priorities lower than the timers
122 NVIC_SetPriority(ADC_IRQn, 5);
123 NVIC_SetPriority(USB_IRQn, 5);
124
125 // If MRI is enabled
126 if( MRI_ENABLE ){
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); }
131 }else{
132 NVIC_SetPriority(UART0_IRQn, 5);
133 NVIC_SetPriority(UART1_IRQn, 5);
134 NVIC_SetPriority(UART2_IRQn, 5);
135 NVIC_SetPriority(UART3_IRQn, 5);
136 }
137
138 // Configure the step ticker
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();
142
143 // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
144 this->step_ticker->set_unstep_time( microseconds_per_step_pulse );
145 this->step_ticker->set_frequency( this->base_stepping_frequency );
146
147 // Core modules
148 this->add_module( this->gcode_dispatch = new GcodeDispatch() );
149 this->add_module( this->robot = new Robot() );
150 this->add_module( this->conveyor = new Conveyor() );
151 this->add_module( this->simpleshell = new SimpleShell() );
152
153 this->planner = new Planner();
154 this->configurator = new Configurator();
155 }
156
157 // return a GRBL-like query string for serial ?
158 std::string Kernel::get_query_string()
159 {
160 std::string str;
161 bool homing;
162 bool ok = PublicData::get_value(endstops_checksum, get_homing_status_checksum, 0, &homing);
163 if(!ok) homing= false;
164 bool running= false;
165
166 str.append("<");
167 if(halted) {
168 str.append("Alarm,");
169 }else if(homing) {
170 str.append("Home,");
171 }else if(feed_hold) {
172 str.append("Hold,");
173 }else if(this->conveyor->is_idle()) {
174 str.append("Idle,");
175 }else{
176 running= true;
177 str.append("Run,");
178 }
179
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 }
218 return str;
219 }
220
221 // Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
222 void Kernel::add_module(Module* module){
223 module->on_module_loaded();
224 }
225
226 // Adds a hook for a given module and event
227 void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){
228 this->hooks[id_event].push_back(mod);
229 }
230
231 // Call a specific event with an argument
232 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
233 bool was_idle= true;
234 if(id_event == ON_HALT) {
235 this->halted= (argument == nullptr);
236 was_idle= conveyor->is_idle(); // see if we were doing anything like printing
237 }
238
239 // send to all registered modules
240 for (auto m : hooks[id_event]) {
241 (m->*kernel_callback_functions[id_event])(argument);
242 }
243
244 if(id_event == ON_HALT && this->halted && !was_idle) {
245 // we need to try to correct current positions if we were running
246 this->robot->reset_position_from_current_actuator_position();
247 }
248 }
249
250 // These are used by tests to test for various things. basically mocks
251 bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod)
252 {
253 for (auto m : hooks[id_event]) {
254 if(m == mod) return true;
255 }
256 return false;
257 }
258
259 void 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