Skip to content

Commit b2ce15b

Browse files
committed
Add additional model to dynamics.py that allows for various function mappings from the desired acceleration to pedal position. Also allows for variation in response based on current velocity. Adds corresponding settings into gem_e4_dynamics.yaml
1 parent a0ed900 commit b2ce15b

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

GEMstack/knowledge/vehicle/dynamics.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,63 @@ def acceleration_to_pedal_positions(acceleration : float, velocity : float, pitc
3131
if acceleration < -dry_decel*0.5 or (acceleration <= 0 and velocity < 0.1): # a little deadband to avoid oscillation
3232
throttle_percent = 0.0 #drift to stop
3333
else:
34-
throttle_percent = accel_active_range[0] + (acceleration+dry_decel)/max_accel * (accel_active_range[1]-accel_active_range[0])
34+
throttle_percent = accel_active_range[0] + ((acceleration+dry_decel)/max_accel * (accel_active_range[1]-accel_active_range[0]))
3535
brake_percent = 0
3636
else:
37-
brake_percent = brake_active_range[0] + -(acceleration+dry_decel)/max_brake * (brake_active_range[1]-brake_active_range[0])
37+
brake_percent = brake_active_range[0] + (-(acceleration+dry_decel)/max_brake * (brake_active_range[1]-brake_active_range[0]))
3838
throttle_percent = 0
39+
print(acceleration, (max(throttle_percent,0.0),max(brake_percent,0.0),1))
3940
return (max(throttle_percent,0.0),max(brake_percent,0.0),1)
4041

42+
43+
# Model that's built on top of Hang's model.
44+
# Designed to allow for various scaling based on current vehicle velocity, and use of functions other than linear
45+
# for scaling.
46+
# DANGER: NEEDS TESTED
47+
elif model == 'avery_v1':
48+
model = settings.get('vehicle.dynamics.acceleration_model', 'avery_v1')
49+
if gear != 1:
50+
print("WARNING: Can't handle gears other than 1 yet")
51+
52+
max_accel = settings.get('vehicle.dynamics.max_accelerator_acceleration')[1] # m/s^2
53+
max_brake = settings.get('vehicle.dynamics.max_brake_deceleration') # m/s^2
54+
dry_decel = settings.get('vehicle.dynamics.internal_dry_deceleration') # m/s^2
55+
accel_active_range = settings.get('vehicle.dynamics.accelerator_active_range') # pedal position fraction
56+
brake_active_range = settings.get('vehicle.dynamics.brake_active_range') # pedal position fraction
57+
mapping_function = settings.get('vehicle.dynamics.pedal_mapping_function', 'linear')
58+
velocity_scalar = settings.get('vehicle.dynamics.velocity_scaling_factor', 0) # Adjust sensitivity based on velocity
59+
60+
61+
def apply_function(value, function='linear'):
62+
if function == 'linear':
63+
value = value # already linear mapping
64+
65+
# no guarantee that these functions will give good results until we get more test data about accel -> pedal mapping.
66+
elif function == 'quadratic':
67+
value = value ** 2 # value squared
68+
elif function == 'exponential':
69+
value = (math.exp(value) - 1) / 2
70+
elif function == 'log':
71+
value = math.sqrt(math.log(value + 1)) + 0.2
72+
73+
value += velocity * velocity_scalar
74+
return value
75+
76+
if acceleration > -dry_decel:
77+
if acceleration < -dry_decel*0.5 or (acceleration <= 0 and velocity < 0.1): # a little deadband to avoid oscillation
78+
throttle_percent = 0.0 #drift to stop
79+
else:
80+
throttle_percent = accel_active_range[0] + ((acceleration+dry_decel)/max_accel * (accel_active_range[1]-accel_active_range[0]))
81+
throttle_percent = apply_function(throttle_percent, mapping_function)
82+
brake_percent = 0
83+
else:
84+
brake_percent = brake_active_range[0] + (-(acceleration+dry_decel)/max_brake * (brake_active_range[1]-brake_active_range[0]))
85+
brake_percent = apply_function(brake_percent, mapping_function)
86+
throttle_percent = 0
87+
print(acceleration, (max(throttle_percent,0.0),max(brake_percent,0.0),1))
88+
return (max(throttle_percent,0.0),max(brake_percent,0.0),1)
89+
90+
4191
elif model == 'kris_v1':
4292
brake_max = settings.get('vehicle.dynamics.max_brake_deceleration')
4393
reverse_accel_max = settings.get('vehicle.dynamics.max_accelerator_acceleration_reverse')

GEMstack/knowledge/vehicle/gem_e4_dynamics.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ internal_dry_deceleration: 0.2 #m/s^2: deceleration due to internal dry fri
2020
internal_viscous_deceleration: 0.05 #1/s: scales the current velocity to get deceleration due to internal viscous friction (speed dependent)
2121
aerodynamic_drag_coefficient: 0.01 #units in s, scaled by velocity^2 to get deceleration due to aerodynamic drag
2222
acceleration_deadband: 0.1 #m/s^2: minimum acceleration to be considered non-zero
23+
velocity_scaling_factor: 0.0 # Velocity * this scalar will be added to brake and throttle percent.
24+
pedal_mapping_function: linear

0 commit comments

Comments
 (0)