# Ts = 0.01 # sim_time = 400 # sim_time_range = np.arange(0,sim_time,0.01) # # # ----------------------------- select input set --------------------------- # veh_speed_set = np.concatenate((0 * np.ones(int(len(sim_time_range)*0.1)), 20 * np.ones(int(len(sim_time_range)*0.4)), 0 * np.ones(int(len(sim_time_range)*0.5)))) u_veh_vel_set = data_driver['var_Desired_Velocity_km_h_']/3.6 Ts = 0.01 sim_time_range = data_driver['var_Time_s_'] #%% 3. Run simulation for test_case in range(2): # Set logging data veh_data = type_DataLog(['Veh_Vel','Pos_X','Pos_Y','Acc_Set','Brk_Set','t_brk']) motor_data = type_DataLog(['t_mot','t_mot_reg','t_mot_des','w_mot']) power_data = type_DataLog(['p_mot','p_loss','p_elec','soc','v_mot','i_mot']) shaft_data = type_DataLog(['w_shaft','w_wheel','w_veh','w_dot_mot','w_dot_veh','w_dot_shaft']) # Powertrain import and configuration kona_power = Mod_Power() # Bodymodel import and configuration kona_body = Mod_Body() # Vehicle set kona_vehicle = Mod_Veh(kona_power, kona_body) drv_kyunghan = Mod_Driver() # Behavior model beh_speed = Mod_Behavior(drv_kyunghan)
# ~~~~ # Driver model drv_kyunghan = Mod_Driver() # ~~~~ # Behavior model beh_cycle = Mod_Behavior(drv_kyunghan) # ~~~~ #%% 2. Simulation config # 2. brake torque u_veh_vel_set = data_driver['var_Desired_Velocity_km_h_'] / 3.6 Ts = 0.01 sim_time_range = data_driver['var_Time_s_'] #%% 3. Simulation # Set logging data beh_cycle.conf_filtnum_pedal = 0.5 sim_driver = type_DataLog(['u_acc', 'u_brk']) sim_vehicle = type_DataLog(['Veh_vel', 't_mot', 't_brk', 'w_mot']) veh_vel = 0 w_mot = 0 w_shaft = 0 w_wheel = 0 start_time = time.time() for sim_step in range(len(sim_time_range)): # Arrange cruise input u_veh_vel_set_in = u_veh_vel_set[sim_step] u_acc_in, u_brk_in = beh_cycle.Lon_control(u_veh_vel_set_in, veh_vel) # Vehicle model sim # 1. Cockfit system t_mot = kona_vehicle.Acc_system(u_acc_in) t_brk, t_reg = kona_vehicle.Brake_system(u_brk_in) # 1. Drag force
# Behavior model beh_driving = Mod_Behavior(drv_kyunghan) #%% 2. Simulation config # 2. brake torque data_driver = get_config.load_mat('Data_Kyunghan.mat') data_result = get_config.load_mat('SimResult_Kyunghan.mat') u_trq_mot_set = data_driver['Trq_Mot'] u_trq_brk_set = data_driver['Trq_BrkMec'] sim_time_range = data_driver['Veh_Time'] result_veh_vel = data_driver['Veh_Vel'] u_trq_drag = data_result['simresult_trq_drag'] Ts = 0.01 #%% 3. Simulation # Set logging data sim_vehicle = type_DataLog(['veh_vel', 'veh_acc', 'wdot_veh']) sim_states = type_DataLog([ 'w_mot', 'w_shaft', 'w_wheel', 'trq_shaft_in', 'trq_wheel_load', 'trq_drag' ]) sim_wheel = type_DataLog(['t_wheel_in', 't_wheel_traction_f']) veh_vel = result_veh_vel[0] w_wheel = data_driver['Rot_Wheel'][0] w_mot = w_wheel * kona_drivetrain.conf_gear w_shaft = w_wheel kona_drivetrain.w_vehicle = w_wheel start_time = time.time() for sim_step in range(len(sim_time_range)): # Arrange cruise input u_t_mot = u_trq_mot_set[sim_step] u_t_brk = u_trq_brk_set[sim_step]
kona_drive = Mod_Drive() kona_vehicle = Mod_Veh(kona_power, kona_drive) #%% 3. Simulation config # ----------------------------- select input set --------------------------- # input for drive-train # 1. motor torque u_t_mot = data_emachine['var_Torque_Nm_'] # 2. brake torque u_t_brk = -(data_brake_f['var_Braking_Torque_Nm_'] + data_brake_r['var_Braking_Torque_Nm_']) * 2 Ts = 0.01 sim_time_range = data_emachine['var_Time_s_'] #%% 3. Run simulation # Set logging data data_sim_torque = type_DataLog( ['t_mot_load', 't_wheel_load', 't_traction', 't_drag']) data_sim_dynamics = type_DataLog(['w_mot', 'w_shaft', 'w_wheel']) data_sim_veh = type_DataLog(['veh_acc', 'veh_vel', 'f_lon', 'f_drag']) veh_vel = 0 w_mot = 0 w_shaft = 0 w_wheel = 0 for sim_step in range(len(sim_time_range)): # Arrange vehicle input u_t_mot_in = u_t_mot[sim_step] u_t_brk_in = u_t_brk[sim_step] # Vehicle model sim # 1. Drag force t_drag, f_drag = kona_vehicle.Drag_system(veh_vel) # 2. Torque equivalence
ItNumMax = 100 Learning_result = [] Learning_control = [] Learning_qval = [] swt_valid = 1 # Learning results learn_log_list_veh = [ 'veh_vel', 'veh_dis', 'veh_acc', 'veh_acc_set', 'veh_acc_ref', 'mot_trq_set', 'mot_trq_reg', 'mot_trq' ] learn_log_list_drv = ['drv_acc', 'drv_brk', 'coast_vel', 'coast_dis'] learn_log_list_result = [ 'q_list', 'act_val', 'r_drv', 'r_reg', 'r_saf', 'step' ] lrn_veh = type_DataLog(learn_log_list_veh) lrn_drv = type_DataLog(learn_log_list_drv) lrn_result = type_DataLog(learn_log_list_result) LS = [] for ItNum in range(ItNumMax): swt_valid = ItNum % 100 egreedy_dis_fac = ItNum // 10 # if swt_valid == 0: # agent_dqn.conf_egreedy = 0 # print('============================= validation =================================') # else: # agent_dqn.conf_egreedy = e_greedy_config - egreedy_dis_fac/50 # 3. Import model - set the initial value # Powertrain import and configuration kona_power = Mod_PowerTrain()
len(road_x) * 0.8 * 0.003, 0.003)) road_y = np.concatenate((100 * np.ones(int(len(road_x) * 0.2)), road_sin)) # Environment set env_sl = Mod_Env(road_x, road_y) # Set initial vehicle position env_sl.Vehicle_init_config(kona_vehicle, 2) #%% 2. Simulation config Ts = 0.01 sim_time = 150 sim_time_range = np.arange(0, sim_time, 0.01) u_speed_val = np.concatenate( (0 * np.ones(int(len(sim_time_range) * 0.1)), 20 * np.ones(int(len(sim_time_range) * 0.9)))) #%% 3. Run simulation # Set logging data veh_data_sim = type_DataLog( ['vel_veh', 'the_wheel', 'u_acc', 'u_brk', 'u_str']) veh_state_sim = type_DataLog(['pos_x', 'pos_y', 'psi_veh']) # Vehicle initial state vel_veh = kona_vehicle.vel_veh pos_x = kona_vehicle.pos_x_veh pos_y = kona_vehicle.pos_y_veh psi_veh = kona_vehicle.psi_veh for sim_step in range(len(sim_time_range)): # Arrange behavior input u_vel_veh_set_in = u_speed_val[sim_step] # Behavior control u_acc_in, u_brk_in = beh_steer.Lon_control(u_vel_veh_set_in, vel_veh) u_steer_in = beh_steer.Lat_behavior(pos_x, pos_y, psi_veh, road_x, road_y) # Vehicle driven
(0 * np.ones(int(len(sim_time_range) * 0.3)), 0.01 * np.ones(int(len(sim_time_range) * 0.7)))) elif Input_index == 1: # Sin input u_acc_val = np.concatenate( (0 * np.ones(int(len(sim_time_range) * 0.1)), 0.3 * np.ones(int(len(sim_time_range) * 0.9)))) u_brk_val = 0 * np.ones(len(sim_time_range)) u_steer_val = np.concatenate( (0 * np.ones(int(len(sim_time_range) * 0.3)), 0.01 * np.sin(0.01 * np.arange(int(len(sim_time_range) * 0.7))))) else: print('입력을 똑바로 하세요 ~_~') #%% 3. Run simulation # Set logging data sim2 = type_DataLog(['Veh_Vel', 'Pos_X', 'Pos_Y', 'Acc_Set', 'Str_Set']) for sim_step in range(len(sim_time_range)): # Arrange vehicle input u_acc_in = u_acc_val[sim_step] u_brk_in = u_brk_val[sim_step] u_str_in = u_steer_val[sim_step] # Vehicle model sim [veh_vel, the_wheel] = kona_vehicle.Veh_driven(u_acc=u_acc_in, u_brake=u_brk_in, u_steer=u_str_in) [pos_x, pos_y, pos_s, pos_n, psi_veh] = kona_vehicle.Veh_position_update(veh_vel, the_wheel) # Store data sim2.StoreData([veh_vel, pos_x, pos_y, u_acc_in, u_str_in]) [sim2_veh_vel, sim2_pos_x, sim2_pos_y, sim2_u_acc,
plt.title('Road data') # Environment set env_st = Mod_Env(road_x,road_y) # Set initial vehicle state at environment env_st.Vehicle_init_config(kona_vehicle, 2) # env_st.Obj_add('Tl','red',495) road_env_obj = env_st.object_list road_env_road_len = env_st.road_len beh_driving.conf_filtnum_spdset = 3 #%% 2. Simulation config Ts = 0.01 sim_time = 668 sim_time_range = np.arange(0,sim_time,0.01) # Set logging data simdata_veh = type_DataLog(['veh_vel','vel_set','acc_in','brk_in','str_in','pos_x','pos_y','pos_s']) simdata_body = type_DataLog(['t_mot','t_wheel_load','t_drag','t_brk']) envdata = type_DataLog(['stc_objec','lat_off','head_off']) # initial vehicle position pos_x = kona_vehicle.pos_x_veh pos_y = kona_vehicle.pos_y_veh pos_s = kona_vehicle.pos_s_veh psi_veh = kona_vehicle.psi_veh veh_vel = kona_vehicle.vel_veh start_time = time.time() for sim_step in range(len(sim_time_range)): # Arrange vehicle position # Behavior control [u_acc_in, u_brk_in] = beh_driving.Lon_behavior(road_env_obj, pos_s, road_env_road_len, veh_vel)
u_acc_val = np.concatenate((0 * np.ones(int(len(sim_time_range)*0.1)), 0.3 * np.ones(int(len(sim_time_range)*0.9)))) u_brk_val = 0 * np.ones(len(sim_time_range)) elif Input_index == 1: # Sin wave : Input_index = 1 u_acc_val = np.concatenate((0 * np.ones(int(len(sim_time_range)*0.1)), 0.3 + 0.1*np.sin(0.01*np.arange(int(len(sim_time_range)*0.9))))) u_brk_val = 0 * np.ones(len(sim_time_range)) # Brake : Input_index = 2 elif Input_index == 2: u_acc_val = np.concatenate((0 * np.ones(int(len(sim_time_range)*0.1)), 0.3 * np.ones(int(len(sim_time_range)*0.4)), 0 * np.ones(int(len(sim_time_range)*0.5)))) u_brk_val = np.concatenate((0 * np.ones(int(len(sim_time_range)*0.55)), 0.3 * np.ones(int(len(sim_time_range)*0.45)))) else: print('입력을 똑바로 하세요 ~_~') #%% 3. Run simulation # Set logging data sim1 = type_DataLog(['Veh_Vel','Pos_X','Pos_Y','Acc_Set','Brk_Set']) w_veh_deb = [] for sim_step in range(len(sim_time_range)): # Arrange vehicle input u_acc_in = u_acc_val[sim_step] u_brk_in = u_brk_val[sim_step] # Vehicle model sim [veh_vel, the_wheel] = kona_vehicle.Veh_driven(u_acc = u_acc_in, u_brake = u_brk_in) [pos_x, pos_y, pos_s, pos_n, psi_veh] = kona_vehicle.Veh_position_update(veh_vel, the_wheel) # Store data sim1.StoreData([veh_vel, pos_x, pos_y, u_acc_in, u_brk_in]) w_veh_deb.append(kona_vehicle.ModDrive.w_vehicle) [sim1_veh_vel, sim1_pos_x, sim1_pos_y, sim1_u_acc, sim1_u_brk] = sim1.get_profile_value(['Veh_Vel','Pos_X','Pos_Y','Acc_Set','Brk_Set']) #%% 4. Result plot fig = plt.figure(figsize=(8,4))
Ts = 0.01 sim_time = 100 sim_time_range = np.arange(0, sim_time, 0.01) # Initial state w_mot = kona_power.ModMotor.w_mot t_mot_load = kona_power.ModMotor.t_load # ----------------------------- select input set --------------------------- u_acc_val = Get_SqrVal([0.3, 0.1, 0], [0.1, 0.3, 0.5], sim_time / Ts) u_brk_val = Get_SqrVal([0.3, 0], [0.55, 0.8], sim_time / Ts) #%% 3. Run simulation # Set logging data data_log_list = [ 'veh_vel', 'w_wheel', 'w_mot', 'u_acc', 'u_brk', 'SOC', 't_mot_set', 't_mot', 'v_mot', 't_brk', 't_drag', 't_load' ] sim = type_DataLog(data_log_list) sim_reg = type_DataLog(data_log_list) for sim_step in range(len(sim_time_range)): # Arrange vehicle input u_acc_in = u_acc_val[sim_step] u_brk_in = u_brk_val[sim_step] # Vehicle model sim kona_vehicle.Veh_driven(u_acc_in, u_brk_in) kona_vehicle_reg.Veh_driven(u_acc_in, u_brk_in) # Module driven sim # [t_mot_des, t_brake] = kona_body.Lon_driven_in(u_acc_in, u_brk_in) # [w_mot, t_mot] = kona_power.ModMotor.Motor_control(t_mot_des, t_mot_load) # [w_wheel, t_mot_load, vel_veh] = kona_body.Lon_driven_out(t_brake, w_mot) # Store data mot = kona_power.ModMotor