Prusa-MMU-Private
PrusaMultiMaterialUpgradev3firmwareforMK3SMK4
axisunit.h
Go to the documentation of this file.
1 #pragma once
3 #include "../config/axis.h"
4 #include "pulse_gen.h"
5 
6 namespace modules {
7 namespace motion {
8 
9 // Import required types
10 using config::Axis;
11 using config::Idler;
12 using config::Pulley;
13 using config::Selector;
14 
15 using config::Accel;
16 using config::Lenght;
17 using config::Speed;
18 
19 using pulse_gen::pos_t;
20 using pulse_gen::steps_t;
21 
67 template <typename T, Axis A, config::UnitType U>
68 struct AxisUnit {
69  T v;
70 
71  static constexpr Axis axis = A;
72  static constexpr config::UnitType unit = U;
73 
74  typedef T type_t;
75  typedef AxisUnit<T, A, U> self_t;
76 
77  // same-type operations
78  constexpr self_t operator+(const self_t r) const { return { v + r.v }; }
79  constexpr self_t operator-(const self_t r) const { return { v - r.v }; }
80  constexpr self_t operator-() const { return { -v }; }
81  constexpr self_t operator*(const self_t r) const { return { v * r.v }; }
82  constexpr self_t operator/(const self_t r) const { return { v / r.v }; }
83 
84  // allow an unitless multiplier to scale the quantity: AU * f => AU
85  constexpr self_t operator*(const long double f) const { return { (T)(v * f) }; }
86  constexpr self_t operator/(const long double f) const { return { (T)(v / f) }; }
87 };
88 
89 // complementary f * AU => AU * f
90 template <typename T, Axis A, config::UnitType U>
91 constexpr AxisUnit<T, A, U> operator*(const long double f, const AxisUnit<T, A, U> u) { return u * f; }
92 
94 struct AxisScale {
95  unit::UnitBase base;
96  long double stepsPerUnit;
97 };
98 
99 static constexpr AxisScale axisScale[config::NUM_AXIS] = {
103 };
104 
108 template <typename AU, typename U>
109 static constexpr AU unitToAxisUnit(U v) {
110  static_assert(AU::unit == U::unit, "incorrect unit type conversion");
111  static_assert(U::base == axisScale[AU::axis].base, "incorrect unit base conversion");
112  return { (typename AU::type_t)(v.v * axisScale[AU::axis].stepsPerUnit) };
113 }
114 
125 template <typename U, typename AU, typename T = int32_t>
126 static constexpr T axisUnitToTruncatedUnit(AU v, long double mul = 1.) {
127  static_assert(AU::unit == U::unit, "incorrect unit type conversion");
128  static_assert(U::base == axisScale[AU::axis].base, "incorrect unit base conversion");
129  return { ((T)v.v / (T)(axisScale[AU::axis].stepsPerUnit / mul)) };
130 }
131 
136 template <typename U, typename T = int32_t>
137 static constexpr T truncatedUnit(U v, long double mul = 1.) {
138  return (T)(v.v * mul);
139 }
140 
143 template <typename AU, typename U>
144 static constexpr typename AU::type_t unitToSteps(U v) {
145  return unitToAxisUnit<AU>(v).v;
146 }
147 
148 // Pulley
152 
153 static constexpr P_pos_t operator"" _P_mm(long double mm) {
154  return { unitToAxisUnit<P_pos_t>(config::U_mm { mm }) };
155 }
156 
157 static constexpr P_speed_t operator"" _P_mm_s(long double mm_s) {
158  return { unitToAxisUnit<P_speed_t>(config::U_mm_s { mm_s }) };
159 }
160 
161 static constexpr P_accel_t operator"" _P_mm_s2(long double mm_s2) {
162  return { unitToAxisUnit<P_accel_t>(config::U_mm_s2 { mm_s2 }) };
163 }
164 
165 // Selector
169 
170 static constexpr S_pos_t operator"" _S_mm(long double mm) {
171  return { unitToAxisUnit<S_pos_t>(config::U_mm { mm }) };
172 }
173 
174 static constexpr S_speed_t operator"" _S_mm_s(long double mm_s) {
175  return { unitToAxisUnit<S_speed_t>(config::U_mm_s { mm_s }) };
176 }
177 
178 static constexpr S_accel_t operator"" _S_mm_s2(long double mm_s2) {
179  return { unitToAxisUnit<S_accel_t>(config::U_mm_s2 { mm_s2 }) };
180 }
181 
182 // Idler
186 
187 static constexpr I_pos_t operator"" _I_deg(long double deg) {
188  return { unitToAxisUnit<I_pos_t>(config::U_deg { deg }) };
189 }
190 
191 static constexpr I_speed_t operator"" _I_deg_s(long double deg_s) {
192  return { unitToAxisUnit<I_speed_t>(config::U_deg_s { deg_s }) };
193 }
194 
195 static constexpr I_accel_t operator"" _I_deg_s2(long double deg_s2) {
196  return { unitToAxisUnit<I_accel_t>(config::U_deg_s2 { deg_s2 }) };
197 }
198 
199 } // namespace motion
200 } // namespace modules
201 
202 // Inject literal operators into the global namespace for convenience
203 using modules::motion::operator"" _P_mm;
204 using modules::motion::operator"" _P_mm_s;
205 using modules::motion::operator"" _P_mm_s2;
206 using modules::motion::operator"" _S_mm;
207 using modules::motion::operator"" _S_mm_s;
208 using modules::motion::operator"" _S_mm_s2;
209 using modules::motion::operator"" _I_deg;
210 using modules::motion::operator"" _I_deg_s;
211 using modules::motion::operator"" _I_deg_s2;
static constexpr AxisConfig selector
End: Pulley axis configuration.
Definition: config.h:136
static constexpr IdlerLimits idlerLimits
Idler motion limits.
Definition: config.h:197
static constexpr SelectorLimits selectorLimits
Selector motion limits.
Definition: config.h:149
static constexpr AxisConfig idler
End: Selector configuration.
Definition: config.h:186
static constexpr PulleyLimits pulleyLimits
Pulley motion limits.
Definition: config.h:122
static constexpr AxisConfig pulley
Begin: Pulley axis configuration.
Definition: config.h:111
static constexpr uint8_t NUM_AXIS
Number of available axes.
Definition: axis.h:43
Axis
List of available axes.
Definition: axis.h:35
Definition: command_base.h:9
AxisUnit< pos_t, Selector, Lenght > S_pos_t
Selector position type (steps)
Definition: axisunit.h:166
static constexpr T truncatedUnit(U v, long double mul=1.)
Definition: axisunit.h:137
AxisUnit< steps_t, Idler, Accel > I_accel_t
Idler acceleration type (steps/s2)
Definition: axisunit.h:185
AxisUnit< pos_t, Idler, Lenght > I_pos_t
Idler position type (steps)
Definition: axisunit.h:183
AxisUnit< steps_t, Idler, Speed > I_speed_t
Idler speed type (steps/s)
Definition: axisunit.h:184
AxisUnit< steps_t, Pulley, Speed > P_speed_t
Pulley speed type (steps/s)
Definition: axisunit.h:150
AxisUnit< steps_t, Selector, Accel > S_accel_t
Selector acceleration type (steps/s2)
Definition: axisunit.h:168
static constexpr AU::type_t unitToSteps(U v)
Definition: axisunit.h:144
AxisUnit< pos_t, Pulley, Lenght > P_pos_t
Pulley position type (steps)
Definition: axisunit.h:149
static constexpr AU unitToAxisUnit(U v)
Definition: axisunit.h:109
AxisUnit< steps_t, Selector, Speed > S_speed_t
Selector speed type (steps/s)
Definition: axisunit.h:167
AxisUnit< steps_t, Pulley, Accel > P_accel_t
Pulley acceleration type (steps/s2)
Definition: axisunit.h:151
static constexpr T axisUnitToTruncatedUnit(AU v, long double mul=1.)
Definition: axisunit.h:126
int32_t pos_t
Axis position (signed)
Definition: pulse_gen.h:22
uint32_t steps_t
Absolute step units.
Definition: pulse_gen.h:20
The modules namespace contains models of MMU's components.
Definition: command_base.h:8
Definition: unit.h:25
UnitType
Unit types for conformability testing.
Definition: unit.h:34
UnitBase
Base units for conformability testing.
Definition: unit.h:28
long double stepsPerUnit
steps per unit
Definition: axis.h:30
Axis type conversion table for template expansion.
Definition: axisunit.h:94
Definition: axisunit.h:68
Generic unit type for compile-time conformability testing.
Definition: unit.h:42