-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedServo.cpp
79 lines (71 loc) · 1.74 KB
/
FeedServo.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
79
#include "Arduino.h"
#include "FeedServo.h"
FeedServo::FeedServo(byte Port, byte Feedback_Port)
{
port = Port;
feedback_port = Feedback_Port;
last_pos = 180;
runs = 0;
}
void FeedServo::init()
{
attach(port);
pinMode(feedback_port, INPUT);
}
double FeedServo::read_pos()
{
long tHigh, tLow, tCycle;
while (true) //From Parallax spec sheet
{
tHigh = pulseIn(feedback_port, HIGH);
tLow = pulseIn(feedback_port, LOW);
tCycle = tHigh + tLow;
if ( tCycle > 1000 && tCycle < 1200)
break; //valid tCycle;
}
double dc = (100.00 * (double)tHigh) / (double)tCycle; //From Parallax spec sheet, you are trying to determine the percentage of the HIGH in the pulse
double angle = ((dc - dcMin) * 360.00) / (dcMax - dcMin + 1.00);
angle = (angle < 0) ? 0 : angle;
angle = (angle > 359) ? 359 : angle;
runs += (last_pos > angle + 180);
runs -= (last_pos < angle - 180);
last_pos = angle;
angle += 360 * runs;
return angle;
}
void FeedServo::Compute()
{
double error;
error = mySetpoint - myInput;
error = min(30, error);
error = max(-30, error);
myOutput = -(error * Kp);
}
void FeedServo::write_pos(int pos)
{
mySetpoint = pos;
myInput = read_pos();
double last_pos = 0;
do
{
Compute();
write(myOutput + s_stop);
//delay(1);
last_pos = myInput;
myInput = read_pos();
} while (last_pos != myInput || myInput > mySetpoint + 4 || myInput < mySetpoint - 4);
for (int i = 0; i < 200; i++)
{
myInput = read_pos();
Compute();
write(myOutput + s_stop);
}
write(s_stop);
}
void FeedServo::write_p_error(int pos)
{
mySetpoint = pos;
myInput = read_pos();
Compute();
write(myOutput + s_stop);
}