forked from anidev/612-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turntable.cpp
149 lines (135 loc) · 3.49 KB
/
turntable.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
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
#include "turntable.h"
#include "update.h"
#include "utility.h"
#include <SpeedController.h>
#include <DigitalInput.h>
//a proportional constant
const float TURNTABLE_MULTIPLIER = 1.0;
const int TURNTABLE_TOLERANCE = 5; //px
const float TURNTABLE_DEFPOWER = 0.3;
const bool RIGHT_POSITIVE = false;
turntable::turntable(SpeedController& j, DigitalInput& l, DigitalInput& m, DigitalInput& r) {
jag = &j;
left = &l;
mid = &m;
right = &r;
registry().register_func(update_help, (void*)this);
power = 0.0;
//enable by default
enabled = true;
centering = false;
pos = UNSURE;
}
turntable::~turntable() {
registry().unregister_func(update_help, (void*)this);
}
void turntable::update_help(void * obj) {
((turntable*)obj)->update();
}
void turntable::update() {
bool right_val = !right->Get();
bool left_val = !left->Get();
bool center_val = !mid->Get();
//at a limit
if (right_val) {
pos = RIGHT;
}
else if (center_val) {
pos = CENTER;
}
else if (left_val) {
pos = LEFT;
}
else if (pos == CENTER) {
//passing over center, center limit no longer pressed.
if ((RIGHT_POSITIVE && power > 0) || (!RIGHT_POSITIVE && power < 0)) {
pos = RIGHT;
}
else if (power == 0) {
//we got pushed. Not sure anymore.
pos = UNSURE;
}
else {
pos = LEFT;
}
}
//move turntable
if (enabled) {
if (
(RIGHT_POSITIVE && (
(power > 0 && right_val) || (power < 0 && left_val)
))
||
(!RIGHT_POSITIVE && (
(power < 0 && right_val) || (power > 0 && left_val)
))
) {
//at a mechanical stop and trying to head into the mechanical stop
power = 0.0;
}
else if (centering) {
if (pos == UNSURE) {
//just go in any direction. When we hit a limit then we'll know
//can't be at mechanical stop as limit switch never pressed.
power = TURNTABLE_DEFPOWER;
}
else if (pos == LEFT) {
//move right
if (RIGHT_POSITIVE) {
power = TURNTABLE_DEFPOWER;
}
else {
power = -TURNTABLE_DEFPOWER;
}
}
else if (pos == RIGHT) {
//move left
if (RIGHT_POSITIVE) {
power = -TURNTABLE_DEFPOWER;
}
else {
power = TURNTABLE_DEFPOWER;
}
}
else {
//we're already centered
power = 0.0;
}
}
}
else {
//disabled
power = 0.0;
}
jag->Set(power);
}
void turntable::enable() {
enabled = true;
}
void turntable::disable() {
enabled = false;
}
void turntable::new_offset(int offset, int max) {
centering = false;
power = coerce((offset / max) * TURNTABLE_MULTIPLIER, -1.0f, 1.0f);
if (!RIGHT_POSITIVE) {
power = -power;
}
}
void turntable::center() {
centering = true;
}
void turntable::manual_control(float val) {
if (!RIGHT_POSITIVE) {
val = -val;
}
centering = false;
power = coerce(val, -0.3f, 0.3f);
}
float turntable::get_power() const {
float ret = power;
if (!RIGHT_POSITIVE) {
ret = -ret;
}
return ret;
}