-
Notifications
You must be signed in to change notification settings - Fork 0
/
joint.cpp
76 lines (75 loc) · 1.46 KB
/
joint.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
#include "joint.h"
Joint::Joint(int pin_rotary_1, int pin_rotary_2, int pin_power, int pin_direction)
{
pinRotary1 = pin_rotary_1;
pinRotary2 = pin_rotary_2;
pinDirection = pin_direction;
pinPower = pin_power;
inPositionCounter = 0;
relaxing = true;
}
void Joint::setup()
{
pinMode(pinRotary1, INPUT_PULLUP);
pinMode(pinRotary2, INPUT_PULLUP);
pinMode(pinDirection, OUTPUT);
pinMode(pinPower, OUTPUT);
encoder = new Encoder(pinRotary1, pinRotary2);
input = 0;
output = 0;
setpoint = 0;
inPosition = false;
pid = new PID(&input, &output, &setpoint, 1, 0.04, 0.1, DIRECT);
encoder->write(0);
pid->SetMode(AUTOMATIC);
currentDirection = -100;
}
int Joint::getPosition()
{
return input;
}
bool Joint::isInPosition()
{
return inPosition;
}
void Joint::relax()
{
relaxing = true;
}
void Joint::operate()
{
input = encoder->read();
if (relaxing)
{
analogWrite(pinPower, 0);
return;
}
isInPosition(input);
pid->Compute();
int direction = output > 127 ? HIGH : LOW;
if (currentDirection != direction) {
currentDirection = direction;
digitalWrite(pinDirection, direction);
}
analogWrite(pinPower, abs(output - 127));
}
void Joint::isInPosition(int current)
{
if (abs(setpoint - current) > 150)
{
inPositionCounter = 0;
inPosition = false;
return;
}
if (inPositionCounter > 100)
{
inPosition = true;
return;
}
inPositionCounter++;
}
void Joint::setSetpoint(int _setpoint){
setpoint = _setpoint;
relaxing = false;
inPosition = false;
}