def restore_clicked(self): progress = self.create_progress_bar('Factory Calibration') uid = self.parent.parent.uid progress.setLabelText('Downloading factory calibration') progress.setMaximum(0) progress.setValue(0) progress.show() try: imu_calibration_text = '# This is the factory calibration\n\n' response = urllib2.urlopen(IMU_CALIBRATION_URL + '{0}.txt'.format(uid)) chunk = response.read(1024) while len(chunk) > 0: imu_calibration_text += chunk chunk = response.read(1024) response.close() except urllib2.HTTPError as e: if e.code == 404: progress.cancel() self.popup_ok('Factory Calibration', 'No factory calibration available') return else: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return except urllib2.URLError: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return progress.cancel() self.text_edit.setPlainText(imu_calibration_text) try: parsed = parse_imu_calibration(imu_calibration_text) except: self.popup_fail('Factory Calibration', 'Factory calibration is malformed, please report to [email protected]') return try: for value in parsed: self.imu.set_calibration(value[0], value[1]) except: self.popup_fail('Factory Calibration', 'Could not apply calibration') return self.parent.refresh_values() self.popup_ok('Factory Calibration', 'Successfully restored factory calibration')
def import_clicked(self): text = self.text_edit.toPlainText() try: for value in parse_imu_calibration(text): self.imu.set_calibration(value[0], value[1]) except: message = """Could not parse data, please check the syntax: Each line starts with "calibration type:" followed by the x, y and z calibration, separated by a comma. Multiplier and Divider are written as "mul/div" """ self.popup_fail('Calibration Import', message) return self.parent.refresh_values() self.popup_ok('Calibration Import', 'Successfully imported calibration')
def import_clicked(self): text = self.text_edit.toPlainText() try: for value in parse_imu_calibration(text): self.imu.set_calibration(value[0], value[1]) except: message = """Could not parse data, please check the syntax: Each line starts with "calibration type:" followed by the x, y and z calibration, separated by a comma. Multiplier and Divider are written as "mul/div" """ self.popup_fail('Calibration Import', message) return self.parent.refresh_values() self.popup_ok('Calibration Import', 'Successfully imported calibration')
def firmware_save_clicked(self): port_name = self.combo_serial_port.itemData(self.combo_serial_port.currentIndex()) try: samba = SAMBA(port_name) except SAMBAException as e: self.refresh_serial_ports() self.popup_fail('Brick', 'Could not connect to Brick: {0}'.format(str(e))) return except SerialException as e: self.refresh_serial_ports() self.popup_fail('Brick', str(e)[0].upper() + str(e)[1:]) return except: self.refresh_serial_ports() self.popup_fail('Brick', 'Could not connect to Brick') return progress = ProgressWrapper(self.create_progress_bar('Flashing')) samba.progress = progress current_text = self.combo_firmware.currentText() # Get firmware name = None version = None if current_text == SELECT: return elif current_text == CUSTOM: firmware_file_name = self.edit_custom_firmware.text() try: with open(firmware_file_name, 'rb') as f: firmware = f.read() except IOError: progress.cancel() self.popup_fail('Brick', 'Could not read firmware file') return else: url_part = self.combo_firmware.itemData(self.combo_firmware.currentIndex()) name = self.firmware_infos[url_part].name version = self.firmware_infos[url_part].firmware_version_latest progress.reset('Downloading {0} Brick firmware {1}.{2}.{3}'.format(name, *version), 0) response = None try: response = urllib2.urlopen(FIRMWARE_URL + 'bricks/{0}/brick_{0}_firmware_{1}_{2}_{3}.bin'.format(url_part, *version), timeout=10) except urllib2.URLError: pass beta = 5 while response is None and beta > 0: try: response = urllib2.urlopen(FIRMWARE_URL + 'bricks/{0}/brick_{0}_firmware_{2}_{3}_{4}_beta{1}.bin'.format(url_part, beta, *version), timeout=10) except urllib2.URLError: beta -= 1 if response is None: progress.cancel() self.popup_fail('Brick', 'Could not download {0} Brick firmware {1}.{2}.{3}'.format(name, *version)) return try: length = int(response.headers['Content-Length']) progress.setMaximum(length) progress.update(0) QApplication.processEvents() firmware = '' chunk = response.read(1024) while len(chunk) > 0: firmware += chunk progress.update(len(firmware)) chunk = response.read(1024) response.close() except urllib2.URLError: progress.cancel() self.popup_fail('Brick', 'Could not download {0} Brick firmware {1}.{2}.{3}'.format(name, *version)) return # Get IMU UID imu_uid = None imu_calibration = None lock_imu_calibration_pages = False if name == 'IMU': # IMU 1.0.9 and earlier have a bug in their flash locking that makes # them unlook the wrong pages. Therefore, the calibration pages # must not be locked for this versions if version[1] > 0 or (version[1] == 0 and version[2] > 9): lock_imu_calibration_pages = True try: imu_uid = base58encode(uid64_to_uid32(samba.read_uid64())) except SerialException as e: progress.cancel() self.popup_fail('Brick', 'Could read UID of IMU Brick: {0}'.format(str(e))) return except: progress.cancel() self.popup_fail('Brick', 'Could read UID of IMU Brick') return result = QMessageBox.question(self, 'IMU Brick', 'Restore factory calibration for IMU Brick [{0}] from tinkerforge.com?'.format(imu_uid), QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) # Download IMU calibration if result == QMessageBox.Yes: progress.reset('Downloading factory calibration for IMU Brick', 0) try: imu_calibration_text = '' response = urllib2.urlopen(IMU_CALIBRATION_URL + '{0}.txt'.format(imu_uid), timeout=10) chunk = response.read(1024) while len(chunk) > 0: imu_calibration_text += chunk chunk = response.read(1024) response.close() except urllib2.HTTPError as e: if e.code == 404: imu_calibration_text = None self.popup_ok('IMU Brick', 'No factory calibration for IMU Brick [{0}] available'.format(imu_uid)) else: progress.cancel() self.popup_fail('IMU Brick', 'Could not download factory calibration for IMU Brick [{0}]'.format(imu_uid)) return except urllib2.URLError: progress.cancel() self.popup_fail('IMU Brick', 'Could not download factory calibration for IMU Brick [{0}]'.format(imu_uid)) return if imu_calibration_text is not None: if len(imu_calibration_text) == 0: progress.cancel() self.popup_fail('IMU Brick', 'Could not download factory calibration for IMU Brick [{0}]'.format(imu_uid)) return try: imu_calibration_matrix = parse_imu_calibration(imu_calibration_text) # Ensure proper temperature relation if imu_calibration_matrix[5][1][7] <= imu_calibration_matrix[5][1][3]: imu_calibration_matrix[5][1][7] = imu_calibration_matrix[5][1][3] + 1 imu_calibration_array = imu_calibration_matrix[0][1][:6] + \ imu_calibration_matrix[1][1][:3] + \ imu_calibration_matrix[2][1][:6] + \ imu_calibration_matrix[3][1][:3] + \ imu_calibration_matrix[4][1][:6] + \ imu_calibration_matrix[5][1][:8] imu_calibration = struct.pack('<32h', *imu_calibration_array) except: progress.cancel() self.popup_fail('IMU Brick', 'Could not parse factory calibration for IMU Brick [{0}]'.format(imu_uid)) return # Flash firmware def report_result(reboot_okay): if current_text == CUSTOM: if reboot_okay: message = 'Successfully restarted Brick!' else: message = 'Manual restart of Brick required!' else: if reboot_okay: message = 'Successfully restarted {0} Brick!'.format(name) else: message = 'Manual restart of {0} Brick required!'.format(name) if current_text == CUSTOM: self.popup_ok('Brick', 'Successfully flashed firmware.\n' + message) elif imu_calibration is not None: self.popup_ok('Brick', 'Successfully flashed {0} Brick firmware {1}.{2}.{3}.\n'.format(name, *version) + 'Successfully restored factory calibration.\n' + message) else: self.popup_ok('Brick', 'Successfully flashed {0} Brick firmware {1}.{2}.{3}.\n'.format(name, *version) + message) try: samba.flash(firmware, imu_calibration, lock_imu_calibration_pages) # close serial device before showing dialog, otherwise exchanging # the brick while the dialog is open will force it to show up as ttyACM1 samba = None progress.cancel() report_result(True) except SAMBARebootError as e: samba = None progress.cancel() self.refresh_serial_ports() report_result(False) except SAMBAException as e: samba = None progress.cancel() self.refresh_serial_ports() self.popup_fail('Brick', 'Could not flash Brick: {0}'.format(str(e))) except SerialException as e: samba = None progress.cancel() self.refresh_serial_ports() self.popup_fail('Brick', 'Could not flash Brick: {0}'.format(str(e))) except: samba = None progress.cancel() self.refresh_serial_ports() self.popup_fail('Brick', 'Could not flash Brick')
def restore_clicked(self): progress = self.create_progress_bar('Factory Calibration') uid = self.parent.parent.uid progress.setLabelText('Downloading factory calibration') progress.setMaximum(0) progress.setValue(0) progress.show() try: imu_calibration_text = '# This is the factory calibration\n\n' response = urllib2.urlopen(IMU_CALIBRATION_URL + '{0}.txt'.format(uid)) chunk = response.read(1024) while len(chunk) > 0: imu_calibration_text += chunk chunk = response.read(1024) response.close() except urllib2.HTTPError as e: if e.code == 404: progress.cancel() self.popup_ok('Factory Calibration', 'No factory calibration available') return else: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return except urllib2.URLError: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return progress.cancel() self.text_edit.setPlainText(imu_calibration_text) try: parsed = parse_imu_calibration(imu_calibration_text) except: self.popup_fail( 'Factory Calibration', 'Factory calibration is malformed, please report to [email protected]' ) return try: for value in parsed: self.imu.set_calibration(value[0], value[1]) except: self.popup_fail('Factory Calibration', 'Could not apply calibration') return self.parent.refresh_values() self.popup_ok('Factory Calibration', 'Successfully restored factory calibration')
def restore_clicked(self): progress = self.create_progress_bar('Factory Calibration') uid = self.parent.parent.uid progress.setLabelText('Downloading factory calibration') progress.setMaximum(0) progress.setValue(0) progress.show() try: imu_calibration_text = b'# This is the factory calibration\n\n' response = urlopen(IMU_CALIBRATION_URL + '{0}.txt'.format(uid)) chunk = response.read(1024) while len(chunk) > 0: imu_calibration_text += chunk chunk = response.read(1024) response.close() except urllib.error.HTTPError as e: if e.code == 404: progress.cancel() self.popup_ok('Factory Calibration', 'No factory calibration available') return else: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return except urllib.error.URLError: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return try: imu_calibration_text = imu_calibration_text.decode('utf-8') except UnicodeDecodeError as e: self.popup_fail('Factory Calibration', ( "Factory calibration is malformed, please report this error to " + "<a href='mailto:[email protected]'>[email protected]</a>:<br/><br/> {0}" ).format(html.escape(str(e)))) return progress.cancel() self.text_edit.setPlainText(imu_calibration_text) try: parsed = parse_imu_calibration(imu_calibration_text) except (ValueError, TypeError): self.popup_fail( 'Factory Calibration', "Factory calibration is malformed, please report this error to " + "<a href='mailto:[email protected]'>[email protected]</a>" ) return try: for value in parsed: self.imu.set_calibration(value[0], value[1]) except ip_connection.Error as e: self.popup_fail('Factory Calibration', 'Could not apply calibration: ' + e.description) return self.parent.refresh_values() self.popup_ok('Factory Calibration', 'Successfully restored factory calibration')
def restore_clicked(self): progress = self.create_progress_bar('Factory Calibration') uid = self.parent.parent.uid progress.setLabelText('Downloading factory calibration') progress.setMaximum(0) progress.setValue(0) progress.show() try: imu_calibration_text = b'# This is the factory calibration\n\n' response = urllib.request.urlopen(IMU_CALIBRATION_URL + '{0}.txt'.format(uid)) chunk = response.read(1024) while len(chunk) > 0: imu_calibration_text += chunk chunk = response.read(1024) response.close() except urllib.error.HTTPError as e: if e.code == 404: progress.cancel() self.popup_ok('Factory Calibration', 'No factory calibration available') return else: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return except urllib.error.URLError: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return try: imu_calibration_text = imu_calibration_text.decode('utf-8') except UnicodeDecodeError as e: self.popup_fail('Factory Calibration', ("Factory calibration is malformed, please report this error to " + "<a href='mailto:[email protected]'>[email protected]</a>:<br/><br/> {0}") .format(html.escape(str(e)))) return progress.cancel() self.text_edit.setPlainText(imu_calibration_text) try: parsed = parse_imu_calibration(imu_calibration_text) except (ValueError, TypeError): self.popup_fail('Factory Calibration', "Factory calibration is malformed, please report this error to " + "<a href='mailto:[email protected]'>[email protected]</a>") return try: for value in parsed: self.imu.set_calibration(value[0], value[1]) except ip_connection.Error as e: self.popup_fail('Factory Calibration', 'Could not apply calibration: ' + e.description) return self.parent.refresh_values() self.popup_ok('Factory Calibration', 'Successfully restored factory calibration')
return else: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return except urllib2.URLError: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return progress.cancel() self.text_edit.setPlainText(imu_calibration_text) try: parsed = parse_imu_calibration(imu_calibration_text) except: self.popup_fail('Factory Calibration', 'Factory calibration is malformed, please report to [email protected]') return try: for value in parsed: self.imu.set_calibration(value[0], value[1]) except: self.popup_fail('Factory Calibration', text) return self.parent.refresh_values() self.popup_ok('Factory Calibration', 'Successfully restored factory calibration')
progress.cancel() self.popup_fail('IMU Brick', 'Could not download factory calibration for IMU Brick [{0}]'.format(imu_uid)) return except urllib2.URLError: progress.cancel() self.popup_fail('IMU Brick', 'Could not download factory calibration for IMU Brick [{0}]'.format(imu_uid)) return if imu_calibration_text is not None: if len(imu_calibration_text) == 0: progress.cancel() self.popup_fail('IMU Brick', 'Could not download factory calibration for IMU Brick [{0}]'.format(imu_uid)) return try: imu_calibration_matrix = parse_imu_calibration(imu_calibration_text) # Ensure proper temperature relation if imu_calibration_matrix[5][1][7] <= imu_calibration_matrix[5][1][3]: imu_calibration_matrix[5][1][7] = imu_calibration_matrix[5][1][3] + 1 imu_calibration_array = imu_calibration_matrix[0][1][:6] + \ imu_calibration_matrix[1][1][:3] + \ imu_calibration_matrix[2][1][:6] + \ imu_calibration_matrix[3][1][:3] + \ imu_calibration_matrix[4][1][:6] + \ imu_calibration_matrix[5][1][:8] imu_calibration = struct.pack('<32h', *imu_calibration_array) except: progress.cancel()