-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmctl.cpp
184 lines (148 loc) · 3.46 KB
/
mmctl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//mmctl.cpp - multimaterial switcher control
#include "main.h"
#include <Arduino.h>
#include <stdio.h>
#include <string.h>
#include <avr/io.h>
#include "shr16.h"
#include "spi.h"
#include "tmc2130.h"
#include "mmctl.h"
#include "motion.h"
#include "Buttons.h"
int lengthCorrection = 0;
int active_extruder = -1;
int previous_extruder = -1;
bool isFilamentLoaded = false;
bool isIdlerParked = false;
int toolChanges = 0;
bool isPrinting = false;
bool isHomed = false;
bool feed_filament()
{
bool _feed = true;
bool _loaded = false;
int _c = 0;
int _delay = 0;
park_idler(true);
set_pulley_dir_push();
tmc2130_init_axis_current(0, 1, 15);
do
{
do_pulley_step();
_c++;
if (_c > 50) { shr16_set_led(2 << 2 * (4 - active_extruder)); };
if (_c > 100) { shr16_set_led(0x000); _c = 0; _delay++; };
if (digitalRead(A1) == 1) { _loaded = true; _feed = false; };
if (buttonClicked() != 0 && _delay > 10) { _loaded = false; _feed = false; }
delayMicroseconds(4000);
} while (_feed);
if (_loaded)
{
// unload to PTFE tube
set_pulley_dir_pull();
for (int i = 600; i > 0; i--) // 570
{
do_pulley_step();
delayMicroseconds(3000);
}
}
tmc2130_init_axis_current(0, 0, 0);
park_idler(false);
shr16_set_led(1 << 2 * (4 - active_extruder));
return true;
}
bool switch_extruder_withSensor(int new_extruder)
{
isPrinting = true;
bool _return = false;
if (!isHomed) { home(); }
if (active_extruder == 5)
{
move(0, -700, 0);
active_extruder = 4;
}
toolChanges++;
shr16_set_led(2 << 2 * (4 - active_extruder));
previous_extruder = active_extruder;
active_extruder = new_extruder;
if (previous_extruder == active_extruder)
{
if (!isFilamentLoaded)
{
shr16_set_led(2 << 2 * (4 - active_extruder));
load_filament_withSensor(); // just load filament if not loaded
_return = true;
}
else
{
_return = false; // nothing really happened
}
}
else
{
if (isFilamentLoaded) { unload_filament_withSensor(); } // unload filament first
set_positions(previous_extruder, active_extruder); // move idler and selector to new filament position
shr16_set_led(2 << 2 * (4 - active_extruder));
load_filament_withSensor(); // load new filament
_return = true;
}
shr16_set_led(0x000);
shr16_set_led(1 << 2 * (4 - active_extruder));
return _return;
}
bool select_extruder(int new_extruder)
{
bool _return = false;
if (!isHomed) { home(); }
shr16_set_led(2 << 2 * (4 - active_extruder));
int previous_extruder = active_extruder;
active_extruder = new_extruder;
if (previous_extruder == active_extruder)
{
if (!isFilamentLoaded)
{
_return = true;
}
}
else
{
if (new_extruder == 5)
{
move(0, 700, 0);
}
else
{
if (previous_extruder == 5)
{
move(0, -700, 0);
}
else
{
if (isIdlerParked) park_idler(true);
set_positions(previous_extruder, active_extruder); // move idler and selector to new filament position
park_idler(false);
}
}
_return = true;
}
shr16_set_led(0x000);
shr16_set_led(1 << 2 * (4 - active_extruder));
return _return;
}
bool service_position(bool service)
{
move(0, 600, 0);
return true;
}
void led_blink(int _no)
{
shr16_set_led(1 << 2 * _no);
delay(40);
shr16_set_led(0x000);
delay(20);
shr16_set_led(1 << 2 * _no);
delay(40);
shr16_set_led(0x000);
delay(10);
}