Skip to content

Commit f1cb636

Browse files
committed
Merge branch 'develop'
2 parents 6502fe4 + 168cd68 commit f1cb636

3 files changed

Lines changed: 55 additions & 52 deletions

File tree

benchbot_api/api_callbacks.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import base64
22
import cv2
3-
import jsonpickle
4-
import jsonpickle.ext.numpy as jet
53
import numpy as np
64

7-
jet.register_handlers()
5+
ENCODING_TO_CONVERSION = {'bgr8': cv2.COLOR_BGR2RGB}
6+
7+
8+
def convert_to_rgb(data):
9+
cvt = ENCODING_TO_CONVERSION.get(data['encoding'], None)
10+
return data['data'] if cvt is None else cv2.cvtColor(data['data'], cvt)
811

912

1013
def decode_color_image(data):
@@ -13,10 +16,14 @@ def decode_color_image(data):
1316
cv2.imdecode(
1417
np.fromstring(base64.b64decode(data['data']), np.uint8),
1518
cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)
19+
elif data['encoding'] == 'rgb8':
20+
return cv2.imdecode(
21+
np.fromstring(base64.b64decode(data['data']), np.uint8),
22+
cv2.IMREAD_COLOR)
1623
else:
1724
raise ValueError(
18-
"decode_ros_image: received image data with unsupported encoding: %s",
19-
data['encoding'])
25+
"decode_ros_image: received image data with unsupported encoding: %s"
26+
% data['encoding'])
2027

2128

2229
def decode_jsonpickle(data):

benchbot_api/benchbot.py

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
from enum import Enum, unique
44
import importlib
5+
import jsonpickle
6+
import jsonpickle.ext.numpy as jet
57
import os
68
import requests
79
import sys
810
import time
911

1012
from .agent import Agent
1113

14+
jet.register_handlers()
15+
1216
DEFAULT_ADDRESS = 'benchbot_supervisor'
1317
DEFAULT_PORT = 10000
1418

@@ -53,9 +57,8 @@ class RouteType(Enum):
5357
""" """
5458
CONNECTION = 0,
5559
CONFIG = 1,
56-
SIMULATOR = 2,
57-
STATUS = 3,
58-
EXPLICIT = 4
60+
ROBOT = 2,
61+
EXPLICIT = 3
5962

6063
def __init__(self,
6164
agent=None,
@@ -94,10 +97,8 @@ def _build_address(self, route_name, route_type=RouteType.CONNECTION):
9497
return base + 'connections/' + route_name
9598
elif route_type == BenchBot.RouteType.CONFIG:
9699
return base + 'config/' + route_name
97-
elif route_type == BenchBot.RouteType.SIMULATOR:
98-
return base + 'simulator/' + route_name
99-
elif route_type == BenchBot.RouteType.STATUS:
100-
return base + 'status/' + route_name
100+
elif route_type == BenchBot.RouteType.ROBOT:
101+
return base + 'robot/' + route_name
101102
elif route_type == BenchBot.RouteType.EXPLICIT:
102103
return base + route_name
103104
else:
@@ -124,10 +125,10 @@ def _receive(self, route_name=None, route_type=RouteType.CONNECTION):
124125
resp = requests.get(self._build_address(route_name, route_type))
125126
if resp.status_code >= 300:
126127
raise _UnexpectedResponseError(resp.status_code)
127-
return resp.json()
128+
return jsonpickle.decode(resp.content)
128129
except:
129130
raise requests.ConnectionError(
130-
"Failed to establish a connection to BenchBot supervisor")
131+
"Communication failed with the BenchBot supervisor")
131132

132133
def _send(self,
133134
route_name=None,
@@ -151,8 +152,8 @@ def _send(self,
151152
"""
152153
data = {} if data is None else data
153154
try:
154-
resp = requests.get(self._build_address(route_name, route_type),
155-
json=data)
155+
resp = requests.post(self._build_address(route_name, route_type),
156+
json=data)
156157
if resp.status_code >= 300:
157158
raise _UnexpectedResponseError(resp.status_code)
158159
except:
@@ -189,10 +190,10 @@ def actions(self):
189190
list
190191
A list of actions the robot can take. If the robot has collided with an obstacle or finished its task, this list will be empty.
191192
"""
192-
return ([] if self._receive(
193-
'is_collided', BenchBot.RouteType.SIMULATOR)['is_collided'] or
193+
return ([] if self._receive('is_collided',
194+
BenchBot.RouteType.ROBOT)['is_collided'] or
194195
self._receive('is_finished',
195-
BenchBot.RouteType.STATUS)['is_finished'] else
196+
BenchBot.RouteType.ROBOT)['is_finished'] else
196197
self._receive('actions', BenchBot.RouteType.CONFIG))
197198

198199
@property
@@ -273,7 +274,7 @@ def empty_results(self):
273274
def next_scene(self):
274275
# Bail if next is not a valid operation
275276
if (self._receive('is_collided',
276-
BenchBot.RouteType.SIMULATOR)['is_collided']):
277+
BenchBot.RouteType.ROBOT)['is_collided']):
277278
raise RuntimeError("Collision stated detected for robot; "
278279
"cannot proceed to next scene")
279280
elif 'semantic_slam' in self.task_details['type']:
@@ -283,12 +284,12 @@ def next_scene(self):
283284
# Move to the next scene
284285
print("Moving to next scene ... ", end='')
285286
resp = self._receive(
286-
'next', BenchBot.RouteType.SIMULATOR) # This should be a send...
287+
'next', BenchBot.RouteType.ROBOT) # This should be a send...
287288
print("Done.")
288289

