-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLatheArduinoAssistant.ino
120 lines (96 loc) · 2.74 KB
/
LatheArduinoAssistant.ino
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
#include "Config.h"
#include "GDisplay.h"
#include "GSpindle.h"
#include "GStepper.h"
#include "GHandWheel.h"
#include "GJoystick.h"
#include "GMenu.h"
/*********************************************************
Common variables
*********************************************************/
unsigned long timeNow = 0;
unsigned long timeOld = 0;
int temp;
LiquidCrystal_I2C lcd(0x3F, 20, 4);
GStepper zStepper;
GDisplay display(&lcd);
GSpindle spindle(SPINDLE_A);
Encoder zHandWheelEncoder(Z_HANDWHEEL_B, Z_HANDWHEEL_A);
GHandWheel zHandWheel;
Encoder menuEncoder(MENU_A, MENU_B);
GMenu menu;
GJoystick joystick;
OneButton menuEncoderButton(MENU_BUTTON, true);
void setup() {
Serial.begin(9600);
// initialize the LCD
lcd.begin();
//lcd.print(F("Init ..."));
joystick.initJoystick();
zStepper.setHandWheelFactor(zHandWheel.getSpeed());
attachInterrupt(0, callbackSpindle, RISING); //PIN2
Timer3.initialize(200);
menuEncoderButton.attachClick(callbackMenuClick);
menuEncoderButton.attachDoubleClick(callbackMenuDoubleClick);
menuEncoderButton.attachLongPressStart(callbackMenuLongPressStart);
menuEncoderButton.attachDuringLongPress(callbackMenuAttachDuringLongPress);
}
//volatile long oldPosition = 0;
void callbackSpindle()
{
spindle.callbackSpindleUpdate();
//Serial.println(zStepper.getCurrentPosition() - oldPosition);
//oldPosition = zStepper.getCurrentPosition();
if (zStepper.getFeedMode() == AUTO_FEED_MODE && zStepper.getFeedAutoMode() == AUTO_FEED_MODE_SYNC && spindle.isReady() )
{
//Serial.println(spindle.calculateMMPerSpindleRevolutionPerSecond(zStepper.getSyncSpeed()));
zStepper.setSpeed(spindle.calculateMMPerSpindleRevolutionPerSecond(zStepper.getSyncSpeed()));
}
}
void callbackTimer()
{
if (zStepper.getFeedMode() == MANUALLY_FEED_MODE) //manually feed
{
zStepper.run();
}
else
{
zStepper.runSpeed();
}
}
void callbackMenuClick()
{
menu.menuClick();
}
void callbackMenuDoubleClick()
{
menu.menuDoubleClick();
}
void callbackMenuAttachDuringLongPress()
{
menu.menuAttachDuringLongPress();
}
void callbackMenuLongPressStart()
{
menu.menuLongPressStart();
}
void loop() {
timeNow = millis();
joystick.checkCurrentJoystickPosition(timeNow);
menuEncoderButton.tick();
menu.checkMenu(timeNow, menuEncoder.read());
if (zStepper.getFeedMode() == MANUALLY_FEED_MODE) //manually feed
{
zHandWheel.handleUpdate(timeNow, zHandWheelEncoder.read());
}
//Serial.println(zStepper._delta);
if (timeNow - timeOld > 200)
{
//lcd.setCursor(1, 3);
//lcd.print(temp++);
//display.displayDistance(temp++);
display.displayDistance(zStepper.distanceInMM());
display.displaySpindleSpeed(spindle.calculateSpindleRevolutionsPerMinute());
timeOld = timeNow;
}
}