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