Skip to content

Commit

Permalink
Format by autopep8
Browse files Browse the repository at this point in the history
  • Loading branch information
TayYim committed Jun 3, 2024
1 parent 2a23a7e commit f46bf35
Show file tree
Hide file tree
Showing 54 changed files with 387 additions and 306 deletions.
30 changes: 17 additions & 13 deletions manual_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def destroy(self):

class KeyboardControl(object):
"""Class that handles keyboard input."""

def __init__(self, world, start_in_autopilot):
self._autopilot_enabled = start_in_autopilot
self._control = carla.VehicleControl()
Expand Down Expand Up @@ -277,7 +278,7 @@ def parse_events(self, client, world, clock):
self._control.manual_gear_shift = not self._control.manual_gear_shift
self._control.gear = world.player.get_control().gear
world.hud.notification('%s Transmission' %
('Manual' if self._control.manual_gear_shift else 'Automatic'))
('Manual' if self._control.manual_gear_shift else 'Automatic'))
elif self._control.manual_gear_shift and event.key == K_COMMA:
self._control.gear = max(-1, self._control.gear - 1)
elif self._control.manual_gear_shift and event.key == K_PERIOD:
Expand Down Expand Up @@ -321,13 +322,13 @@ def parse_events(self, client, world, clock):
# Set automatic control-related vehicle lights
if self._control.brake:
current_lights |= carla.VehicleLightState.Brake
else: # Remove the Brake flag
else: # Remove the Brake flag
current_lights &= ~carla.VehicleLightState.Brake
if self._control.reverse:
current_lights |= carla.VehicleLightState.Reverse
else: # Remove the Reverse flag
else: # Remove the Reverse flag
current_lights &= ~carla.VehicleLightState.Reverse
if current_lights != self._lights: # Change the light state only if necessary
if current_lights != self._lights: # Change the light state only if necessary
self._lights = current_lights
world.player.set_light_state(carla.VehicleLightState(self._lights))
world.player.apply_control(self._control)
Expand Down Expand Up @@ -536,6 +537,7 @@ def render(self, display):

class HelpText(object):
"""Helper class to handle text output using pygame"""

def __init__(self, font, width, height):
lines = __doc__.split('\n')
self.font = font
Expand Down Expand Up @@ -709,7 +711,7 @@ def __init__(self, parent_actor):
bound_y = 0.5 + self._parent.bounding_box.extent.y
bound_z = 0.5 + self._parent.bounding_box.extent.z

self.velocity_range = 7.5 # m/s
self.velocity_range = 7.5 # m/s
world = self._parent.get_world()
self.debug = world.debug
bp = world.get_blueprint_library().find('sensor.other.radar')
Expand All @@ -718,7 +720,7 @@ def __init__(self, parent_actor):
self.sensor = world.spawn_actor(
bp,
carla.Transform(
carla.Location(x=bound_x + 0.05, z=bound_z+0.05),
carla.Location(x=bound_x + 0.05, z=bound_z + 0.05),
carla.Rotation(pitch=5)),
attach_to=self._parent)
# We need a weak reference to self to avoid circular reference.
Expand Down Expand Up @@ -752,7 +754,7 @@ def _Radar_callback(weak_self, radar_data):
def clamp(min_v, max_v, value):
return max(min_v, min(value, max_v))

norm_velocity = detect.velocity / self.velocity_range # range [-1, 1]
norm_velocity = detect.velocity / self.velocity_range # range [-1, 1]
r = int(clamp(0.0, 1.0, 1.0 - norm_velocity) * 255.0)
g = int(clamp(0.0, 1.0, 1.0 - abs(norm_velocity)) * 255.0)
b = int(abs(clamp(- 1.0, 0.0, - 1.0 - norm_velocity)) * 255.0)
Expand Down Expand Up @@ -781,11 +783,13 @@ def __init__(self, parent_actor, hud):
Attachment = carla.AttachmentType

