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/>.
9 #include "SimpleShell.h"
10 #include "libs/Kernel.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/utils.h"
13 #include "libs/SerialMessage.h"
14 #include "libs/StreamOutput.h"
15 #include "modules/robot/Conveyor.h"
16 #include "DirHandle.h"
19 #include "PublicDataRequest.h"
20 #include "AppendFileStream.h"
21 #include "FileStream.h"
22 #include "checksumm.h"
23 #include "PublicData.h"
26 #include "ToolManagerPublicAccess.h"
27 #include "GcodeDispatch.h"
28 #include "BaseSolution.h"
29 #include "StepperMotor.h"
31 #include "TemperatureControlPublicAccess.h"
32 #include "EndstopsPublicAccess.h"
33 #include "NetworkPublicAccess.h"
34 #include "platform_memory.h"
35 #include "SwitchPublicAccess.h"
37 #include "Thermistor.h"
41 #include "system_LPC17xx.h"
44 #include "mbed.h" // for wait_ms()
46 extern unsigned int g_maximumHeapAddress
;
53 extern "C" uint32_t __end__
;
54 extern "C" uint32_t __malloc_free_list
;
55 extern "C" uint32_t _sbrk(int size
);
57 // command lookup table
58 const SimpleShell::ptentry_t
SimpleShell::commands_table
[] = {
59 {"ls", SimpleShell::ls_command
},
60 {"cd", SimpleShell::cd_command
},
61 {"pwd", SimpleShell::pwd_command
},
62 {"cat", SimpleShell::cat_command
},
63 {"rm", SimpleShell::rm_command
},
64 {"mv", SimpleShell::mv_command
},
65 {"upload", SimpleShell::upload_command
},
66 {"reset", SimpleShell::reset_command
},
67 {"dfu", SimpleShell::dfu_command
},
68 {"break", SimpleShell::break_command
},
69 {"help", SimpleShell::help_command
},
70 {"?", SimpleShell::help_command
},
71 {"version", SimpleShell::version_command
},
72 {"mem", SimpleShell::mem_command
},
73 {"get", SimpleShell::get_command
},
74 {"set_temp", SimpleShell::set_temp_command
},
75 {"switch", SimpleShell::switch_command
},
76 {"net", SimpleShell::net_command
},
77 {"load", SimpleShell::load_command
},
78 {"save", SimpleShell::save_command
},
79 {"remount", SimpleShell::remount_command
},
80 {"calc_thermistor", SimpleShell::calc_thermistor_command
},
81 {"thermistors", SimpleShell::print_thermistors_command
},
82 {"md5sum", SimpleShell::md5sum_command
},
88 int SimpleShell::reset_delay_secs
= 0;
90 // Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556
91 static uint32_t heapWalk(StreamOutput
*stream
, bool verbose
)
93 uint32_t chunkNumber
= 1;
94 // The __end__ linker symbol points to the beginning of the heap.
95 uint32_t chunkCurr
= (uint32_t)&__end__
;
96 // __malloc_free_list is the head pointer to newlib-nano's link list of free chunks.
97 uint32_t freeCurr
= __malloc_free_list
;
98 // Calling _sbrk() with 0 reserves no more memory but it returns the current top of heap.
99 uint32_t heapEnd
= _sbrk(0);
101 uint32_t freeSize
= 0;
102 uint32_t usedSize
= 0;
104 stream
->printf("Used Heap Size: %lu\n", heapEnd
- chunkCurr
);
106 // Walk through the chunks until we hit the end of the heap.
107 while (chunkCurr
< heapEnd
) {
108 // Assume the chunk is in use. Will update later.
110 // The first 32-bit word in a chunk is the size of the allocation. newlib-nano over allocates by 8 bytes.
111 // 4 bytes for this 32-bit chunk size and another 4 bytes to allow for 8 byte-alignment of returned pointer.
112 uint32_t chunkSize
= *(uint32_t *)chunkCurr
;
113 // The start of the next chunk is right after the end of this one.
114 uint32_t chunkNext
= chunkCurr
+ chunkSize
;
116 // The free list is sorted by address.
117 // Check to see if we have found the next free chunk in the heap.
118 if (chunkCurr
== freeCurr
) {
119 // Chunk is free so flag it as such.
121 // The second 32-bit word in a free chunk is a pointer to the next free chunk (again sorted by address).
122 freeCurr
= *(uint32_t *)(freeCurr
+ 4);
125 // Skip past the 32-bit size field in the chunk header.
127 // 8-byte align the data pointer.
128 chunkCurr
= (chunkCurr
+ 7) & ~7;
129 // newlib-nano over allocates by 8 bytes, 4 bytes for the 32-bit chunk size and another 4 bytes to allow for 8
130 // byte-alignment of the returned pointer.
133 stream
->printf(" Chunk: %lu Address: 0x%08lX Size: %lu %s\n", chunkNumber
, chunkCurr
, chunkSize
, isChunkFree
? "CHUNK FREE" : "");
135 if (isChunkFree
) freeSize
+= chunkSize
;
136 else usedSize
+= chunkSize
;
138 chunkCurr
= chunkNext
;
141 stream
->printf("Allocated: %lu, Free: %lu\r\n", usedSize
, freeSize
);
146 void SimpleShell::on_module_loaded()
148 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
149 this->register_for_event(ON_GCODE_RECEIVED
);
150 this->register_for_event(ON_SECOND_TICK
);
152 reset_delay_secs
= 0;
155 void SimpleShell::on_second_tick(void *)
157 // we are timing out for the reset
158 if (reset_delay_secs
> 0) {
159 if (--reset_delay_secs
== 0) {
165 void SimpleShell::on_gcode_received(void *argument
)
167 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
168 string args
= get_arguments(gcode
->get_command());
171 if (gcode
->m
== 20) { // list sd card
172 gcode
->stream
->printf("Begin file list\r\n");
173 ls_command("/sd", gcode
->stream
);
174 gcode
->stream
->printf("End file list\r\n");
176 } else if (gcode
->m
== 30) { // remove file
177 rm_command("/sd/" + args
, gcode
->stream
);
179 } else if(gcode
->m
== 501) { // load config override
181 load_command("/sd/config-override", gcode
->stream
);
183 load_command("/sd/config-override." + args
, gcode
->stream
);
186 } else if(gcode
->m
== 504) { // save to specific config override file
188 save_command("/sd/config-override", gcode
->stream
);
190 save_command("/sd/config-override." + args
, gcode
->stream
);
196 bool SimpleShell::parse_command(const char *cmd
, string args
, StreamOutput
*stream
)
198 for (const ptentry_t
*p
= commands_table
; p
->command
!= NULL
; ++p
) {
199 if (strncasecmp(cmd
, p
->command
, strlen(p
->command
)) == 0) {
200 p
->func(args
, stream
);
208 // When a new line is received, check if it is a command, and if it is, act upon it
209 void SimpleShell::on_console_line_received( void *argument
)
211 SerialMessage new_message
= *static_cast<SerialMessage
*>(argument
);
212 string possible_command
= new_message
.message
;
214 // ignore anything that is not lowercase or a $ as it is not a command
215 if(possible_command
.size() == 0 || (!islower(possible_command
[0]) && possible_command
[0] != '$')) {
219 // it is a grbl compatible command
220 if(possible_command
[0] == '$' && possible_command
.size() >= 2) {
221 switch(possible_command
[1]) {
224 get_command("state", new_message
.stream
);
228 THEKERNEL
->call_event(ON_HALT
, (void *)1); // clears on_halt
229 new_message
.stream
->printf("[Caution: Unlocked]\n");
233 grblDP_command("", new_message
.stream
);
237 if(THEKERNEL
->is_grbl_mode()) {
238 // issue G28.2 which is force homing cycle
239 Gcode
gcode("G28.2", new_message
.stream
);
240 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, &gcode
);
242 new_message
.stream
->printf("error:only supported in GRBL mode\n");
247 new_message
.stream
->printf("error:Invalid statement\n");
253 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
254 string cmd
= shift_parameter(possible_command
);
256 // find command and execute it
257 if(!parse_command(cmd
.c_str(), possible_command
, new_message
.stream
)) {
258 new_message
.stream
->printf("error:Unsupported command\n");
263 // Act upon an ls command
264 // Convert the first parameter into an absolute path, then list the files in that path
265 void SimpleShell::ls_command( string parameters
, StreamOutput
*stream
)
268 while(!parameters
.empty()) {
269 string s
= shift_parameter( parameters
);
270 if(s
.front() == '-') {
274 if(!parameters
.empty()) {
276 path
.append(parameters
);
282 path
= absolute_from_relative(path
);
286 d
= opendir(path
.c_str());
288 while ((p
= readdir(d
)) != NULL
) {
289 stream
->printf("%s", lc(string(p
->d_name
)).c_str());
292 } else if(opts
.find("-s", 0, 2) != string::npos
) {
293 stream
->printf(" %d", p
->d_fsize
);
295 stream
->printf("\r\n");
299 stream
->printf("Could not open directory %s\r\n", path
.c_str());
303 extern SDFAT mounter
;
305 void SimpleShell::remount_command( string parameters
, StreamOutput
*stream
)
308 stream
->printf("remounted\r\n");
312 void SimpleShell::rm_command( string parameters
, StreamOutput
*stream
)
314 const char *fn
= absolute_from_relative(shift_parameter( parameters
)).c_str();
316 if (s
!= 0) stream
->printf("Could not delete %s \r\n", fn
);
320 void SimpleShell::mv_command( string parameters
, StreamOutput
*stream
)
322 string from
= absolute_from_relative(shift_parameter( parameters
));
323 string to
= absolute_from_relative(shift_parameter(parameters
));
324 int s
= rename(from
.c_str(), to
.c_str());
325 if (s
!= 0) stream
->printf("Could not rename %s to %s\r\n", from
.c_str(), to
.c_str());
326 else stream
->printf("renamed %s to %s\r\n", from
.c_str(), to
.c_str());
329 // Change current absolute path to provided path
330 void SimpleShell::cd_command( string parameters
, StreamOutput
*stream
)
332 string folder
= absolute_from_relative( parameters
);
335 d
= opendir(folder
.c_str());
337 stream
->printf("Could not open directory %s \r\n", folder
.c_str() );
339 THEKERNEL
->current_path
= folder
;
344 // Responds with the present working directory
345 void SimpleShell::pwd_command( string parameters
, StreamOutput
*stream
)
347 stream
->printf("%s\r\n", THEKERNEL
->current_path
.c_str());
350 // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
351 void SimpleShell::cat_command( string parameters
, StreamOutput
*stream
)
353 // Get parameters ( filename and line limit )
354 string filename
= absolute_from_relative(shift_parameter( parameters
));
355 string limit_parameter
= shift_parameter( parameters
);
358 bool send_eof
= false;
359 if ( limit_parameter
== "-d" ) {
360 string d
= shift_parameter( parameters
);
362 delay
= strtol(d
.c_str(), &e
, 10);
363 if (e
<= d
.c_str()) {
367 send_eof
= true; // we need to terminate file send with an eof
370 }else if ( limit_parameter
!= "" ) {
372 limit
= strtol(limit_parameter
.c_str(), &e
, 10);
373 if (e
<= limit_parameter
.c_str())
377 // we have been asked to delay before cat, probably to allow time to issue upload command
379 safe_delay(delay
*1000);
383 FILE *lp
= fopen(filename
.c_str(), "r");
385 stream
->printf("File not found: %s\r\n", filename
.c_str());
392 // Print each line of the file
393 while ((c
= fgetc (lp
)) != EOF
) {
394 buffer
.append((char *)&c
, 1);
395 if ( c
== '\n' || ++linecnt
> 80) {
396 if(c
== '\n') newlines
++;
397 stream
->puts(buffer
.c_str());
399 if(linecnt
> 80) linecnt
= 0;
400 // we need to kick things or they die
401 THEKERNEL
->call_event(ON_IDLE
);
403 if ( newlines
== limit
) {
410 stream
->puts("\032"); // ^Z terminates the upload
414 void SimpleShell::upload_command( string parameters
, StreamOutput
*stream
)
416 // this needs to be a hack. it needs to read direct from serial and not allow on_main_loop run until done
417 // NOTE this will block all operation until the upload is complete, so do not do while printing
418 if(!THEKERNEL
->conveyor
->is_queue_empty()) {
419 stream
->printf("upload not allowed while printing or busy\n");
423 // open file to upload to
424 string upload_filename
= absolute_from_relative( parameters
);
425 FILE *fd
= fopen(upload_filename
.c_str(), "w");
427 stream
->printf("uploading to file: %s, send control-D or control-Z to finish\r\n", upload_filename
.c_str());
429 stream
->printf("failed to open file: %s.\r\n", upload_filename
.c_str());
434 bool uploading
= true;
436 if(!stream
->ready()) {
437 // we need to kick things or they die
438 THEKERNEL
->call_event(ON_IDLE
);
442 char c
= stream
->_getc();
443 if( c
== 4 || c
== 26) { // ctrl-D or ctrl-Z
447 stream
->printf("uploaded %d bytes\n", cnt
);
451 // write character to file
453 if(fputc(c
, fd
) != c
) {
454 // error writing to file
455 stream
->printf("error writing to file. ignoring all characters until EOF\r\n");
461 if ((cnt
%400) == 0) {
462 // HACK ALERT to get around fwrite corruption close and re open for append
464 fd
= fopen(upload_filename
.c_str(), "a");
465 // we need to kick things or they die
466 THEKERNEL
->call_event(ON_IDLE
);
471 // we got an error so ignore everything until EOF
474 if(stream
->ready()) {
477 THEKERNEL
->call_event(ON_IDLE
);
480 } while(c
!= 4 && c
!= 26);
483 // loads the specified config-override file
484 void SimpleShell::load_command( string parameters
, StreamOutput
*stream
)
486 // Get parameters ( filename )
487 string filename
= absolute_from_relative(parameters
);
488 if(filename
== "/") {
489 filename
= THEKERNEL
->config_override_filename();
492 FILE *fp
= fopen(filename
.c_str(), "r");
495 stream
->printf("Loading config override file: %s...\n", filename
.c_str());
496 while(fgets(buf
, sizeof buf
, fp
) != NULL
) {
497 stream
->printf(" %s", buf
);
498 if(buf
[0] == ';') continue; // skip the comments
499 struct SerialMessage message
= {&(StreamOutput::NullStream
), buf
};
500 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
502 stream
->printf("config override file executed\n");
506 stream
->printf("File not found: %s\n", filename
.c_str());
510 // saves the specified config-override file
511 void SimpleShell::save_command( string parameters
, StreamOutput
*stream
)
513 // Get parameters ( filename )
514 string filename
= absolute_from_relative(parameters
);
515 if(filename
== "/") {
516 filename
= THEKERNEL
->config_override_filename();
519 THEKERNEL
->conveyor
->wait_for_empty_queue(); //just to be safe as it can take a while to run
521 //remove(filename.c_str()); // seems to cause a hang every now and then
523 FileStream
fs(filename
.c_str());
524 fs
.printf("; DO NOT EDIT THIS FILE\n");
525 // this also will truncate the existing file instead of deleting it
528 // stream that appends to file
529 AppendFileStream
*gs
= new AppendFileStream(filename
.c_str());
530 // if(!gs->is_open()) {
531 // stream->printf("Unable to open File %s for write\n", filename.c_str());
536 // issue a M500 which will store values in the file stream
537 Gcode
*gcode
= new Gcode("M500", gs
);
538 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, gcode
);
543 stream
->printf("Settings Stored to %s\r\n", filename
.c_str());
547 void SimpleShell::mem_command( string parameters
, StreamOutput
*stream
)
549 bool verbose
= shift_parameter( parameters
).find_first_of("Vv") != string::npos
;
550 unsigned long heap
= (unsigned long)_sbrk(0);
551 unsigned long m
= g_maximumHeapAddress
- heap
;
552 stream
->printf("Unused Heap: %lu bytes\r\n", m
);
554 uint32_t f
= heapWalk(stream
, verbose
);
555 stream
->printf("Total Free RAM: %lu bytes\r\n", m
+ f
);
557 stream
->printf("Free AHB0: %lu, AHB1: %lu\r\n", AHB0
.free(), AHB1
.free());
564 static uint32_t getDeviceType()
566 #define IAP_LOCATION 0x1FFF1FF1
569 typedef void (*IAP
)(uint32_t *, uint32_t *);
570 IAP iap
= (IAP
) IAP_LOCATION
;
575 iap(command
, result
);
582 // get network config
583 void SimpleShell::net_command( string parameters
, StreamOutput
*stream
)
586 bool ok
= PublicData::get_value( network_checksum
, get_ipconfig_checksum
, &returned_data
);
588 char *str
= (char *)returned_data
;
589 stream
->printf("%s\r\n", str
);
593 stream
->printf("No network detected\n");
597 // print out build version
598 void SimpleShell::version_command( string parameters
, StreamOutput
*stream
)
601 uint32_t dev
= getDeviceType();
602 const char *mcu
= (dev
& 0x00100000) ? "LPC1769" : "LPC1768";
603 stream
->printf("Build version: %s, Build date: %s, MCU: %s, System Clock: %ldMHz\r\n", vers
.get_build(), vers
.get_build_date(), mcu
, SystemCoreClock
/ 1000000);
607 void SimpleShell::reset_command( string parameters
, StreamOutput
*stream
)
609 stream
->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
610 reset_delay_secs
= 5; // reboot in 5 seconds
613 // go into dfu boot mode
614 void SimpleShell::dfu_command( string parameters
, StreamOutput
*stream
)
616 stream
->printf("Entering boot mode...\r\n");
620 // Break out into the MRI debugging system
621 void SimpleShell::break_command( string parameters
, StreamOutput
*stream
)
623 stream
->printf("Entering MRI debug mode...\r\n");
627 static int get_active_tool()
630 bool ok
= PublicData::get_value(tool_manager_checksum
, get_active_tool_checksum
, &returned_data
);
632 int active_tool
= *static_cast<int *>(returned_data
);
639 void SimpleShell::grblDP_command( string parameters
, StreamOutput
*stream
)
642 [G54:95.000,40.000,-23.600]
643 [G55:0.000,0.000,0.000]
644 [G56:0.000,0.000,0.000]
645 [G57:0.000,0.000,0.000]
646 [G58:0.000,0.000,0.000]
647 [G59:0.000,0.000,0.000]
648 [G28:0.000,0.000,0.000]
649 [G30:0.000,0.000,0.000]
650 [G92:0.000,0.000,0.000]
652 [PRB:0.000,0.000,0.000:0]
654 std::vector
<Robot::wcs_t
> v
= THEKERNEL
->robot
->get_wcs_state();
655 int n
= std::get
<1>(v
[0]);
656 for (int i
= 1; i
<= n
; ++i
) {
657 stream
->printf("[%s:%1.3f,%1.3f,%1.3f]\n", wcs2gcode(i
-1).c_str(), std::get
<0>(v
[i
]), std::get
<1>(v
[i
]), std::get
<2>(v
[i
]));
661 PublicData::get_value( endstops_checksum
, saved_position_checksum
, &rd
);
662 stream
->printf("[G28:%1.3f,%1.3f,%1.3f]\n", rd
[0], rd
[1], rd
[2]);
663 stream
->printf("[G30:%1.3f,%1.3f,%1.3f]\n", 0.0F
, 0.0F
, 0.0F
); // not implemented
665 stream
->printf("[G92:%1.3f,%1.3f,%1.3f]\n", std::get
<0>(v
[n
+1]), std::get
<1>(v
[n
+1]), std::get
<2>(v
[n
+1]));
666 stream
->printf("[TL0:%1.3f]\n", std::get
<2>(v
[n
+2]));
668 // TODO this should be the last probe position, which will be this if probe was the last thing done
669 float current_machine_pos
[3];
670 THEKERNEL
->robot
->get_axis_position(current_machine_pos
);
671 stream
->printf("[PRB:%1.3f,%1.3f,%1.3f:%d]\n", current_machine_pos
[X_AXIS
], current_machine_pos
[Y_AXIS
], current_machine_pos
[Z_AXIS
], 0);
674 // used to test out the get public data events
675 void SimpleShell::get_command( string parameters
, StreamOutput
*stream
)
677 string what
= shift_parameter( parameters
);
679 if (what
== "temp") {
680 struct pad_temperature temp
;
681 string type
= shift_parameter( parameters
);
683 // scan all temperature controls
684 std::vector
<struct pad_temperature
> controllers
;
685 bool ok
= PublicData::get_value(temperature_control_checksum
, poll_controls_checksum
, &controllers
);
687 for (auto &c
: controllers
) {
688 stream
->printf("%s (%d) temp: %f/%f @%d\r\n", c
.designator
.c_str(), c
.id
, c
.current_temperature
, c
.target_temperature
, c
.pwm
);
692 stream
->printf("no heaters found\r\n");
696 bool ok
= PublicData::get_value( temperature_control_checksum
, current_temperature_checksum
, get_checksum(type
), &temp
);
699 stream
->printf("%s temp: %f/%f @%d\r\n", type
.c_str(), temp
.current_temperature
, temp
.target_temperature
, temp
.pwm
);
701 stream
->printf("%s is not a known temperature device\r\n", type
.c_str());
705 } else if (what
== "fk" || what
== "ik") {
706 string p
= shift_parameter( parameters
);
710 p
= shift_parameter( parameters
);
713 std::vector
<float> v
= parse_number_list(p
.c_str());
714 if(p
.empty() || v
.size() < 1) {
715 stream
->printf("error:usage: get [fk|ik] [-m] x[,y,z]\n");
720 float y
= (v
.size() > 1) ? v
[1] : x
;
721 float z
= (v
.size() > 2) ? v
[2] : y
;
724 // do forward kinematics on the given actuator position and display the cartesian coordinates
725 ActuatorCoordinates apos
{x
, y
, z
};
727 THEKERNEL
->robot
->arm_solution
->actuator_to_cartesian(apos
, pos
);
728 stream
->printf("cartesian= X %f, Y %f, Z %f, Steps= A %lu, B %lu, C %lu\n",
729 pos
[0], pos
[1], pos
[2],
730 lroundf(x
*THEKERNEL
->robot
->actuators
[0]->get_steps_per_mm()),
731 lroundf(y
*THEKERNEL
->robot
->actuators
[1]->get_steps_per_mm()),
732 lroundf(z
*THEKERNEL
->robot
->actuators
[2]->get_steps_per_mm()));
738 // do inverse kinematics on the given cartesian position and display the actuator coordinates
739 float pos
[3]{x
, y
, z
};
740 ActuatorCoordinates apos
;
741 THEKERNEL
->robot
->arm_solution
->cartesian_to_actuator(pos
, apos
);
742 stream
->printf("actuator= A %f, B %f, C %f\n", apos
[0], apos
[1], apos
[2]);
746 // move to the calculated, or given, XYZ
748 snprintf(cmd
, sizeof(cmd
), "G53 G0 X%f Y%f Z%f", x
, y
, z
);
749 struct SerialMessage message
;
750 message
.message
= cmd
;
751 message
.stream
= &(StreamOutput::NullStream
);
752 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
753 THEKERNEL
->conveyor
->wait_for_empty_queue();
756 } else if (what
== "pos") {
757 // convenience to call all the various M114 variants
759 THEKERNEL
->robot
->print_position(0, buf
, sizeof buf
); stream
->printf("last %s\n", buf
);
760 THEKERNEL
->robot
->print_position(1, buf
, sizeof buf
); stream
->printf("realtime %s\n", buf
);
761 THEKERNEL
->robot
->print_position(2, buf
, sizeof buf
); stream
->printf("%s\n", buf
);
762 THEKERNEL
->robot
->print_position(3, buf
, sizeof buf
); stream
->printf("%s\n", buf
);
763 THEKERNEL
->robot
->print_position(4, buf
, sizeof buf
); stream
->printf("%s\n", buf
);
764 THEKERNEL
->robot
->print_position(5, buf
, sizeof buf
); stream
->printf("%s\n", buf
);
766 } else if (what
== "wcs") {
767 // print the wcs state
768 std::vector
<Robot::wcs_t
> v
= THEKERNEL
->robot
->get_wcs_state();
769 char current_wcs
= std::get
<0>(v
[0]);
770 stream
->printf("current WCS: %s\n", wcs2gcode(current_wcs
).c_str());
771 int n
= std::get
<1>(v
[0]);
772 for (int i
= 1; i
<= n
; ++i
) {
773 stream
->printf("%s: %1.4f, %1.4f, %1.4f\n", wcs2gcode(i
-1).c_str(), std::get
<0>(v
[i
]), std::get
<1>(v
[i
]), std::get
<2>(v
[i
]));
776 stream
->printf("G92: %1.4f, %1.4f, %1.4f\n", std::get
<0>(v
[n
+1]), std::get
<1>(v
[n
+1]), std::get
<2>(v
[n
+1]));
777 stream
->printf("ToolOffset: %1.4f, %1.4f, %1.4f\n", std::get
<0>(v
[n
+2]), std::get
<1>(v
[n
+2]), std::get
<2>(v
[n
+2]));
779 } else if (what
== "state") {
781 // [G0 G54 G17 G21 G90 G94 M0 M5 M9 T0 F0.]
782 stream
->printf("[G%d %s G%d G%d G%d G94 M0 M5 M9 T%d F%1.1f]\n",
783 THEKERNEL
->gcode_dispatch
->get_modal_command(),
784 wcs2gcode(THEKERNEL
->robot
->get_current_wcs()).c_str(),
785 THEKERNEL
->robot
->plane_axis_0
== X_AXIS
&& THEKERNEL
->robot
->plane_axis_1
== Y_AXIS
&& THEKERNEL
->robot
->plane_axis_2
== Z_AXIS
? 17 :
786 THEKERNEL
->robot
->plane_axis_0
== X_AXIS
&& THEKERNEL
->robot
->plane_axis_1
== Z_AXIS
&& THEKERNEL
->robot
->plane_axis_2
== Y_AXIS
? 18 :
787 THEKERNEL
->robot
->plane_axis_0
== Y_AXIS
&& THEKERNEL
->robot
->plane_axis_1
== Z_AXIS
&& THEKERNEL
->robot
->plane_axis_2
== X_AXIS
? 19 : 17,
788 THEKERNEL
->robot
->inch_mode
? 20 : 21,
789 THEKERNEL
->robot
->absolute_mode
? 90 : 91,
791 THEKERNEL
->robot
->get_feed_rate());
794 stream
->printf("error:unknown option %s\n", what
.c_str());
798 // used to test out the get public data events
799 void SimpleShell::set_temp_command( string parameters
, StreamOutput
*stream
)
801 string type
= shift_parameter( parameters
);
802 string temp
= shift_parameter( parameters
);
803 float t
= temp
.empty() ? 0.0 : strtof(temp
.c_str(), NULL
);
804 bool ok
= PublicData::set_value( temperature_control_checksum
, get_checksum(type
), &t
);
807 stream
->printf("%s temp set to: %3.1f\r\n", type
.c_str(), t
);
809 stream
->printf("%s is not a known temperature device\r\n", type
.c_str());
813 void SimpleShell::print_thermistors_command( string parameters
, StreamOutput
*stream
)
815 Thermistor::print_predefined_thermistors(stream
);
818 void SimpleShell::calc_thermistor_command( string parameters
, StreamOutput
*stream
)
820 string s
= shift_parameter( parameters
);
822 // see if we have -sn as first argument
823 if(s
.find("-s", 0, 2) != string::npos
) {
824 // save the results to thermistor n
825 saveto
= strtol(s
.substr(2).c_str(), nullptr, 10);
830 std::vector
<float> trl
= parse_number_list(parameters
.c_str());
831 if(trl
.size() == 6) {
832 // calculate the coefficients
834 std::tie(c1
, c2
, c3
) = Thermistor::calculate_steinhart_hart_coefficients(trl
[0], trl
[1], trl
[2], trl
[3], trl
[4], trl
[5]);
835 stream
->printf("Steinhart Hart coefficients: I%1.18f J%1.18f K%1.18f\n", c1
, c2
, c3
);
837 stream
->printf(" Paste the above in the M305 S0 command, then save with M500\n");
840 int n
= snprintf(buf
, sizeof(buf
), "M305 S%d I%1.18f J%1.18f K%1.18f", saveto
, c1
, c2
, c3
);
842 Gcode
gcode(g
, &(StreamOutput::NullStream
));
843 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, &gcode
);
844 stream
->printf(" Setting Thermistor %d to those settings, save with M500\n", saveto
);
849 stream
->printf("Usage: calc_thermistor T1,R1,T2,R2,T3,R3\n");
853 // used to test out the get public data events for switch
854 void SimpleShell::switch_command( string parameters
, StreamOutput
*stream
)
856 string type
= shift_parameter( parameters
);
857 string value
= shift_parameter( parameters
);
859 if(value
== "on" || value
== "off") {
860 bool b
= value
== "on";
861 ok
= PublicData::set_value( switch_checksum
, get_checksum(type
), state_checksum
, &b
);
863 float v
= strtof(value
.c_str(), NULL
);
864 ok
= PublicData::set_value( switch_checksum
, get_checksum(type
), value_checksum
, &v
);
867 stream
->printf("switch %s set to: %s\r\n", type
.c_str(), value
.c_str());
869 stream
->printf("%s is not a known switch device\r\n", type
.c_str());
873 void SimpleShell::md5sum_command( string parameters
, StreamOutput
*stream
)
875 string filename
= absolute_from_relative(parameters
);
878 FILE *lp
= fopen(filename
.c_str(), "r");
880 stream
->printf("File not found: %s\r\n", filename
.c_str());
886 size_t n
= fread(buf
, 1, sizeof buf
, lp
);
887 if(n
> 0) md5
.update(buf
, n
);
888 THEKERNEL
->call_event(ON_IDLE
);
891 stream
->printf("%s %s\n", md5
.finalize().hexdigest().c_str(), filename
.c_str());
897 void SimpleShell::help_command( string parameters
, StreamOutput
*stream
)
899 stream
->printf("Commands:\r\n");
900 stream
->printf("version\r\n");
901 stream
->printf("mem [-v]\r\n");
902 stream
->printf("ls [-s] [folder]\r\n");
903 stream
->printf("cd folder\r\n");
904 stream
->printf("pwd\r\n");
905 stream
->printf("cat file [limit] [-d 10]\r\n");
906 stream
->printf("rm file\r\n");
907 stream
->printf("mv file newfile\r\n");
908 stream
->printf("remount\r\n");
909 stream
->printf("play file [-v]\r\n");
910 stream
->printf("progress - shows progress of current play\r\n");
911 stream
->printf("abort - abort currently playing file\r\n");
912 stream
->printf("reset - reset smoothie\r\n");
913 stream
->printf("dfu - enter dfu boot loader\r\n");
914 stream
->printf("break - break into debugger\r\n");
915 stream
->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
916 stream
->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
917 stream
->printf("get [pos|wcs|state|fk|ik]\r\n");
918 stream
->printf("get temp [bed|hotend]\r\n");
919 stream
->printf("set_temp bed|hotend 185\r\n");
920 stream
->printf("net\r\n");
921 stream
->printf("load [file] - loads a configuration override file from soecified name or config-override\r\n");
922 stream
->printf("save [file] - saves a configuration override file as specified filename or as config-override\r\n");
923 stream
->printf("upload filename - saves a stream of text to the named file\r\n");
924 stream
->printf("calc_thermistor [-s0] T1,R1,T2,R2,T3,R3 - calculate the Steinhart Hart coefficients for a thermistor\r\n");
925 stream
->printf("thermistors - print out the predefined thermistors\r\n");
926 stream
->printf("md5sum file - prints md5 sum of the given file\r\n");