Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/carla carma state integration #49

Merged
merged 26 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions carla-carma-integration/carla_carma_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ project(carla_carma_bridge)
find_package(catkin REQUIRED COMPONENTS
autoware_msgs
rospy
cav_srvs
cav_msgs
health_monitor
kjrush marked this conversation as resolved.
Show resolved Hide resolved

)

catkin_python_setup()
Expand All @@ -21,6 +24,8 @@ catkin_install_python(PROGRAMS
src/carla_carma_bridge/carla_to_carma_localization
src/carla_carma_bridge/carma_to_carla_ackermann_cmd
src/carla_carma_bridge/carla_to_carma_external_objects
src/carla_carma_bridge/carma_carla_route
src/carla_carma_bridge/carma_carla_plugins
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#String: Route to be initialized
selected_route: '/opt/carma/routes/tfhrc_test_route.csv'

# List of String: Required plugins for the platform to be functional
selected_plugins:
- RouteFollowing
- InLaneCruisingPlugin
- StopAndWaitPlugin
- Pure Pursuit
3 changes: 3 additions & 0 deletions carla-carma-integration/carla_carma_bridge/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<exec_depend>autoware_msgs</exec_depend>
<exec_depend>carla_msgs</exec_depend>
<exec_depend>cav_msgs</exec_depend>
<exec_depend>cav_srvs</exec_depend>
<exec_depend>health_monitor</exec_depend>
kjrush marked this conversation as resolved.
Show resolved Hide resolved

<export>
</export>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# Copyright (C) 2021 LEIDOS.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
#
# This file is loosely based on the reference architecture developed by Intel Corporation for Leidos located here
# https://github.com/usdot-fhwa-stol/carma-platform/blob/develop/health_monitor/src/plugin_manager.cpp
#
# That file has the following license and some code snippets from it may be present in this file as well and are under the same license.
#
# Copyright (c) 2018-2019 Intel Corporation
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
#

import rospy
from cav_msgs.msg import Plugin, PluginList, PluginStatus
from cav_srvs.srv import GetRegisteredPlugins

plugin_pub = rospy.Publisher('/guidance/plugin_discovery', Plugin())
kjrush marked this conversation as resolved.
Show resolved Hide resolved
selected_plugins = rospy.get_param('selected_plugins')

#List to contain the registered plugins from the service call in initialize()
plugin_list = PluginList()


#Check if the plugin is included in the list of required plugins
def check_selected_plugins(name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function is equivalent to name in selected_plugins if you want to simplify things a bit.

for i in selected_plugins:
if i == name:
return True
else:
return False

def plugin_status_cb(plugin_msg):
print("Received status from: " ++ plugin_msg.name)
requested = get_plugin(plugin_msg)
plugin = Plugin()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the intent to do something with this plugin object? It looks like it gets created here in the callback, some data gets copied into it from the callback data, and then it falls out of scope and is deleted. If the intent is to save the data for later this probably needs to get added to a list somewhere with a longer lifetime (maybe the global plugin_list?). Or if it's just for temporary use, I think we can do away with it entirely and just use the data directly from the callback.

plugin.available = plugin_msg.available
plugin.name = plugin_msg.name
plugin.capability = plugin_msg.capability
plugin.type = plugin_msg.type
activation_srv = rospy.ServiceProxy("/activate_plugin")
kjrush marked this conversation as resolved.
Show resolved Hide resolved

#If the requested plugin is registered, don't change the activation status
if requested != None:
plugin.activated = plugin_msg.activated
elif check_selected_plugins(plugin_msg.name) == True:
plugin.activated = True
activation_srv.call(plugin.name, plugin.activated)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we probably want to just call this service once per plugin. We'll be getting these plugin status updates at ~10hz I think, and while I don't think it'll necessarily cause issues to call the service repeatedly with the same data, it's not how the service is intended to be used. It's probably best to call it maybe only the first time we see a given plugin report that it's registered, or maybe even just directly after our initial call to the get_registered_plugins service.



#Search through list of registered plugins. If it exists, return the requested plugin
def get_plugin(plugin):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain a little bit of the intent here? We pass in a plugin object (containing all the plugin data fields) and if that exact object exists already in the plugin list, we get that object back, otherwise we get None? Is the intent for this to check if we've seen data about this plugin already?

if plugin in plugin_list.plugins:
return plugin
else:
return None


def initialize():
registered_plugins_serv = rospy.ServiceProxy('/get_registered_plugins')
plugin_list.plugins = registered_plugins_serv.call()
plugin_sub = rospy.Subscriber('/plugin_discovery',Plugin(), plugin_status_cb)
rospy.spin()
kjrush marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == '__main__':
print("carma_carla_plugins")
initialize()
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/usr/bin/env python
# Copyright (C) 2021 LEIDOS.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""
Call Services from CARMA:
Service: /guidance/get_available_routes
/guidance/set_active_route

Publish to CARMA :cav_msgs::Route

"""
from asyncio.windows_events import NULL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the types on line 23-25 should be looked at, this are pretty abnormal python types so I'm not sure where they're coming into play here. Windows API stuff and the XML packages definitely aren't needed for this, and I'm not sure what the "typing" package is.

from typing import Iterator
from xml.dom.minidom import Document
import rospy
import os
from geometry_msgs.msg import PoseStamped
from cav_msgs.msg import RouteState
from cav_msgs.msg import Route
from cav_msgs.msg import RouteEvent, GuidanceState
from cav_srvs.srv import GetAvailableRoutes
from cav_srvs.srv import SetActiveRoute

localized_sub = rospy.Subscriber('/localization/current_pose', PoseStamped)
state_pub = rospy.Publisher('/guidance/state', GuidanceState)
selected_route = rospy.get_param('selected_route')

class ErrorStatus:
def __init__(self, value, text):
self.value = value
self.text = text

#Get the routes to be set after localization
def get_available_routes():
service = rospy.ServiceProxy('/get_available_routes', GetAvailableRoutes)
try:
resp = service.call(0)
available_routes = GetAvailableRoutes()
available_routes.availableRoutes = resp.availableRoutes

except:
print("Invalid: /guidance/get_available_routes failed.")
else:
print("Available Routes acquired")
return available_routes.availableRoutes



def set_route(id):
state = GuidanceState()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like this state variable is used in this function.

service = rospy.ServiceProxy('/set_active_route', SetActiveRoute)

NO_ERROR = ErrorStatus(0, "NO_ERROR")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend moving these constants out of this function to be global constants instead.

NO_ROUTE = ErrorStatus(1, "NO_ROUTE")
ALREADY_FOLLOWING_ROUTE = ErrorStatus(2, "ALREADY_FOLLOWING_ROUTE")
ROUTE_FILE_ERROR = ErrorStatus(3, "ROUTE_FILE_ERROR")
ROUTING_FAILURE = ErrorStatus(4, "ROUTING_FAILURE")
TRANSFORM_ERROR = ErrorStatus(5, "TRANSFORM_ERROR")

try:
#Service Call Request
resp = service(0, id)
if resp.errorStatus != NO_ERROR.value:
errorDescription = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this errorDescription used anywhere? Not sure what all this error handling is intended for, it can probably be simplified to a check for the "NO_ERROR" value and a generic exception or error notice on all other cases. I don't think there's a meaningful way for this node to handle any of these other errors outside of maybe waiting a brief delay and retrying right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any update on this?

if resp.errorStatus == NO_ROUTE.value:
lewisid marked this conversation as resolved.
Show resolved Hide resolved
errorDescription = NO_ROUTE.text

elif resp.errorStatus == ALREADY_FOLLOWING_ROUTE.value:
errorDescription = ALREADY_FOLLOWING_ROUTE.text

elif resp.errorStatus == ROUTE_FILE_ERROR.value:
errorDescription = ROUTE_FILE_ERROR.text

elif resp.errorStatus == ROUTING_FAILURE.value:
errorDescription = ROUTING_FAILURE.text

elif resp.errorStatus == TRANSFORM_ERROR.value:
errorDescription = TRANSFORM_ERROR.text

else:
errorDescription = resp.errorStatus

else:
#Call succeeded: Setting Route
print('call set active route success!')
#Change Guidance from READY to ACTIVE
#set_guidance_active()



except:
print("Fail")

#RouteEvent
route_event_sub = rospy.Subscriber('/guidance/route_event', RouteEvent,route_event_callback)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this subscriber setup to something like the initialize method, just so that it's setup somewhere that's more likely to only be called once.



def route_event_callback(event):
route_event = RouteEvent()
route_event.event = event
if route_event.event == 6:
print("ROUTE_GEN_FAILED")

def check_selected_route(available_routes):
if selected_route in available_routes:
kjrush marked this conversation as resolved.
Show resolved Hide resolved
return True
else:
return False

"""TODO: Listen to a topic indicating that CARLA Sim has started before seting guidance to active
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any progress on this todo? I think maybe the /clock topic might be a decent place to start but I'd double check that with @chengyuan0124

def set_guidance_active():
service = rospy.ServiceProxy('/set_active_guidance') """

def initialize():
available_routes = get_available_routes()
if available_routes == None:
rospy.spin()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to infinite loop the node right? If there's no routes (for example, maybe they haven't finished loading yet) then we'll hit this line and never get to check for any routes again. I'd recommend re-working this logic.

else:
"""Check if the selected route is one of the available routes. If it isn't,then use the first of the available
routes received from the service call"""
if check_selected_route(available_routes) == True:
route = selected_route
else:
route = available_routes.availableRoutes[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for this we'd probably be better of throwing an exception or logging an error (or publishing system FATAL?) since the user's intent (run this route) can't be executed.

set_route(route.route_id)
chengyuan0124 marked this conversation as resolved.
Show resolved Hide resolved
rospy.spin()


if __name__ == '__main__':
print("")
initialize()