99
1010import numpy as np
1111import matplotlib .pyplot as plt
12- from mobotpy import models
12+ from mobotpy . models import DiffDrive
1313
1414# Set the simulation time [s] and the sample period [s]
1515SIM_TIME = 30.0
1919t = np .arange (0.0 , SIM_TIME , T )
2020N = np .size (t )
2121
22+ # Set the track of the vehicle [m]
23+ ELL = 0.35
24+
2225# %%
2326# FUNCTION DEFINITIONS
2427
2528
26- def rk_four (f , x , u , T , P ):
29+ def rk_four (f , x , u , T ):
2730 """Fourth-order Runge-Kutta numerical integration."""
28- k_1 = f (x , u , P )
29- k_2 = f (x + T * k_1 / 2.0 , u , P )
30- k_3 = f (x + T * k_2 / 2.0 , u , P )
31- k_4 = f (x + T * k_3 , u , P )
31+ k_1 = f (x , u )
32+ k_2 = f (x + T * k_1 / 2.0 , u )
33+ k_3 = f (x + T * k_2 / 2.0 , u )
34+ k_4 = f (x + T * k_3 , u )
3235 x_new = x + T / 6.0 * (k_1 + 2.0 * k_2 + 2.0 * k_3 + k_4 )
3336 return x_new
3437
3538
36- def diffdrive_f (x , u , ell ):
39+ def diffdrive_f (x , u ):
3740 """Differential drive kinematic vehicle model."""
3841 f = np .zeros (3 )
3942 f [0 ] = 0.5 * (u [0 ] + u [1 ]) * np .cos (x [2 ])
4043 f [1 ] = 0.5 * (u [0 ] + u [1 ]) * np .sin (x [2 ])
41- f [2 ] = 1.0 / ell * (u [1 ] - u [0 ])
44+ f [2 ] = 1.0 / ELL * (u [1 ] - u [0 ])
4245 return f
4346
4447
45- def uni2diff (u , ell ):
48+ def uni2diff (u ):
4649 """Convert speed and angular rate to wheel speeds."""
4750 v = u [0 ]
4851 omega = u [1 ]
49- v_L = v - ell / 2 * omega
50- v_R = v + ell / 2 * omega
52+ v_L = v - ELL / 2 * omega
53+ v_R = v + ELL / 2 * omega
5154 return np .array ([v_L , v_R ])
5255
5356
@@ -65,19 +68,16 @@ def openloop(t):
6568x = np .zeros ((3 , N ))
6669u = np .zeros ((2 , N ))
6770
68- # Set the track of the vehicle [m]
69- ELL = 0.35
70-
7171# Set the initial pose [m, m, rad], velocities [m/s, m/s]
7272x [0 , 0 ] = 0.0
7373x [1 , 0 ] = 0.0
7474x [2 , 0 ] = np .pi / 2.0
75- u [:, 0 ] = uni2diff (openloop (t [0 ]), ELL )
75+ u [:, 0 ] = uni2diff (openloop (t [0 ]))
7676
7777# Run the simulation
7878for k in range (1 , N ):
79- x [:, k ] = rk_four (diffdrive_f , x [:, k - 1 ], u [:, k - 1 ], T , ELL )
80- u [:, k ] = uni2diff (openloop (t [k ]), ELL )
79+ x [:, k ] = rk_four (diffdrive_f , x [:, k - 1 ], u [:, k - 1 ], T )
80+ u [:, k ] = uni2diff (openloop (t [k ]))
8181
8282# %%
8383# MAKE PLOTS
@@ -118,20 +118,19 @@ def openloop(t):
118118plt .savefig ("../agv-book/figs/ch3/diffdrive_kinematic_fig1.pdf" )
119119
120120# Let's now use the class DiffDrive for plotting
121- vehicle = models . DiffDrive (ELL )
121+ vehicle = DiffDrive (ELL )
122122
123123# Plot the position of the vehicle in the plane
124124fig2 = plt .figure (2 )
125125plt .plot (x [0 , :], x [1 , :], "C0" )
126126plt .axis ("equal" )
127- X_L , Y_L , X_R , Y_R , X_B , Y_B , X_C , Y_C = vehicle .draw (
128- x [0 , 0 ], x [1 , 0 ], x [2 , 0 ], ELL )
127+ X_L , Y_L , X_R , Y_R , X_B , Y_B , X_C , Y_C = vehicle .draw (x [0 , 0 ], x [1 , 0 ], x [2 , 0 ])
129128plt .fill (X_L , Y_L , "k" )
130129plt .fill (X_R , Y_R , "k" )
131130plt .fill (X_C , Y_C , "k" )
132131plt .fill (X_B , Y_B , "C2" , alpha = 0.5 , label = "Start" )
133132X_L , Y_L , X_R , Y_R , X_B , Y_B , X_C , Y_C = vehicle .draw (
134- x [0 , N - 1 ], x [1 , N - 1 ], x [2 , N - 1 ], ELL
133+ x [0 , N - 1 ], x [1 , N - 1 ], x [2 , N - 1 ]
135134)
136135plt .fill (X_L , Y_L , "k" )
137136plt .fill (X_R , Y_R , "k" )
@@ -151,8 +150,7 @@ def openloop(t):
151150# MAKE AN ANIMATION
152151
153152# Create and save the animation
154- ani = vehicle .animate (
155- x , T , ELL , True , "../agv-book/gifs/ch3/diffdrive_kinematic.gif" )
153+ ani = vehicle .animate (x , T , True , "../agv-book/gifs/ch3/diffdrive_kinematic.gif" )
156154
157155# Show the movie to the screen
158156plt .show ()
0 commit comments