Skip to content

Commit 16be57a

Browse files
committed
current state
1 parent eb5cfab commit 16be57a

File tree

1 file changed

+61
-62
lines changed
  • bitbots_motion/bitbots_animation_rqt/bitbots_animation_rqt

1 file changed

+61
-62
lines changed

bitbots_motion/bitbots_animation_rqt/bitbots_animation_rqt/record_ui.py

+61-62
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,50 @@ class RecordUI(Plugin):
4040

4141
def __init__(self, context):
4242
super().__init__(context)
43+
# Store reference to node
4344
self._node: Node = context.node
4445

46+
# Set Name of the plugin
47+
self.setObjectName("Record Animation")
48+
49+
# Create publishers
50+
self._joint_pub = self._node.create_publisher(JointCommand, "record_motor_goals", 1)
51+
self.effort_pub = self._node.create_publisher(JointTorque, "ros_control/set_torque_individual", 1)
52+
53+
# Initialize the recorder module
54+
self._recorder = Recorder(self._node)
55+
4556
# Initialize the window
4657
self._widget = QMainWindow()
4758

4859
# Load XML ui definition
4960
ui_file = os.path.join(get_package_share_directory("bitbots_animation_rqt"), "resource", "RecordUI.ui")
5061
loadUi(ui_file, self._widget, {})
5162

52-
# Initialize the recorder module
53-
self._recorder = Recorder(self._node)
54-
5563
# Initialize the GUI state
5664
self._sliders = {}
5765
self._text_fields = {}
5866
self._motor_switched = {}
5967
self._selected_frame = None
68+
69+
self.update_time = 0.1 # TODO what is this for?
6070

6171
self._current_goals = {} # this is the data about the current unsaved frame
6272
self._current_duration = 1.0
6373
self._current_pause = 0.0
6474
self._current_name = "new frame"
6575

76+
# Save current keyframe when switching to other frames in the ui
6677
self._working_values = {} # this is the data about the frame that is displayed
6778
self._working_duration = 1.0
6879
self._working_pause = 0.0
69-
self._working_name = self._currentName
70-
80+
self._working_name = self._current_name
7181
self._current = True
7282

7383
# QT directory for saving files
7484
self._save_directory = None
7585

76-
# save current frame when switching to other frames for reference
77-
# working frame
78-
86+
# Initialize the motor tree structure where we can select which motors are stiff
7987
self._treeItems = {}
8088
self._motor_check_body = QTreeWidgetItem(self._widget.motorTree)
8189
self._motor_check_legs = QTreeWidgetItem(self._motor_check_body)
@@ -86,49 +94,46 @@ def __init__(self, context):
8694
self._motor_check_left_leg = QTreeWidgetItem(self._motor_check_legs)
8795
self._motor_check_right_leg = QTreeWidgetItem(self._motor_check_legs)
8896

89-
# saves configuration of the trees checkboxes for each of the tree modes.
97+
# Save configuration of the trees checkboxes for each of the tree modes
9098
self._check_boxes_save = {}
9199
self._check_boxes_power = {}
92100
self._previous_tree_mode = 0
93101

94-
self.setObjectName("Record Animation")
95-
102+
# Create drag and dop list for keyframes
96103
self._widget.frameList = DragDropList(self._widget, self)
97104
self._widget.verticalLayout_2.insertWidget(0, self._widget.frameList)
98105
self._widget.frameList.setDragDropMode(QAbstractItemView.InternalMove)
99106

100107
# Create a dictionary to map joint names to ids # TODO this should not be hardcoded
101-
self.ids = {
102-
"HeadPan": 0,
103-
"HeadTilt": 1,
104-
"LShoulderPitch": 2,
105-
"LShoulderRoll": 3,
106-
"LElbow": 4,
107-
"RShoulderPitch": 5,
108-
"RShoulderRoll": 6,
109-
"RElbow": 7,
110-
"LHipYaw": 8,
111-
"LHipRoll": 9,
112-
"LHipPitch": 10,
113-
"LKnee": 11,
114-
"LAnklePitch": 12,
115-
"LAnkleRoll": 13,
116-
"RHipYaw": 14,
117-
"RHipRoll": 15,
118-
"RHipPitch": 16,
119-
"RKnee": 17,
120-
"RAnklePitch": 18,
121-
"RAnkleRoll": 19,
122-
}
123-
124-
self._initial_joints = JointState()
125-
126-
self.update_time = 0.1
127-
128-
for k, _v in self.ids.items():
129-
self._node.get_logger().info(f"Adding {k} to initial_joints")
130-
self._initial_joints.name.append(k)
131-
self._initial_joints.position.append(0)
108+
self.motors = [
109+
"HeadPan",
110+
"HeadTilt",
111+
"LShoulderPitch",
112+
"LShoulderRoll",
113+
"LElbow",
114+
"RShoulderPitch",
115+
"RShoulderRoll",
116+
"RElbow",
117+
"LHipYaw",
118+
"LHipRoll",
119+
"LHipPitch",
120+
"LKnee",
121+
"LAnklePitch",
122+
"LAnkleRoll",
123+
"RHipYaw",
124+
"RHipRoll",
125+
"RHipPitch",
126+
"RKnee",
127+
"RAnklePitch",
128+
"RAnkleRoll",
129+
]
130+
131+
# Create the initial joint state
132+
self._initial_joints = JointState(
133+
name=self.motors,
134+
position=[0.0] * len(self.motors),
135+
)
136+
132137

