Merge remote-tracking branch 'upstream/edge' into feature/e-endstop
[clinton/Smoothieware.git] / src / modules / robot / Robot.h
CommitLineData
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
8#ifndef ROBOT_H
9#define ROBOT_H
10
11#include <string>
12using std::string;
66383b80 13#include <string.h>
33742399 14#include <functional>
562db364 15#include <stack>
89288956 16#include <vector>
4cff3ded 17
38bf9a1c 18#include "libs/Module.h"
807b9b57 19#include "ActuatorCoordinates.h"
29e809e0 20#include "nuts_bolts.h"
4cff3ded 21
5673fe39
MM
22class Gcode;
23class BaseSolution;
24class StepperMotor;
4cff3ded 25
0b8b81b6
JM
26// 9 WCS offsets
27#define MAX_WCS 9UL
29e809e0 28#define N_PRIMARY_AXIS 3
0b8b81b6 29
4cff3ded
AW
30class Robot : public Module {
31 public:
aaf0c0ee 32 using wcs_t= std::tuple<float, float, float>;
4cff3ded
AW
33 Robot();
34 void on_module_loaded();
35 void on_gcode_received(void* argument);
5647f709 36
1ad23cd3 37 void reset_axis_position(float position, int axis);
cef9acea 38 void reset_axis_position(float x, float y, float z);
93f20a8c 39 void reset_actuator_position(const ActuatorCoordinates &ac);
728477c4 40 void reset_position_from_current_actuator_position();
dd0a7cfa 41 float get_seconds_per_minute() const { return seconds_per_minute; }
29e809e0
JM
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]);
0ec2f63a 45 float get_feed_rate() const;
73cc27d2 46 float get_s_value() const { return s_value; }
b5008339 47 void set_s_value(float s) { s_value= s; }
212caccd
JM
48 void push_state();
49 void pop_state();
3c9fee28 50 void check_max_actuator_speeds();
2791c829
JM
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; }
45ca77ec
JM
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]); }
fdfa00d2 56 void get_current_machine_position(float *pos) const;
e03f2747 57 int print_position(uint8_t subcode, char *buf, size_t bufsize) const;
40843ebc 58 uint8_t get_current_wcs() const { return current_wcs; }
34210908 59 std::vector<wcs_t> get_wcs_state() const;
4440e123
JM
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; }
121094a5 62 bool delta_move(const float delta[], float rate_mm_s, uint8_t naxis);
29e809e0 63 uint8_t register_motor(StepperMotor*);
8a9f9313 64 uint8_t get_number_registered_motors() const {return n_motors; }
34210908 65
6de8ab5b 66 BaseSolution* arm_solution; // Selected Arm solution ( millimeters to step calculation )
dd0a7cfa
JM
67
68 // gets accessed by Panel, Endstops, ZProbe
2cd32d70 69 std::vector<StepperMotor*> actuators;
a974cd04 70
3632a517 71 // set by a leveling strategy to transform the target of a move according to the current plan
8fe38353 72 std::function<void(float*, bool)> compensationTransform;
325ed08b 73 // set by an active extruder, returns the amount to scale the E parameter by (to convert mm³ to mm)
121094a5 74 std::function<float(void)> get_e_scale_fnc;
33742399 75
a395f085 76 // Workspace coordinate systems
31c6c2c2 77 wcs_t mcs2wcs(const wcs_t &pos) const;
29e809e0 78 wcs_t mcs2wcs(const float *pos) const { return mcs2wcs(wcs_t(pos[X_AXIS], pos[Y_AXIS], pos[Z_AXIS])); }
a395f085 79
02e4b295
JM
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
29e809e0 83 bool e_absolute_mode:1; // true for absolute mode for E ( default ), false for relative mode
00e607c7 84 bool next_command_is_MCS:1; // set by G53
778093ce 85 bool disable_segmentation:1; // set to disable segmentation
84cf4071 86 bool disable_arm_solution:1; // set to disable the arm solution
a3e1326a 87 bool segment_z_moves:1;
3aad33c7 88 bool save_g92:1; // save g92 on M500 if set
e560f057 89 bool is_g123:1;
40843ebc
JM
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;
02e4b295
JM
93 };
94
a974cd04 95 private:
29e809e0
JM
96 enum MOTION_MODE_T {
97 NONE,
98 SEEK, // G0
99 LINEAR, // G1
100 CW_ARC, // G2
101 CCW_ARC // G3
102 };
103
807b9b57 104 void load_config();
c1ebb1fe 105 bool append_milestone(const float target[], float rate_mm_s);
29e809e0 106 bool append_line( Gcode* gcode, const float target[], float rate_mm_s, float delta_e);
350c8a60 107 bool append_arc( Gcode* gcode, const float target[], const float offset[], float radius, bool is_clockwise );
29e809e0
JM
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);
6de8ab5b 110
1ad23cd3 111 float theta(float x, float y);
4cff3ded 112 void select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2);
dd0a7cfa 113 void clearToolOffset();
325ed08b 114 int get_active_extruder() const;
c2f7c261 115
0b8b81b6 116 std::array<wcs_t, MAX_WCS> wcs_offsets; // these are persistent once saved with M500
34210908 117 uint8_t current_wcs{0}; // 0 means G54 is enabled this is persistent once saved with M500
807b9b57 118 wcs_t g92_offset;
c2f7c261 119 wcs_t tool_offset; // used for multiple extruders, sets the tool offset for the current extruder applied first
078f76e0 120 std::tuple<float, float, float, uint8_t> last_probe_position{0,0,0,0};
807b9b57 121
29e809e0 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
562db364 123 std::stack<saved_state_t> state_stack; // saves state from M120
807b9b57 124
45ca77ec
JM
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)
29e809e0 127
788e5809
JM
128 float seek_rate; // Current rate for seeking moves ( mm/min )
129 float feed_rate; // Current rate for feeding moves ( mm/min )
1ad23cd3 130 float mm_per_line_segment; // Setting : Used to split lines into segments
a3be54e3 131 float mm_per_arc_segment; // Setting : Used to split arcs into segments
83c6e067 132 float mm_max_arc_error; // Setting : Used to limit total arc segments to max error
1ad23cd3 133 float delta_segments_per_second; // Setting : Used to split lines into segments for delta based on speed
dd0a7cfa 134 float seconds_per_minute; // for realtime speed change
29e809e0 135 float default_acceleration; // the defualt accleration if not set for each axis
e560f057 136 float s_value; // modal S value
7369629d 137
b66fb830 138 // Number of arc generation iterations by small angle approximation before exact arc trajectory
a3be54e3 139 // correction. This parameter may be decreased if there are issues with the accuracy of the arc
b66fb830
AW
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.
29e809e0
JM
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
29e809e0 145
29e809e0 146 uint8_t n_motors; //count of the motors/axis registered
b66fb830 147
8a9f9313 148 // Used by Planner
dd0a7cfa 149 friend class Planner;
4cff3ded
AW
150};
151
6de8ab5b 152
4cff3ded 153#endif