refactor ON_HALT, add THEKERNEL->is_halted() for modules that just need to test it...
[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
19 class Gcode;
20 class BaseSolution;
21 class StepperMotor;
22
23 class Robot : public Module {
24 public:
25 Robot();
26 void on_module_loaded();
27 void on_config_reload(void* argument);
28 void on_gcode_received(void* argument);
29
30 void reset_axis_position(float position, int axis);
31 void reset_axis_position(float x, float y, float z);
32 void reset_position_from_current_actuator_position();
33 void get_axis_position(float position[]);
34 float to_millimeters(float value);
35 float from_millimeters(float value);
36 float get_seconds_per_minute() const { return seconds_per_minute; }
37 float get_z_maxfeedrate() const { return this->max_speeds[2]; }
38 void setToolOffset(const float offset[3]);
39 float get_feed_rate() const { return feed_rate; }
40
41 BaseSolution* arm_solution; // Selected Arm solution ( millimeters to step calculation )
42
43 // gets accessed by Panel, Endstops, ZProbe
44 std::vector<StepperMotor*> actuators;
45
46 // set by a leveling strategy to transform the target of a move according to the current plan
47 std::function<void(float[3])> compensationTransform;
48
49 struct {
50 bool inch_mode:1; // true for inch mode, false for millimeter mode ( default )
51 bool absolute_mode:1; // true for absolute mode ( default ), false for relative mode
52 };
53
54 private:
55 void distance_in_gcode_is_known(Gcode* gcode);
56 void append_milestone( Gcode *gcode, float target[], float rate_mm_s);
57 void append_line( Gcode* gcode, float target[], float rate_mm_s);
58 void append_arc( Gcode* gcode, float target[], float offset[], float radius, bool is_clockwise );
59 void compute_arc(Gcode* gcode, float offset[], float target[]);
60
61 float theta(float x, float y);
62 void select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2);
63 void clearToolOffset();
64 void check_max_actuator_speeds();
65
66 typedef std::tuple<float, float, bool> saved_state_t; // save current feedrate and absolute mode
67 std::stack<saved_state_t> state_stack; // saves state from M120
68 float last_milestone[3]; // Last position, in millimeters
69 float transformed_last_milestone[3]; // Last transformed position
70 int8_t motion_mode; // Motion mode for the current received Gcode
71 uint8_t plane_axis_0, plane_axis_1, plane_axis_2; // Current plane ( XY, XZ, YZ )
72 float seek_rate; // Current rate for seeking moves ( mm/s )
73 float feed_rate; // Current rate for feeding moves ( mm/s )
74 float mm_per_line_segment; // Setting : Used to split lines into segments
75 float mm_per_arc_segment; // Setting : Used to split arcs into segmentrs
76 float delta_segments_per_second; // Setting : Used to split lines into segments for delta based on speed
77 float seconds_per_minute; // for realtime speed change
78
79 // Number of arc generation iterations by small angle approximation before exact arc trajectory
80 // correction. This parameter maybe decreased if there are issues with the accuracy of the arc
81 // generations. In general, the default value is more than enough for the intended CNC applications
82 // of grbl, and should be on the order or greater than the size of the buffer to help with the
83 // computational efficiency of generating arcs.
84 int arc_correction; // Setting : how often to rectify arc computation
85 float max_speeds[3]; // Setting : max allowable speed in mm/m for each axis
86
87 float toolOffset[3];
88
89 // Used by Stepper, Planner
90 friend class Planner;
91 friend class Stepper;
92
93 StepperMotor* alpha_stepper_motor;
94 StepperMotor* beta_stepper_motor;
95 StepperMotor* gamma_stepper_motor;
96 };
97
98 // Convert from inches to millimeters ( our internal storage unit ) if needed
99 inline float Robot::to_millimeters( float value ){
100 return this->inch_mode ? value * 25.4 : value;
101 }
102 inline float Robot::from_millimeters( float value){
103 return this->inch_mode ? value/25.4 : value;
104 }
105 inline void Robot::get_axis_position(float position[]){
106 memcpy(position, this->last_milestone, sizeof(float)*3 );
107 }
108
109 #endif