|
1 | 1 | import math
|
2 | 2 | import pandas as pd
|
3 | 3 | import numpy as np
|
| 4 | +import argparse |
| 5 | +import openpyxl |
| 6 | +from openpyxl.styles import PatternFill |
4 | 7 |
|
5 | 8 |
|
6 | 9 | class Params:
|
7 |
| - def __init__(self, pos=0, speed=0, reaction_time=0, accel=0): |
| 10 | + def __init__(self, pos=0, speed=0, reaction_time=0, accel=0, length=0): |
8 | 11 | self.pos = pos
|
9 | 12 | self.speed = speed
|
10 | 13 | self.reaction_time = reaction_time
|
11 | 14 | self.accel = accel
|
| 15 | + self.length = length |
12 | 16 |
|
13 | 17 |
|
14 | 18 | def calculate_collision_with_rss(ego_params: Params, front_vehicle_params: Params, rear_vehicle_params: Params):
|
15 |
| - ego_pos, ego_speed, rt, ego_acc = ego_params.pos, ego_params.speed, ego_params.reaction_time, ego_params.accel |
| 19 | + ego_pos, ego_speed, rt, ego_acc, ego_length = ego_params.pos, ego_params.speed, ego_params.reaction_time, ego_params.accel, ego_params.length |
16 | 20 |
|
17 |
| - front_pos, front_speed, _, front_acc = front_vehicle_params.pos, front_vehicle_params.speed, front_vehicle_params.reaction_time, front_vehicle_params.accel |
18 |
| - is_front_collision = front_pos - ego_pos < ego_speed * rt + \ |
19 |
| - (ego_speed ** 2 / (-2.0 * ego_acc)) - (front_speed ** 2) / (-2*front_acc) |
| 21 | + front_pos, front_speed, _, front_acc, front_length = front_vehicle_params.pos, front_vehicle_params.speed, front_vehicle_params.reaction_time, front_vehicle_params.accel, front_vehicle_params.length |
| 22 | + is_front_collision = front_pos - ego_pos - front_length / 2 < ego_speed * rt + \ |
| 23 | + (ego_speed ** 2 / (-2.0 * ego_acc)) - (front_speed ** 2) / (-2 * front_acc) + ego_length / 2 |
20 | 24 |
|
21 |
| - rear_pos, rear_speed, rt, rear_acc = rear_vehicle_params.pos, rear_vehicle_params.speed, rear_vehicle_params.reaction_time, rear_vehicle_params.accel |
22 |
| - is_rear_collision = ego_pos - rear_pos < rear_speed * rt + \ |
23 |
| - (rear_speed ** 2 / (-2.0 * rear_acc)) - (ego_speed ** 2) / (-2*ego_acc) |
| 25 | + rear_pos, rear_speed, rt, rear_acc, rear_length = rear_vehicle_params.pos, rear_vehicle_params.speed, rear_vehicle_params.reaction_time, rear_vehicle_params.accel, rear_vehicle_params.length |
| 26 | + is_rear_collision = ego_pos - rear_pos - rear_length / 2 < rear_speed * rt + \ |
| 27 | + (rear_speed ** 2 / (-2.0 * rear_acc)) - (ego_speed ** 2) / (-2 * ego_acc) + ego_length / 2 |
24 | 28 |
|
25 | 29 | return is_front_collision, is_rear_collision
|
26 | 30 |
|
27 | 31 |
|
28 |
| -# Set the parameters for front and rear vehicles |
29 |
| -front_vehicle_params = Params(pos=30, speed=10, reaction_time=1.0, accel=-3.0) |
30 |
| -rear_vehicle_params = Params(pos=-30, speed=10, reaction_time=1.0, accel=-3.0) |
| 32 | +def save_to_excel_with_colors_and_params(collision_table, file_name, ego_params, front_vehicle_params, rear_vehicle_params, use_same_speed, use_same_accel): |
| 33 | + writer = pd.ExcelWriter(file_name, engine='openpyxl') |
| 34 | + collision_table.to_excel(writer, sheet_name='Collision Table') |
31 | 35 |
|
32 |
| -# Generate the data for the table |
33 |
| -speeds = np.arange(1, 31) # 1 m/s to 30 m/s |
34 |
| -accelerations = np.linspace(-1.0, -10, 19) # -1.0 m/s² to -10 m/s² |
| 36 | + workbook = writer.book |
| 37 | + worksheet = writer.sheets['Collision Table'] |
35 | 38 |
|
36 |
| -collision_table = pd.DataFrame(index=accelerations, columns=speeds) |
| 39 | + # Add header for units |
| 40 | + worksheet['A1'] = 'Acceleration (m/s²) / Speed (m/s)' |
37 | 41 |
|
38 |
| -for speed in speeds: |
39 |
| - for accel in accelerations: |
40 |
| - ego_params = Params(pos=0, speed=speed, reaction_time=1.5, accel=accel) |
41 |
| - is_front_collision, is_rear_collision = calculate_collision_with_rss( |
42 |
| - ego_params, front_vehicle_params, rear_vehicle_params) |
43 |
| - if is_front_collision and is_rear_collision: |
44 |
| - collision_table.at[accel, speed] = "Both" |
45 |
| - elif is_front_collision: |
46 |
| - collision_table.at[accel, speed] = "Front" |
47 |
| - elif is_rear_collision: |
48 |
| - collision_table.at[accel, speed] = "Rear" |
49 |
| - else: |
50 |
| - collision_table.at[accel, speed] = "None" |
| 42 | + # Coloring the cells based on collision type |
| 43 | + color_mapping = { |
| 44 | + 'None': '00FF00', # Green |
| 45 | + 'Rear': '0000FF', # Blue |
| 46 | + 'Front': 'FF0000', # Red |
| 47 | + 'Both': '800080' # Purple |
| 48 | + } |
51 | 49 |
|
52 |
| -print(collision_table) |
| 50 | + for row in worksheet.iter_rows(min_row=0, min_col=0, max_row=worksheet.max_row, max_col=worksheet.max_column): |
| 51 | + for cell in row: |
| 52 | + collision_type = cell.value |
| 53 | + if collision_type in color_mapping: |
| 54 | + cell.fill = PatternFill( |
| 55 | + start_color=color_mapping[collision_type], end_color=color_mapping[collision_type], fill_type='solid') |
| 56 | + |
| 57 | + # Add vehicle parameters below the table |
| 58 | + params = [ |
| 59 | + ('Ego Vehicle', ego_params), |
| 60 | + ('Front Vehicle', front_vehicle_params), |
| 61 | + ('Rear Vehicle', rear_vehicle_params) |
| 62 | + ] |
| 63 | + |
| 64 | + start_row = worksheet.max_row + 2 |
| 65 | + for label, param in params: |
| 66 | + worksheet.cell(row=start_row, column=1, value=label) |
| 67 | + worksheet.cell(row=start_row + 1, column=1, value='Position (m)') |
| 68 | + worksheet.cell(row=start_row + 1, column=2, value=param.pos) |
| 69 | + worksheet.cell(row=start_row + 2, column=1, value='Speed (m/s)') |
| 70 | + worksheet.cell(row=start_row + 2, column=2, |
| 71 | + value='same as ego vehicle' if use_same_speed else param.speed) |
| 72 | + worksheet.cell(row=start_row + 3, column=1, value='Reaction Time (s)') |
| 73 | + worksheet.cell(row=start_row + 3, column=2, value=param.reaction_time) |
| 74 | + worksheet.cell(row=start_row + 4, column=1, value='Acceleration (m/s²)') |
| 75 | + worksheet.cell(row=start_row + 4, column=2, |
| 76 | + value='same as ego vehicle' if use_same_accel else param.accel) |
| 77 | + worksheet.cell(row=start_row + 5, column=1, value='Length (m)') |
| 78 | + worksheet.cell(row=start_row + 5, column=2, value=param.length) |
| 79 | + start_row += 7 |
| 80 | + |
| 81 | + writer.save() |
| 82 | + |
| 83 | + |
| 84 | +def main(args): |
| 85 | + front_vehicle_params = Params(pos=30, speed=args.speed if args.use_same_speed else 10, |
| 86 | + reaction_time=1.0, accel=args.acceleration if args.use_same_acceleration else -3.0, length=5) |
| 87 | + rear_vehicle_params = Params(pos=-30, speed=args.speed if args.use_same_speed else 10, |
| 88 | + reaction_time=1.0, accel=args.acceleration if args.use_same_acceleration else -3.0, length=5) |
| 89 | + |
| 90 | + speeds = np.arange(1, 31) # 1 m/s to 30 m/s |
| 91 | + accelerations = np.linspace(-1.0, -10, 19) # -1.0 m/s² to -10 m/s² |
| 92 | + |
| 93 | + collision_table = pd.DataFrame( |
| 94 | + index=[f"{a:.1f} m/s²" for a in accelerations], columns=[f"{s} m/s" for s in speeds]) |
| 95 | + |
| 96 | + for speed in speeds: |
| 97 | + for accel in accelerations: |
| 98 | + ego_params = Params(pos=0, speed=speed, reaction_time=1.5, accel=accel, length=5) |
| 99 | + is_front_collision, is_rear_collision = calculate_collision_with_rss( |
| 100 | + ego_params, front_vehicle_params, rear_vehicle_params) |
| 101 | + if is_front_collision and is_rear_collision: |
| 102 | + collision_table.at[f"{accel:.1f} m/s²", f"{speed} m/s"] = "Both" |
| 103 | + elif is_front_collision: |
| 104 | + collision_table.at[f"{accel:.1f} m/s²", f"{speed} m/s"] = "Front" |
| 105 | + elif is_rear_collision: |
| 106 | + collision_table.at[f"{accel:.1f} m/s²", f"{speed} m/s"] = "Rear" |
| 107 | + else: |
| 108 | + collision_table.at[f"{accel:.1f} m/s²", f"{speed} m/s"] = "None" |
| 109 | + |
| 110 | + if args.output_file: |
| 111 | + save_to_excel_with_colors_and_params(collision_table, args.output_file, ego_params, |
| 112 | + front_vehicle_params, rear_vehicle_params, args.use_same_speed, args.use_same_acceleration) |
| 113 | + print(f"Collision table saved to {args.output_file}") |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + parser = argparse.ArgumentParser(description="Calculate collision table using RSS model.") |
| 118 | + parser.add_argument('--use-same-speed', action='store_true', |
| 119 | + help='Use the same speed for front and rear vehicles as the ego vehicle.') |
| 120 | + parser.add_argument('--use-same-acceleration', action='store_true', |
| 121 | + help='Use the same acceleration for front and rear vehicles as the ego vehicle.') |
| 122 | + parser.add_argument('--output-file', type=str, |
| 123 | + help='Output file path for the collision table (Excel format).') |
| 124 | + args = parser.parse_args() |
| 125 | + main(args) |
0 commit comments