1a8852939e58f49b823f54779e486371ee6f08c8
[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 #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 void push_state();
47 void pop_state();
48 void check_max_actuator_speeds();
49 float to_millimeters( float value ) const { return this->inch_mode ? value * 25.4F : value; }
50 float from_millimeters( float value) const { return this->inch_mode ? value/25.4F : value; }
51 void get_axis_position(float position[]) const { memcpy(position, this->last_milestone, N_PRIMARY_AXIS); }
52 wcs_t get_axis_position() const { return wcs_t(last_milestone[X_AXIS], last_milestone[Y_AXIS], last_milestone[Z_AXIS]); }
53 int print_position(uint8_t subcode, char *buf, size_t bufsize) const;
54 uint8_t get_current_wcs() const { return current_wcs; }
55 std::vector<wcs_t> get_wcs_state() const;
56 std::tuple<float, float, float, uint8_t> get_last_probe_position() const { return last_probe_position; }
57 void set_last_probe_position(std::tuple<float, float, float, uint8_t> p) { last_probe_position = p; }
58 bool solo_move(const float target[], float rate_mm_s);
59 uint8_t register_motor(StepperMotor*);
60
61 BaseSolution* arm_solution; // Selected Arm solution ( millimeters to step calculation )
62
63 // gets accessed by Panel, Endstops, ZProbe
64 std::array<StepperMotor*, k_max_actuators> actuators;
65
66 // set by a leveling strategy to transform the target of a move according to the current plan
67 std::function<void(float[3])> compensationTransform;
68
69 // Workspace coordinate systems
70 wcs_t mcs2wcs(const wcs_t &pos) const;
71 wcs_t mcs2wcs(const float *pos) const { return mcs2wcs(wcs_t(pos[X_AXIS], pos[Y_AXIS], pos[Z_AXIS])); }
72
73 struct {
74 bool inch_mode:1; // true for inch mode, false for millimeter mode ( default )
75 bool absolute_mode:1; // true for absolute mode ( default ), false for relative mode
76 bool e_absolute_mode:1; // true for absolute mode for E ( default ), false for relative mode
77 bool next_command_is_MCS:1; // set by G53
78 bool disable_segmentation:1; // set to disable segmentation
79 bool segment_z_moves:1;
80 bool save_g92:1; // save g92 on M500 if set
81 uint8_t plane_axis_0:2; // Current plane ( XY, XZ, YZ )
82 uint8_t plane_axis_1:2;
83 uint8_t plane_axis_2:2;
84 };
85
86 private:
87 enum MOTION_MODE_T {
88 NONE,
89 SEEK, // G0
90 LINEAR, // G1
91 CW_ARC, // G2
92 CCW_ARC // G3
93 };
94
95 void load_config();
96 bool append_milestone( Gcode *gcode, const float target[], float rate_mm_s);
97 bool append_line( Gcode* gcode, const float target[], float rate_mm_s, float delta_e);
98 bool append_arc( Gcode* gcode, const float target[], const float offset[], float radius, bool is_clockwise );
99 bool compute_arc(Gcode* gcode, const float offset[], const float target[], enum MOTION_MODE_T motion_mode);
100 void process_move(Gcode *gcode, enum MOTION_MODE_T);
101
102 float theta(float x, float y);
103 void select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2);
104 void clearToolOffset();
105
106
107 std::array<wcs_t, MAX_WCS> wcs_offsets; // these are persistent once saved with M500
108 uint8_t current_wcs{0}; // 0 means G54 is enabled thisĀ is persistent once saved with M500
109 wcs_t g92_offset;
110 wcs_t tool_offset; // used for multiple extruders, sets the tool offset for the current extruder applied first
111 std::tuple<float, float, float, uint8_t> last_probe_position{0,0,0,0};
112
113 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
114 std::stack<saved_state_t> state_stack; // saves state from M120
115
116 float last_milestone[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
117 float last_machine_position[k_max_actuators]; // Last machine position, which is the position before converting to actuator coordinates (includes compensation transform)
118
119 float seek_rate; // Current rate for seeking moves ( mm/min )
120 float feed_rate; // Current rate for feeding moves ( mm/min )
121 float mm_per_line_segment; // Setting : Used to split lines into segments
122 float mm_per_arc_segment; // Setting : Used to split arcs into segments
123 float mm_max_arc_error; // Setting : Used to limit total arc segments to max error
124 float delta_segments_per_second; // Setting : Used to split lines into segments for delta based on speed
125 float seconds_per_minute; // for realtime speed change
126 float default_acceleration; // the defualt accleration if not set for each axis
127
128 // Number of arc generation iterations by small angle approximation before exact arc trajectory
129 // correction. This parameter may be decreased if there are issues with the accuracy of the arc
130 // generations. In general, the default value is more than enough for the intended CNC applications
131 // of grbl, and should be on the order or greater than the size of the buffer to help with the
132 // computational efficiency of generating arcs.
133 int arc_correction; // Setting : how often to rectify arc computation
134 float max_speeds[3]; // Setting : max allowable speed in mm/s for each axis
135 float e_scale; // how much to scale ay e motion by
136
137 uint8_t selected_extruder;
138 uint8_t n_motors; //count of the motors/axis registered
139
140 // Used by Stepper, Planner
141 friend class Planner;
142 friend class Stepper;
143 };
144
145
146 #endif