def simple_run(): """ Example of how to extract the acceleration curves of a vehicle and the corresponding plot. The final acceleration curvers (Curves), the engine acceleration potential curves (poly_spline), before the calculation of the resistances and the limitation due to max possible acceleration (friction). """ # A sample car id from the database car_id = 35135 # The gear shifting style as described in the TRR paper. gs_style = 0.8 # How to use co2mpas_driver library # You can also pass vehicle database path db_path='path to vehicle db' sol = driver(dict(vehicle_id=car_id, inputs=dict(inputs=dict( gear_shifting_style=gs_style, degree=4, use_linear_gs=True, use_cubic=False))))[ 'outputs'] # driver.plot(1) # poly_spline = sol['poly_spline'] for curve in sol['discrete_acceleration_curves']: sp_bins = list(curve['x']) acceleration = list(curve['y']) plt.plot(sp_bins, acceleration, 'o') '''import vehicle object, curves and gear shifting strategy''' plt.show() return 0
def simple_run(): """ Vehicle simulation. :return: """ # A sample car id from the database car_id = 8188 # The gear shifting style as described in the TRR paper. gs_style = 0.99 # The desired speed vdes = 124 / 3.6 # Current speed v_start = 0 # The simulation step in seconds sim_step = 0.1 # The driving style as described in the TRR paper. driver_style = 0.2 # Duration of the simulation in seconds. duration = 100 # sample time series times = np.arange(0, duration + sim_step, sim_step) sol = driver( dict(vehicle_id=car_id, inputs=dict(inputs=dict(gear_shifting_style=gs_style, desired_velocity=vdes, starting_velocity=v_start, degree=4, driver_style=driver_style, sim_start=0, sim_step=sim_step, duration=duration, use_linear_gs=True, use_cubic=False))))['outputs'] velocities = sol['velocities'] accelerations = sol['accelerations'] # driver.plot(1) plt.figure('Speed-Acceleration') plt.plot(velocities[1:], accelerations[1:]) plt.grid() plt.figure('Time-Speed') plt.plot(times, velocities[1:]) plt.grid() plt.figure('Acceleration-Time') plt.plot(times, accelerations[1:]) plt.grid() plt.show() return 0
def run_simulation(db, car_id, ds, v_start, gs, v_des, sim_start, sim_step, duration, plot): """ This script runs simulation and plots the final acceleration versus velocity graph of the vehicle. :return: """ driver_model = driver( dict( vehicle_id=car_id, db_path=db, inputs=dict( inputs={ "gear_shifting_style": gs, "starting_velocity": v_start, "driver_style": ds, "desired_velocity": v_des, "sim_start": sim_start, "sim_step": sim_step, "duration": duration, }), )) if plot: driver_model.plot() outputs = driver_model["outputs"] discrete_acceleration_curves = outputs["discrete_acceleration_curves"] fig = plt.figure() for curve in discrete_acceleration_curves: sp_bins = list(curve["x"]) acceleration = list(curve["y"]) plt.plot(sp_bins, acceleration) plt.plot(outputs["velocities"][1:], outputs["accelerations"][1:]) plt.xlabel("Speed", fontsize=18) plt.ylabel("Acceleration", fontsize=16) plt.legend([ "acceleration per gear 0", "acceleration per gear 1", "acceleration per gear 2", "acceleration per gear 3", "acceleration per gear 4", "final acceleration", ]) plt.grid() plt.show() return 0
def run_simulation(db, car_id, ds, v_start, gs, v_des, sim_start, sim_step, duration, plot): """ This script runs simulation and plots the final acceleration versus velocity graph of the vehicle. :return: """ driver_model = driver( dict(vehicle_id=car_id, db_path=db, inputs=dict( inputs={ 'gear_shifting_style': gs, 'starting_velocity': v_start, 'driver_style': ds, 'desired_velocity': v_des, 'sim_start': sim_start, 'sim_step': sim_step, 'duration': duration }))) if plot: driver_model.plot() outputs = driver_model['outputs'] discrete_acceleration_curves = outputs['discrete_acceleration_curves'] fig = plt.figure() for curve in discrete_acceleration_curves: sp_bins = list(curve['x']) acceleration = list(curve['y']) plt.plot(sp_bins, acceleration) plt.plot(outputs['velocities'][1:], outputs['accelerations'][1:]) plt.xlabel('Speed', fontsize=18) plt.ylabel('Acceleration', fontsize=16) plt.legend([ 'acceleration per gear 0', 'acceleration per gear 1', 'acceleration per gear 2', 'acceleration per gear 3', 'acceleration per gear 4', 'final acceleration' ]) plt.grid() plt.show() return 0
def simple_run(): car_id = 47844 # How to use co2mpas_driver library # You can also pass vehicle database path db_path='path to vehicle db' sol = driver(dict(vehicle_id=47844))['outputs'] discrete_acceleration_curves = sol['discrete_acceleration_curves'] discrete_deceleration_curves = sol['discrete_deceleration_curves'] # start = sol['start'] # stop = sol['stop'] # driver.plot(1) for d in discrete_acceleration_curves: plt.plot(d['x'], d['y']) plt.grid() for d in discrete_deceleration_curves: plt.plot(d['x'], d['y']) plt.grid() plt.show() return 0
def simple_run(): car_id = 39393 gs_style = 0.8 # gear shifting can take value from 0(timid driver) degree = 2 # How to use co2mpas_driver library # You can also pass vehicle database path db_path='path to vehicle db' sol = driver( dict(vehicle_id=car_id, inputs=dict(inputs=dict(gear_shifting_style=gs_style, degree=degree, use_linear_gs=True, use_cubic=False))))['outputs'] discrete_acceleration_curves = sol['discrete_acceleration_curves'] # driver.plot(1) for curve in discrete_acceleration_curves: sp_bins = list(curve['x']) acceleration = list(curve['y']) plt.plot(sp_bins, acceleration) plt.show() return 0
def simple_run(): """ Test vehicle simulation. :return: """ veh_ids = [39393, 8188, 40516, 35452, 40225, 7897, 7972, 41388, 5766, 9645, 9639, 5798, 8280, 34271, 34265, 6378, 39723, 34092, 2592, 5635, 5630, 7661, 7683] v_des = 124/3.6 v_start = 0 dt = 0.1 times = np.arange(0, 100, dt) vehicles = [driver(dict(vehicle_id=i, inputs=dict(inputs=dict( gear_shifting_style=1, driver_style=1, starting_velocity=0, duration=100, sim_start=0, sim_step=dt, use_linear_gs=True, use_cubic=False))))['outputs']['driver_simulation_model'] for i in veh_ids] res = {} for myt in times: for my_veh in vehicles: if myt == times[0]: my_veh.reset(v_start) res[my_veh] = {'accel': [0], 'speed': [v_start], 'position': [0], 'gear': [0]} continue gear, next_velocity, acc, position = my_veh(dt, v_des) res[my_veh]['accel'].append(acc) res[my_veh]['speed'].append(next_velocity) res[my_veh]['gear'].append(gear) res[my_veh]['position'].append(position) for my_veh in vehicles: plt.figure('Acceleration-Speed') plt.plot(res[my_veh]['speed'], res[my_veh]['accel']) plt.show() return 0
def simple_run(): car_id = 27748 gs_style = 1 degree = 2 # How to use co2mpas_driver library # You can also pass vehicle database path db_path='path to vehicle db' sol = driver( dict(vehicle_id=car_id, inputs=dict(inputs=dict(gear_shifting_style=gs_style, gedree=degree, use_linear_gs=False, use_cubic=True))))['outputs'] # driver.plot(1) gs = sol['gs'] coefs_per_gear = sol['coefs_per_gear'] speed_per_gear = sol['speed_per_gear'] acc_per_gear = sol['acc_per_gear'] degree = len(coefs_per_gear[0]) - 1 vars = np.arange(degree, -1, -1) plt.figure('speed acceleration regression results of degree = ' + str(degree)) for gear in gs: plt.plot([gear, gear], [0, 5]) for speeds, acceleration, fit_coef in zip(speed_per_gear, acc_per_gear, coefs_per_gear): plt.plot(speeds, acceleration) # x_new = np.linspace(speeds[0], speeds[-1], 100) x_new = np.arange(speeds[0], speeds[-1], 0.1) a_new = np.array([np.dot(fit_coef, np.power(i, vars)) for i in x_new]) plt.plot(x_new, a_new) plt.show()
def simple_run(): car_id = 40516 gs_style = 0.8 degree = 4 # You can also pass vehicle database path db_path='path to vehicle db' sol = driver( dict(vehicle_id=car_id, inputs=dict(inputs=dict(gear_shifting_style=gs_style, degree=degree, use_linear_gs=True, use_cubic=False))))['outputs'] # start = sol['start'] # Start/stop speed for each gear # stop = sol['stop'] # sp_bins = sol['sp_bins'] # full_load_speeds = sol['full_load_speeds'] # Full load curves of speed # and torque # full_load_torque = sol['full_load_torque'] # speed_per_gear = sol['speed_per_gear'] # speed and acceleration ranges # and poitns for each gear # acc_per_gear = sol['acc_per_gear'] # car_res_curve = sol['car_res_curve'] # get resistances # car_res_curve_force = sol['car_res_curve_force'] # curves = sol['curves'] # gs = sol['gs'] # poly_spline = sol['poly_spline'] # extract speed acceleration Splines discrete_acceleration_curves = sol['discrete_acceleration_curves'] # driver.plot(1) for curve in discrete_acceleration_curves: sp_bins = list(curve['x']) acceleration = list(curve['y']) plt.plot(sp_bins, acceleration) plt.show() return 0
def simple_run(): """ Function "light_co2mpas_series" computes the CO2 emissions in grams for a series of speed profile. If the gear-shifting is not given as input it uses the an internal function to compute the current gear. """ car_id = 35135 # Sample speed profile. sp = [0.075647222, 0.138130556, 0.165027778, 0.093338889, 0.050647222, 0.073841667, 0.067722222, 0.041172222, 0.094272222, 0.240147222, 0.421988889, 0.601022222, 0.805477778, 1.067511111, 1.360083333, 1.650283333, 1.913175, 2.176333333, 2.444797222, 2.700288889, 2.946313889, 3.189297222, 3.448358333, 3.704702778, 3.940416667, 4.130133333, 4.260580556, 4.3409, 4.388002778, 4.426941667, 4.455319444, 4.476166667, 4.515033333, 4.539722222, 4.54225, 4.563194444, 4.616366667, 4.794819444, 4.925277778, 5.010258333, 5.270727778, 5.526880556, 5.698258333, 5.863777778, 6.025444444, 6.178002778, 6.320294444, 6.455533333, 6.586655556, 6.7208, 6.850394444, 6.973597222, 7.076808333, 7.125569444, 7.125688889, 7.095327778, 7.045141667, 6.987052778, 6.942780556, 6.943469444, 6.972669444, 6.987636111, 6.995147222, 7.01125, 7.034722222, 7.064722222, 7.095263889, 7.144930556, 7.228544444, 7.351388889, 7.516902778, 7.705436111, 7.912636111, 8.142141667, 8.384738889, 8.638480556, 8.896288889, 9.157808333, 9.427988889, 9.695, 9.931672222, 10.09765, 10.17970556, 10.211575, 10.22029444, 10.21634444, 10.22308056, 10.25685278, 10.32316667, 10.43054444, 10.55200833, 10.67687222, 10.81433889, 10.94932778, 11.08748889, 11.23079444, 11.37732778, 11.52675278, 11.67602222, 11.83357778, 11.99132222, 12.127225, 12.217525, 12.27826111, 12.33689444, 12.41279167, 12.52231944, 12.648375, 12.78034722, 12.91521111, 13.04194444, 13.16066111, 13.28333333, 13.40571944, 13.53175556, 13.64644444, 13.75571111, 13.8666, 13.93222222, 13.95751111, 13.96354444, 13.95462222, 13.92623333, 13.89566111, 13.88161111, 13.90078611, 13.92424167, 13.95039722, 13.98454444, 14.02729722, 14.07866111, 14.15, 14.24663333, 14.3537, 14.45984444, 14.58112778, 14.7043, 14.83035556, 14.96040278, 15.09104722, 15.22573889, 15.36123611, 15.49455833, 15.63419444, 15.77003889, 15.90303333, 16.04134167, 16.17628056, 16.30461111, 16.4286, 16.54910556, 16.66977778, 16.77255278, 16.85622222, 16.94144444, 17.02344444, 17.09977778, 17.17553056, 17.24705278, 17.31889444, 17.39001389, 17.44721389] # Gear shifting points as can be derived from gs = [5.715589018826222, 10.974960637586783, 16.396951475513028, 22.832902038337586] sim_step = 0.1 # You can also pass vehicle database path db_path='path to vehicle db' sol = driver(dict(vehicle_id=car_id, inputs=dict(inputs=dict( gs=gs, velocities=sp, sim_step=sim_step))))[ 'outputs'] fp = sol['fp'] # driver.plot(1) plt.plot(fp) plt.show()
def simple_run(): """ This example computes and plots the driving trajectories with the varying desired speed. """ # A sample car id from the database = { car_id_db = { "fuel_engine": [ 35135, 39393, 27748, 8188, 40516, 35452, 40225, 7897, 7972, 41388, 5766, 9645, 9639, 5798, 8280, 34271, 34265, 6378, 39723, 34092, 2592, 5635, 5630, 7661, 7683, 8709, 9769, 1872, 10328, 35476, 41989, 26799, 26851, 27189, 23801, 3079, 36525, 47766, 6386, 33958, 33986, 5725, 5718, 36591, 4350, 39396, 40595, 5909, 5897, 5928, 5915, 40130, 42363, 34760, 34766, 1835, 36101, 42886, 1431, 46547, 44799, 41045, 39820, 34183, 34186, 20612, 20605, 1324, 9882, 4957, 5595, 18831, 18833, 9575, 5380, 9936, 7995, 6331, 18173, 34286, 34279, 20706, 34058, 34057, 24268, 19028, 19058, 7979, 22591, 34202, 40170, 44599, 5358, 5338, 34015, 9872, 9856, 6446, 8866, 9001, 9551, 6222, ], "electric_engine": [47844], } car_id = 47844 # The desired speed vdes = 124 / 3.6 # Current speed v_start = 0 # The simulation step in seconds sim_step = 0.1 # The driving style as described in the TRR paper. driver_style = 0.6 # Duration of the simulation in seconds. duration = 300 # sample time series times = np.arange(0, duration + sim_step, sim_step) # core model, this will select and execute the proper functions for the given inputs and returns the output # You can also pass vehicle database path db_path='path to vehicle db' if car_id in car_id_db["electric_engine"]: my_veh = driver( dict( vehicle_id=car_id, inputs=dict(inputs=dict( starting_velocity=v_start, driver_style=driver_style, sim_start=0, sim_step=sim_step, duration=duration, use_linear_gs=True, use_cubic=False, )), ))["outputs"]["driver_simulation_model"] else: # The gear shifting style as described in the TRR paper. gs_style = 0.8 my_veh = driver( dict( vehicle_id=car_id, inputs=dict(inputs=dict( gear_shifting_style=gs_style, starting_velocity=v_start, driver_style=driver_style, sim_start=0, sim_step=sim_step, duration=duration, degree=4, use_linear_gs=True, use_cubic=False, )), ))["outputs"]["driver_simulation_model"] # Plots workflow of the core model, this will automatically open an internet browser and show the work flow # of the core model. you can click all the rectangular boxes to see in detail sub models like load, model, # write and plot. # you can uncomment to plot the work flow of the model # driver.plot(1) # Note: run your IDE as Administrator if file permission error. f_des = interp1d( [0, 210, 700, 1050, 1400, 3150, 4550, 5250], # Position (m) [20, 20, 30, 5, 25, 35, 15, 0], # Desired speed (m/s) kind="next", ) res = {} for myt in times: if myt == times[0]: my_veh.reset(v_start) res = { "accel": [0], "speed": [v_start], "position": [0], "gear": [0], "v_des": [f_des(0)], "fc": [0], } continue res["v_des"].append(f_des(res["position"][-1])) gear, gear_count, next_velocity, acc = my_veh(sim_step, res["v_des"][-1]) if car_id in car_id_db["electric_engine"]: fc = 0 else: fc = my_veh.calculate_fuel_consumption(next_velocity, acc, gear, gear_count, sim_step) res["accel"].append(acc) res["speed"].append(next_velocity) res["gear"].append(gear) res["position"].append(res["position"][-1] + next_velocity * sim_step) res["fc"].append(fc) fig = plt.figure("Time-Speed") plt.plot(times, res["speed"], label=f"Simulation car:{car_id}, DS: {driver_style}") plt.plot(times, res["v_des"], label=f"Desired speed (m/s)") plt.legend() fig.suptitle("Speed over time", x=0.54, y=1, fontsize=12) plt.xlabel("Time (s)", fontsize=14) plt.ylabel("Speed (m/s)", fontsize=12) plt.grid() fig = plt.figure("Time-Acceleration") plt.plot(times, res["accel"], label=f"Simulation car:{car_id}, DS: {driver_style}") plt.legend() fig.suptitle("Acceleration over time", x=0.54, y=1, fontsize=12) plt.xlabel("Time (s)", fontsize=14) plt.ylabel("Acceleration (m/s2)", fontsize=12) plt.grid() if car_id in car_id_db["electric_engine"]: pass else: fig = plt.figure("Time-Fuel Consumption") plt.plot( times, np.dot(res["fc"], sim_step), label=f"Simulation car:{car_id}, DS: {driver_style}", ) plt.legend() fig.suptitle("Fuel Consumption over time", x=0.54, y=1, fontsize=12) plt.xlabel("Time (s)", fontsize=14) plt.ylabel("Fuel consumption rate (g/s)", fontsize=12) plt.grid() fig = plt.figure("Distance-Speed") plt.plot( res["position"], res["speed"], label=f"Simulation car:{car_id}, DS: {driver_style}", ) plt.plot(res["position"], res["v_des"], label=f"Desired speed (m/s)") plt.legend() fig.suptitle("Speed over distance", x=0.54, y=1, fontsize=12) plt.xlabel("Distance (m)", fontsize=14) plt.ylabel("Speed (m/s)", fontsize=12) plt.grid() fig = plt.figure("Distance-Acceleration") plt.plot( res["position"], res["accel"], label=f"Simulation car:{car_id}, DS: {driver_style}", ) plt.legend() fig.suptitle("Acceleration over distance", x=0.54, y=1, fontsize=12) plt.xlabel("Distance (m)", fontsize=14) plt.ylabel("Acceleration (m/s2)", fontsize=12) plt.grid() plt.show()
def simple_run(): """ Test vehicle simulation. :return: """ veh_ids = [ 39393, 8188, 40516, 35452, 40225, 7897, 7972, 41388, 5766, 9645, 9639, 5798, 8280, 34271, 34265, 6378, 39723, 34092, 2592, 5635, 5630, 7661, 7683, ] v_des = 124 / 3.6 v_start = 0 # coefficients of resistances in case provided by user f0 = 200 f1 = 0.2 f2 = 0.005 dt = 0.1 times = np.arange(0, 100, dt) vehicles = [( i, driver( dict( vehicle_id=i, inputs=dict(inputs=dict( f0=f0, f1=f1, f2=f2, gear_shifting_style=0.8, driver_style=0.8, starting_velocity=0, duration=100, sim_start=0, sim_step=dt, use_linear_gs=True, use_cubic=False, )), ))["outputs"]["driver_simulation_model"], ) for i in veh_ids] res = {} for myt in times: for veh_id, my_veh in vehicles: if myt == times[0]: my_veh.reset(v_start) res[my_veh] = {"accel": [0], "speed": [v_start], "gear": [0]} continue gear, gear_count, next_velocity, acc = my_veh(dt, v_des) res[my_veh]["accel"].append(acc) res[my_veh]["speed"].append(next_velocity) res[my_veh]["gear"].append(gear) for car_id, my_veh in vehicles: plt.figure("Acceleration-Speed") plt.plot(res[my_veh]["speed"], res[my_veh]["accel"]) plt.show() return 0
def simple_run(): """ This sample file plots the speed/acceleration curves of the vehicle and regression results of 2nd degree polynomial based on gear shifting style. :return: """ # A sample car id from the database car_id = 27748 # The gear shifting style as described in the TRR paper. gs_style = 1 # 2nd degree polynomial degree = 2 # Core model, this will select and execute the proper functions for the given inputs and returns the output # You can also pass your vehicle's database path db_path='path to vehicle db' sol = driver( dict( vehicle_id=car_id, inputs=dict(inputs=dict( gear_shifting_style=gs_style, gedree=degree, use_linear_gs=False, use_cubic=True, )), ))["outputs"] # Plots workflow of the core model, this will automatically open an internet browser and shows the work flow # of the core model. you can click all the rectangular boxes to see in detail sub models like load, model, # write and plot. # you can uncomment to plot the work flow of the model # driver.plot(1) # Note: run your IDE as Administrator if file permission error. # gear cuts based on linear gear shifting style gs = sol["gs"] # coefficients of speed acceleration relation for each gear # calculated using 2nd degree polynomial coefs_per_gear = sol["coefs_per_gear"] speed_per_gear = sol["speed_per_gear"] acc_per_gear = sol["acc_per_gear"] degree = len(coefs_per_gear[0]) - 1 vars = np.arange(degree, -1, -1) fig = plt.figure("speed acceleration regression results of degree = " + str(degree)) for gear in gs: plt.plot([gear, gear], [0, 5]) for gear, (speeds, acceleration, fit_coef) in enumerate( zip(speed_per_gear, acc_per_gear, coefs_per_gear)): plt.plot(speeds, acceleration, label=f"gear {gear}") x_new = np.arange(speeds[0], speeds[-1], 0.1) a_new = np.array([np.dot(fit_coef, np.power(i, vars)) for i in x_new]) plt.plot(x_new, a_new, label=f"{degree} degree reg. gear {gear}") plt.legend() plt.text(46, 1.8, f"Simulation car:{car_id}, GS:{gs_style}") fig.suptitle( "Speed acceleration regression results of degree = " + str(degree), x=0.54, y=1, fontsize=12, ) plt.xlabel("Speed (m/s)", fontsize=12) plt.ylabel("Acceleration (m/s2)", fontsize=12) plt.show()
def simple_run(): """ This example computes and plots the CO2 emissions in grams for a simulated trajectory. """ car_id = 35135 # The gear shifting style as described in the TRR paper. gs_style = 0.8 # The desired speed vdes = 124 / 3.6 # Current speed v_start = 0 # The simulation step in seconds sim_step = 0.1 # The driving style as described in the TRR paper. driver_style = 0.6 # Duration of the simulation in seconds. duration = 100 # sample time series times = np.arange(0, duration + sim_step, sim_step) # core model, this will select and execute the proper functions for the given inputs and returns the output # You can also pass vehicle database path db_path='path to vehicle db' sol = driver( dict( vehicle_id=car_id, inputs=dict(inputs=dict( gear_shifting_style=gs_style, desired_velocity=vdes, starting_velocity=v_start, driver_style=driver_style, sim_start=0, sim_step=sim_step, duration=duration, degree=4, use_linear_gs=True, use_cubic=False, )), ))["outputs"] # Plots workflow of the core model, this will automatically open an internet browser and show the work flow # of the core model. you can click all the rectangular boxes to see in detail sub models like load, model, # write and plot. # you can uncomment to plot the work flow of the model # driver.plot(1) # Note: run your IDE as Administrator if file permission error. driver_simulation_model = sol["driver_simulation_model"] res = {} for myt in times: if myt == times[0]: driver_simulation_model.reset(v_start) res = { "accel": [0], "speed": [v_start], "position": [0], "gear": [0] } continue gear, next_velocity, acc, position = driver_simulation_model( sim_step, vdes) res["accel"].append(acc) res["speed"].append(next_velocity) res["gear"].append(gear) res["position"].append(position) fig = plt.figure("Speed-Acceleration") plt.plot( res["speed"], res["accel"], label=f"Simulation car:{car_id}, DS: {driver_style}, GS:{gs_style}", ) plt.legend() fig.suptitle("Acceleration over Speed", x=0.54, y=1, fontsize=12) plt.xlabel("Speed (m/s)", fontsize=14) plt.ylabel("Acceleration (m/s2)", fontsize=12) plt.grid() plt.show() fp = sol["fp"] fig = plt.figure("Speed-co2") plt.plot( res["speed"], fp, label=f"Simulation car:{car_id}, DS: {driver_style}, GS:{gs_style}", ) plt.legend() fig.suptitle("Co2 emission for a simulated trajectory", x=0.54, y=1, fontsize=12) plt.xlabel("Speed (m/s)", fontsize=12) plt.ylabel("Co2 (gms)", fontsize=12) plt.show()