| 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 | #ifndef ROBOT_H |
| 9 | #define ROBOT_H |
| 10 | |
| 11 | #include <string> |
| 12 | using std::string; |
| 13 | #include <string.h> |
| 14 | #include <functional> |
| 15 | #include <stack> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "libs/Module.h" |
| 19 | #include "ActuatorCoordinates.h" |
| 20 | #include "nuts_bolts.h" |
| 21 | |
| 22 | class Gcode; |
| 23 | class BaseSolution; |
| 24 | class StepperMotor; |
| 25 | |
| 26 | // 9 WCS offsets |
| 27 | #define MAX_WCS 9UL |
| 28 | #define N_PRIMARY_AXIS 3 |
| 29 | |
| 30 | class Robot : public Module { |
| 31 | public: |
| 32 | using wcs_t= std::tuple<float, float, float>; |
| 33 | Robot(); |
| 34 | void on_module_loaded(); |
| 35 | void on_gcode_received(void* argument); |
| 36 | |
| 37 | void reset_axis_position(float position, int axis); |
| 38 | void reset_axis_position(float x, float y, float z); |
| 39 | void reset_actuator_position(const ActuatorCoordinates &ac); |
| 40 | void reset_position_from_current_actuator_position(); |
| 41 | float get_seconds_per_minute() const { return seconds_per_minute; } |
| 42 | float get_z_maxfeedrate() const { return this->max_speeds[Z_AXIS]; } |
| 43 | float get_default_acceleration() const { return default_acceleration; } |
| 44 | void setToolOffset(const float offset[N_PRIMARY_AXIS]); |
| 45 | float get_feed_rate() const; |
| 46 | float get_s_value() const { return s_value; } |
| 47 | void set_s_value(float s) { s_value= s; } |
| 48 | void push_state(); |
| 49 | void pop_state(); |
| 50 | void check_max_actuator_speeds(); |
| 51 | float to_millimeters( float value ) const { return this->inch_mode ? value * 25.4F : value; } |
| 52 | float from_millimeters( float value) const { return this->inch_mode ? value/25.4F : value; } |
| 53 | float get_axis_position(int axis) const { return(this->machine_position[axis]); } |
| 54 | void get_axis_position(float position[], size_t n= N_PRIMARY_AXIS) const { memcpy(position, this->machine_position, n*sizeof(float)); } |
| 55 | wcs_t get_axis_position() const { return wcs_t(machine_position[X_AXIS], machine_position[Y_AXIS], machine_position[Z_AXIS]); } |
| 56 | void get_current_machine_position(float *pos) const; |
| 57 | int print_position(uint8_t subcode, char *buf, size_t bufsize) const; |
| 58 | uint8_t get_current_wcs() const { return current_wcs; } |
| 59 | std::vector<wcs_t> get_wcs_state() const; |
| 60 | std::tuple<float, float, float, uint8_t> get_last_probe_position() const { return last_probe_position; } |
| 61 | void set_last_probe_position(std::tuple<float, float, float, uint8_t> p) { last_probe_position = p; } |
| 62 | bool delta_move(const float delta[], float rate_mm_s, uint8_t naxis); |
| 63 | uint8_t register_motor(StepperMotor*); |
| 64 | uint8_t get_number_registered_motors() const {return n_motors; } |
| 65 | |
| 66 | BaseSolution* arm_solution; // Selected Arm solution ( millimeters to step calculation ) |
| 67 | |
| 68 | // gets accessed by Panel, Endstops, ZProbe |
| 69 | std::vector<StepperMotor*> actuators; |
| 70 | |
| 71 | // set by a leveling strategy to transform the target of a move according to the current plan |
| 72 | std::function<void(float*, bool)> compensationTransform; |
| 73 | // set by an active extruder, returns the amount to scale the E parameter by (to convert mm³ to mm) |
| 74 | std::function<float(void)> get_e_scale_fnc; |
| 75 | |
| 76 | // Workspace coordinate systems |
| 77 | wcs_t mcs2wcs(const wcs_t &pos) const; |
| 78 | wcs_t mcs2wcs(const float *pos) const { return mcs2wcs(wcs_t(pos[X_AXIS], pos[Y_AXIS], pos[Z_AXIS])); } |
| 79 | |
| 80 | struct { |
| 81 | bool inch_mode:1; // true for inch mode, false for millimeter mode ( default ) |
| 82 | bool absolute_mode:1; // true for absolute mode ( default ), false for relative mode |
| 83 | bool e_absolute_mode:1; // true for absolute mode for E ( default ), false for relative mode |
| 84 | bool next_command_is_MCS:1; // set by G53 |
| 85 | bool disable_segmentation:1; // set to disable segmentation |
| 86 | bool disable_arm_solution:1; // set to disable the arm solution |
| 87 | bool segment_z_moves:1; |
| 88 | bool save_g92:1; // save g92 on M500 if set |
| 89 | bool is_g123:1; |
| 90 | uint8_t plane_axis_0:2; // Current plane ( XY, XZ, YZ ) |
| 91 | uint8_t plane_axis_1:2; |
| 92 | uint8_t plane_axis_2:2; |
| 93 | }; |
| 94 | |
| 95 | private: |
| 96 | enum MOTION_MODE_T { |
| 97 | NONE, |
| 98 | SEEK, // G0 |
| 99 | LINEAR, // G1 |
| 100 | CW_ARC, // G2 |
| 101 | CCW_ARC // G3 |
| 102 | }; |
| 103 | |
| 104 | void load_config(); |
| 105 | bool append_milestone(const float target[], float rate_mm_s); |
| 106 | bool append_line( Gcode* gcode, const float target[], float rate_mm_s, float delta_e); |
| 107 | bool append_arc( Gcode* gcode, const float target[], const float offset[], float radius, bool is_clockwise ); |
| 108 | bool compute_arc(Gcode* gcode, const float offset[], const float target[], enum MOTION_MODE_T motion_mode); |
| 109 | void process_move(Gcode *gcode, enum MOTION_MODE_T); |
| 110 | |
| 111 | float theta(float x, float y); |
| 112 | void select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2); |
| 113 | void clearToolOffset(); |
| 114 | int get_active_extruder() const; |
| 115 | |
| 116 | std::array<wcs_t, MAX_WCS> wcs_offsets; // these are persistent once saved with M500 |
| 117 | uint8_t current_wcs{0}; // 0 means G54 is enabled this is persistent once saved with M500 |
| 118 | wcs_t g92_offset; |
| 119 | wcs_t tool_offset; // used for multiple extruders, sets the tool offset for the current extruder applied first |
| 120 | std::tuple<float, float, float, uint8_t> last_probe_position{0,0,0,0}; |
| 121 | |
| 122 | using saved_state_t= std::tuple<float, float, bool, bool, bool, uint8_t>; // save current feedrate and absolute mode, e absolute mode, inch mode, current_wcs |
| 123 | std::stack<saved_state_t> state_stack; // saves state from M120 |
| 124 | |
| 125 | float machine_position[k_max_actuators]; // Last requested position, in millimeters, which is what we were requested to move to in the gcode after offsets applied but before compensation transform |
| 126 | float compensated_machine_position[k_max_actuators]; // Last machine position, which is the position before converting to actuator coordinates (includes compensation transform) |
| 127 | |
| 128 | float seek_rate; // Current rate for seeking moves ( mm/min ) |
| 129 | float feed_rate; // Current rate for feeding moves ( mm/min ) |
| 130 | float mm_per_line_segment; // Setting : Used to split lines into segments |
| 131 | float mm_per_arc_segment; // Setting : Used to split arcs into segments |
| 132 | float mm_max_arc_error; // Setting : Used to limit total arc segments to max error |
| 133 | float delta_segments_per_second; // Setting : Used to split lines into segments for delta based on speed |
| 134 | float seconds_per_minute; // for realtime speed change |
| 135 | float default_acceleration; // the defualt accleration if not set for each axis |
| 136 | float s_value; // modal S value |
| 137 | |
| 138 | // Number of arc generation iterations by small angle approximation before exact arc trajectory |
| 139 | // correction. This parameter may be decreased if there are issues with the accuracy of the arc |
| 140 | // generations. In general, the default value is more than enough for the intended CNC applications |
| 141 | // of grbl, and should be on the order or greater than the size of the buffer to help with the |
| 142 | // computational efficiency of generating arcs. |
| 143 | int arc_correction; // Setting : how often to rectify arc computation |
| 144 | float max_speeds[3]; // Setting : max allowable speed in mm/s for each axis |
| 145 | |
| 146 | uint8_t n_motors; //count of the motors/axis registered |
| 147 | |
| 148 | // Used by Planner |
| 149 | friend class Planner; |
| 150 | }; |
| 151 | |
| 152 | |
| 153 | #endif |