Prusa-MMU-Private
PrusaMultiMaterialUpgradev3firmwareforMK3SMK4
globals.h
Go to the documentation of this file.
1 #pragma once
3 #include <stdint.h>
4 #include "../config/config.h"
5 
6 namespace modules {
7 
9 namespace globals {
10 
15 enum FilamentLoadState : uint8_t {
16  NotLoaded = 0,
17  AtPulley = 1,
18  InSelector = 2,
19  InFSensor = 4,
20  InNozzle = 8,
21 };
22 
23 static_assert(
24  /*(FilamentLoadState::NotLoaded < FilamentLoadState::AtPulley)
25 &&*/
29  "incorrect order of Slot Filament Load States");
30 
32 class Globals {
33 public:
35  void Init();
36 
39  uint8_t ActiveSlot() const;
40 
43 
48  void SetFilamentLoaded(uint8_t slot, FilamentLoadState newFilamentLoaded);
49 
52  uint16_t DriveErrors() const;
53 
55  void IncDriveErrors();
56 
61  void SetMotorsMode(bool stealth);
62 
64  bool MotorsStealth() const { return stealthMode; }
65 
66  config::U_mm FSensorToNozzle_mm() const { return config::U_mm({ (long double)fsensorToNozzle_mm }); }
67  void ResetFSensorToNozzle() { fsensorToNozzle_mm = config::fsensorToNozzle.v; }
68  void SetFSensorToNozzle_mm(uint8_t fss2Nozzle_mm) { fsensorToNozzle_mm = fss2Nozzle_mm; }
69 
70  config::U_mm FSensorUnloadCheck_mm() const { return config::U_mm({ (long double)fsensorUnloadCheck_mm }); }
71  void ResetFSensorUnloadCheck() { fsensorUnloadCheck_mm = config::fsensorUnloadCheckDistance.v; }
72  void SetFSensorUnloadCheck_mm(uint8_t fs2UnlCheck_mm) { fsensorUnloadCheck_mm = fs2UnlCheck_mm; }
73 
74  config::U_mm_s PulleyLoadFeedrate_mm_s() const { return config::U_mm_s({ (long double)pulleyLoadFeedrate_mm_s }); }
75  void ResetPulleyLoadFeedrate() { pulleyLoadFeedrate_mm_s = config::pulleyLoadFeedrate.v; }
76  void SetPulleyLoadFeedrate_mm_s(uint16_t pulleyLoadFR_mm_s) { pulleyLoadFeedrate_mm_s = pulleyLoadFR_mm_s; }
77 
78  config::U_mm_s PulleySlowFeedrate_mm_s() const { return config::U_mm_s({ (long double)pulleySlowFeedrate_mm_s }); }
79  void ResetPulleySlowFeedrate() { pulleySlowFeedrate_mm_s = config::pulleySlowFeedrate.v; }
80  void SetPulleySlowFeedrate_mm_s(uint16_t pulleySlowFR_mm_s) { pulleySlowFeedrate_mm_s = pulleySlowFR_mm_s; }
81 
82  config::U_mm_s PulleyUnloadFeedrate_mm_s() const { return config::U_mm_s({ (long double)pulleyUnloadFeedrate_mm_s }); }
83  void ResetPulleyUnloadFeedrate() { pulleyUnloadFeedrate_mm_s = config::pulleyUnloadFeedrate.v; }
84  void SetPulleyUnloadFeedrate_mm_s(uint16_t pulleyUnloadFR_mm_s) { pulleyUnloadFeedrate_mm_s = pulleyUnloadFR_mm_s; }
85 
86  config::U_mm_s SelectorFeedrate_mm_s() const { return config::U_mm_s({ (long double)selectorFeedrate_mm_s }); }
87  void ResetSelectorFeedrate() { selectorFeedrate_mm_s = config::selectorFeedrate.v; }
88  void SetSelectorFeedrate_mm_s(uint16_t selectorFR_mm_s) { selectorFeedrate_mm_s = selectorFR_mm_s; }
89 
90  config::U_deg_s IdlerFeedrate_deg_s() const { return config::U_deg_s({ (long double)idlerFeedrate_deg_s }); }
91  void ResetIdlerFeedrate() { idlerFeedrate_deg_s = config::idlerFeedrate.v; }
92  void SetIdlerFeedrate_deg_s(uint16_t idlerFR_deg_s) { idlerFeedrate_deg_s = idlerFR_deg_s; }
93 
94  config::U_mm_s SelectorHomingFeedrate_mm_s() const { return config::U_mm_s({ (long double)selectorHomingFeedrate_mm_s }); }
95  void ResetSelectorHomingFeedrate() { selectorHomingFeedrate_mm_s = config::selectorHomingFeedrate.v; }
96  void SetSelectorHomingFeedrate_mm_s(uint16_t selectorFR_mm_s) { selectorHomingFeedrate_mm_s = selectorFR_mm_s; }
97 
98  config::U_deg_s IdlerHomingFeedrate_deg_s() const { return config::U_deg_s({ (long double)idlerHomingFeedrate_deg_s }); }
99  void ResetIdlerHomingFeedrate() { idlerHomingFeedrate_deg_s = config::idlerHomingFeedrate.v; }
100  void SetIdlerHomingFeedrate_deg_s(uint16_t idlerFR_deg_s) { idlerHomingFeedrate_deg_s = idlerFR_deg_s; }
101 
103  uint8_t StallGuardThreshold(config::Axis axis) const;
105  void SetStallGuardThreshold(config::Axis axis, uint8_t sgthrs);
106 
108  uint8_t CutIRunCurrent() const { return cutIRunCurrent; }
109  void ResetCutIRunCurrent() { cutIRunCurrent = config::selectorCutIRun; }
110  void SetCutIRunCurrent(uint8_t v) { cutIRunCurrent = v; }
111 
112  config::U_mm CutLength() const { return config::U_mm({ (long double)cutLength_mm }); }
113  void ResetCutLength() { cutLength_mm = config::cutLength.v; }
114  void SetCutLength(uint8_t v) { cutLength_mm = v; }
115 
116 private:
120  void SetActiveSlot(uint8_t newActiveSlot);
121 
122  uint8_t activeSlot;
123  FilamentLoadState filamentLoaded;
124  bool stealthMode;
125  uint8_t fsensorToNozzle_mm;
126  uint8_t fsensorUnloadCheck_mm;
127 
128  uint16_t pulleyLoadFeedrate_mm_s;
129  uint16_t pulleySlowFeedrate_mm_s;
130  uint16_t pulleyUnloadFeedrate_mm_s;
131 
132  uint16_t selectorFeedrate_mm_s;
133  uint16_t selectorHomingFeedrate_mm_s;
134 
135  uint16_t idlerFeedrate_deg_s;
136  uint16_t idlerHomingFeedrate_deg_s;
137 
138  uint8_t cutIRunCurrent;
139  uint8_t cutLength_mm;
140 };
141 
143 extern Globals globals;
144 
145 } // namespace globals
146 } // namespace modules
147 
148 namespace mg = modules::globals;
Globals keep track of global state variables in the firmware.
Definition: globals.h:32
FilamentLoadState FilamentLoaded() const
Definition: globals.cpp:50
bool MotorsStealth() const
Definition: globals.h:64
uint8_t ActiveSlot() const
Definition: globals.cpp:42
void SetMotorsMode(bool stealth)
Definition: globals.cpp:80
void SetFilamentLoaded(uint8_t slot, FilamentLoadState newFilamentLoaded)
uint16_t DriveErrors() const
Definition: globals.cpp:72
void SetStallGuardThreshold(config::Axis axis, uint8_t sgthrs)
Stores the new StallGuard threshold for an axis into EEPROM (does not affect the current state of TMC...
Definition: globals.cpp:91
uint8_t StallGuardThreshold(config::Axis axis) const
Definition: globals.cpp:85
uint8_t CutIRunCurrent() const
Definition: globals.h:108
void IncDriveErrors()
Increment MMU errors by 1.
Definition: globals.cpp:76
void Init()
Initializes the global storage hive - basically looks into EEPROM to gather information.
Definition: globals.cpp:12
static constexpr U_mm_s pulleyLoadFeedrate
Definition: config.h:131
static constexpr U_mm fsensorUnloadCheckDistance
Check the state of FSensor after this amount of filament got (hopefully) pulled out while unloading.
Definition: config.h:108
static constexpr uint8_t selectorCutIRun
660mA
Definition: config.h:146
static constexpr U_mm fsensorToNozzle
~20mm from MK4's filament sensor through extruder gears into nozzle
Definition: config.h:105
Axis
List of available axes.
Definition: axis.h:35
The globals namespace provides all necessary facilities related to keeping track of global state of t...
Definition: globals.cpp:8
Globals globals
The one and only instance of global state variables.
Definition: globals.cpp:10
FilamentLoadState
Definition: globals.h:15
@ AtPulley
loaded to mmu (idler and pulley can grab it)
Definition: globals.h:17
@ InFSensor
'f1' loaded into printer's filament sensor
Definition: globals.h:19
@ InNozzle
'f2' loaded into printer's nozzle
Definition: globals.h:20
@ InSelector
'P1' loaded in selector (i.e. unsure where the filament is while doing some operation)
Definition: globals.h:18
@ NotLoaded
not loaded in the MMU at all
Definition: globals.h:16
The modules namespace contains models of MMU's components.
Definition: command_base.h:8
Generic unit type for compile-time conformability testing.
Definition: unit.h:42