add option to not segment z only moves, useful for deltas.
[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
17 #include "libs/Module.h"
18 #include "ActuatorCoordinates.h"
19
20 class Gcode;
21 class BaseSolution;
22 class StepperMotor;
23
24 // 9 WCS offsets
25 #define MAX_WCS 9UL
26
27 class Robot : public Module {
28 public:
29 using wcs_t= std::tuple<float, float, float>;
30 Robot();
31 void on_module_loaded();
32 void on_gcode_received(void* argument);
33
34 void reset_axis_position(float position, int axis);
35 void reset_axis_position(float x, float y, float z);
36 void reset_actuator_position(const ActuatorCoordinates &ac);
37 void reset_position_from_current_actuator_position();
38 float get_seconds_per_minute() const { return seconds_per_minute; }
39 float get_z_maxfeedrate() const { return this->max_speeds[2]; }
40 void setToolOffset(const float offset[3]);
41 float get_feed_rate() const;
42 void push_state();
43 void pop_state();
44 void check_max_actuator_speeds();
45 float to_millimeters( float value ) const { return this->inch_mode ? value * 25.4F : value; }
46 float from_millimeters( float value) const { return this->inch_mode ? value/25.4F : value; }
47 void get_axis_position(float position[]) const { memcpy(position, this->last_milestone, sizeof this->last_milestone); }
48 wcs_t get_axis_position() const { return wcs_t(last_milestone[0], last_milestone[1], last_milestone[2]); }
49 int print_position(uint8_t subcode, char *buf, size_t bufsize) const;
50 uint8_t get_current_wcs() const { return current_wcs; }
51 std::vector<wcs_t> get_wcs_state() const;
52 std::tuple<float, float, float, uint8_t> get_last_probe_position() const { return last_probe_position; }
53 void set_last_probe_position(std::tuple<float, float, float, uint8_t> p) { last_probe_position = p; }
54
55 BaseSolution* arm_solution; // Selected Arm solution ( millimeters to step calculation )
56
57 // gets accessed by Panel, Endstops, ZProbe
58 std::array<StepperMotor*, k_max_actuators> actuators;
59
60 // set by a leveling strategy to transform the target of a move according to the current plan
61 std::function<void(float[3])> compensationTransform;
62
63 // Workspace coordinate systems
64 wcs_t mcs2wcs(const wcs_t &pos) const;
65 wcs_t mcs2wcs(const float *pos) const { return mcs2wcs(wcs_t(pos[0], pos[1], pos[2])); }
66
67 struct {
68 bool inch_mode:1; // true for inch mode, false for millimeter mode ( default )
69 bool absolute_mode:1; // true for absolute mode ( default ), false for relative mode
70 bool next_command_is_MCS:1; // set by G53
71 bool disable_segmentation:1; // set to disable segmentation
72 bool segment_z_moves:1;
73 uint8_t plane_axis_0:2; // Current plane ( XY, XZ, YZ )
74 uint8_t plane_axis_1:2;
75 uint8_t plane_axis_2:2;
76 };
77
78 private:
79 void load_config();
80 void distance_in_gcode_is_known(Gcode* gcode);
81 bool append_milestone( Gcode *gcode, const float target[], float rate_mm_s);
82 bool append_line( Gcode* gcode, const float target[], float rate_mm_s);
83 bool append_arc( Gcode* gcode, const float target[], const float offset[], float radius, bool is_clockwise );
84 bool compute_arc(Gcode* gcode, const float offset[], const float target[]);
85 void process_move(Gcode *gcode);
86
87 float theta(float x, float y);
88 void select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2);
89 void clearToolOffset();
90
91
92 std::array<wcs_t, MAX_WCS> wcs_offsets; // these are persistent once saved with M500
93 uint8_t current_wcs{0}; // 0 means G54 is enabled thisĀ is persistent once saved with M500
94 wcs_t g92_offset;
95 wcs_t tool_offset; // used for multiple extruders, sets the tool offset for the current extruder applied first
96 std::tuple<float, float, float, uint8_t> last_probe_position{0,0,0,0};
97
98 using saved_state_t= std::tuple<float, float, bool, bool, uint8_t>; // save current feedrate and absolute mode, inch mode, current_wcs
99 std::stack<saved_state_t> state_stack; // saves state from M120
100
101 float last_milestone[3]; // Last requested position, in millimeters, which is what we were requested to move to in the gcode after offsets applied but before compensation transform
102 float last_machine_position[3]; // Last machine position, which is the position before converting to actuator coordinates (includes compensation transform)
103 int8_t motion_mode; // Motion mode for the current received Gcode
104 float seek_rate; // Current rate for seeking moves ( mm/s )
105 float feed_rate; // Current rate for feeding moves ( mm/s )
106 float mm_per_line_segment; // Setting : Used to split lines into segments
107 float mm_per_arc_segment; // Setting : Used to split arcs into segmentrs
108 float delta_segments_per_second; // Setting : Used to split lines into segments for delta based on speed
109 float seconds_per_minute; // for realtime speed change
110
111 // Number of arc generation iterations by small angle approximation before exact arc trajectory
112 // correction. This parameter maybe decreased if there are issues with the accuracy of the arc
113 // generations. In general, the default value is more than enough for the intended CNC applications
114 // of grbl, and should be on the order or greater than the size of the buffer to help with the
115 // computational efficiency of generating arcs.
116 int arc_correction; // Setting : how often to rectify arc computation
117 float max_speeds[3]; // Setting : max allowable speed in mm/m for each axis
118
119 // Used by Stepper, Planner
120 friend class Planner;
121 friend class Stepper;
122 };
123
124
125 #endif