-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadMotor.cpp
78 lines (64 loc) · 1.66 KB
/
loadMotor.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
#include "mbed.h"
PwmOut loadMotor(p23);
AnalogIn loadEncoderYellow(p15);
AnalogIn loadEncoderGreen(p16);
DigitalOut loadDirection(p5);
Timer timer;
Timer freqReader;
Serial pc(USBTX, USBRX); // tx, rx
int loadDirectionLocal;
float loadDuty;
float loadEncoderValue;
float max;
float min;
int count;
void risingEdge() {
float timePassed = freqReader.read();
loadEncoderValue = count / timePassed;
pc.printf("Frequency: %f\r\n", loadEncoderValue);
freqReader.reset();
}
int main() {
pc.printf("Starting up\r\n");
timer.start();
freqReader.start();
loadMotor.period(0.01f); // 100 Hz
loadMotor.write(0.1f); // Initially 50% duty cycle
max = 0.0f;
min = 1.0f;
count = 0;
loadDuty = 0.2f;
loadDirection = 1;
while(1) {
float yellow = loadEncoderYellow.read();
float green = loadEncoderGreen.read();
float net = green - yellow;
pc.printf("Net Output: %f\r\n", net);
/* Maximum and minimum value calculations for testing */
if(net > max) {
max = net;
}
if(net < min) {
min = net;
}
if(net > 0.5f) {
count++;
}
if(count > 25) {
risingEdge();
count = 0;
}
float currentTime = timer.read();
if(currentTime > 5) {
break;
loadDuty = loadDuty + 0.05f;
timer.reset();
}
if(loadDirectionLocal < 0) {
loadDirection = 0;
} else {
loadDirection = 1;
}
loadMotor.write(loadDuty);
}
}