Skip to content

Commit edcd285

Browse files
added piecewise polynomial trajectory
1 parent cf03ac4 commit edcd285

File tree

7 files changed

+150
-82
lines changed

7 files changed

+150
-82
lines changed

GainsController.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,8 +1449,8 @@ def MRACwithBASELINE_SafetyMechanism(mass_total_estimated,air_density_estimated,
14491449

14501450
# Mu - elliptic cone intersection
14511451
EllipticConeEpsilon = 1e-2
1452-
maximumRollAngle = math.radians(80) # [rad] 25 - 32
1453-
maximumPitchAngle = math.radians(80) # [rad] 25 - 32
1452+
maximumRollAngle = math.radians(60) # [rad] 25 - 32
1453+
maximumPitchAngle = math.radians(60) # [rad] 25 - 32
14541454

14551455
# Mu - plane intersection
14561456
planeEpsilon = 1e-2

drone_bb_simulator.py

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def WrapperMain_function(target_folder, controller_type, wrapper_control_paramet
5757
global angular_error_dot, u2, u3, u4, mu_baseline_tran, mu_adaptive_tran, Moment_baseline, Moment_adaptive
5858
global mu_PD_baseline_tran, Moment_baseline_PI, e_tran, integral_eQe_tran, e_rot, integral_eQe_rot
5959
global epsilon_tran, integral_epsQeps_tran, epsilon_rot, integral_epsQeps_rot, e_transient_tran
60-
global integral_etQet_tran, e_transient_rot, integral_etQet_rot, omega_ref_dot
60+
global integral_etQet_tran, e_transient_rot, integral_etQet_rot, omega_ref_dot, omega_cmd, omega_cmd_dot
61+
global Jacobian_matrix, Jacobian_matrix_dot
6162

6263
#%% File settings
6364

@@ -336,7 +337,8 @@ def WrapperMain_function(target_folder, controller_type, wrapper_control_paramet
336337
mfloor = chrono.ChBodyEasyBox(50, 0.1, 50, 1000,True,True, contact_material_floor)
337338
mfloor.SetName('Floor')
338339
mfloor.SetBodyFixed(True)
339-
mfloor.SetPos(chrono.ChVectorD(0,-0.3,0))
340+
mfloor_Yposition = 0.3
341+
mfloor.SetPos(chrono.ChVectorD(0,-mfloor_Yposition,0))
340342
# mfloor.SetPos(chrono.ChVectorD(0,-0.5,0))
341343
mfloor.GetVisualShape(0).SetTexture(chrono.GetChronoDataFile("textures/light_gray.png"))
342344
# mfloor.GetVisualShape(0).SetTexture(chrono.GetChronoDataFile("textures/concrete.jpg"))
@@ -900,13 +902,34 @@ def WrapperMain_function(target_folder, controller_type, wrapper_control_paramet
900902
####################################################################################################################################################################
901903

902904
elif trajectory_type == 'piecewisePolynomial_trajectory':
903-
pp_coefficients = np.loadtxt(open("trajectory_PolynomialCoefficientMatrix.csv",
904-
"rb"), delimiter=",", skiprows=0)
905+
pp_coefficients = np.loadtxt(open(
906+
"trajectory_PolynomialCoefficientMatrix.csv",
907+
"rb"), delimiter=",", skiprows=0)
905908
waypointTimes = np.loadtxt(open("trajectory_WaypointTimes.csv", "rb"),
906909
delimiter=",", skiprows=0)
907910

908911
trajectory_instance = piecewisePolynomial_trajectory()
909-
trajectory_instance.set_parameters(pp_coefficients)
912+
trajectory_instance.set_parameters(pp_coefficients, waypointTimes)
913+
914+
# Create a ChLinePath geometry, and insert sub-paths
915+
mpath = chrono.ChLinePath()
916+
[pos_x, pos_y, pos_z] = trajectory_instance.ComputePositionVector(0.01)
917+
918+
for i in range(pos_x.size - 1):
919+
mpath.AddSubLine(chrono.ChLineSegment(
920+
chrono.ChVectorD(pos_x[i],
921+
-pos_z[i] + mfloor_Yposition,
922+
pos_y[i]),
923+
chrono.ChVectorD(pos_x[i+1],
924+
-pos_z[i+1] + mfloor_Yposition,
925+
pos_y[i+1])))
926+
927+
# Create a ChLineShape, a visualization asset for lines.
928+
# The ChLinePath is a special type of ChLine and it can be visualized.
929+
mpathasset = chrono.ChLineShape()
930+
mpathasset.SetLineGeometry(mpath)
931+
mpathasset.SetColor(chrono.ChColor(0,0,0))
932+
mfloor.AddVisualShape(mpathasset)
910933

911934
#%% Controller Inizialization
912935

@@ -926,6 +949,11 @@ def WrapperMain_function(target_folder, controller_type, wrapper_control_paramet
926949
Moment_adaptive = np.zeros((3,1))
927950

928951
omega_ref_dot = np.zeros((3,1))
952+
omega_ref = np.zeros((3,1))
953+
omega_cmd = np.zeros((3,1))
954+
omega_cmd_dot = np.zeros((3,1))
955+
Jacobian_matrix = np.matrix(np.zeros((3,3)))
956+
Jacobian_matrix_dot = np.matrix(np.zeros((3,3)))
929957

930958

931959
# Maximum thrust produced by a single motor = 1355 grams = 1.355 kg = 1.355 kg * 9.81 m/s^2 = 13.28 N
@@ -1260,7 +1288,8 @@ def MRACwithBASELINE(t, y):
12601288
global mu_x, mu_y, mu_z, u1, roll_ref, pitch_ref, roll_ref_dot, pitch_ref_dot, roll_ref_ddot, pitch_ref_ddot
12611289
global angular_position_ref_dot, angular_position_ref_ddot, Jacobian_matrix_inverse, angular_position_dot
12621290
global angular_error_dot, u2, u3, u4, mu_baseline_tran, mu_adaptive_tran, Moment_baseline, Moment_adaptive
1263-
global mu_PD_baseline_tran, Moment_baseline_PI, omega_ref_dot
1291+
global mu_PD_baseline_tran, Moment_baseline_PI, omega_ref_dot, omega_cmd, omega_cmd_dot, Jacobian_matrix
1292+
global Jacobian_matrix_dot
12641293

12651294

12661295
state_phi_ref_diff = y[0:2] # State of the differentiator for phi_ref (roll_ref)
@@ -3104,7 +3133,8 @@ def MRACwithBASELINE_SafetyMechanism(t, y):
31043133
global mu_x, mu_y, mu_z, u1, roll_ref, pitch_ref, roll_ref_dot, pitch_ref_dot, roll_ref_ddot, pitch_ref_ddot
31053134
global angular_position_ref_dot, angular_position_ref_ddot, Jacobian_matrix_inverse, angular_position_dot
31063135
global angular_error_dot, u2, u3, u4, mu_baseline_tran, mu_adaptive_tran, Moment_baseline, Moment_adaptive
3107-
global mu_PD_baseline_tran, Moment_baseline_PI, omega_ref_dot
3136+
global mu_PD_baseline_tran, Moment_baseline_PI, omega_ref_dot, omega_cmd, omega_cmd_dot, Jacobian_matrix
3137+
global Jacobian_matrix_dot
31083138

31093139

31103140
state_phi_ref_diff = y[0:2] # State of the differentiator for phi_ref (roll_ref)
@@ -3263,7 +3293,7 @@ def MRACwithBASELINE_SafetyMechanism(t, y):
32633293
Jacobian_matrix = np.matrix([[1, 0, -math.sin(pitch)],
32643294
[0, math.cos(roll), math.sin(roll) * math.cos(pitch)],
32653295
[0, -math.sin(roll), math.cos(roll) * math.cos(pitch)]])
3266-
3296+
print('HELLO Jacobian_matrix')
32673297
omega_cmd = Jacobian_matrix * (-KP_rot*angular_error + angular_position_ref_dot)
32683298
omega_cmd_dot = Jacobian_matrix_dot * (-KP_rot*angular_error + angular_position_ref_dot) + Jacobian_matrix * (-KP_rot*angular_error_dot + angular_position_ref_ddot)
32693299

@@ -3673,8 +3703,7 @@ def MRACwithBASELINE_SafetyMechanism(t, y):
36733703
[translational_position_in_I_user,
36743704
translational_velocity_in_I_user,
36753705
translational_acceleration_in_I_user] = (trajectory_instance.
3676-
user_defined_trajectory(time_now - controller_start_time,
3677-
waypointTimes))
3706+
user_defined_trajectory(time_now - controller_start_time))
36783707
[yaw_ref,
36793708
yaw_ref_dot,
36803709
yaw_ref_ddot] = trajectory_instance.user_defined_yaw()
@@ -4657,7 +4686,16 @@ def MRACwithBASELINE_SafetyMechanism(t, y):
46574686
print ('\nSimulation time: ', time_now)
46584687

46594688

4660-
print('omega_ref_dot: ', omega_ref_dot)
4689+
print('omega_ref_dot: ', '%.4f'%omega_ref_dot[0], '%.4f'%omega_ref_dot[1], '%.4f'%omega_ref_dot[2])
4690+
print('omega_ref: ', '%.4f'%omega_ref[0], '%.4f'%omega_ref[1], '%.4f'%omega_ref[2])
4691+
print('omega_cmd_dot: ', '%.8f'%omega_cmd_dot[0],
4692+
'%.8f'%omega_cmd_dot[1],
4693+
'%.8f'%omega_cmd_dot[2])
4694+
print('omega_cmd: ', '%.4f'%omega_cmd[0], '%.4f'%omega_cmd[1], '%.4f'%omega_cmd[2])
4695+
print('Jacobian_matrix_dot: ', Jacobian_matrix_dot)
4696+
print('Jacobian_matrix: ', Jacobian_matrix)
4697+
4698+
46614699
# print('z - z_ref: ', pos_pixhawk_LOC_to_GLOB_NED.z - z_ref)
46624700
print('Z_onboard: ', '%.4f'%pos_pixhawk_LOC_to_GLOB_NED.z)
46634701
# print('(G_acc - controller_z_output)*mass_total: ', (G_acc + controller_z_output)*mass_total)
@@ -4709,11 +4747,13 @@ def MRACwithBASELINE_SafetyMechanism(t, y):
47094747

47104748

47114749
# print('Pixhawk LOCAL Position: ', chvector_to_list(pos_pixhawk_LOC))
4712-
print('Pixhawk GLOBAL Position: ', chcoordsys_to_list(coord_pixhawk_GLOB)[0])
4750+
print('Pixhawk GLOBAL Position: ', '%.4f'%chcoordsys_to_list(coord_pixhawk_GLOB)[0][0],
4751+
'%.4f'%chcoordsys_to_list(coord_pixhawk_GLOB)[0][1],
4752+
'%.4f'%chcoordsys_to_list(coord_pixhawk_GLOB)[0][2])
47134753
# print('Pixhawk GLOBAL Velocity: ', chcoordsys_to_list(coord_dt_pixhawk_GLOB)[0])
47144754
# print('Pixhawk LOCAL Velocity_T: ', chvector_to_list(vel_pixhawk_LOC_T))
47154755
# print('Pixhawk LOCAL Velocity: ', chvector_to_list(vel_pixhawk_LOC))
4716-
print('Pixhawk GLOBAL Velocity NORM: ', LA.norm(np.asarray(chcoordsys_to_list(coord_dt_pixhawk_GLOB)[0])))
4756+
print('Pixhawk GLOBAL Velocity NORM: ', '%.4f'%LA.norm(np.asarray(chcoordsys_to_list(coord_dt_pixhawk_GLOB)[0])))
47174757
# print('Pixhawk LOCAL Velocity_T NORM: ', LA.norm(np.asarray(chvector_to_list(vel_pixhawk_LOC_T))))
47184758
# print('Pixhawk LOCAL Velocity NORM: ', LA.norm(np.asarray(chvector_to_list(vel_pixhawk_LOC))),'\n')
47194759

loop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# controller_type = 'PID' # Check mass_total_estimated
3535

36-
controller_type = 'MRACwithBASELINE'
36+
# controller_type = 'MRACwithBASELINE'
3737

3838
# controller_type = 'TwoLayerMRACwithBASELINE'
3939

@@ -53,7 +53,7 @@
5353

5454
# controller_type = 'FunnelTwoLayerMRACwithBASELINE'
5555

56-
# controller_type = 'MRACwithBASELINE_SafetyMechanism'
56+
controller_type = 'MRACwithBASELINE_SafetyMechanism'
5757

5858
# ----------------------------------------------------------------
5959
# %%%%%%%%%%%%%%%%%%%%%%

trajectory.py

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,9 @@ def __init__(self):
418418
self.psi_ref_ddot = 0
419419
self.psi_ref_previous = 0
420420

421-
def set_parameters(self, pp_coefficients):
421+
def set_parameters(self, pp_coefficients, waypointTimes):
422+
423+
self.waypointTimes = waypointTimes
422424

423425
[self.position_coef_x,
424426
self.position_coef_y,
@@ -436,7 +438,7 @@ def set_parameters(self, pp_coefficients):
436438
self.jerk_coef_y = traj_functions.PolyderMatrix(self.acceleration_coef_y)
437439
self.jerk_coef_z = traj_functions.PolyderMatrix(self.acceleration_coef_z)
438440

439-
def user_defined_trajectory(self, t, waypointTimes):
441+
def user_defined_trajectory(self, t):
440442
"""Piecewise polynomial trajectory.
441443
t: current simulation time
442444
pp_coefficients: matrix of the coefficients of the piecewise polynomial
@@ -445,37 +447,37 @@ def user_defined_trajectory(self, t, waypointTimes):
445447
"""
446448

447449
[self.t_adjusted,
448-
self.segment] = traj_functions.PolyTimeAdjusted(waypointTimes, t)
450+
self.segment] = traj_functions.PolyTimeAdjusted(self.waypointTimes, t)
449451

450-
self.translational_position_in_I_user[0] = (polynomial.
451-
polyval(self.t_adjusted,
452-
self.position_coef_x[self.segment,:]))
453-
self.translational_position_in_I_user[1] = (polynomial.
454-
polyval(self.t_adjusted,
455-
self.position_coef_y[self.segment,:]))
456-
self.translational_position_in_I_user[2] = (polynomial.
457-
polyval(self.t_adjusted,
458-
self.position_coef_z[self.segment,:]))
452+
self.translational_position_in_I_user[0] = polynomial.polyval(
453+
self.t_adjusted,
454+
self.position_coef_x[self.segment,:])
455+
self.translational_position_in_I_user[1] = polynomial.polyval(
456+
self.t_adjusted,
457+
self.position_coef_y[self.segment,:])
458+
self.translational_position_in_I_user[2] = polynomial.polyval(
459+
self.t_adjusted,
460+
self.position_coef_z[self.segment,:])
459461

460-
self.translational_velocity_in_I_user[0] = (polynomial.
461-
polyval(self.t_adjusted,
462-
self.velocity_coef_x[self.segment,:]))
463-
self.translational_velocity_in_I_user[1] = (polynomial.
464-
polyval(self.t_adjusted,
465-
self.velocity_coef_y[self.segment,:]))
466-
self.translational_velocity_in_I_user[2] = (polynomial.
467-
polyval(self.t_adjusted,
468-
self.velocity_coef_z[self.segment,:]))
462+
self.translational_velocity_in_I_user[0] = polynomial.polyval(
463+
self.t_adjusted,
464+
self.velocity_coef_x[self.segment,:])
465+
self.translational_velocity_in_I_user[1] = polynomial.polyval(
466+
self.t_adjusted,
467+
self.velocity_coef_y[self.segment,:])
468+
self.translational_velocity_in_I_user[2] = polynomial.polyval(
469+
self.t_adjusted,
470+
self.velocity_coef_z[self.segment,:])
469471

470-
self.translational_acceleration_in_I_user[0] = (polynomial.
471-
polyval(self.t_adjusted,
472-
self.acceleration_coef_x[self.segment,:]))
473-
self.translational_acceleration_in_I_user[1] = (polynomial.
474-
polyval(self.t_adjusted,
475-
self.acceleration_coef_y[self.segment,:]))
476-
self.translational_acceleration_in_I_user[2] = (polynomial.
477-
polyval(self.t_adjusted,
478-
self.acceleration_coef_z[self.segment,:]))
472+
self.translational_acceleration_in_I_user[0] = polynomial.polyval(
473+
self.t_adjusted,
474+
self.acceleration_coef_x[self.segment,:])
475+
self.translational_acceleration_in_I_user[1] = polynomial.polyval(
476+
self.t_adjusted,
477+
self.acceleration_coef_y[self.segment,:])
478+
self.translational_acceleration_in_I_user[2] = polynomial.polyval(
479+
self.t_adjusted,
480+
self.acceleration_coef_z[self.segment,:])
479481

480482
return [self.translational_position_in_I_user,
481483
self.translational_velocity_in_I_user,
@@ -484,10 +486,10 @@ def user_defined_trajectory(self, t, waypointTimes):
484486
def user_defined_yaw(self):
485487
"User-defined reference yaw angle"
486488

487-
self.velocity_norm2D = (traj_functions.
488-
Norm2D(self.velocity_coef_x[self.segment,:],
489-
self.velocity_coef_y[self.segment,:],
490-
self.t_adjusted))
489+
self.velocity_norm2D = traj_functions.Norm2D(
490+
self.velocity_coef_x[self.segment,:],
491+
self.velocity_coef_y[self.segment,:],
492+
self.t_adjusted)
491493

492494
if self.t_adjusted == 0:
493495
self.psi_ref = 0
@@ -500,33 +502,51 @@ def user_defined_yaw(self):
500502
self.psi_ref_ddot = 0
501503

502504
else:
503-
self.psi_ref = (traj_functions.
504-
YawComputation(self.velocity_coef_x[self.segment,:],
505-
self.velocity_coef_y[self.segment,:],
506-
self.t_adjusted))
507-
self.psi_ref_dot = (traj_functions.
508-
YawDotComputation(
505+
self.psi_ref = traj_functions.YawComputation(
506+
self.velocity_coef_x[self.segment,:],
507+
self.velocity_coef_y[self.segment,:],
508+
self.t_adjusted)
509+
self.psi_ref_dot = traj_functions.YawDotComputation(
509510
self.velocity_coef_x[self.segment,:],
510511
self.velocity_coef_y[self.segment,:],
511512
self.acceleration_coef_x[self.segment,:],
512513
self.acceleration_coef_y[self.segment,:],
513-
self.t_adjusted))
514-
self.psi_ref_ddot = (traj_functions.
515-
YawDotDotComputation(
514+
self.t_adjusted)
515+
self.psi_ref_ddot = traj_functions.YawDotDotComputation(
516516
self.velocity_coef_x[self.segment,:],
517517
self.velocity_coef_y[self.segment,:],
518518
self.acceleration_coef_x[self.segment,:],
519519
self.acceleration_coef_y[self.segment,:],
520520
self.jerk_coef_x[self.segment,:],
521521
self.jerk_coef_y[self.segment,:],
522-
self.t_adjusted))
522+
self.t_adjusted)
523523

524524
self.psi_ref_previous = self.psi_ref
525525

526526
return [self.psi_ref,
527527
self.psi_ref_dot,
528528
self.psi_ref_ddot]
529-
529+
530+
def ComputePositionVector(self, samplingTime = 0.01):
531+
"Drawing ChLineSegement for visualization of the trajectory in simulation"
532+
533+
self.samplingTime = samplingTime
534+
535+
sampling_time_vector = traj_functions.SamplingTimeVector(self.waypointTimes,
536+
self.samplingTime)
537+
pos_x = np.zeros(sampling_time_vector.size)
538+
pos_y = np.zeros(sampling_time_vector.size)
539+
pos_z = np.zeros(sampling_time_vector.size)
540+
541+
for i in range(sampling_time_vector.size):
542+
543+
[position, _ , _]= self.user_defined_trajectory(sampling_time_vector[i])
544+
545+
pos_x[i] = position[0]
546+
pos_y[i] = position[1]
547+
pos_z[i] = position[2]
548+
549+
return [pos_x, pos_y, pos_z]
530550

531551

532552

0 commit comments

Comments
 (0)