-
Notifications
You must be signed in to change notification settings - Fork 17
/
PositionControl.cpp
93 lines (82 loc) · 1.67 KB
/
PositionControl.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// PositionControl
// by Andrew Kramer
// Provides position control for a single DC motor
// via the PID control loop in Speed Control
#include"Arduino.h"
#include<PositionControl.h>
#include<SpeedControl.h>
const double defaultKP = 2.0;
PositionControl::PositionControl(SpeedControl *speedControl)
{
_speedControl = speedControl;
_distance = 0;
_kP = defaultKP;
_positioning = false;
_error = 0;
_speed = 0;
}
void PositionControl::setKP(double kP)
{
_kP = kP;
}
void PositionControl::setSpeed(int speed)
{
_speedControl->setSpeed(speed);
_speed = speed;
}
// rotates motor by the specified number of degrees at the
// specified speed
void PositionControl::rotate(int degrees, int speed)
{
if (!_positioning)
{
if (degrees < 0 && speed > 0)
{
speed *= -1;
}
else if (speed < 0 && degrees > 0)
{
degrees *= -1;
}
else if (speed < 0 && degrees < 0)
{
degrees *= -1;
speed *= -1;
}
_distance += _speedControl->getDistance();
_error = degrees;
_speed = speed;
_positioning = true;
}
}
void PositionControl::adjustPWM()
{
int thisDistance = _speedControl->getDistance();
_distance += thisDistance;
if (_positioning)
{
_error -= thisDistance;
int newSpeed = (double)_error * _kP;
constrainSpeed(newSpeed);
if (_error < 2 && _error > -2)
{
_positioning = false;
_speed = 0;
newSpeed = 0;
_error = 0;
}
_speedControl->setSpeed(newSpeed);
}
_speedControl->adjustPWM();
}
// doesn't return correct distance
int PositionControl::getDistance()
{
int tempDistance = _distance;
_distance = 0;
return tempDistance;
}
void PositionControl::constrainSpeed(int &newSpeed)
{
if (abs(_speed) < abs(newSpeed)) newSpeed = _speed;
}