Skip to content

Behavior modification examples

Example: behavior modification with 'in'

OSC2 code: behavior modification with 'in'
import "$FTX_PACKAGES/base_scenarios/scenarios/vehicle_cut_in/vehicle_cut_in_and_slow/vehicle_cut_in_and_slow_top.osc"
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"


extend top.main:

    in a.phase_essence.vehicle_cut_in_at_essence.drive_cut_in_vehicle_at_essence with:
        lane(leftmost: true, at: start)

    do a: sut.vehicle_cut_in_and_slow_main()

Example: behavior modification in 'with' block

OSC2 code: behavior modification in 'with' block
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"

extend top.main:
    car1: vehicle


    do parallel(overlap:equal):
        sut.car.drive() with:
            s1: speed([50..120]kph)
        car1.drive() with:
            p1: position(distance: [5..100]m, behind: sut.car, at: start)
            p2: position(distance: [5..15]m, ahead_of: sut.car, at: end)

Example: behavior modification with the 'in' directive

OSC2 code: behavior modification with the 'in' directive
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

# The cut-in scenario

scenario sut.cut_in:
    car1: vehicle        # The other car
    side: av_side    # A side: left or right

    do serial():
        get_ahead: parallel(duration:[1..5]s, overlap:equal): # get_ahead is a label
            sut.car.drive() with:
                speed(speed: [30..70]kph)
            car1.drive() with:
                p1: position(distance: [5..100]m,
                     behind: sut.car, at: start)
                p2: position(distance: [5..15]m,
                    ahead_of: sut.car, at: end)
        change_lane: parallel(duration:[2..5]s, overlap:equal): # change_lane is a label
            sut.car.drive()
            car1.drive() with:
                l1: lane(side_of: sut.car, side: side, at: start)
                l2: lane(same_as: sut.car, at: end)

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"

extend top.main:

    in label(sut).label(get_ahead.car1) with:
        position(distance:[7..10]m, ahead_of: sut.car, at: end)
        lane(1)

    do sut.cut_in()

Example: behavior modification with 'in' and enclosing/nested

OSC2 code: behavior modification with 'in' and enclosing/nested
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/hooder.xodr"

struct drive_attributes:
    my_speed: speed with:
        keep(it <= 70kph)

scenario sut.nested_scenario:
    car1: vehicle

    do serial:
        start_up: car1.drive()

scenario sut.enclosing_scenario:
    my_car: vehicle

    do nested: sut.nested_scenario(car1: my_car)

extend top.main:
    drv_attr: drive_attributes

    in enclosing.nested.start_up with:
        speed(outer.drv_attr.my_speed)

    do enclosing: sut.enclosing_scenario()

Example: override() with best_effort

OSC2 code: override() with best_effort
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"
import "$FTX_PACKAGES/base_scenarios/scenarios/crossing_vrus_family/crossing_person/crossing_person_top.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/M77_FTX_highway_straight_long_road.xodr"



scenario accelerate:
    v: vehicle

    do serial:
        drv: v.drive() with:
            s_spd: speed([40..45]kph, at: start)
            e_spd: speed([50..55]kph, at: end)

extend top.main:
    sut_v: sut_vehicle

    do parallel:
        acc: accelerate(sut_v, duration: [15..20]sec)
        pedestrian_crossing: sut.crossing_person(ref_drive: acc.drv, gen_drive_path_fraction: 95)

    override(acc.e_spd, run_mode: best_effort)

Example: disabling a checker during a phase

OSC2 code: disabling a checker during a phase
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/M77_FTX_highway_straight_long_road.xodr"

extend issue_kind: [too_fast] #define a new issue kind

# A watcher that creates intervals when the speed of a vehicle exceeds a limit
watcher modifier speed_above:
    v: vehicle
    limit: speed
    var max_speed: speed

    on @top.clk:
        if(data == null):
            if(v.state.speed > limit):
                start_interval()
        else:
            if(v.state.speed <= limit):
                end_interval()
    on @i_clock:
        if(max_speed < v.state.speed):
            max_speed = v.state.speed
            sut_warning(kind: too_fast, details: "Vehicle $(v) exceeded maximal speed $(limit) reaching up to $(max_speed)")


extend top.main:
    sut_v: sut_vehicle
    checker overspeed_checker is speed_above(sut_v, 50kph)

    do serial:
        drive_slowly: sut_v.drive(duration: [5..10]sec) with:
            speed([35..45]kph, at: all)
        accelerate: sut_v.drive(duration: [5..10]sec) with:
            speed([60..70]kph, at: end)
        decelerate: sut_v.drive(duration: [5..10]sec) with:
            speed([35..45]kph, at: end)
        drive_slowly_again: sut_v.drive(duration: [5..10]sec) with:
            speed([35..45]kph, at: all)
        accelerate_again: sut_v.drive(duration: [5..10]sec) with:
            speed([60..70]kph, at: end)

    # Freeze overspeed_checker for the accelerate+decelerate phases.
    # The checker will be active during accelerate_again.

    on @accelerate.start:
        override(overspeed_checker, run_mode: freeze)

    on @decelerate.end:
        override(overspeed_checker, run_mode: unfreeze)



extend top.main:

    on @overspeed_checker.i_clock:
        logger.log_info("Overspeed: $(sut_v.state.speed)")
    on @overspeed_checker.i_end:
        logger.log_info("MAX: $(overspeed_checker.max_speed)")

Example: override() with disable

OSC2 code: override() with disable
import "$FTX_BASIC/exe_platforms/sumo_ssp/config/sumo_config.osc"

extend test_config:
    set map = "$FTX_PACKAGES/maps/M77_FTX_highway_straight_long_road.xodr"


extend top.main:
    sut_v: sut_vehicle

    do drive1: sut_v.drive() with:
           spd: speed([10..20]kph)
           duration([10..20]sec)

# In another file:
extend top.main:

    override(spd, run_mode: disable)

    in drive1 with:
       speed([30..40]kph)