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