json_parser.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #################################################################
00003 ##\file
00004 #
00005 # \note
00006 # Copyright (c) 2011, 2012 \n
00007 # Cardiff University \n
00008 #
00009 #################################################################
00010 #
00011 # \note
00012 # Project name: Multi-Role Shadow Robotic System for Independent Living
00013 # \note
00014 # ROS stack name: srs
00015 # \note
00016 # ROS package name: srs_decision_making
00017 #
00018 # \author
00019 # Author: Ze Ji, Renxi Qiu
00020 #
00021 # \date Date of creation: June 2012
00022 #
00023 # \brief
00024 # parsing json commands
00025 #
00026 #################################################################
00027 
00028 import roslib 
00029 roslib.load_manifest('srs_decision_making')
00030 #import json
00031 import rospy
00032 import simplejson as json
00033 
00034 def decode_action(json_param):
00035     """
00036     return the action name
00037     """
00038     try:
00039         json_decoded = json.loads(json_param)
00040     
00041         act = json_decoded['action']
00042     except json.JSONDecodeError:
00043         print "Oops, error when decoding json_param", json_param
00044         return None
00045 
00046     return act 
00047 
00048 def decode_move_parameters(json_param):
00049     """
00050     return a dict containing pose2d of the destination
00051     """
00052     try:
00053         json_decoded = json.loads(json_param)
00054     
00055         json_dest = json_decoded['destination']
00056         json_pose2d = json_dest['pose2d']
00057 
00058         #pose_dict = dict()
00059     
00060         #pose_dict['x'] = json_pose2d['x']
00061         #pose_dict['y'] = json_pose2d['y']
00062         #pose_dict['theta'] = json_pose2d['theta']
00063     except json.JSONDecodeError:
00064         print "Oops, error when decoding json_param", json_param
00065         return None
00066 
00067     return json_pose2d
00068 
00069 def decode_detect_parameters(json_param):
00070     """
00071     return a dict containing object info (name etc)
00072     """
00073     try:
00074         json_decoded = json.loads(json_param)
00075     
00076         json_obj = json_decoded['object']
00077         #json_obj_type = json_obj['object_type']
00078     except json.JSONDecodeError:
00079         print "Oops, error when decoding json_param", json_param
00080         return None
00081 
00082     return json_obj
00083 
00084 def decode_grasp_parameters(json_param):
00085     """
00086     return a dict containing object info (name etc)
00087     """
00088     try:
00089         json_decoded = json.loads(json_param)
00090     
00091         json_obj = json_decoded['object']
00092         #json_obj_type = json_obj['object_type']
00093     except json.JSONDecodeError:
00094         print "Oops, error when decoding json_param", json_param
00095         return None
00096 
00097     return json_obj
00098 
00099 def decode_check_ws_parameters(json_param):
00100     """
00101     return a dict containing workspace info (name etc) -- check workspace
00102     """
00103     try:
00104         json_decoded = json.loads(json_param)
00105     
00106         json_obj = json_decoded['workspace']
00107         #json_obj_type = json_obj['object_type']
00108         json_dest = json_decoded['destination']
00109         json_pose2d = json_dest['pose2d']
00110 
00111     except json.JSONDecodeError:
00112         print "Oops, error when decoding json_param", json_param
00113         return None
00114 
00115     return json_obj, json_pose2d
00116 
00117 def detect_feedback_to_json(userdata):
00118     jsonPose = ''
00119    
00120     pose = dict()
00121     pose['x'] = userdata.target_object_pose.pose.position.x
00122     pose['y'] = userdata.target_object_pose.pose.position.y
00123     pose['z'] = userdata.target_object_pose.pose.position.z
00124     pose['rotx'] = userdata.target_object_pose.pose.orientation.x
00125     pose['roty'] = userdata.target_object_pose.pose.orientation.y
00126     pose['rotz'] = userdata.target_object_pose.pose.orientation.z
00127     pose['rotw'] = userdata.target_object_pose.pose.orientation.w
00128 
00129     feedback = dict()
00130     feedback['action'] = 'detect'
00131 
00132     jsonObjectType = dict()
00133     jsonObjectType['object_type'] = userdata.target_object_name
00134     feedback['object'] = jsonObjectType
00135     feedback['pose'] = pose
00136 
00137     wrapAll = dict()
00138     wrapAll['feedback'] = feedback
00139 
00140     jsonPose = json.dumps(wrapAll)
00141 
00142     return jsonPose
00143 
00144 class Task:
00145     def __init__(self, json_task):
00146         self.json_task = json_task
00147         self.task_json_string = json.dumps(self.json_task)
00148 
00149     def addItem(self, key, value):
00150         self.json_task[key] = value
00151         self.task_json_string = json.dumps(self.json_task)
00152 
00153 class Tasks:
00154 
00155     def __init__(self, json_raw_string):
00156         self.json_raw_string = json_raw_string
00157         self.json_decoded = json.loads(self.json_raw_string)
00158         self.tasks_json = []
00159         self.tasks_dec = []
00160         
00161         self.tasks_list = []
00162         self.device_id = ''   # invalid default empty
00163         self.device_type = '' # empty
00164         self.decode()
00165 
00166     def decode(self):
00167         
00168         self.device_id = self.json_decoded['initializer']['device_id']
00169         self.device_type = self.json_decoded['initializer']['device_type']
00170         #self.tasks_json = str(self.json_decoded['tasks'])
00171         self.tasks_dec = self.json_decoded['tasks']
00172 
00173         for t in self.tasks_dec:
00174             tempTask = Task(t)
00175             self.tasks_list.append(tempTask)
00176             #self.tasks_json.append(json.dumps(t))
00177             #self.tasks_json.append(str(t))
00178 
00179 
00180 class Task_Feedback:
00181     def __init__(self, task_id, task_initializer, task_initializer_type, task_json_string):
00182         self.task_id = task_id
00183         self.task_initializer = task_initializer
00184         self.task_initializer_type = task_initializer_type
00185         self.task_name = ''  #to be updated after decoding
00186         self.task_parameter = '' #to be updated after decoding
00187         self.task_schedule = '' #to be updated after decoding
00188         self.json_decoded = json.loads(task_json_string)
00189         #print self.json_decoded
00190         
00191         self.action_object = ''    #target of individual action, to be updated every action steps at the semantic DM state
00192         
00193         self.action_object_parent = ''  #parent object of the above action, to be updated in semantic DM state 
00194         
00195         self.decode()
00196     
00197     def decode(self):
00198         
00199         self.task_name = self.json_decoded['task']
00200         
00201         if 'task_schedule' in self.json_decoded:
00202             self.task_schedule = self.json_decoded['task_schedule']
00203             
00204         
00205         command_list = ['search','get','fetch','deliver', 'move']
00206         
00207         if self.task_name.lower() in command_list:
00208             if    self.task_name.lower() == 'move':
00209                 destination = self.json_decoded['destination']
00210                 
00211                 print destination
00212                 
00213                 if 'pose2d_string' in destination:
00214                     self.task_parameter = destination ['pose2d_string']
00215                 elif 'predefined_pose' in destination:
00216                     self.task_parameter = destination ['predefined_pose']
00217                 elif 'pose2d' in destination:
00218                     self.task_parameter = destination ['pose2d']
00219                 else:
00220                     print 'WARNING not understandable destination found for move'
00221             else:
00222                 try:
00223                     self.task_parameter = self.json_decoded['object']['object_type']
00224                 except keyError:
00225                      print 'WARNING no target object given for search, get, fetch or deliver'
00226         else:
00227             print 'WARNING not understandable command found, available commands are'
00228             print command_list
00229         
00230         
00231         
00232 
00233 """
00234 JASON request formation
00235             
00236 =============== To Move to Coordinate in one single string format at time 1263798000000 ==================
00237 
00238 {"tasks":[{"time_schedule":1263798000000,"task":"move","destination":{"pose2d_string":"[0 1 3.14]"}}],"initializer":{"device_type":"ui_loc","device_id":"ui_loc_0001"}}
00239 
00240 
00241 =============== To Move to a predefined position (immediately, no time specified) ==================
00242 
00243 {"tasks":[{"task":"move","destination":{"predefined_pose":"sofa_right"}}],"initializer":{"device_type":"ui_loc","device_id":"ui_loc_0001"}}
00244 
00245 
00246 =============== To Move to Coordinate by specifying the pose parameters (as numbers) (immediately, no time specified) ==================
00247 
00248 {"tasks":[{"task":"move","destination":{"pose2d":{"theta":3.14,"y":1.0,"x":0.0}}}],"initializer":{"device_type":"ui_loc","device_id":"ui_loc_0001"}}
00249 
00250 
00251 =============== To Fetch an object to a predefined position (soft_left) ==================
00252 
00253 {"tasks":[{"time_schedule":1263798000000,"task":"fetch","deliver_destination":{"predefined_pose":"sofa_left"},"object":{"object_type":"Milkbox"}}],"initializer":{"device_type":"ui_loc","device_id":"ui_loc_0001"}}
00254 
00255 
00256 =============== To Fetch an object to a predefined position (soft_left), from a list of possible workspaces (Table0, Table1) ==================
00257 
00258 {"tasks":[{"task":"fetch","workspaces":["Table0","Table1"],"deliver_destination":{"predefined_pose":"sofa_left"},"object":{"object_type":"Milkbox"}}],"initializer":{"device_type":"ui_loc","device_id":"ui_loc_0001"}}
00259 
00260 
00261 =============== Similar to Fetch, To Get a Book ==================
00262 
00263 {"tasks":[{"task":"get","object":{"object_type":"Book"}}],"initializer":{"device_type":"ui_loc","device_id":"ui_loc_0001"}}
00264 
00265 
00266 =============== Similar to Fetch, To Get a Book from a list of workspaces (BookShelf0, Table0) ==================
00267 
00268 {"tasks":[{"task":"get","workspaces":["BookShelf0","Table0"],"object":{"object_type":"Book"}}],"initializer":{"device_type":"ui_loc","device_id":"ui_loc_0001"}}
00269 
00270 
00271 =============== User specifies a list of tasks 1) To Get a MilkBox 2) move back to sofa_right with different time schedules ==================
00272 
00273 {"tasks":[{"time_schedule":1263798000000,"task":"get","object":{"object_type":"Milkbox"}},{"time_schedule":1263798000000,"task":"move","destination":{"predefined_pose":"sofa_right"}}],"initializer":{"device_type":"ui_loc","device_id":"ui_loc_0001"}}
00274 
00275 
00276 Manual command format:
00277 
00278 {"tasks":[{"task":"move", "mode":"manual", "component":"torso","destination":{"predefined_pose":"nod"}}],"initializer":{"device_type":"ui_pri","device_id":"ui_pri_101"}}
00279 
00280 {"tasks":[{"task":"move", "mode":"manual", "component":"torso","destination":{"torso_pose":{"tilt1":-0.1,"pan":0.1,"tilt2":0.15}}}],"initializer":{"device_type":"ui_pri","device_id":"ui_pri_101"}}
00281 
00282 {"tasks":[{"task":"move", "mode":"manual", "component":"head","destination":{"predefined_pose":"home"}}],"initializer":{"device_type":"ui_pri","device_id":"ui_pri_101"}}
00283 
00284 """          
00285             
00286             
00287             
00288 """
00289 JASON feedback formation
00290             
00291             
00292 current_action: the action excuting in the robot at the moment
00293     name: the name of the state machine  e.g. sm_srs_navigation, sm_srs_detection, sm_srs_grasp, sm_srs_put_on_tray, sm_enviroment_update
00294     state: the state of the operation e.g. started, completed
00295     step_id: the step in the action sequence
00296 
00297 feedback: description message send back to UI
00298     lang: the language of the message  e.g. en, it, de
00299     message: the message itself
00300 
00301 last_action: the information of the previous action
00302     name: the name of the state machine  e.g. sm_srs_navigation, sm_srs_detection, sm_srs_grasp, sm_srs_put_on_tray, sm_enviroment_update    
00303     outcome: the outcome of the operation e.g. succeeded, not_completed, failed, preempted
00304 
00305 task: the information of the overall task operated by the robot
00306     task_id: a unique id of the task
00307     task_initializer: the device id of the ui which initialised the task
00308     task_initializer_type: the type of the device which initialised the task
00309     task_name: the name of the overall task  e.g. get, fetch, move
00310     task_parameter: the main parameter of the overall task  e.g. milk
00311 
00312 Example:
00313 
00314 [
00315   {
00316     "current_action": {
00317       "name": "sm_srs_navigation",
00318       "state": "started",
00319       "step_id": 3
00320     },
00321     "feedback": {
00322       "lang": "en",
00323       "message": "navigation started"
00324     },
00325     "last_action": {
00326       "name": "sm_srs_detection",
00327       "outcome": "succeeded",
00328       "step_id": 2
00329     },
00330     "task": {
00331       "task_id": "dm_10001_1",
00332       "task_initializer": "ui_loc_0001",
00333       "task_initializer_type": "ui_loc",
00334       "task_name": "fetch",
00335       "task_parameter": "milk"
00336     }
00337   }
00338 ]      
00339             
00340 """  


srs_decision_making
Author(s): Renxi Qiu
autogenerated on Mon Oct 6 2014 08:38:32