| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard, |
| * Regent L'Archeveque - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.addons.sensors.pose.impl; |
| |
| import java.io.BufferedWriter; |
| import java.io.FileWriter; |
| import java.io.IOException; |
| |
| import org.eclipse.apogy.addons.sensors.pose.ApogyAddonsSensorsPosePackage; |
| import org.eclipse.apogy.common.geometry.data3d.Pose; |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.ecore.impl.ENotificationImpl; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class CSVDataLoggerCustomImpl extends CSVDataLoggerImpl { |
| |
| private static final Logger Logger = LoggerFactory.getLogger(CSVDataLoggerImpl.class); |
| |
| private BufferedWriter csvWriter = null; |
| |
| @Override |
| public void setOutputFile(String newOutputFile) { |
| String oldOutputFile = this.outputFile; |
| this.outputFile = newOutputFile; |
| if (eNotificationRequired()) |
| eNotify(new ENotificationImpl(this, Notification.SET, |
| ApogyAddonsSensorsPosePackage.CSV_DATA_LOGGER__OUTPUT_FILE, oldOutputFile, this.outputFile)); |
| |
| if (this.csvWriter != null) { |
| try { |
| this.csvWriter.flush(); |
| this.csvWriter.close(); |
| this.csvWriter = null; |
| } catch (Exception e) { |
| Logger.error("Failed to open file <" + " for output."); |
| } |
| } |
| } |
| |
| @Override |
| public void logPose(Pose pose) throws IOException { |
| double tx = pose.getX(); |
| double ty = pose.getY(); |
| double tz = pose.getZ(); |
| |
| double rx = pose.getXRotation(); |
| double ry = pose.getYRotation(); |
| double rz = pose.getZRotation(); |
| |
| long timeStamp = System.currentTimeMillis(); |
| |
| String line = Long.toString(timeStamp) + "," + Double.toString(tx) + "," + Double.toString(ty) + "," |
| + Double.toString(tz) + ","; |
| line += Double.toString(rx) + "," + Double.toString(ry) + "," + Double.toString(rz) + "\n"; |
| |
| getCsvWriter().write(line); |
| } |
| |
| private BufferedWriter getCsvWriter() throws IOException { |
| if (this.csvWriter == null) { |
| this.csvWriter = new BufferedWriter(new FileWriter(getOutputFile())); |
| String header = "Time stam,tx,ty,tz,rx,ry,rz \n"; |
| this.csvWriter.write(header); |
| |
| Logger.info("Sucessfully openned file <" + " for output."); |
| } |
| |
| return this.csvWriter; |
| |
| } |
| |
| @Override |
| protected void finalize() throws Throwable { |
| if (this.csvWriter != null) { |
| this.csvWriter.close(); |
| } |
| } |
| |
| } // CSVDataLoggerImpl |