I would like to implement an outside controller to change the steering of the ego car. I made a simple script to get keyboard actions from python's "keyboard" lib and used the SEReportSpeed function to change the speed in the simulation. This works just fine. But when I change SEReportWheelStatus, my vehicle doesn't change its heading. Do I have to use the ReportObjPosMode function to change it's heading? I think it wouldn't be realistic if the vehicle changes the heading without turning the wheel. Here is my code:
Thank you
import ctypes as ct
import keyboard
from keyboard._keyboard_event import KEY_DOWN, KEY_UP
import math
class Vehicle:
def __init__(self):
self.acc = 10
self.speed = 0
self.steering_angle = 0
self.steering_max = math.pi / 2
self.steering_min = - math.pi / 2
self.steering_speed = math.pi / 2
self.wheel = 0 # -1 for left turn, 0 for no turn, 1 for right turn
self.pedal = 0 # -1 for braking, 0 for no acceleration, 1 for accelerating
def turn_right(self, dt: float):
d_steering = self.steering_speed * dt
self.steering_angle = min(self.steering_angle + d_steering, self.steering_max)
def turn_left(self, dt: float):
d_steering = self.steering_speed * dt
self.steering_angle = max(self.steering_angle - d_steering, self.steering_min)
def decelerate(self, dt: float):
dt_speed = self.acc * dt
self.speed -= dt_speed
def accelerate(self, dt: float):
dt_speed = self.acc * dt
self.speed += dt_speed
def update(self, dt: float):
if (self.pedal == -1):
self.decelerate(dt)
elif (self.pedal == 1):
self.accelerate(dt)
if (self.wheel == -1):
self.turn_left(dt)
elif (self.wheel == 1):
self.turn_right(dt)
vehicle = Vehicle()
def on_press(event):
if event.name == 'up':
vehicle.pedal = 1
elif event.name == 'down':
vehicle.pedal = -1
elif event.name == 'left':
vehicle.wheel = -1
elif event.name == 'right':
vehicle.wheel = 1
def on_release(event):
if event.name == 'up' or event.name == 'down':
vehicle.pedal = 0
if event.name == 'left' or event.name == 'right':
vehicle.wheel = 0
def on_action(event):
if event.event_type == KEY_DOWN:
on_press(event)
elif event.event_type == KEY_UP:
on_release(event)
keyboard.hook(lambda e: on_action(e))
se = ct.CDLL("../bin/esminiLib.dll")
# specify some arguments and return types of useful functions
se.SE_StepDT.argtypes = [ct.c_float]
se.SE_GetSimulationTime.restype = ct.c_float
se.SE_ReportObjectSpeed.argtypes = [ct.c_int, ct.c_float]
se.SE_ReportObjectPos.argtypes = [ct.c_int, ct.c_float, ct.c_float, ct.c_float, ct.c_float, ct.c_float, ct.c_float, ct.c_float]
se.SE_ReportObjectAngularVel.argtypes = [ct.c_int, ct.c_float, ct.c_float, ct.c_float, ct.c_float]
se.SE_StepDT.argtypes = [ct.c_float]
se.SE_ReportObjectWheelStatus.argtypes = [ct.c_int, ct.c_float, ct.c_float]
se.SE_Init(b"./esmini/EnvironmentSimulator/Unittest/xosc/dummy_mw.xosc", 0, 1, 0, 0)
prev_t = 0
while se.SE_GetQuitFlag() == 0:
curr_t = se.SE_GetSimulationTime()
dt = curr_t - prev_t
vehicle.update(dt)
se.SE_ReportObjectAngularVel(0, 0.0, 1.0, 0.0, 0.0)
se.SE_ReportObjectSpeed(0, vehicle.speed)
se.SE_ReportObjectWheelStatus(0, 0, vehicle.steering_angle)
se.SE_Step()
prev_t = curr_t
I would like to implement an outside controller to change the steering of the ego car. I made a simple script to get keyboard actions from python's "keyboard" lib and used the SEReportSpeed function to change the speed in the simulation. This works just fine. But when I change SEReportWheelStatus, my vehicle doesn't change its heading. Do I have to use the ReportObjPosMode function to change it's heading? I think it wouldn't be realistic if the vehicle changes the heading without turning the wheel. Here is my code:
Thank you