-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMT2TA4_Lab5.cpp
More file actions
327 lines (295 loc) · 8.03 KB
/
MT2TA4_Lab5.cpp
File metadata and controls
327 lines (295 loc) · 8.03 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
Calculation for the angular resolution of the motor:
Angular resolution = 360 degrees / Number of steps per revolution
= 360 degrees / 48 steps per revolution
≈ 7.5 degrees per step
Student 1:
Name: Marcus DeMaria
Number: 400455619
Time period between two steps for half-stepping: ~0.542 seconds
Time period between two steps for full-stepping: ~1.083 seconds
Student 2:
Name: Vineet Aggarwal
Number: 400432692
Time period between two steps for half-stepping: ~0.615 seconds
Time period between two steps for full-stepping: ~1.229 seconds
*/
#include "LCD_DISCO_F429ZI.h"
#include "mbed.h"
#include <math.h>
#include "DebouncedInterrupt.h"
#include <string>
LCD_DISCO_F429ZI LCD;
DigitalOut In1(PB_7); //PD_8
DigitalOut In2(PC_3); //PD_10
DigitalOut In3(PF_6); //PD_14
DigitalOut In4(PC_8);
InterruptIn User_Button(BUTTON1);
DebouncedInterrupt StepperMode(PA_7);
DebouncedInterrupt SetDirection(PA_5);
DebouncedInterrupt SpeedUp(PE_4);
DebouncedInterrupt SpeedDown(PE_3);
Timeout timeout;
Ticker ticker;
int speed = 0;
// Define motor parameters
const float ANGULAR_RESOLUTION = 360.0 / 48; // Degrees per step
string s1Name = "Marcus";
int s1Num = 400455619;
float s1full = 1083; // Initialize time period for student 1
float s1half = 542;
string s2Name = "Vineet";
int s2Num = 400432692;
float s2full = 1229; // Initialize time period for student 2
float s2half = 615;
bool isStudent1 = false; // Flags to keep track of what is being displayed and ran
bool StepMode = true; // To abide by initial condition
bool direction = true; // To abide by initial condition
int initial = 1; // Initial condition
void step1f();
void step2f();
void step3f();
void step4f();
void step1h();
void step2h();
void step3h();
void step4h();
void step5h();
void step6h();
void step7h();
void step8h();
void displayStudentInfo() {
// LCD Initial Display
LCD.SetTextColor(LCD_COLOR_DARKBLUE);
LCD.DisplayStringAt(0, 35, (uint8_t *)"Name:", CENTER_MODE);
//timeout.detach();
//ticker.detach();
if (initial = 1){ // For initial press of user
LCD.DisplayStringAt(0, 125, (uint8_t *)"Step Mode: Full Step", CENTER_MODE);
LCD.DisplayStringAt(0, 140, (uint8_t *)"Direction: C-W", CENTER_MODE);
initial = 0;
}
else {} // Will go back to 0 permantly after initial press and not execute again
if (isStudent1) {
LCD.DisplayStringAt(0, 50, (uint8_t *)s1Name.c_str(), CENTER_MODE);
LCD.DisplayStringAt(0, 65, (uint8_t *)"Num:", CENTER_MODE);
char s1NumBuff[20];
sprintf(s1NumBuff, "%d", s1Num);
LCD.DisplayStringAt(0, 80, (uint8_t *)s1NumBuff, CENTER_MODE);
int s1full_int = (int)(s1full * 10);
char s1full_buff[20];
snprintf(s1full_buff, 95, "Full Step (ms): %d.%d", s1full_int / 10, s1full_int % 10);
LCD.DisplayStringAt(0, 95, (uint8_t *)s1full_buff, CENTER_MODE);
int s1half_int = (int)(s1half * 10);
char s1half_buff[20];
snprintf(s1half_buff, 110, "Half Step (ms): %d.%d", s1half_int / 10, s1half_int % 10);
LCD.DisplayStringAt(0, 110, (uint8_t *)s1half_buff, CENTER_MODE);
ticker.detach();
if (StepMode){
speed = s1full;
ticker.attach(&step1f, speed*0.004);
}
if(!StepMode){
speed = s1half;
ticker.attach(&step1h, speed*0.008);
}
} else {
LCD.DisplayStringAt(0, 50, (uint8_t *)s2Name.c_str(), CENTER_MODE);
LCD.DisplayStringAt(0, 65, (uint8_t *)"Num:", CENTER_MODE);
char s2NumBuff[20];
sprintf(s2NumBuff, "%d", s2Num);
LCD.DisplayStringAt(0, 80, (uint8_t *)s2NumBuff, CENTER_MODE);
int s2full_int = (int)(s2full * 10);
char s2full_buff[20];
snprintf(s2full_buff, 95, "Full Step (ms): %d.%d", s2full_int / 10, s2full_int % 10);
LCD.DisplayStringAt(0, 95, (uint8_t *)s2full_buff, CENTER_MODE);
int s2half_int = (int)(s2half * 10);
char s2half_buff[20];
snprintf(s2half_buff, 110, "Half Step (ms): %d.%d", s2half_int / 10, s2half_int % 10);
LCD.DisplayStringAt(0, 110, (uint8_t *)s2half_buff, CENTER_MODE);
ticker.detach();
if (StepMode){
speed = s2full;
ticker.attach(&step1f, speed*0.004);
}
if(!StepMode){
speed = s2half;
ticker.attach(&step1h, speed*0.008);
}
}
}
void studentButtonPressed() {
isStudent1 = !isStudent1; // Toggle between student 1 and student 2
displayStudentInfo(); // Update the LCD with the new student's information
}
void displayStepInfo() {
if (StepMode) {
LCD.DisplayStringAt(0, 125, (uint8_t *)"Step Mode: Full Step", CENTER_MODE);
} else {
LCD.DisplayStringAt(0, 125, (uint8_t *)"Step Mode: Half Step", CENTER_MODE);
}
}
void stepButtonPressed() {
StepMode = !StepMode;
displayStepInfo();
}
void displayDirectionInfo() {
if (direction) {
LCD.DisplayStringAt(0, 140, (uint8_t *)"Direction: C-W", CENTER_MODE);
} if (!direction) {
LCD.DisplayStringAt(0, 140, (uint8_t *)"Direction: CCW", CENTER_MODE);
}
}
void dirButtonPressed() {
direction = !direction;
displayDirectionInfo();
}
void incrementSpeed() {
speed = speed-100;
LCD.DisplayStringAt(0, 155, (uint8_t *)"Motor Speed Up!", CENTER_MODE);
}
void decrementSpeed() {
speed = speed+100;
LCD.DisplayStringAt(0, 155, (uint8_t *)"Motor Speed Down!", CENTER_MODE);
}
void step1f(){
In1 = 1;
In2 = 0;
In3 = 1;
In4 = 0;
if (direction==0){
timeout.attach(&step2f, speed*0.001);
}
if (direction==1){
timeout.attach(&step4f, speed*0.001);
}
}
void step2f(){
In1 = 1;
In2 = 0;
In3 = 0;
In4 = 1;
if (direction==0){
timeout.attach(&step3f, speed*0.001);
}
}
void step3f(){
In1 = 0;
In2 = 1;
In3 = 0;
In4 = 1;
if (direction==0){
timeout.attach(&step4f, speed*0.001);
}
if (direction==1){
timeout.attach(&step2f, speed*0.001);
}
}
void step4f(){
In1 = 0;
In2 = 1;
In3 = 1;
In4 = 0;
if (direction==1){
timeout.attach(&step3f, speed*0.001);
}
}
void step1h(){
In1 = 1;
In2 = 0;
In3 = 1;
In4 = 0;
if (direction==0){
timeout.attach(&step2h, speed*0.001);
}
if (direction==1){
timeout.attach(&step8h, speed*0.001);
}
}
void step2h(){
In1 = 1;
In2 = 0;
In3 = 0;
In4 = 0;
if (direction==0){
timeout.attach(&step3h, speed*0.001);
}
}
void step3h(){
In1 = 1;
In2 = 0;
In3 = 0;
In4 = 1;
if (direction==0){
timeout.attach(&step4h, speed*0.001);
}
if (direction==1){
timeout.attach(&step2h, speed*0.001);
}
}
void step4h(){
In1 = 0;
In2 = 0;
In3 = 0;
In4 = 1;
if (direction==0){
timeout.attach(&step5h, speed*0.001);
}
if (direction==1){
timeout.attach(&step3h, speed*0.001);
}
}
void step5h(){
In1 = 0;
In2 = 1;
In3 = 0;
In4 = 1;
if (direction==0){
timeout.attach(&step6h, speed*0.001);
}
if (direction==1){
timeout.attach(&step4h, speed*0.001);
}
}
void step6h(){
In1 = 0;
In2 = 1;
In3 = 0;
In4 = 0;
if (direction==0){
timeout.attach(&step7h, speed*0.001);
}
if (direction==1){
timeout.attach(&step5h, speed*0.001);
}
}
void step7h(){
In1 = 0;
In2 = 1;
In3 = 1;
In4 = 0;
if (direction==0){
timeout.attach(&step8h, speed*0.001);
}
if (direction==1){
timeout.attach(&step6h, speed*0.001);
}
}
void step8h(){
In1 = 0;
In2 = 0;
In3 = 1;
In4 = 0;
if (direction==1){
timeout.attach(&step7h, speed*0.001);
}
}
int main() {
User_Button.rise(&studentButtonPressed);
StepperMode.attach(&stepButtonPressed, IRQ_FALL, 50, false);
SetDirection.attach(&dirButtonPressed, IRQ_FALL, 50, false);
SpeedUp.attach(&incrementSpeed, IRQ_FALL, 50, false);
SpeedDown.attach(&decrementSpeed, IRQ_FALL, 50, false);
while (true) {
}
}