forked from fehlfarbe/arduino-motorfocus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduino-motorfocus.ino
More file actions
168 lines (151 loc) · 4.12 KB
/
arduino-motorfocus.ino
File metadata and controls
168 lines (151 loc) · 4.12 KB
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
//https://github.com/laurb9/StepperDriver
//https://www.omc-stepperonline.com/geared-stepper-motor/nema-17-stepper-motor-bipolar-l33mm-w-gear-raio-271-planetary-gearbox-17hs13-0404s-pg27.html
#include <SoftwareSerial.h>
#include <EEPROM.h>
#include "DRV8825.h"
#define RPM 2 //speed
#define ENABLE 7
#define DIR 5
#define STEP 6
#define MOTOR_STEPS 5370 //200*26.85
DRV8825 stepper(MOTOR_STEPS, DIR, STEP, ENABLE);
int direction = 1; //switch this from 1 to -1 to inverse direction
long currentPosition = 0;
long targetPosition = 0;
// read commands
const int maxCmd = 8;
bool eoc = false;
String line;
void setup() {
Serial.begin(9600);
// initalize motor
stepper.begin(RPM, 2);
stepper.enable();
currentPosition = 10000;
targetPosition = currentPosition;
}
void loop() {
// process the command we got
if (eoc) {
String cmd, param;
int len = line.length();
if (len >= 2) {
cmd = line.substring(0, 2);
}
if (len > 2) {
param = line.substring(2);
}
line = "";
eoc = false;
// LED backlight value, always return "00"
if (cmd.equalsIgnoreCase("GB")) {
Serial.print("00#");
}
// home the motor, hard-coded, ignore parameters since we only have one motor
else if (cmd.equalsIgnoreCase("PH")) {
targetPosition = -1*currentPosition;
}
// firmware value, always return "10"
if (cmd.equalsIgnoreCase("GV")) {
Serial.print("10#");
}
// get the current motor position
else if (cmd.equalsIgnoreCase("GP")) {
char tempString[6];
sprintf(tempString, "%04X", currentPosition);
Serial.print(tempString);
Serial.print("#");
}
// get the new motor position (target)
else if (cmd.equalsIgnoreCase("GN")) {
char tempString[6];
sprintf(tempString, "%04X", targetPosition);
Serial.print(tempString);
Serial.print("#");
}
// get the current temperature, hard-coded
else if (cmd.equalsIgnoreCase("GT")) {
char tempString[6];
sprintf(tempString, "%04X", 20.0);
Serial.print(tempString);
Serial.print("#");
}
// get the temperature coefficient
else if (cmd.equalsIgnoreCase("GC")) {
char tempString[6];
sprintf(tempString, "%02X", 20.0);
Serial.print(tempString);
Serial.print("#");
}
// set the temperature coefficient
else if (cmd.equalsIgnoreCase("SC")) {
long test = hexstr2long(param);
//TODO
}
// get the current motor speed, only values of 02, 04, 08, 10, 20
else if (cmd.equalsIgnoreCase("GD")) {
char tempString[6];
//TODO
}
// set speed, only acceptable values are 02, 04, 08, 10, 20
else if (cmd.equalsIgnoreCase("SD")) {
//TODO
}
// whether half-step is enabled or not, always return "00"
else if (cmd.equalsIgnoreCase("GH")) {
Serial.print("00#");
}
// motor is moving - 01 if moving, 00 otherwise
else if (cmd.equalsIgnoreCase("GI")) {
if(abs(targetPosition - currentPosition) > 0){
Serial.print("01#");
} else {
Serial.print("00#");
}
}
// set current motor position
else if (cmd.equalsIgnoreCase("SP")) {
currentPosition = hexstr2long(param) * 16;
}
// set new motor position
else if (cmd.equalsIgnoreCase("SN")) {
targetPosition = hexstr2long(param) * 16;
}
// initiate a move
else if (cmd.equalsIgnoreCase("FG")) {
stepper.enable();
stepper.move((currentPosition - targetPosition)*direction);
currentPosition = targetPosition;
}
// stop a move
else if (cmd.equalsIgnoreCase("FQ")) {
stepper.disable();
}
else if (cmd.equals("")){;}
//unknown command
else {
}
}
//...
}
void serialEvent() {
// read the command until the terminating # character
while (Serial.available() && !eoc) {
char c = Serial.read();
if (c != '#' && c != ':') {
line = line + c;
}
else {
if (c == '#') {
eoc = true;
}
}
}
}
long hexstr2long(String line) {
char buf[line.length()+1];
line.toCharArray(buf, line.length());
long ret = 0;
ret = strtol(buf, NULL, 16);
return (ret);
}