self._camera_transforms = [
(carla.Transform(carla.Location(x=-2.0*bound_x, y=+0.0*bound_y, z=2.0*bound_z), carla.Rotation(pitch=8.0)), Attachment.SpringArm),
(carla.Transform(carla.Location(x=+0.8*bound_x, y=+0.0*bound_y, z=1.3*bound_z)), Attachment.Rigid),
(carla.Transform(carla.Location(x=+1.9*bound_x, y=+1.0*bound_y, z=1.2*bound_z)), Attachment.SpringArm),
(carla.Transform(carla.Location(x=-2.8*bound_x, y=+0.0*bound_y, z=4.6*bound_z), carla.Rotation(pitch=6.0)), Attachment.SpringArm),
(carla.Transform(carla.Location(x=-1.0, y=-1.0*bound_y, z=0.4*bound_z)), Attachment.Rigid)]
(carla.Transform(carla.Location(x=-2.0 * bound_x, y=+0.0 * bound_y,
z=2.0 * bound_z), carla.Rotation(pitch=8.0)), Attachment.SpringArm),
(carla.Transform(carla.Location(x=+0.8 * bound_x, y=+0.0 * bound_y, z=1.3 * bound_z)), Attachment.Rigid),
(carla.Transform(carla.Location(x=+1.9 * bound_x, y=+1.0 * bound_y, z=1.2 * bound_z)), Attachment.SpringArm),
(carla.Transform(carla.Location(x=-2.8 * bound_x, y=+0.0 * bound_y,
z=4.6 * bound_z), carla.Rotation(pitch=6.0)), Attachment.SpringArm),
(carla.Transform(carla.Location(x=-1.0, y=-1.0 * bound_y, z=0.4 * bound_z)), Attachment.Rigid)]

self.transform_index = 1
self.sensors = [['sensor.camera.rgb', cc.Raw, 'Camera RGB']]
Expand Down Expand Up @@ -894,7 +898,7 @@ def game_loop(args):
display = pygame.display.set_mode(
(args.width, args.height),
pygame.HWSURFACE | pygame.DOUBLEBUF)
display.fill((0,0,0))
display.fill((0, 0, 0))
pygame.display.flip()

hud = HUD(args.width, args.height)
Expand Down
6 changes: 4 additions & 2 deletions metrics_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ def main():
"""

# pylint: disable=line-too-long
description = ("Scenario Runner's metrics module. Evaluate the execution of a specific scenario by developing your own metric.\n")
description = (
"Scenario Runner's metrics module. Evaluate the execution of a specific scenario by developing your own metric.\n")

parser = argparse.ArgumentParser(description=description,
formatter_class=RawTextHelpFormatter)
formatter_class=RawTextHelpFormatter)
parser.add_argument('--host', default='127.0.0.1',
help='IP of the host server (default: localhost)')
parser.add_argument('--port', '-p', default=2000,
Expand All @@ -150,5 +151,6 @@ def main():

MetricsManager(args)


if __name__ == "__main__":
sys.exit(main())
2 changes: 2 additions & 0 deletions no_rendering_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,7 @@ def game_loop(args):
if world_module is not None:
world_module.destroy()


def exit_game():
module_manager.clear_modules()
pygame.quit()
Expand Down Expand Up @@ -1490,5 +1491,6 @@ def main():

game_loop(args)


if __name__ == '__main__':
main()
9 changes: 6 additions & 3 deletions scenario_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
try:
from packaging.version import Version
except ImportError:
from distutils.version import LooseVersion as Version # Python 2 fallback
from distutils.version import LooseVersion as Version # Python 2 fallback
import importlib
import inspect
import os
Expand All @@ -35,11 +35,13 @@
try:
# requires Python 3.8+
from importlib.metadata import metadata

def get_carla_version():
return Version(metadata("carla")["Version"])
except ModuleNotFoundError:
# backport checking for older Python versions; module is deprecated
import pkg_resources

def get_carla_version():
return Version(pkg_resources.get_distribution("carla").version)

Expand Down Expand Up @@ -106,7 +108,8 @@ def __init__(self, args):
self.client.set_timeout(self.client_timeout)
carla_version = get_carla_version()
if carla_version < Version(MIN_CARLA_VERSION):
raise ImportError("CARLA version {} or newer required. CARLA version found: {}".format(MIN_CARLA_VERSION, carla_version))
raise ImportError("CARLA version {} or newer required. CARLA version found: {}".format(
MIN_CARLA_VERSION, carla_version))

# Load agent if requested via command line args
# If something goes wrong an exception will be thrown by importlib (ok here)
Expand Down Expand Up @@ -660,4 +663,4 @@ def main():


if __name__ == "__main__":
sys.exit(main())
sys.exit(main())
1 change: 1 addition & 0 deletions srunner/metrics/examples/basic_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
This module provide BasicMetric, the basic class of all the metrics.
"""


