-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong_MASTER.py
executable file
·519 lines (428 loc) · 17.1 KB
/
pong_MASTER.py
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/usr/bin/env python
#######################################
# Authors: Petras Swissler,Andrew Thompson,Victor Ozoh,Evan Li,Ethan Park
# Created: Dec 11, 2018
#######################################
# List of To-Dos:
# None
# List of Bugs:
# None
# List of Future Work:
# Smooth the motion of the ball
# Get pyfiglet working; crashes program when it tries to print to the screen. Pyfiglet would print wordart to the terminal
#######################################
# Import Required Libraries
# Standad and ROS imports
import rospy
import numpy as np
from intera_interface import Limb
# Project-specific imports
import hand_interface as hif
#import pyfiglet
from pong_plot import *
# Messages
from sawyer_pong.msg import measured_distances
from geometry_msgs.msg import (
Twist,
Pose
)
#######################################
# Create Classes
class bound_lrud:
# This class packages the boundary information into one simple-to-access object
def __init__(self, xlow, xhigh, yhigh, ylow):
self.xlow = xlow
self.xhigh = xhigh
self.yhigh = yhigh
self.ylow = ylow
class xy_vector:
# This class simply packages 2-dimensional vectors into one simple-to-access object
def __init__(self, x, y):
self.x = x
self.y = y
#######################################
# Global and Configuration Variables
# Positions of the player hands
hand_positions = [50,50]
# Position of the ball
ball_positions = xy_vector(50,50)
ball_measured_position = xy_vector(0,0)
# Logic locations of the ball
expected_position=xy_vector(0,0)
combined=xy_vector(0,0)
# Ball command velocities
vel_new=xy_vector(0,0)
# Display configuration
plot_size = xy_vector(200,50)
#######################################
# Helper Functions
def disp_score(score):
# Purpose: Displays the score after a point is scored
# Inputs: The score as a [player 1 score, player 2 score] array
# Outputs: Prints the score to the terminal
#pyfiglet.figlet_format('SCORE', font = "drpepper")
print(score[0]," to ",score[1])
return 1
def disp_win(score):
<<<<<<< HEAD
if score[0] > score[1]:
pyfiglet.figlet('LEFT PLAYER WINS')
else:
pyfiglet.figlet_format('RIGHT PLAYER WINS')
def disp_game(bounds, left_hand_position,right_hand_position, ball_logic_position, paddle_size):
arenaDim = xy_vector(50,20)
print('--------------------------------------------------')
lh_ratio = left_hand_position/(bounds.yhigh - bounds.ylow)
rh_ratio = right_hand_position/(bounds.yhigh - bounds.ylow)
ballPos = xy_vector(ball_logic_position.x/(bounds.xhigh - bounds.xlow),ball_logic_position.y/(bounds.yhigh - bounds.ylow))
ps_ratio = paddle_size / (bounds.yhigh - bounds.ylow)
=======
# Purpose: Displays the winning player's information
# Inputs: The score as a [player 1 score, player 2 score] array
# Outputs: Prints winning player information to the termial
if score[0] > score[1]:
#print(" LEFT PLAYER WINS ")
pyfiglet.figlet('LEFT PLAYER WINS')
else:
#print(" RIGHT PLAYER WINS ")
pyfiglet.figlet_format('RIGHT PLAYER WINS')
>>>>>>> b99b87f266d8c5d67ef1d087d75792ec19af86fa
def rand_sign():
<<<<<<< HEAD
choose = np.random.rand()
if choose >= 0.5:
return 1
else:
return -1
=======
# Purpose: Generates a random sign with 50/50 odds
# Inputs: None
# Outputs: Either [-1] or [1]
choose = np.random.rand()
if choose >= 0.5:
return 1
else:
return -1
##
>>>>>>> b99b87f266d8c5d67ef1d087d75792ec19af86fa
def get_ball_start_velocity(ball_speed):
# Purpose: Generates a random start velocity for the ball to take at the start of the game or after a point is scored
# Inputs: Desired ball speed (scalar value)
# Outputs: Initial ball velocity (xy_vector object)
xx = (np.random.rand()*0.8 + 0.2)*rand_sign()
yy = (np.random.rand()*0.4 + 0.0)*rand_sign()
norm = np.sqrt(xx*xx + yy*yy)
xx = ball_speed * xx / norm
yy = ball_speed * yy / norm
<<<<<<< HEAD
start_vel = xy_vector(xx,yy)
return start_vel
def ball_expected_position(ball_logic_position, ball_cmd_vel, delta_time):
expected_position.x = ball_logic_position.x + ball_cmd_vel.x * delta_time
expected_position.y = ball_logic_position.y + ball_cmd_vel.y * delta_time
return expected_position
def combine_ball_positions(simulated, actual):
simweight = 0.8
combined.x = simulated.x*simweight + actual.x*(1-simweight)
combined.y = simulated.y*simweight + actual.y*(1-simweight)
return combined
=======
start_vel = xy_vector(xx,yy)
return start_vel
####
def ball_expected_position(ball_logic_position, ball_cmd_vel, delta_time):
# Purpose: UNUSED: predict the ball position given necessary information
# Inputs: ball_logic_position is the last known position of the ball (xy_vector)
# ball_cmd_vel is the velocity of the ball (xy_vector)
# delta_time is the time since the last measurement (float)
# Outputs: The expected ball position (xy_vector)
expected_position.x = ball_logic_position.x + ball_cmd_vel.x * delta_time
expected_position.y = ball_logic_position.y + ball_cmd_vel.y * delta_time
return expected_position
####
def combine_ball_positions(simulated, actual):
# Purpose: UNUSED Calculates a weighted average between the simulated and actual ball positions
# Inputs: Simulated ball position (xy_vector)
# Actual (measured) ball position (xy_vector)
# Outputs: Combined ball position (xy_vector)
simweight = 0.8
combined.x = simulated.x*simweight + actual.x*(1-simweight)
combined.y = simulated.y*simweight + actual.y*(1-simweight)
return combined
####
>>>>>>> b99b87f266d8c5d67ef1d087d75792ec19af86fa
def check_bounds(position, bound, last_bounce):
# Purpose: Checks to see if the ball has reached any of the bounds
# Inputs: position is the position of the ball (xy_vector)
# bound is the boundaries of the game arena (bound_lrud)
# last_bounce is the boundary off which the last bounce occurred
# Outputs: do_bounce signals whether a bounce should be executed
# new_bounce is the updated version of last_bounce
# Init for later logic
new_bounce = last_bounce
# Check for collisions
if position.x > bound.xhigh:
new_bounce = 'rt'
if position.x < bound.xlow:
new_bounce = 'lf'
if position.y > bound.yhigh:
new_bounce = 'up'
if position.y < bound.ylow:
new_bounce = 'dn'
if new_bounce != last_bounce:
do_bounce = True
else:
do_bounce = False
return [do_bounce, new_bounce]
#######################################
# Callbacks
def hand_position_callback(latest_msg):
# Purpose: Interfaces the main pong function with the
# Inputs: latest_msg (measured_distance message)
# Outputs: Updates values of hand_positions global variable
hand_positions[0] = latest_msg.left_distance
hand_positions[1] = latest_msg.right_distance
def checkPosition(posemsg):
# Purpose: Checks the actual position of the pong ball
# Inputs: posemsg (Pose message)
# Outputs: Updates the values of ball_measured_postion global variables
<<<<<<< HEAD
=======
ball_measured_position.x = posemsg.position.x
ball_measured_position.y = posemsg.position.z
>>>>>>> b99b87f266d8c5d67ef1d087d75792ec19af86fa
#########################################
# Primary function
def pong_logic():
<<<<<<< HEAD
#Initialize the _node
rospy.init_node('pong_Master')
# define initial joint positions
thetalistHOME = [1.3395771484375, -3.5355, 2.0365224609375, -1.489580078125, -0.4218515625, 1.1975029296875, -3.419748046875, 0.0]
hif.move_to_home(thetalistHOME)
#######################
# Get parameters
paddle_size = rospy.get_param('~paddle_size',0.2)
ball_speed = rospy.get_param('~ball_velocity',0.1)
max_score = rospy.get_param('~max_score',5)
circle_tick_rate = rospy.get_param('~circle_tick_rate', 150)
#######################
# Create Publishers
hand_velocity_publisher = rospy.Publisher('pongvelocity', Twist, queue_size=1)
#######################
# Create Subscribers
# position subscriber:
poscheck = rospy.Subscriber("endpoint_Pose", Pose, checkPosition, queue_size=1)
# hand position subscriber:
hand_position_subscriber = rospy.Subscriber('hand_positions', measured_distances, hand_position_callback,queue_size=1)
#######################
# Create the message variables
veltwist=Twist()
#######################
# Create start variables
score = [0,0]
ball_cmd_vel = get_ball_start_velocity(ball_speed)
veltwist.linear.x=ball_cmd_vel.x
veltwist.linear.z=ball_cmd_vel.y
left_hand_position = hand_positions[0]/1000.0
right_hand_position = hand_positions[1]/1000.0
last_bounce = 0
#######################
# Perform time setup
r = rospy.Rate(circle_tick_rate)
start_time = rospy.get_time()
now_time = start_time
ball_logic_position = ball_measured_position #get from sub #!!!!!!!!!!!!!!
bounds = bound_lrud(-0.275,0.42,0.44,0.14) # get from Subscriber (or param or paramserver or sub, based on ambition)
#######################
# Main Loop
while not rospy.is_shutdown():
# Time Calcs
last_time = now_time
now_time = rospy.get_time() - start_time
delta_time = now_time - last_time
# Calculate position to use for logic
ball_logic_position = ball_measured_position#combine_ball_positions(ball_expected_position(ball_logic_position, ball_cmd_vel, delta_time), ball_recieved_position)
# Get Hand Positions
left_hand_position = hand_positions[0]/1000.0
right_hand_position = hand_positions[1]/1000.0
# Display
print(ball_measured_position.y, left_hand_position)
pong_plot(bounds, plot_size, left_hand_position, right_hand_position, paddle_size, ball_logic_position)
# Check Bounds
[do_bounce,last_bounce] = check_bounds(ball_logic_position, bounds, last_bounce)
# Event?
if do_bounce == True:
score_happened = False
if last_bounce == 'lf':
veltwist.linear.x = np.abs(veltwist.linear.x)
if np.abs(ball_measured_position.y-bounds.ylow-left_hand_position) > (paddle_size/2):
score_happened = True
print('miss:' + str(score_happened))
if np.abs(ball_measured_position.y-bounds.ylow-left_hand_position) <= (paddle_size/2):
score_happened = False
print('recovery:' + str(score_happened))
elif last_bounce == 'rt':
veltwist.linear.x = -np.abs(veltwist.linear.x)
if np.abs(ball_measured_position.y-bounds.ylow-right_hand_position) > (paddle_size/2):
score_happened = True
print('miss:' + str(score_happened))
if np.abs(ball_measured_position.y-bounds.ylow-right_hand_position) <= (paddle_size/2):
score_happened = False
print('recovery:' + str(score_happened))
if last_bounce == 'dn':
veltwist.linear.z = np.abs(veltwist.linear.z)
elif last_bounce == 'up':
veltwist.linear.z = -np.abs(veltwist.linear.z)
# Score?
if score_happened == True:
# Incr score, return hand
if last_bounce == 'lf':
score[0] = score[0] + 1
if last_bounce == 'rt':
score[1] = score[1] + 1
veltwist.linear.x=0
veltwist.linear.z=0
hand_velocity_publisher.publish(veltwist)
disp_score(score)
last_bounce = 0
# return hand to default position
hif.move_to_home(thetalistHOME)
ball_cmd_vel = get_ball_start_velocity(ball_speed)
veltwist.linear.x=ball_cmd_vel.x
veltwist.linear.z=ball_cmd_vel.y
#Publish hand twist
hand_velocity_publisher.publish(veltwist)
# If win, end game0[1]
if np.max(score) >= max_score:
disp_win(score)
quit()
# Update Rate Limiter
r.sleep()
=======
# Purpose: Executes the main pong logic behavior (i.e. all game code)
# Inputs: See parameters and subscribers below
# Outputs: Moves robot arm, displays the game state. See publishers below
#Initialize the _node
rospy.init_node('pong_Master')
# define initial joint positions
thetalistHOME = [1.3395771484375, -3.5355, 2.0365224609375, -1.489580078125, -0.4218515625, 1.1975029296875, -3.419748046875, 0.0]
hif.move_to_home(thetalistHOME)
#######################
# Get parameters
# UNUSED english_amount = rospy.get_param('~english_amount',0)
paddle_size = rospy.get_param('~paddle_size',0.2)
# UNUSED ball_velocity_incr = rospy.get_param('~ball_velocity_incr',1)
ball_speed = rospy.get_param('~ball_velocity',0.1)
max_score = rospy.get_param('~max_score',5)
circle_tick_rate = rospy.get_param('~circle_tick_rate', 150)
#######################
# Create Publishers
hand_velocity_publisher = rospy.Publisher('pongvelocity', Twist, queue_size=1)
#######################
# Create Subscribers
# position subscriber:
poscheck = rospy.Subscriber("endpoint_Pose", Pose, checkPosition, queue_size=1)
# hand position subscriber:
hand_position_subscriber = rospy.Subscriber('hand_positions', measured_distances, hand_position_callback,queue_size=1)
#######################
# Create the message variables
veltwist=Twist()
#######################
# Create start variables
score = [0,0]
ball_cmd_vel = get_ball_start_velocity(ball_speed)
veltwist.linear.x=ball_cmd_vel.x
veltwist.linear.z=ball_cmd_vel.y
left_hand_position = hand_positions[0]/1000.0
right_hand_position = hand_positions[1]/1000.0
last_bounce = 0
ball_logic_position = ball_measured_position
bounds = bound_lrud(-0.275,0.42,0.44,0.14)
#######################
# Perform time setup
r = rospy.Rate(circle_tick_rate)
start_time = rospy.get_time()
now_time = start_time
#######################
# Main Loop
while not rospy.is_shutdown():
# Time Calcs
last_time = now_time
now_time = rospy.get_time() - start_time
delta_time = now_time - last_time
# Calculate position to use for logic
ball_logic_position = ball_measured_position #combine_ball_positions(ball_expected_position(ball_logic_position, ball_cmd_vel, delta_time), ball_recieved_position)
# Get Hand Positions in meters
left_hand_position = hand_positions[0]/1000.0
right_hand_position = hand_positions[1]/1000.0
# Display
print(ball_measured_position.y, left_hand_position)
###print(right_hand_position, left_hand_position)
pong_plot(bounds, plot_size, left_hand_position, right_hand_position, paddle_size, ball_logic_position)
# Check Bounds
[do_bounce,last_bounce] = check_bounds(ball_logic_position, bounds, last_bounce)
# Event?
if do_bounce == True:
score_happened = False
if last_bounce == 'lf':
veltwist.linear.x = np.abs(veltwist.linear.x)
if np.abs(ball_measured_position.y-bounds.ylow-left_hand_position) > (paddle_size/2):
score_happened = True
print('miss:' + str(score_happened))
if np.abs(ball_measured_position.y-bounds.ylow-left_hand_position) <= (paddle_size/2):
score_happened = False
print('recovery:' + str(score_happened))
elif last_bounce == 'rt':
veltwist.linear.x = -np.abs(veltwist.linear.x)
if np.abs(ball_measured_position.y-bounds.ylow-right_hand_position) > (paddle_size/2):
score_happened = True
print('miss:' + str(score_happened))
if np.abs(ball_measured_position.y-bounds.ylow-right_hand_position) <= (paddle_size/2):
score_happened = False
print('recovery:' + str(score_happened))
if last_bounce == 'dn':
veltwist.linear.z = np.abs(veltwist.linear.z)
elif last_bounce == 'up':
veltwist.linear.z = -np.abs(veltwist.linear.z)
# Score?
if score_happened == True:
# Incr score, return hand, and pause
if last_bounce == 'lf':
score[0] = score[0] + 1
if last_bounce == 'rt':
score[1] = score[1] + 1
veltwist.linear.x=0
veltwist.linear.z=0
hand_velocity_publisher.publish(veltwist)
disp_score(score)
last_bounce = 0
# return hand to default position
hif.move_to_home(thetalistHOME)
ball_cmd_vel = get_ball_start_velocity(ball_speed)
veltwist.linear.x=ball_cmd_vel.x
veltwist.linear.z=ball_cmd_vel.y
# TODO: increase speed
#Publish hand twist
hand_velocity_publisher.publish(veltwist)
# If win, end game
if np.max(score) >= max_score:
disp_win(score)
quit()
# Update Rate Limiter
r.sleep()
>>>>>>> b99b87f266d8c5d67ef1d087d75792ec19af86fa
#########################################
# Boilerplate Code
if __name__ == '__main__':
<<<<<<< HEAD
try:
pong_logic()
except rospy.ROSInterruptException:
pass
=======
try:
pong_logic()
except rospy.ROSInterruptException:
pass
>>>>>>> b99b87f266d8c5d67ef1d087d75792ec19af86fa