-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_score.py
executable file
·50 lines (40 loc) · 1.28 KB
/
get_score.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import rospy
from std_msgs.msg import String
import json
import sys
class Sample():
def __init__(self):
# war state
topicname_war_state = "war_state"
self.war_state = rospy.Subscriber(topicname_war_state, String, self.stateCallback)
self.my_score = 0
self.enemy_score = 0
self.FLAG = 0
self.COUNT = 0
def stateCallback(self, state):
dic = json.loads(state.data)
self.my_score = int(dic["scores"]["r"])
self.enemy_score = int(dic["scores"]["b"])
print('my_score=%d' % self.my_score)
print('enemy_score=%d' % self.enemy_score)
self.FLAG = 1
def strategy(self):
# Main Loop --->
r = rospy.Rate(100)
while not rospy.is_shutdown():
r.sleep()
if self.FLAG > 0:
sys.exit(0)
# error exit
# the case of "no state callback is published".
# basically state callback come in 1s,
# but sometimes no state callback. when it occurs, go exit..
self.COUNT += 1
if self.COUNT > 3000:
sys.exit(0)
if __name__ == '__main__':
rospy.init_node('sample')
bot = Sample()
bot.strategy()