-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmovement.hpp
350 lines (290 loc) · 7.37 KB
/
movement.hpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#ifndef MOVEMENT_HPP
#define MOVEMENT_HPP
/**
* Ryan Domigan <ryan_domigan@[email protected]>
* Kaitlyn Carcia <[email protected]>
* @file /home/ryan/uml/robotics/checkers/movement.hpp
* Created on Apr 04, 2014
*
* Move and read position of the gantry arm
*/
/* includes */
#include <cbc.h>
#include <cstdlib>
#include "./points.hpp"
#include "./Board.hpp"
const static int sensor_shoulder_positive = 13
, sensor_shoulder_negative = 15
, sensor_arm_positive = 8
, sensor_arm_negative = 10
, motor_arm = 3
, motor_shoulder = 0
, servo_elevation = 2
, servo_grip = 0;
/**
* returns -1 for input < 0, 1 for input > 0
*/
int sign(int input) { return input > 0 ? 1 : -1; }
struct SensorGroup {
int _sensor[2]
, _position;
bool _gap_open;
bool is_open(int n) { return digital( _sensor[n] ); };
bool toggle() {
if( is_open( 1 ) == _gap_open )
return false;
else {
_gap_open = !_gap_open;
return true;
}}
SensorGroup(int pos, int neg) {
_sensor[0] = pos;
_sensor[1] = neg;
_position = 0;
_gap_open = true;
}
int position() const { return _position; }
void step(int direction) {
if( toggle() && _gap_open ) {
//std::cout << " Sensor " << _sensor[1] << " pos: " << _position << "\n";
_position += sign(direction);
}
}
/* assuming we've counted to the desired position, keep moving towards the centered-point. */
int is_centered() {
for(int i = 0; i < 2; ++i) {
switch( is_open( 0 ) | (is_open( 1 ) << 1) ) {
case 3:
_gap_open = true;
return 0;
case 1: return -1;
case 2: return 1;
}
sleep(0.1);
}
std::cout << "Can't center at " << _position << "; two sensors block at once (sensors "
<< _sensor[0] << ", "<< _sensor[1] << ")" << std::endl;
exit(1);
}
};
/*************************/
/* ____ _ */
/* / ___|__ _ _ __| |_ */
/* | | / _` | '__| __| */
/* | |__| (_| | | | |_ */
/* \____\__,_|_| \__| */
/*************************/
struct Cart {
static const int
_max_velocity = 600
, _slow_velocity = 200
, _slowest_velocity = 80;
int _motor, _destination, _current_velocity;
SensorGroup _sense;
enum State { seeking, centering , done };
State _state;
Cart(int motor, int s_pos, int s_neg)
: _motor(motor), _current_velocity(_max_velocity), _sense(s_pos, s_neg), _state(done) {}
void set_velocity(int vel) {
_current_velocity = vel;
mav(_motor, vel);
}
bool start_centering() {
if(_sense.is_centered() == 0) {
set_velocity(0);
return true;
}
set_velocity(direction() * Cart::_slowest_velocity);
_state = centering;
return false;
}
bool start_seeking(int dst) {
_destination = dst;
if( dst == _sense.position() ) {
return start_centering();
}
mav(_motor, -150);
sleep(0.1);
mav(_motor, 0);
_state = seeking;
if( dst > _sense.position() )
set_velocity(Cart::_max_velocity);
else
set_velocity(-Cart::_max_velocity);
return false;
}
int abs_velocity() { return abs(_current_velocity); }
int direction() { return _current_velocity > 0 ? 1 : -1; }
/* A step in the movement loop, returns the state of this cart. */
bool step() {
switch(_state) {
case seeking:
_sense.step( direction() );
/* well, there's a gap in my encoder between column 0 and 1.
I didn't think it would be a pain in the neck, but it is. */
if( _sense.position() == _destination ) {
if( (_motor != motor_shoulder)
|| !_sense.is_open(1)
|| _destination != 1
|| direction() < 0) {
_sense._position = _destination;
return start_centering();
} else set_velocity(direction() * _slow_velocity);
}
return false;
case centering: {
int sense_state = _sense.is_centered();
if( sense_state == 0 ) {
set_velocity(0);
_state = done;
return true;
}
else {
if( sense_state > 0 )
set_velocity( Cart::_slow_velocity );
else
set_velocity( -Cart::_slow_velocity );
}
return false;
}
case done:
return true;
}
return true;
}
};
/****************************/
/* _ */
/* / \ _ __ _ __ ___ */
/* / _ \ | '__| '_ ` _ \ */
/* / ___ \| | | | | | | | */
/* /_/ \_\_| |_| |_| |_| */
/****************************/
/**
* The arm has two carts, one for the X and one for the Y axis. The arm is given a position
* and then runs the two carts concurrently (a single thread steps both carts) to reach that
* position.
*/
class Arm {
public:
Cart _row, _column;
private:
static void nap() { sleep(0.2); }
/* controls hand - open, close, raise, lower */
void open_hand() { set_servo_position(servo_grip, 1680); }
void close_hand() { set_servo_position(servo_grip, 723); }
static void raise_hand() { set_servo_position(servo_elevation, 1987); }
void lower_hand() { set_servo_position(servo_elevation, 1031); }
/* sets hand position */
void position_hand(int i) {
set_servo_position(servo_elevation
, i + get_servo_position(servo_elevation));
}
struct Servos {
Servos() { enable_servos(); }
~Servos() {
Arm::raise_hand();
nap();
disable_servos(); }};
void run_loop() {
bool a, b;
while( true ) {
a = _row.step();
b = _column.step();
if( a && b )
return;
}}
public:
Arm() : _row(motor_arm, sensor_arm_positive, sensor_arm_negative)
, _column(motor_shoulder, sensor_shoulder_positive, sensor_shoulder_negative) {
open_hand();
raise_hand();
}
~Arm() {
open_hand();
raise_hand();
ao();
}
/* Absolute co-ordinant on an 8x9 grid */
void move_to(const iPair& pos) {
_row.start_seeking( pos.row() );
_column.start_seeking( pos.column() );
run_loop();
}
/* Move arm based on ipair */
void board_move(iPair pos) {
std::cout << "Going to " << pos;
pos = iPair(7 - pos.x, 2 * pos.y + (even(pos.x) ? 1 : 2));
std::cout << " translated to " << pos << std::endl;
move_to( pos );
sleep(0.1);
reset();
}
void reset() {
_row.start_centering();
_column.start_centering();
run_loop();
}
void pick_up_all() {
Servos s;
open_hand();
nap();
nap();
lower_hand();
nap();
close_hand();
nap();
}
void put_down_all() {
Servos s;
lower_hand();
nap();
open_hand();
nap();
}
void put_down_one() {
Servos s;
lower_hand();
nap();
open_hand();
position_hand(10);
close_hand();
}
/* Remove piece from board - i.e. jumping */
void remove_piece(const iPair& capture) {
std::cout << "Taking ";
board_move(capture);
pick_up_all();
move_to( iPair(3,0) );
put_down_all();
nap();
reset();
}
/* Given move, execute it */
void execute_move(const Move &m) {
board_move(m.src);
pick_up_all();
board_move(m.dst);
put_down_all();
}
};
/* Keeps resetting the arm. For reasons I can't explain,
the arm looses track of its position count. Since it's a lightweght class,
and I'm running out of time, I'll just fource a reset
every time the arm is used.
*/
struct Sling {
Arm arm;
Sling() : arm() {
arm.reset();
}
~Sling() {
arm.move_to( iPair(0,0) );
mav(motor_shoulder, -200);
mav(motor_arm, -200);
sleep(0.2);
ao();
}
Arm& operator()() { return arm; }
};
#endif