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