Skip to content

Commit

Permalink
bitcraze#349 Added functionality to save/load anchor positions to file
Browse files Browse the repository at this point in the history
  • Loading branch information
krichardsson committed Sep 4, 2018
1 parent 7528617 commit abf07bc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ The Crazyflie PC client has the following dependencies:
* PyQtGraph
* ZMQ
* appdirs
* PyYAML
### Setting udev permissions
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def relative(lst, base=''):
},

install_requires=platform_requires + ['cflib>=0.1.6', 'appdirs>=1.4.0',
'pyzmq', 'pyqtgraph>=0.10'],
'pyzmq', 'pyqtgraph>=0.10', 'PyYAML'],

# List of dev and qt dependencies
# You can install them by running
Expand Down
40 changes: 34 additions & 6 deletions src/cfclient/ui/dialogs/anchor_position_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
from PyQt5 import uic
from PyQt5.QtCore import QAbstractTableModel, QVariant, Qt
from PyQt5.QtGui import QBrush, QColor
from PyQt5.QtWidgets import QInputDialog
from PyQt5.QtWidgets import QInputDialog, QFileDialog
import yaml

__author__ = 'Bitcraze AB'
__all__ = ['AnchorPositionDialog']
Expand Down Expand Up @@ -110,13 +111,14 @@ def add_anchor(self, anchor_id, x=0.0, y=0.0, z=0.0):
self._anchor_positions.sort(key=lambda row: row[1])
self.layoutChanged.emit()

def replaceAnchorsFromLatestKnownPositions(self):
self.layoutAboutToBeChanged.emit()
def replace_anchors_from_latest_known_positions(self):
self.replace_anchor_positions(self._latest_known_anchor_positions)

def replace_anchor_positions(self, anchor_positions):
self.layoutAboutToBeChanged.emit()
self._anchor_positions = []
for id, position in self._latest_known_anchor_positions.items():
for id, position in anchor_positions.items():
self.add_anchor(id, x=position[0], y=position[1], z=position[2])

self.layoutChanged.emit()

def get_anchor_postions(self):
Expand Down Expand Up @@ -186,6 +188,10 @@ def __init__(self, lps_tab, *args):
self._write_to_anchors_button.clicked.connect(
self._write_to_anchors_button_clicked)
self._close_button.clicked.connect(self.close)
self._load_button.clicked.connect(
self._load_button_clicked)
self._save_button.clicked.connect(
self._save_button_clicked)

def _add_anchor_button_clicked(self):
anchor_id, ok = QInputDialog.getInt(
Expand All @@ -194,11 +200,33 @@ def _add_anchor_button_clicked(self):
self._data_model.add_anchor(anchor_id)

def _get_from_anchors_button_clicked(self):
self._data_model.replaceAnchorsFromLatestKnownPositions()
self._data_model.replace_anchors_from_latest_known_positions()

def _write_to_anchors_button_clicked(self):
anchor_positions = self._data_model.get_anchor_postions()
self._lps_tab.write_positions_to_anchors(anchor_positions)

def anchor_postions_updated(self, anchor_positions):
self._data_model.anchor_postions_updated(anchor_positions)

def _load_button_clicked(self):
names = QFileDialog.getOpenFileName(self, 'Open file')
f = open(names[0], 'r')
with f:
data = yaml.load(f)

anchor_positions = {}
for id, pos in data.items():
anchor_positions[id] = (pos['x'], pos['y'], pos['z'])
self._data_model.replace_anchor_positions(anchor_positions)

def _save_button_clicked(self):
anchor_positions = self._data_model.get_anchor_postions()
data = {}
for id, pos in anchor_positions.items():
data[id] = {'x': pos[0], 'y': pos[1], 'z': pos[2]}

names = QFileDialog.getSaveFileName(self, 'Save file')
f = open(names[0], 'w')
with f:
yaml.dump(data, f)
38 changes: 38 additions & 0 deletions src/cfclient/ui/dialogs/anchor_position_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,44 @@
<item>
<widget class="QTableView" name="_table_view"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QPushButton" name="_load_button">
<property name="text">
<string>Load from file...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="_save_button">
<property name="text">
<string>Save to file...</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
Expand Down

0 comments on commit abf07bc

Please sign in to comment.