metadata = []
for height in np.linspace(max_height, min_height, num=heights):
print(f"Simulating {sims_per_height} drops from {height}m")
for i in range(sims_per_height):
# Set initial position and orientation of object
x = 0
y = 0
z = height
orientation = p.getQuaternionFromEuler((random.uniform(0, 2 * np.pi), random.uniform(0, 2 * np.pi), random.uniform(0, 2 * np.pi)))
p.resetBasePositionAndOrientation(obj_id, [x, y, z], orientation)
prev_linear_vel = np.zeros(3)
# Initialize the object position and velocity
pos_prev, orn_prev = p.getBasePositionAndOrientation(obj_id)
vel_prev, ang_vel_prev = p.getBaseVelocity(obj_id)
timestamp=0
dt=1/sample_freq
p.setTimeStep(dt)
filename=f"drop_{height}m_{i}.csv"
with open(f"output/{filename}", mode="w") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['timestamp','accX','accY','accZ'])
while timestamp < sample_length:
p.stepSimulation()
linear_vel, angular_vel = p.getBaseVelocity(obj_id)
lin_acc = [(v - prev_v)/dt for v, prev_v in zip(linear_vel, prev_linear_vel)]
prev_linear_vel = linear_vel
timestamp += dt
# Get the current position and orientation of the object
pos, orn = p.getBasePositionAndOrientation(obj_id)
# Get the linear and angular velocity of the object in world coordinates
vel, ang_vel = p.getBaseVelocity(obj_id)
# Calculate the change in position and velocity between steps
pos_diff = np.array(pos) - np.array(pos_prev)
vel_diff = np.array(vel) - np.array(vel_prev)
# Convert the orientation quaternion to a rotation matrix
rot_matrix = np.array(p.getMatrixFromQuaternion(orn)).reshape(3, 3)
# Calculate the local linear acceleration of the object, subtracting gravity
local_acc = np.dot(rot_matrix.T, vel_diff / dt) - np.array([0, 0, -9.81])
# Restrict the acceleration to the range of the accelerometer
imu_rel_lin_acc_scaled = np.clip(local_acc, -range_acc, range_acc)
# Round the acceleration to the nearest resolution of the accelerometer
imu_rel_lin_acc_rounded = np.round(imu_rel_lin_acc_scaled/resolution_acc) * resolution_acc
# Update the previous position and velocity
pos_prev, orn_prev = pos, orn
vel_prev, ang_vel_prev = vel, ang_vel
# Save acceleration data to CSV file
with open(f"{output_folder}{filename}", mode="a") as csv_file:
writer = csv.writer(csv_file)
writer.writerow([timestamp*1000] + imu_rel_lin_acc_rounded.tolist())
nearestheight = round(height, 2)
metadata.append({
"path": filename,
"category": "training",
"label": { "type": "label", "label": str(nearestheight)}
})