Skip to content

Commit

Permalink
Arreglado formto json
Browse files Browse the repository at this point in the history
  • Loading branch information
evafc003 committed Nov 5, 2024
1 parent 3ba1c68 commit 26f0bae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pruebas/moving_nao/moving_nao/moving_in_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Move(Node):
def __init__(self):
super().__init__('butterfly_swim')
super().__init__('move')

with open('/home/2024-tfg-eva-fernandez/pruebas/moving_nao/nao_movement_pattern_creator/movement_pattern.json', 'r') as file:
self.datos = json.load(file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import pybullet_data as pd
import time
import json
import os

# Vaciar el json para que sean todo datos nuevos
with open("movement_pattern.json", "w") as json_file:
json.dump([], json_file, indent=4)

time_to_go = 0
used_joints = [1,2,13,14,15,16,17,18,26,27,28,29,30,31,39,40,41,42,43,56,57,58,59,60]
Expand All @@ -24,7 +29,7 @@
button = p.addUserDebugParameter("DONE", 0, 1, 0)
button_prev_value = p.readUserDebugParameter(button)

desired_time = p.addUserDebugParameter("Time", 0, 300, 0)
desired_time = p.addUserDebugParameter("Time", 0, 30, 0)

# Preparar sliders para movimiento
head_yaw = p.addUserDebugParameter("HeadYaw", -2.09, 2.09, 0)
Expand Down Expand Up @@ -129,28 +134,49 @@
# Coger segundos a los que se quiere la posicion actual y volcarlo a un fichero JSON
time_to_go = desired_time_value

if decided != button_prev_value and decided == 1:
movement_data = []
if decided != button_prev_value:
movement_data = {}

for joint in used_joints:
joint_info = p.getJointInfo(model, joint)
joint_name = joint_info[1].decode("utf-8") # Para que sea legible el nombre del joint
joint_state = p.getJointState(model, joint)
joint_position = joint_state[0]

# Datos para el JSON
data = {
"articulacion": joint_name,
"posicion": joint_position,
"tiempo": time_to_go
}

movement_data.append(data)

# Escribir todos los datos en un archivo JSON
with open("movement_pattern.json", "w") as json_file:
json.dump(movement_data, json_file, indent=4)
if time_to_go not in movement_data:
movement_data[time_to_go] = [] # Creamos una lista para cada tiempo

# Agregar los datos de la articulación al tiempo correspondiente
movement_data[time_to_go].append({
"articulacion": joint_name,
"posicion": joint_position
})

# Convertir el diccionario en una lista de tiempos para sacar fotogramas
fotogram = []
for tiempo, articulaciones in movement_data.items():
fotogram.append({
"tiempo": tiempo,
"articulaciones": articulaciones
})

# Leer datos existentes del archivo JSON, si existe
if os.path.exists("movement_pattern.json"):
with open("movement_pattern.json", "r") as json_file:
existing_data = json.load(json_file)
print(existing_data)
else:
existing_data = []

# Agregar nuevos datos a los existentes
existing_data.extend(fotogram)

# Escribir todos los datos en el archivo JSON
with open("movement_pattern.json", "w") as json_file:
json.dump(existing_data, json_file, indent=4)

print("SAVING...")
time.sleep(0.5)
break

print("SAVED, PLEASE INTRODUCE ANOTHER ONE OR EXIT")
button_prev_value = decided

0 comments on commit 26f0bae

Please sign in to comment.