289290
# Raise an error if it failed (because it was called a second time)
290291
if not resp['next_success']:
291-
raise RuntimeError("Simulator is already at final scene; "
292+
raise RuntimeError("Robot is already at final scene; "
292293
"cannot proceed to next scene")
293294

294295
def reset(self):
@@ -300,13 +301,11 @@ def reset(self):
300301
Observations and action result at the start of the task.
301302
"""
302303
# Only restart the supervisor if it is in a dirty state
303-
if self._receive('is_dirty', BenchBot.RouteType.SIMULATOR)['is_dirty']:
304-
print("Dirty simulator state detected. Performing reset ... ",
305-
end='')
304+
if self._receive('is_dirty', BenchBot.RouteType.ROBOT)['is_dirty']:
305+
print("Dirty robot state detected. Performing reset ... ", end='')
306306
sys.stdout.flush()
307-
self._receive(
308-
'reset',
309-
BenchBot.RouteType.SIMULATOR) # This should be a send...
307+
self._receive('reset',
308+
BenchBot.RouteType.ROBOT) # This should be a send...
310309
print("Complete.")
311310
return self.step(None)
312311

@@ -369,34 +368,32 @@ def start(self):
369368
"Are you sure it is available?" % self.supervisor_address)
370369
print("Connected!")
371370

372-
# Wait until the simulator is running
373-
print("Waiting to establish connection to a running simulator ... ",
371+
# Wait until the robot is running
372+
print("Waiting to establish connection to a running robot ... ",
374373
end='')
375374
sys.stdout.flush()
376375
while (not self._receive("is_running",
377-
BenchBot.RouteType.SIMULATOR)['is_running']):
376+
BenchBot.RouteType.ROBOT)['is_running']):
378377
time.sleep(0.1)
379378
print("Connected!")
380379

381380
# Get references to all of the API callbacks in robot config
382381
self._connection_callbacks = {
383382
k:
384383
BenchBot._attempt_connection_imports(v) for k, v in self._receive(
385-
'robot', BenchBot.RouteType.CONFIG).items()
384+
'robot', BenchBot.RouteType.CONFIG)['connections'].items()
386385
}
387386

388-
# Ensure we are starting in a clean simulator state
389-
if (self._receive('map_selection_number',
390-
BenchBot.RouteType.SIMULATOR)['map_selection_number']
387+
# Ensure we are starting in a clean robot state
388+
if (self._receive('selected_env', BenchBot.RouteType.ROBOT)['number']
391389
!= 0):
392390
print(
393-
"Simulator detected not to be in the first scene. "
391+
"Robot detected not to be in the first scene. "
394392
"Performing restart ... ",
395393
end='')
396394
sys.stdout.flush()
397-
self._receive(
398-
'restart',
399-
BenchBot.RouteType.SIMULATOR) # This should be a send...
395+
self._receive('restart',
396+
BenchBot.RouteType.ROBOT) # This should be a send...
400397
else:
401398
self.reset()
402399

@@ -411,7 +408,7 @@ def step(self, action, **action_kwargs):
411408
Arguments to be used by the action.
412409
Must be empty if action is 'move_next'.
413410
Must be 'distance' if action is 'move_distance'. Distance is in metres.
414-
Must be 'angle' if action is 'move_angle'. Angle is in radians.
411+
Must be 'angle' if action is 'move_angle'. Angle is in degrees.
415412
416413
Returns
417414
-------
@@ -445,9 +442,9 @@ def step(self, action, **action_kwargs):
445442
raise ValueError(
446443
"Action '%s' is unavailable due to: %s" %
447444
(action, ('COLLISION' if self._receive(
448-
'is_collided', BenchBot.RouteType.SIMULATOR)
449-
['is_collided'] else 'FINISHED' if self._receive(
450-
'is_finished', BenchBot.RouteType.STATUS)
445+
'is_collided', BenchBot.RouteType.ROBOT)['is_collided']
446+
else 'FINISHED' if self._receive(
447+
'is_finished', BenchBot.RouteType.ROBOT)
451448
['is_finished'] else 'WRONG_ACTUATION_MODE?')))
452449

453450
# Made it through checks, actually perform the action
@@ -458,24 +455,23 @@ def step(self, action, **action_kwargs):
458455
# Derive action_result (TODO should probably not be this flimsy...)
459456
action_result = ActionResult.SUCCESS
460457
if self._receive('is_collided',
461-
BenchBot.RouteType.SIMULATOR)['is_collided']:
458+
BenchBot.RouteType.ROBOT)['is_collided']:
462459
action_result = ActionResult.COLLISION
463460
elif self._receive('is_finished',
464-
BenchBot.RouteType.STATUS)['is_finished']:
461+
BenchBot.RouteType.ROBOT)['is_finished']:
465462
action_result = ActionResult.FINISHED
466463

467464
# Retrieve and return an updated set of observations
468465
raw_os = {o: self._receive(o) for o in self.observations}
469466
if 'scd' in self.task_details['type']:
470467
raw_os.update({
471468
'scene_number':
472-
self._receive('map_selection_number',
473-
BenchBot.RouteType.SIMULATOR)
474-
['map_selection_number']
469+
self._receive('selected_env', BenchBot.RouteType.ROBOT)
470+
['number']
475471
})
476472
return ({
477-
k: self._connection_callbacks[k](v) if
478-
(k in self._connection_callbacks and
479-
self._connection_callbacks[k] is not None) else v
473+
k: (self._connection_callbacks[k](v) if
474+
(k in self._connection_callbacks and
475+
self._connection_callbacks[k] is not None) else v)
480476
for k, v in raw_os.items()
481477
}, action_result)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
long_description = fh.read()
55

66
setup(name='benchbot_api',
7-
version='0.1.3',
7+
version='1.1.0',
88
author='Ben Talbot',
99
author_email='b.talbot@qut.edu.au',
1010
description=

0 commit comments

Comments
 (0)