-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotor Rotate Function
157 lines (142 loc) · 4.58 KB
/
Motor Rotate Function
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
// defines pins numbers: stepPins1-3 are ms1-3 and tell the motor whether to step or microstep. stepPin just tells the motor to step. dirPin sets the direction
const int stepPin1 = 4;
const int stepPin2 = 5;
const int stepPin3 = 6;
const int dirPin = 2;
const int stepPin = 13;
const int wavePinA = 11;
const int wavePinB = 12;
//global variables
int oldencoderCount = 0;
float currentAngle = 0, encoderCount = 0;
void setup() {
// Sets the two pins as Outputs
Serial.begin(9600);
pinMode(stepPin1,OUTPUT);
pinMode(stepPin2,OUTPUT);
pinMode(stepPin3,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(wavePinA, INPUT);
pinMode(wavePinB, INPUT);
}
//HIGH is CW
float Rotate(float angle, int speed, int stepSize, int timeoutValue) {
//cw is true, ccw is false
//speed 1, 2, 3, 4; 4 is slowest, 1 is the fastest
//stepSize is 1(full step) - 16(16th step)
//declaring internal function variables
int newA = 0, newB = 0, oldA = 0, oldB = 0, stop = 0, c = 0;
//equation to make speeds at higher step sizes equal to 16th step sizes.
c = ((16/stepSize)*speed*1000-1000)/500;
//checks whether the angle the user put requires the motor to spin CW or CWW and applies it
if (angle > currentAngle) {
digitalWrite(dirPin, HIGH);
}
if (angle < currentAngle) {
digitalWrite(dirPin, LOW);
}
//Determines which MS1-3 pins need what values for different step sizes
switch (stepSize) {
case 1:
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
break;
case 2:
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
break;
speed*8;
case 4:
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
break;
case 8:
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
break;
case 16:
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, HIGH);
break;
}
//initiates motor clock
unsigned long StartTime = micros();
//main while loop that steps until submitted angle is reached; runs while the difference between the encoder-read angle and the submitted angle is less than 0.05 - might need to adjust this value
while(abs(angle*2 - encoderCount/300*360) > 1) {
Serial.println(encoderCount);
//standard step
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
//extra time so that higher step sizes are the same speed, and the motor can be slowed down
//checks the encoder values
if (digitalRead(wavePinA) == HIGH) {
newA = 1;
}
else {
newA = 0;
}
if (digitalRead(wavePinB) == HIGH) {
newB = 1;
}
else {
newB = 0;
}
//if there is a change in encoder values, decides whether to incriment or decrement the encoder angle
if ((newA != oldA) || (newB != oldB)) {
if (digitalRead(wavePinA) == HIGH) {
if (digitalRead(wavePinB) == LOW) {
encoderCount++;
}
else {
encoderCount--;
}
}
else {
if (digitalRead(wavePinB == HIGH)) {
encoderCount++;
}
else {
encoderCount--;
}
}
}
//assigns this loops vars to "old" variables to compare on the next loop
oldA = newA;
oldB = newB;
//calculates elapsed motor runtime
unsigned long CurrentTime = micros();
unsigned long ElapsedTime = CurrentTime - StartTime;
//Serial.println(ElapsedTime);
//checks if the motor has moved in the time the user inputted
/*
if (ElapsedTime > (timeoutValue*10^6)) {
StartTime = micros();
if(encoderCount == oldencoderCount) {
stop = 1;
Serial.println("Motor has E-stopped");
}
oldencoderCount = encoderCount;
}
*/
delayMicroseconds(500*c);
}
//updates the code to let future functions know whether to turn CW or CCW
currentAngle = encoderCount/300*360;
return(encoderCount/300*360);
}
void loop() {
digitalWrite(dirPin, HIGH);
Serial.println("Test");
delay(1000);
Rotate(1, 1, 16, 1000);
Rotate(180, 1, 16, 1000);
delay(100000000);
}