class BasicMetric(object):
"""
Base class of all the metrics.
Expand Down
14 changes: 7 additions & 7 deletions srunner/metrics/examples/criteria_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def _create_metric(self, town_map, log, criteria):
for criterion_name in criteria:
criterion = criteria[criterion_name]
results.update({criterion_name:
{
"test_status": criterion["test_status"],
"actual_value": criterion["actual_value"],
"success_value": criterion["success_value"]
}
}
)
{
"test_status": criterion["test_status"],
"actual_value": criterion["actual_value"],
"success_value": criterion["success_value"]
}
}
)

with open('srunner/metrics/data/CriteriaFilter_results.json', 'w') as fw:
json.dump(results, fw, sort_keys=False, indent=4)
2 changes: 1 addition & 1 deletion srunner/metrics/examples/distance_to_lane_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _create_metric(self, town_map, log, criteria):
b_norm = math.sqrt(b.x * b.x + b.y * b.y + b.z * b.z)

ab_dot = a.x * b.x + a.y * b.y + a.z * b.z
dist_v = ab_dot/(b_norm*b_norm)*b
dist_v = ab_dot / (b_norm * b_norm) * b
dist = math.sqrt(dist_v.x * dist_v.x + dist_v.y * dist_v.y + dist_v.z * dist_v.z)

# Get the sign of the distance (left side is positive)
Expand Down
1 change: 1 addition & 0 deletions srunner/metrics/tools/metrics_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import fnmatch
from srunner.metrics.tools.metrics_parser import MetricsParser


class MetricsLog(object): # pylint: disable=too-many-public-methods
"""
Utility class to query the log.
Expand Down
30 changes: 21 additions & 9 deletions srunner/metrics/tools/metrics_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def parse_actor(info):
}
return actor


def parse_transform(info):
"""Parses a list into a carla.Transform"""
transform = carla.Transform(
Expand All @@ -36,12 +37,13 @@ def parse_transform(info):
),
carla.Rotation(
roll=float(info[7][1:-1]),
pitch=float(info[8][:-1]),
pitch=float(info[8][:-1]),
yaw=float(info[9][:-1])
)
)
return transform


def parse_control(info):
"""Parses a list into a carla.VehicleControl"""
control = carla.VehicleControl(
Expand All @@ -55,6 +57,7 @@ def parse_control(info):
)
return control


def parse_vehicle_lights(info):
"""Parses a list into a carla.VehicleLightState"""
srt_to_vlight = {
Expand All @@ -78,6 +81,7 @@ def parse_vehicle_lights(info):

return lights


def parse_traffic_light(info):
"""Parses a list into a dictionary with all the traffic light's information"""
number_to_state = {
Expand All @@ -94,6 +98,7 @@ def parse_traffic_light(info):
}
return traffic_light


def parse_velocity(info):
"""Parses a list into a carla.Vector3D with the velocity"""
velocity = carla.Vector3D(
Expand All @@ -103,6 +108,7 @@ def parse_velocity(info):
)
return velocity


def parse_angular_velocity(info):
"""Parses a list into a carla.Vector3D with the angular velocity"""
velocity = carla.Vector3D(
Expand All @@ -112,6 +118,7 @@ def parse_angular_velocity(info):
)
return velocity


def parse_scene_lights(info):
"""Parses a list into a carla.VehicleLightState"""

Expand All @@ -127,6 +134,7 @@ def parse_scene_lights(info):
)
return scene_light


