I’m using BeamNG.tech for collecting steering rotation data as well as the road conditions using beamNGpy for my college project. But when I’m running the code on localhost jupyter it doesn’t open the test scenario instead it opens the main menu of the beamNG.tech, how can i solve this.
For reference, I’ve attached the beamNGpy code as well:
import csv
import time
import subprocess
from beamngpy import BeamNGpy, Scenario, Vehicle
from beamngpy.sensors import Electrics, Timer
==========================================================
CONFIGURATION
==========================================================
BNG_HOME = r’E:\BeamNG.Tech’
OUTPUT_CSV = ‘legner_industrial_data.csv’
==========================================================
CLOSE OLD BEAMNG INSTANCES
==========================================================
subprocess.run(
[‘taskkill’, ‘/f’, ‘/im’, ‘BeamNG.tech.x64.exe’],
capture_output=True
)
time.sleep(3)
==========================================================
CREATE BEAMNG INSTANCE
==========================================================
bng = BeamNGpy(
host=‘localhost’,
port=64256,
home=BNG_HOME
)
try:
# ======================================================
# OPEN BEAMNG
# ======================================================
print('Opening BeamNG.tech...')
bng.open(launch=True)
print('Waiting for BeamNG initialization...')
time.sleep(10)
# ======================================================
# CREATE VEHICLE
# ======================================================
vehicle = Vehicle(
vid='ego_vehicle',
model='legner',
license='DATA'
)
# ======================================================
# ATTACH SENSORS
# ======================================================
electrics = Electrics()
timer = Timer()
vehicle.sensors.attach('electrics', electrics)
vehicle.sensors.attach('timer', timer)
# ======================================================
# CREATE SCENARIO
# ======================================================
scenario = Scenario(
level='industrial',
name='industrial_logging'
)
# Safe spawn point
scenario.add_vehicle(
vehicle,
pos=(505, 178, 25),
rot_quat=(0, 0, 0, 1)
)
# ======================================================
# BUILD SCENARIO
# ======================================================
print('Building scenario...')
scenario.make(bng)
# ======================================================
# LOAD SCENARIO
# ======================================================
print('Loading scenario...')
bng.scenario.load(scenario)
time.sleep(10)
# ======================================================
# START SCENARIO
# ======================================================
print('Starting scenario...')
bng.scenario.start()
time.sleep(3)
# ======================================================
# CONNECT VEHICLE
# ======================================================
vehicle.connect(bng)
print('Vehicle connected.')
# ======================================================
# SET DETERMINISTIC MODE
# ======================================================
bng.settings.set_deterministic(50)
bng.settings.set_steps_per_second(50)
# ======================================================
# ROAD FRICTION LOOKUP
# ======================================================
mu_lookup = {
'asphalt': 1.0,
'concrete': 0.95,
'dirt': 0.60,
'grass': 0.40,
'mud': 0.30,
'sand': 0.25,
'snow': 0.20,
'ice': 0.10
}
# ======================================================
# START LOGGING
# ======================================================
print(f'\nRecording started -> {OUTPUT_CSV}')
print('Drive vehicle manually.')
print('Press CTRL+C to stop.\n')
with open(OUTPUT_CSV, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow([
'Timestamp',
'Steering_Angle_Deg',
'Friction_Mu',
'Road_Condition'
])
while True:
# Advance simulation by one step
bng.control.step(1)
# Poll sensors
vehicle.sensors.poll()
elec_data = vehicle.sensors['electrics']
timer_data = vehicle.sensors['timer']
# Timestamp
timestamp = timer_data['time']
# Steering input [-1, 1]
steering_norm = elec_data.get(
'steering_input',
0.0
)
# Convert to steering wheel angle
steering_deg = steering_norm * 450.0
# Ground model
road = elec_data.get(
'groundmodel',
'asphalt'
).lower()
# Friction coefficient
mu = mu_lookup.get(road, 0.80)
# Write row
writer.writerow([
f'{timestamp:.4f}',
f'{steering_deg:.2f}',
f'{mu:.2f}',
road
])
except KeyboardInterrupt:
print('\nLogging stopped by user.')
except Exception as e:
print('\nERROR:')
print(e)
finally:
print('\nClosing BeamNG...')
try:
bng.close()
except:
pass
print('Done.')