because of the change of semantics of LoadFilament operation.
LoadFilament pushes the filament into FINDA and then retracts it back just to keep the
filament ready to be grabbed by the idler and pulley and loaded into the printer's nozzle.
So the selector is not blocked by the filament -> filament NOT loaded
I hate when the compiler doesn't check something what it normally does:
`pgm_read_byte` is more than happy with a parameter (*str), which reads
an address at a location where *str points to - which is obviously not the intent.
Load filament performs feed to FINDA and retract:
- engage idler
- feed normal to FINDA with config::feedToFinda distance until FINDA triggers
- retract normal and as soon FINDA un-triggers move back to PTFE config::cuttingEdgeToFindaMidpoint
- disengage the idler
That implied introducing another substate machine - RetractFromFinda, which does the opposite
of FeedToFinda while also checking for the FINDA switching off while retracting filament.
Still, ToolChange and CutFilament need fixing with this change
kudos to @leptun for this original and actually a very clean idea.
For the start we report "Reset finished" which in fact corresponds with the MMU state pretty closely
and plays nicely even with the protocol implementation.
And, since the default startup command is the noCommand, which always returns "Finished"
the implementation is clean and straightforward - the response to the first Q0 messages
will look like "X0 F" until a command (T, L, U ...) has been issued.
My MM-control-board v0.3 has following ADC readings in DEBUG_BUTTONS
- none = 1023
- left = 169
- mid = 91-92
- right = 0
As the comparison was larger than 0 MY MMU2 right button wasn't detected.
This introduces a new #define UNITTEST_MOTION which is used to control
the testing scenario:
- Normal tests, we allow the stub to override the built-in definition.
- For motion tests, we stub the lower-level classes and test the
effective implementation
We also repeat the prototype of the function, which IMHO is more
readable and more flexible: we need to use inline for the real
definition, which would require even more macros otherwise.
That implies changing motor's mode from SpreadCycle into StealtMode (or vice versa)
requires a stand still MMU with no other command (i.e. motor moves) being performed.
This elegantly solves the synchronization problem of TMC2130 mode change, as it results
in severe jerking while a motor is moving.
The change in protocol is minimal - M0/M1 first return `M0 A` (accepted) and another `Q0` then
returns `M0 F` (finished). The MK4 counterpart may ignore the additional report if necessary
as the mode change is done immediately (shortly after responding with `M0 A`)
Motion::SetMode(axis, mode) was incorrectly looping through all axes,
setting the same axis three times.
Fix this and introduce Motion::SetMode(mode) which actually loops
through all axes (see PR #110)
If the queue is full and a new move is queued, panic!
Introduce a new error code QUEUE_FULL to help diagnose situations where
the queue is handled improperly: likely one of the state machines not
waiting for the previous actions to finish.
PulseGen::PlanMove returns a boolean if the queue cannot be moved.
We could extend this to Motion::PlanMove, however all moves would then
have to check for this. Having a global check such as this ensures
we never ignore such situation.
we shall think about the Pulley as well, it looks like it should get its
own class just like Idler and Selector as it behaves very similarly in terms
of stepping and error checking
Test performed by moving 500mm of filament back&forth 5 times across 5
pulleys of a MMU2 unit.
The error with this factor is centered around +/- 1.5mm depending on the
pulley and tension on the idler.
Allow to chain moves by adding one extra parameter to the PlanMove[to]
functions: ending speed.
A move will always be accelerated from the last speed towards end ending
speed. The following:
PlanMove(100._mm, 50._mm_s, 50._mm_s);
PlanMove(200._mm, 100._mm_s);
Will first move the axis 100mm, accelerating towards 50mm/s, then
accelerate again to 100mm/s. The move will for then decelerate towards a
full stop after reaching 300mm in total.
Acceleration can be changed for each segment, so that a custom
acceleration curve can be created:
SetAcceleration(10._mm_s2);
PlanMove(100._mm, 50._mm_s, 50._mm_s);
SetAcceleration(100._mm_s2);
PlanMove(100._mm, 50._mm_s, 50._mm_s);
The ending speed might not always be reached, depending on the current
acceleration settings. The new function "Rate()" will return the ending
feedrate of the last move, if necessary.
AbortPlannedMoves accepts a new "halt" parameter to control how moves
will be chanined when interrupting the current move. By default
(halt=true) the move is completely interrupted.
When halt=false is requested, a subsequent move will be chained starting
at the currently aborted velocity. This allows to chain moves in reponse
to events, for example to accelerate the pulley without stopping as soon
as the FINDA is triggered, it's sufficient to interrupt the current move
followed by a new one:
PlanMove(maximum_loading_lenght, slow_feedrate);
... wait for PINDA trigger ...
AbortPlannedMoves(true);
PlanMove(bowden_lenght, fast_feedrate);
will seamlessy continue loading and transition to the fast feedrate.
Jerk control has been simplified. It now handles only the maximal
velocity change of the last segment, which doesn't require reverse
planning.
Add a new parameter "halt" (default to true) to control the stopping
behavior:
- halt=true: no subsequent moves will be planned, motions stops abruptly
- half=false: a new move will be chained after the current one
Move motion.Step() directly inside the __AVR__ code, silencing an unused
variable warning.
Calling motion.Step() without getting or setting the timer is not useful
anyway.
Avoid calling PulseGen::Step() on idle axes by checking for a non-zero
queue size (which is more efficient to compute).
Increase stepTimerQuantum to 128us to ensure acceleration can be
computed in realtime for 3 axes at the same time.
Fix the logic of the static assertion, which was flipped: we need to
create slices larger than the maximal step frequency in order to ensure
no axis is starved while moving.
TogglePin was incorrectly using the PINx pin for toggling, causing the
current pins (in addition to the requested ones) to be toggled, causing
stepping havoc.
This is a tentative/crude implementation of an Init and ISR for the MMU
in order to check the motion API.
We remove the "extern void Isr", declaring it "static inline" instead.
We need to inline the ISR here in order to avoid the function call.
Include the missing speed_table data in the executable. This bumps the
code size to ~60% of the flash.
Implemement motion::Init to setup the ISR and timers, and replace the
call in main from tmc::Init to motion::Init. Motion will init each
driver every time the axis is enabled, so there should be no need for
a global module initialization (we need SPI, but this is initialized
earlier on by it's own module anyway).
The timer is currently setup without any HAL or proper TIMER1 wrapper.
This is to be improved later.
The real MMU unit seems to slow down quite a bit during acceleration.
At this point we need to inline some methods in PulseGen to avoid
overhead, however this breaks the stubs.
The parameter config::AxisConfig::uSteps was supposed to be
microstepping resolution, but it's instead being used as the driver's
MRES directly.
To avoid a runtime conversion, rename the field to mRes and define a new
enum listing all the possible (and valid) microstepping resolutions.
This simplifies the code and makes clear the stepsPerUnit scale.
Assign correct stepsPerUnit to all axes as a result, including working
limits.
Besides Unload Filament, which only operates on active slot, all other
top level state machines check the validity of the command's parameter.
If the parameter is out of range for available slots, they return
ErrorCode::INVALID_TOOL now.
Extracted from the previous FW, may need some tuning based on units selected
for each of these axes (degrees, millimeters) - waiting for an update
of the motion implementation.
Updated starting conditions of the unit tests to reflect the global configuration.
MMU-58
- Distinguish among FINDA on/off failuje
- The same applies to newly introduced Filament sensor errors
- Add TMC init error
- Add a communication error ID - to be used on the printer
This allows Unit<> and AxisUnit<> to be scaled with a fraction as
expected, promoting the scaler to the same unit:
0.2_mm * 10 => 2_mm (mm*f => mm)
Multiplication type is commutative:
10 * 0.2_mm => 2_mm (f*mm => mm)
Division isn't:
0.2_mm / 10 => 0.02_mm (mm*1/f => mm)
10 / 0.2_mm => error (illegal type conversion)
Introduces a nasty hack to forcefully write into the constexpr SPI descriptor's registers
(which is the correct way in ASM, but kind of cumbersome in C++ now)
Please note Windows CRLF sequence is not supported,
but a separate CR xor a separate LF works now.
This is a workaround for stupid terminals for debugging purposes,
and it is not necessary for the protocol to work on its own.
It may be removed in the future.
- Move unit/step conversion to modules/axisunit.h
- Unify motion::unitToAxisUnit<> and motion::unitToSteps<>,
making conversion in other modules just as easy as motion.
- Improve the documentation
Introduces:
- config::Unit: base class for physical quantities
- motion::AxisUnit: type-checked steps type
"config/unit.h" defines basic physical quantities, which are not
normally used elsewhere besides config.h.
"modules/axisunit.h" extends the modules::motion namespace with
Axis-aware units, with one type per axis per unit.
P_pos_t defines step positions for the pulley, I_pos_t for the idler,
etc. These are defined through the literar operators which are
similarly named and automatically convert a physical quantity to an
AxisUnit at compile time:
P_pos_t pulley_pos = 10.0_P_mm;
Besides type-checking, AxisUnit are otherwise identical to raw step
counts and are intended to be used along with the updated Motion API.
PlanMove/PlanMoveTo has been extended to support moves using these units
or physical quantities. Again, conversion is performed at compile time.
At least for min/max this will ensure types for both arguments are the
same.
This should be a little bit closer to the <cmath> definition that
simply overloads these functions instead.
This matches PulseGen::Position() and avoids confusion around the term
"current": Position() returns the head position in the queue, not the
"live" axis position.
We have PulseGen::CurPosition() now for this purpose, although we don't
expose it to Motion yet.
CurPosition() returns the live axis position, which in this
implementation is inherently expensive to compute.
This shouldn't be required for the MMU, but it /will/ come in handy to
check for the axis position/s in Motion tests.
Make ReadPin return the last value set by WritePin for proper testing.
Add a slow-path to TogglePin that goes through a read-write cycle. This
is useful both for testing and for platforms that don't have an
efficient toggle like AVR.
The cast to Level is incorrect, since the expression returns either 0 or
a positive value if the pin is set. The value is directly assigned to
the underlying uint8_t, meaning that Level::high won't match for levels
greater than 0.
Return a boolean and cast that to Level instead.
Instead of adding #ifdefs for missing headers, create a shim that can be
included everywhere.
"limits.h" includes a bare-bone implementation of numeric_limits (to be
extended as needed).
Motion::Full() (without a specific axis) is counter-productive.
When planning new moves the axis needs to be known beforehand, so it
might be as well be given to Full() to check the proper queue.
Implement Motion::SetEnabled (for symmetry with TMC2130::SetEnabled).
Rename DisableAxis to Disable and use the new SetEnabled. This makes the
member names more consistent.
Make ReadPin return the last value set by WritePin for proper testing.
Add a slow-path to TogglePin that goes through a read-write cycle. This
is useful both for testing and for platforms that don't have an
efficient toggle like AVR.
The cast to Level is incorrect, since the expression returns either 0 or
a positive value if the pin is set. The value is directly assigned to
the underlying uint8_t, meaning that Level::high won't match for levels
greater than 0.
Return a boolean and cast that to Level instead.
We might want to schedule new moves while a single motor is moving.
Allow to do that by introducing per-axis query functions.
The main QueueEmpty() and Full() still function as before:
- Call QueueEmpty() to wait for all moves to finish.
- Use !Full() to know that a Plan() move will never be discarded.
Since scheduling a move on a block which is being executed will jolt the
motors, be extra-safe and perform an extra lower-level check before
committing even if the caller is responsible.
Return the status, which can be useful to build a simple busy loop.
- Remove the combined PlanMove(a,b,c,rate) call. If we allow the units
of the various motors to be changed at compile time, the unit of
rate can vary between axes.
- Build PlanMove on top of the absolute PlanMoveTo.
- Add required stubs for TMC2130.
- Allow each axis mode to be set independently, since we have this
feature for free anyway.
- Rework internals to use PulseGen data types and structs.
To generate optimal code, size itself needs to be of the same type as
the index to avoid promotion to the largest type.
In full(), wrap the subtraction explicity in another type cast to avoid
another automatic type promotion.
The empty/full distinction fails to work if size matches the number of
representable positions for the index type.
Add a static check to ensure this invariant is met where possible.
Comparing head/tail indexes cannot distinguish between empty/full cases,
so we end up wasting one item in the circular buffer. This also limits
the smallest and efficient size choice to be 4.
If the circular buffer is large, there's no issue, however for the
motion planner the block size is significant, and I was planning to use
exactly a ring buffer of two: one busy block, plus one planned.
Modify the indexer to store the internal "index" (aka cursor) pointer to
be one extra bit deeper than the final index. Comparing the underlying
cursor allow to distinguish the empty/full case due to the extra bit,
while producing the final index requires simple masking.
This is just as efficient if the size is a power of two with
2-complement wrap-around logic, which is the optimized case. However
the implementation also works for non-power-of-two sizes.
Add tests for more failure cases in the CircularBuffer which is built on
top.
(tecnique described in the Art of Computer Programming by Knuth)
Allow the lower-level index to be used without an actual container by
splitting off the index management into CircularIndex.
Rebuild CircularBuffer using CircularIndex itself.
Remove the constructor from GPIO_pin so that we can use brace
initialization at compile time.
Rewrite the contents of pins.h to construct GPIO_pin types directly by
the use of a simple preprocessor macro.
Makes the code type-check and easier to read/extend.
To keep these methods efficient no member should be accessed away.
That's the reason MotorParams is passed explicitly for each.
Mark all stepping methods as static.
Change the TMC2130 class after starting to review the Motion API.
Move motor mode handling and stepping to this module.
Create a new MotorParams class for compile-time settings (currently
pins, direction, microstepping).
Keep other settings separate, so that we can pass constexpr expressions
while stepping and still get 1 instruction without overhead.
+ start shaping up main.cpp
+ make the usage of namespaces and class names more consistent throughout the whole project
+ refactor related unit tests accordingly