File size: 7,969 Bytes
36a4110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""
Data models for CARLA environment.

Defines Action, Observation, and State for embodied evaluation scenarios.
"""

from __future__ import annotations

from typing import Any, Literal

from openenv.core.env_server import Action, Observation, State
from pydantic import Field

ActionType = Literal[
    "control",
    "emergency_stop",
    "brake_vehicle",
    "maintain_speed",
    "lane_change",
    "observe",
    "init_navigation_agent",
    "set_destination",
    "follow_route",
    "capture_image",
]


class CarlaAction(Action):
    """
    Action for CARLA vehicle control.

    Attributes:
        action_type: Type of action (control, emergency_stop, lane_change, observe, maintain_speed, brake_vehicle, init_navigation_agent, set_destination, follow_route, capture_image)
        throttle: Throttle value [0.0, 1.0] for "control" actions
        steer: Steering value [-1.0, 1.0] for "control" actions
        brake: Brake value [0.0, 1.0] for "control" actions
        lane_direction: Direction for "lane_change" ("left" or "right")
        target_speed_kmh: Target speed in km/h for "maintain_speed"
        brake_intensity: Brake intensity [0.0, 1.0] for "brake_vehicle"
        target_lane_id: Target lane ID for improved "lane_change"
        navigation_behavior: Behavior for navigation agent ("cautious", "normal", "aggressive")
        destination_x: Destination X coordinate for navigation
        destination_y: Destination Y coordinate for navigation
        destination_z: Destination Z coordinate for navigation
        route_steps: Number of steps to follow route
    """

    action_type: ActionType = Field(default="observe", description="Type of action")
    throttle: float = Field(default=0.0, ge=0.0, le=1.0, description="Throttle value")
    steer: float = Field(default=0.0, ge=-1.0, le=1.0, description="Steering value")
    brake: float = Field(default=0.0, ge=0.0, le=1.0, description="Brake value")
    lane_direction: str | None = Field(
        default=None,
        description="Lane change direction (deprecated, use target_lane_id)",
    )

    # Enhanced action parameters
    target_speed_kmh: float | None = Field(
        default=None,
        ge=0.0,
        le=200.0,
        description="Target speed in km/h for maintain_speed action",
    )
    brake_intensity: float | None = Field(
        default=None,
        ge=0.0,
        le=1.0,
        description="Brake intensity (0.0 = no brake, 1.0 = full brake) for brake_vehicle action",
    )
    target_lane_id: str | None = Field(
        default=None,
        description="Target lane ID for lane_change action (e.g., 'lane_0', 'lane_1')",
    )

    # Navigation action parameters
    navigation_behavior: str | None = Field(
        default="normal",
        description="Behavior for navigation agent: cautious, normal, or aggressive",
    )
    destination_x: float | None = Field(
        default=None, description="Destination X coordinate for set_destination action"
    )
    destination_y: float | None = Field(
        default=None, description="Destination Y coordinate for set_destination action"
    )
    destination_z: float | None = Field(
        default=None, description="Destination Z coordinate for set_destination action"
    )
    route_steps: int | None = Field(
        default=1,
        ge=1,
        description="Number of steps to follow route in follow_route action",
    )


class CarlaObservation(Observation):
    """
    Observation from CARLA environment.

    For text-only mode, provides ground truth scene description.
    """

    # Scene description (text-only mode)
    scene_description: str = Field(
        default="", description="Natural language scene description"
    )

    # Vehicle state
    speed_kmh: float = Field(default=0.0, description="Current speed in km/h")
    location: tuple[float, float, float] = Field(
        default=(0.0, 0.0, 0.0), description="Vehicle location (x, y, z)"
    )
    rotation: tuple[float, float, float] = Field(
        default=(0.0, 0.0, 0.0), description="Vehicle rotation (pitch, yaw, roll)"
    )

    # Navigation/Goal info (for maze and navigation scenarios)
    goal_distance: float | None = Field(
        default=None, description="Distance to goal in meters (if goal is set)"
    )
    goal_direction: str | None = Field(
        default=None, description="Direction to goal: forward, left, right, or behind"
    )

    # Lane info
    current_lane: str = Field(default="unknown", description="Current lane identifier")

    # Nearby actors (for decision-making)
    nearby_actors: list[dict[str, Any]] = Field(
        default_factory=list, description="Nearby actors with distances"
    )

    # Collision detection
    collision_detected: bool = Field(
        default=False, description="Whether collision occurred"
    )
    collision_intensity: float = Field(
        default=0.0, description="Collision force intensity"
    )
    collided_with: str | None = Field(
        default=None, description="ID of actor collided with"
    )

    # Scenario info
    scenario_name: str = Field(default="", description="Name of current scenario")
    simulation_time: float = Field(
        default=0.0, description="Simulation time in seconds"
    )
    step_number: int = Field(default=0, description="Current step number")

    # Episode termination (override done from base Observation)
    done_reason: str = Field(default="", description="Reason for episode termination")

    # Rubric reward for RL training (computed by the rubric, may differ from raw reward)
    rubric_reward: float | None = Field(
        default=None, description="Reward computed by the rubric for RL training"
    )

    # Camera capture (only populated when capture_image action is used)
    camera_image: str | None = Field(
        default=None, description="Base64-encoded JPEG image from front-facing camera"
    )


class CarlaState(State):
    """
    Episode state for CARLA environment.
    """

    # Scenario configuration
    scenario_name: str = Field(
        default="default", description="Name of current scenario"
    )
    town: str = Field(default="Town10HD_Opt", description="CARLA town/map name")
    weather: str = Field(default="ClearNoon", description="Weather preset")

    # Episode metrics
    total_distance: float = Field(
        default=0.0, description="Total distance traveled (meters)"
    )
    total_reward: float = Field(default=0.0, description="Cumulative reward")
    simulation_time: float = Field(
        default=0.0, description="Total simulation time (seconds)"
    )

    # Action tracking metrics
    num_turns: int = Field(default=0, description="Number of steps taken in episode")
    total_tool_calls: int = Field(
        default=0, description="Total number of actions executed"
    )
    tool_call_counts: dict[str, int] = Field(
        default_factory=dict, description="Count of each action type executed"
    )
    is_truncated: bool = Field(
        default=False, description="Whether episode was truncated (max steps)"
    )

    # Movement metrics
    average_speed: float = Field(default=0.0, description="Average speed in km/h")
    max_speed: float = Field(default=0.0, description="Maximum speed reached in km/h")

    # Collision history
    collisions: list[dict[str, Any]] = Field(
        default_factory=list, description="List of collision events"
    )
    collisions_count: int = Field(default=0, description="Total number of collisions")
    collision_intensity_total: float = Field(
        default=0.0, description="Sum of all collision intensities"
    )

    # Scenario-specific data
    scenario_data: dict[str, Any] = Field(
        default_factory=dict, description="Scenario-specific data"
    )