forked from section77/EggDuino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EBBHardware.cpp
182 lines (154 loc) · 4.42 KB
/
EBBHardware.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
#include "EBBHardware.h"
#include "config.h"
#include <EEPROM.h>
#define EEPROM_PEN_UP_POS 0
#define EEPROM_PEN_DOWN_POS 2
#define EEPROM_PEN_UP_RATE 4
#define EEPROM_PEN_DOWN_RATE 6
EBBHardware::EBBHardware(Stream& stream)
: EBBParser(stream)
, mRotMotor(X_STEP_PIN, X_DIR_PIN)
, mPenMotor(Y_STEP_PIN, Y_DIR_PIN)
, mPenState(false)
, mPenUpPos(DEFAULT_PULSE_WIDTH - 200) // can be overwritten from EBB-Command SC
, mPenDownPos(DEFAULT_PULSE_WIDTH) // can be overwritten from EBB-Command SC
, mServoRateUp(0)
, mServoRateDown(0)
, mMotorEnabled(false)
, mPrgButtonState(false)
#ifdef PRG_BUTTON_PIN
, mPrgButtonToggle(PRG_BUTTON_PIN)
#endif
#ifdef PEN_TOGGLE_BUTTON_PIN
, mPenToggle(PEN_TOGGLE_BUTTON_PIN)
#endif
#ifdef MOTORS_BUTTON_PIN
, mMotorsToggle(MOTORS_BUTTON_PIN)
#endif
{
}
void EBBHardware::init()
{
EEPROM.get(EEPROM_PEN_UP_POS, mPenUpPos);
EEPROM.get(EEPROM_PEN_DOWN_POS, mPenDownPos);
EEPROM.get(EEPROM_PEN_UP_RATE, mServoRateUp);
EEPROM.get(EEPROM_PEN_DOWN_RATE, mServoRateDown);
pinMode(X_ENABLE_PIN, OUTPUT);
pinMode(Y_ENABLE_PIN, OUTPUT);
pinMode(ENGRAVER_PIN, OUTPUT);
mPenServo.attach(SERVO_PIN);
enableMotor(0, false);
enableMotor(1, false);
setPenState(mPenState, 0);
}
void EBBHardware::processEvents()
{
#ifdef PRG_BUTTON_PIN
if (mPrgButtonToggle.wasPressed()) {
mPrgButtonState = true;
}
#endif
#ifdef PEN_TOGGLE_BUTTON_PIN
if (mPenToggle.wasPressed()) {
setPenState(!getPenState(), 0);
}
#endif
#ifdef MOTORS_BUTTON_PIN
if (mMotorsToggle.wasPressed()) {
if (mMotorEnabled) {
enableMotor(0, false);
enableMotor(1, false);
} else {
enableMotor(0, true);
enableMotor(1, true);
}
}
#endif
parseStream();
mPenMotor.update();
mRotMotor.update();
}
void EBBHardware::enableMotor(int axis, bool state)
{
const uint8_t pin = (axis == 0) ? X_ENABLE_PIN : Y_ENABLE_PIN;
digitalWrite(pin, state ? LOW : HIGH);
mMotorEnabled = state;
}
void EBBHardware::stepperMove(int duration, int numPenSteps, int numRotSteps)
{
moveToDestination();
// Genuine EggBots are almost all using 16 microstepping.
// A standard nema 17 stepper has 200 steps.
// With 16 microstepping 200 * 16 = 3200 steps for a full revolution.
// If your EggDuino does not have the same number of steps,
// you need to adjust the inkscape plugin STEP_SCALE variable inside eggbot_conf.py.
// set Coordinates and Speed
if (numRotSteps == 0 && numPenSteps == 0) {
delay(duration);
} else {
mRotMotor.setTarget(numRotSteps, duration);
mPenMotor.setTarget(numPenSteps, duration);
}
}
void EBBHardware::moveToDestination()
{
while (mPenMotor.remainingSteps() || mRotMotor.remainingSteps()) {
mPenMotor.update(); // Moving.... moving... moving....
mRotMotor.update();
}
}
void EBBHardware::setPinOutput(char port, int pin, int value)
{
// PO,B,3,0 = disable engraver
// PO,B,3,1 = enable engraver
if (port == 'B' && pin == 3) {
digitalWrite(ENGRAVER_PIN, value);
}
}
void EBBHardware::setEngraverState(bool state, int power)
{
digitalWrite(ENGRAVER_PIN, state);
}
void EBBHardware::setPenState(bool up, short delayMs)
{
moveToDestination();
if (up) {
mPenServo.write(mPenUpPos, mServoRateUp);
} else {
mPenServo.write(mPenDownPos, mServoRateDown);
}
mPenState = up;
delay(delayMs);
}
bool EBBHardware::getPenState()
{
return mPenState;
}
void EBBHardware::setPenUpPos(int percent)
{
// transformation from percent to PWM-Servo
mPenUpPos = MIN_PULSE_WIDTH + (MAX_PULSE_WIDTH - MIN_PULSE_WIDTH) / 100.f * percent;
EEPROM.put(EEPROM_PEN_UP_POS, mPenUpPos);
}
void EBBHardware::setPenDownPos(int percent)
{
// transformation from percent to PWM-Servo
mPenDownPos = MIN_PULSE_WIDTH + (MAX_PULSE_WIDTH - MIN_PULSE_WIDTH) / 100.f * percent;
EEPROM.put(EEPROM_PEN_DOWN_POS, mPenDownPos);
}
void EBBHardware::setServoRateUp(int percentPerSecond)
{
mServoRateUp = percentPerSecond;
EEPROM.put(EEPROM_PEN_UP_RATE, mServoRateUp);
}
void EBBHardware::setServoRateDown(int percentPerSecond)
{
mServoRateDown = percentPerSecond;
EEPROM.put(EEPROM_PEN_DOWN_RATE, mServoRateDown);
}
bool EBBHardware::getPrgButtonState()
{
bool state = mPrgButtonState;
mPrgButtonState = false;
return state;
}