133138
for i in range(0, len(self._initial_joints.name)):
134139
self._current_goals[self._initial_joints.name[i]] = self._initial_joints.position[i]
@@ -141,9 +146,9 @@ def __init__(self, context):
141146

142147
self._check_boxes_power["#CURRENT_FRAME"] = init_torque
143148

144-
self.motor_controller()
145-
self.motor_switcher()
146-
self.action_connect()
149+
self.create_motor_controller()
150+
self.create_motor_switcher()
151+
self.connect_gui_callbacks()
147152
self.box_ticked()
148153
self.frame_list()
149154
self.update_frames()
@@ -154,10 +159,6 @@ def __init__(self, context):
154159
# Create subscriptions
155160
self.state_sub = self._node.create_subscription(JointState, "joint_states", self.state_update, 1)
156161

157-
# Create publishers
158-
self._joint_pub = self._node.create_publisher(JointCommand, "record_motor_goals", 1)
159-
self.effort_pub = self._node.create_publisher(JointTorque, "ros_control/set_torque_individual", 1)
160-
161162
self._widget.statusBar.showMessage("Initialization complete.")
162163

163164
def state_update(self, joint_states):
@@ -175,13 +176,12 @@ def state_update(self, joint_states):
175176
time.sleep(self.update_time)
176177
self.set_sliders_and_text_fields(manual=False)
177178

178-
def motor_controller(self):
179+
def create_motor_controller(self):
179180
"""
180181
Sets up the GUI in the middle of the Screen to control the motors.
181182
Uses self._motorValues to determine which motors are present.
182183
"""
183-
i = 0
184-
for k, _v in sorted(self._current_goals.items()):
184+
for i, k in enumerate(sorted(self._current_goals.keys())):
185185
group = QGroupBox()
186186
slider = QSlider(Qt.Horizontal)
187187
slider.setTickInterval(1)
@@ -207,9 +207,8 @@ def motor_controller(self):
207207
layout.addWidget(self._text_fields[k])
208208
group.setLayout(layout)
209209
self._widget.motorControlLayout.addWidget(group, i // 5, i % 5)
210-
i = i + 1
211210

212-
def action_connect(self):
211+
def connect_gui_callbacks(self):
213212
"""
214213
Connects the actions in the top bar to the corresponding functions, and sets their shortcuts
215214
:return:
@@ -639,7 +638,7 @@ def frame_select(self):
639638
selected_frame_name = self._widget.frameList.currentItem().text()
640639
self._selected_frame = None
641640

642-
for v in []: # TODO self._recorder.get_animation_state():
641+
for v in self._recorder.get_animation_state():
643642
if v["name"] == selected_frame_name:
644643
self._selected_frame = v
645644
break
@@ -652,7 +651,7 @@ def frame_select(self):
652651
self.update_tree_config(0)
653652
self._widget.treeModeSelector.setEnabled(False)
654653
self._working_values = deepcopy(self._current_goals)
655-
self._working_name = deepcopy(self._currentName)
654+
self._working_name = deepcopy(self._current_name)
656655
self._working_duration = deepcopy(self._current_duration)
657656
self._working_pause = deepcopy(self._current_pause)
658657

@@ -663,7 +662,7 @@ def frame_select(self):
663662
self._widget.treeModeSelector.setEnabled(True)
664663
if self._current:
665664
self._current_goals = deepcopy(self._working_values)
666-
self._currentName = deepcopy(self._working_name)
665+
self._current_name = deepcopy(self._working_name)
667666
self._current_duration = deepcopy(self._working_duration)
668667
self._current_pause = deepcopy(self._working_pause)
669668

@@ -686,7 +685,7 @@ def frame_select(self):
686685

687686
self.box_ticked()
688687

689-
def motor_switcher(self):
688+
def create_motor_switcher(self):
690689
"""
691690
Loads the motors into the tree and adds the checkboxes
692691
"""
@@ -724,7 +723,7 @@ def motor_switcher(self):
724723
)
725724
self._motor_check_right_leg.setExpanded(True)
726725

727-
for k, _v in self._current_goals.items():
726+
for k in self._current_goals.keys():
728727
parent = None
729728
if "LHip" in k or "LKnee" in k or "LAnkle" in k:
730729
parent = self._motor_check_left_leg
@@ -844,7 +843,7 @@ def set_sliders_and_text_fields(self, manual):
844843
self._widget.spinBoxDuration.setValue(self._working_duration)
845844
self._widget.spinBoxPause.setValue(self._working_pause)
846845

847-
def box_ticked(self):
846+
def box_ticked(self): # TODO rename
848847
"""
849848
Controls whether a checkbox has been clicked, and reacts.
850849
"""
@@ -890,7 +889,7 @@ def update_frames(self, keep=False):
890889
"""
891890
current_index = self._widget.frameList.currentIndex()
892891
current_mode = self._widget.treeModeSelector.currentIndex()
893-
current_state = [] # TODO self._recorder.get_animation_state()
892+
current_state = self._recorder.get_animation_state()
894893
while self._widget.frameList.takeItem(0):
895894
continue
896895

@@ -912,7 +911,7 @@ def update_frames(self, keep=False):
912911
self._widget.frameList.setCurrentItem(current)
913912
self._current = True
914913

915-
def change_frame_order(self, new_order):
914+
def change_keyframe_order(self, new_order):
916915
"""Calls the recorder to update frame order and updates the gui"""
917916
self._recorder.change_frame_order(new_order)
918917
self.update_frames()

0 commit comments

Comments
 (0)