Skip to content

Commit

Permalink
Fix numpy array casting error
Browse files Browse the repository at this point in the history
Newer versions of numpy resulted in array type cast issues caused by
1) Python treating stiffness/damping matrices as tuples (a syntactical
error on my part) and 2) numpy treating elements of arrays, e.g.,
velocity[1], as 1-D arrays of one element instead of extracting the
element as a float. This is remedied by accessing the item using
ndarray.item(), e.g., velocity[1].item(). This commit makes these
changes, allowing the program to be used with the latest versions of
numpy.
  • Loading branch information
nrsyed committed May 5, 2019
1 parent c77d712 commit f546450
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
50 changes: 26 additions & 24 deletions halfcar/car.py
Expand Up @@ -159,35 +159,39 @@ def __init__(self, road_func=None):

mass_vector = np.array([m_c, I_zz, m_f, m_r])

stiffness_matrix = (np.array([
stiffness_matrix = np.array([
[-(k_fs + k_rs), l_r * k_rs - l_f * k_fs, k_fs, k_rs],
[-(l_f * k_fs - l_r * k_rs), -(l_f**2 * k_fs + l_r**2 * k_rs),
l_f * k_fs, -l_r * k_rs],
[k_fs, l_f * k_fs, -(k_fs + k_ft), 0],
[k_rs, -l_r * k_rs, 0, -(k_rs + k_rt)]])
/ mass_vector[:, None])
[k_rs, -l_r * k_rs, 0, -(k_rs + k_rt)]
])
stiffness_matrix = stiffness_matrix / mass_vector[:, None]

damping_matrix = (np.array([
damping_matrix = np.array([
[-(c_fs + c_rs), l_r * c_rs - l_f * c_fs, c_fs, c_rs],
[-(l_f * c_fs - l_r * c_rs), -(l_f**2 * c_fs + l_r**2 * c_rs),
l_f * c_fs, -l_r * c_rs],
[c_fs, l_f * c_fs, -(c_fs + c_ft), 0],
[c_rs, -l_r * c_rs, 0, -(c_rs + c_rt)]])
/ mass_vector[:, None])
[c_rs, -l_r * c_rs, 0, -(c_rs + c_rt)]
])
damping_matrix = damping_matrix / mass_vector[:, None]

road_stiffness_matrix = (np.array([
road_stiffness_matrix = np.array([
[0, 0],
[0, 0],
[k_ft, 0],
[0, k_rt]])
/ mass_vector[:, None])
[0, k_rt]
])
road_stiffness_matrix = road_stiffness_matrix / mass_vector[:, None]

road_damping_matrix = (np.array([
road_damping_matrix = np.array([
[0, 0],
[0, 0],
[c_ft, 0],
[0, c_rt]])
/ mass_vector[:, None])
[0, c_rt]
])
road_damping_matrix = road_damping_matrix / mass_vector[:, None]

# Compute baseline height of COG above front wheel point of contact.
lowest_point = np.amin(chassis[1,:])
Expand Down Expand Up @@ -246,11 +250,11 @@ def __init__(self, road_func=None):

# Initialize state vectors and other variables.
self.state = {
"position": np.zeros((4,1), dtype=np.float),
"velocity": np.zeros((4,1), dtype=np.float),
"accel": np.zeros((4,1), dtype=np.float),
"road_position": np.zeros((2,1), dtype=np.float),
"road_velocity": np.zeros((2,1), dtype=np.float),
"position": np.zeros((4,1)),
"velocity": np.zeros((4,1)),
"accel": np.zeros((4,1)),
"road_position": np.zeros((2,1)),
"road_velocity": np.zeros((2,1)),
"horizontal_accel": 0,
"horizontal_velocity": 0,
"distance_traveled": 0
Expand All @@ -272,8 +276,6 @@ def __init__(self, road_func=None):
self.road_profile = self.road_func()


# TODO: allow independent gas and brake, fix gas and brake.

def set_accel(self, accel):
"""
Manually set car's horizontal acceleration in m/s^2.
Expand Down Expand Up @@ -322,6 +324,7 @@ def brake(self, decel, units="mps"):
new_decel = decel * max_decel
#elif decel


def update_state(self, time_step):
position = self.state["position"]
velocity = self.state["velocity"]
Expand Down Expand Up @@ -412,15 +415,14 @@ def normal_force_vector(self):
# Obtain the height of the COG above the front wheel point of contact,
# i.e., the height of the COG about the driving force vector (assuming
# a front-wheel drive vehicle).
height = init_height + position[0] + position[2]
height = init_height + position[0].item() + position[2].item()

normal_force_vector = (np.array([
normal_force_vector = np.array([
[0],
[0],
[height * m * horizontal_accel / wheelbase],
[-height * m * horizontal_accel / wheelbase]
])
/ mass_vector[:, None]
)
])
normal_force_vector = normal_force_vector / mass_vector[:, None]

return normal_force_vector
12 changes: 3 additions & 9 deletions requirements.txt
@@ -1,9 +1,3 @@
cycler==0.10.0
kiwisolver==1.1.0
matplotlib==2.2.3
numpy==1.15.1
pyparsing==2.4.0
python-dateutil==2.8.0
pytz==2019.1
scipy==1.2.0
six==1.12.0
matplotlib
numpy
scipy
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -10,7 +10,7 @@
license="GPL",
packages=["halfcar"],
install_requires=[
"numpy==1.15.1",
"numpy",
"scipy",
"matplotlib"
]
Expand Down

0 comments on commit f546450

Please sign in to comment.