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