def parse_bounding_box(info):
"""
Parses a list into a carla.BoundingBox.
Expand All @@ -136,24 +144,25 @@ def parse_bounding_box(info):
location = carla.Location()
else:
location = carla.Location(
float(info[3][1:-1])/100,
float(info[4][:-1])/100,
float(info[5][:-1])/100,
float(info[3][1:-1]) / 100,
float(info[4][:-1]) / 100,
float(info[5][:-1]) / 100,
)

if 'inf' in info[7]:
extent = carla.Vector3D()
else:
extent = carla.Vector3D(
float(info[7][1:-1])/100,
float(info[8][:-1])/100,
float(info[9][:-1])/100,
float(info[7][1:-1]) / 100,
float(info[8][:-1]) / 100,
float(info[9][:-1]) / 100,
)

bbox = carla.BoundingBox(location, extent)

return bbox


def parse_state_times(info):
"""Parses a list into a dict containing the state times of the traffic lights"""
state_times = {
Expand All @@ -163,18 +172,20 @@ def parse_state_times(info):
}
return state_times


def parse_vector_list(info):
"""Parses a list of string into a list of Vector2D"""
vector_list = []
for i in range(0, len(info), 2):
vector = carla.Vector2D(
x=float(info[i][1:-1]),
y=float(info[i+1][:-1]),
y=float(info[i + 1][:-1]),
)
vector_list.append(vector)

return vector_list


def parse_gears_control(info):
"""Parses a list into a GearPhysicsControl"""
gears_control = carla.GearPhysicsControl(
Expand All @@ -184,6 +195,7 @@ def parse_gears_control(info):
)
return gears_control


def parse_wheels_control(info):
"""Parses a list into a WheelsPhysicsControl"""
wheels_control = carla.WheelPhysicsControl(
Expand Down Expand Up @@ -283,7 +295,7 @@ def parse_recorder_info(self):
"platform_time": None
},
"actors": {},
"events":{
"events": {
"scene_lights": {},
"physics_control": {},
"traffic_light_state_time": {},
Expand Down
1 change: 0 additions & 1 deletion srunner/scenarioconfigs/openscenario_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,3 @@ def _set_traffic_signal_controller(self):
controller = rnw.find("TrafficSignals")
if controller is not None:
OpenScenarioParser.set_traffic_signal_controller(controller)

6 changes: 3 additions & 3 deletions srunner/scenarioconfigs/osc2_scenario_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
generate relevant type objects in the standard library, and set parameters
"""
from srunner.tools.osc2_helper import OSC2Helper
from srunner.scenariomanager.carla_data_provider import CarlaDataProvider
import sys
from typing import List, Tuple

Expand All @@ -27,10 +29,8 @@
'''

# pylint: enable=line-too-long
from srunner.scenariomanager.carla_data_provider import CarlaDataProvider

# OSC2
from srunner.tools.osc2_helper import OSC2Helper

vehicle_type = ["Car", "Model3", "Mkz2017", "Carlacola", "Rubicon"]

Expand All @@ -48,7 +48,7 @@ def flat_list(list_of_lists):
class OSC2ScenarioConfiguration(ScenarioConfiguration):
def __init__(self, filename, client):
super(OSC2ScenarioConfiguration, self).__init__()

self.name = self.filename = filename
self.ast_tree = OSC2Helper.gen_osc2_ast(self.filename)

Expand Down
Loading

0 comments on commit f46bf35

Please sign in to comment.