-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToeArm.cpp
152 lines (124 loc) · 3.5 KB
/
TicTacToeArm.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
150
151
152
#include "TicTacToeArm.h"
TicTacToeArm::TicTacToeArm(int pin_base, int pin_shoulder, int pin_elbow) {
this->base.attach(pin_base);
this->shoulder.attach(pin_shoulder);
this->elbow.attach(pin_elbow);
}
void TicTacToeArm::home(unsigned long step_delay) {
float tb, ts, te;
float cb, cs, ce;
float db, ds, de;
this->penState = 1;
this->getAngleMap(80, 90, tb, ts, te);
cb = this->base.readMicroseconds();
cs = this->shoulder.readMicroseconds();
ce = this->elbow.readMicroseconds();
db = (tb - cb) / STEP_MAX;
ds = (ts - cs) / STEP_MAX;
de = (te - ce) / STEP_MAX;
if(db != 0 || ds !=0 || de !=0) {
for(int i = 0; i < STEP_MAX; i++) {
cb += db;
cs += ds;
ce += de;
this->setServoMicroseconds(cb, cs, ce);
delay(step_delay);
}
}
this->pen_X = 0;
this->pen_Y = 80;
}
void TicTacToeArm::penUp() {
if(this->penState == 0) {
float cb, cs, ce;
float tb, ts, te;
float db, ds, de;
this->penState = 1;
this->getAngleMap(this->_toRadius(this->pen_X, this->pen_Y), this->_toAngle(this->pen_X, this->pen_Y), tb, ts, te);
cb = this->base.readMicroseconds();
cs = this->shoulder.readMicroseconds();
ce = this->elbow.readMicroseconds();
db = (tb - cb) / STEP_MAX;
ds = (ts - cs) / STEP_MAX;
de = (te - ce) / STEP_MAX;
if(db != 0 || ds !=0 || de !=0) {
for(int i = 0; i < STEP_MAX; i++) {
cb += db;
cs += ds;
ce += de;
this->setServoMicroseconds(cb, cs, ce);
delay(DEFAULT_STEP_MS);
}
}
}
}
void TicTacToeArm::penDown() {
if(this->penState == 1) {
float cb, cs, ce;
float tb, ts, te;
float db, ds, de;
this->penState = 0;
this->getAngleMap(this->_toRadius(this->pen_X, this->pen_Y), this->_toAngle(this->pen_X, this->pen_Y), tb, ts, te);
cb = this->base.readMicroseconds();
cs = this->shoulder.readMicroseconds();
ce = this->elbow.readMicroseconds();
db = (tb - cb) / STEP_MAX;
ds = (ts - cs) / STEP_MAX;
de = (te - ce) / STEP_MAX;
if(db != 0 || ds !=0 || de !=0) {
for(int i = 0; i < STEP_MAX; i++) {
cb += db;
cs += ds;
ce += de;
this->setServoMicroseconds(cb, cs, ce);
delay(DEFAULT_STEP_MS);
}
}
}
}
void TicTacToeArm::lineTo(float x, float y) {
float dx, dy;
float cx, cy;
float tb, ts, te;
cx = this->pen_X;
cy = this->pen_Y;
DEBUG << "Current (x, y): (" << cx << ", " << cy << ")";
DEBUG << "Target (x, y): (" << x << ", " << y << ")";
dx = (x - cx) / STEP_MAX;
dy = (y - cy) / STEP_MAX;
DEBUG << "Step (dx, dy): (" << dx << ", " << dy << ")";
if(dx != 0 || dy != 0) {
for (int i = 0; i < STEP_MAX; i++ ) {
cx += dx;
cy += dy;
this->getAngleMap(this->_toRadius(cx, cy), this->_toAngle(cx, cy), tb, ts, te);
this->setServoMicroseconds(tb, ts, te);
delay(DEFAULT_STEP_MS);
}
this->pen_X = x;
this->pen_Y = y;
}
}
void TicTacToeArm::getAngleMap(float r, float a, float& b, float& s, float& e) {
if(this->penState == 1) {
b = map(a, 60.0, 120.0, 1087, 1697);
s = map(r, 88.0, 158.0, 1786, 2036);
e = map(r, 88.0, 158.0, 1225, 1675);
} else {
b = map(a, 60.0, 120.0, 1087, 1697);
s = map(r, 88.0, 158.0, 1936, 2156);
e = map(r, 88.0, 158.0, 1105, 1655);
}
}
void TicTacToeArm::setServoMicroseconds(float b, float s, float e) {
DEBUG << "Servo Microseconds (b, s, e): (" << b << ", " << s << ", " << e << ")";
this->base.writeMicroseconds(b);
this->shoulder.writeMicroseconds(s);
this->elbow.writeMicroseconds(e);
}
float TicTacToeArm::_toAngle(float x, float y) {
return (atan2(y, x)*180.0)/PI;
}
float TicTacToeArm::_toRadius(float x, float y) {
return sqrt(x*x + y*y);
}