Skip to content
Snippets Groups Projects
Commit 55d503ff authored by Hailong Zhu's avatar Hailong Zhu
Browse files

updated Odometry.java

parent 34780f74
Branches
No related tags found
No related merge requests found
package ipos.project.Functionality; package ipos.project.Functionality;
import ipos.project.DataModellntegration.iPos_Datamodel.*; import ipos.project.DataModellntegration.iPos_Datamodel.*;
import org.apache.logging.log4j.LogManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -8,66 +9,98 @@ import org.springframework.stereotype.Component; ...@@ -8,66 +9,98 @@ import org.springframework.stereotype.Component;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.MissingResourceException; import java.util.MissingResourceException;
@Component @Component
public class Odometry { public class Odometry {
private final String agentId; private static org.apache.logging.log4j.Logger LOG = LogManager.getLogger();
private double accuracy; private final String agentId = "";
private double timeOfLastCalibration; private static double accuracy;
private double timeLastSensorEvent; private static double timeOfLastCalibration;
private double [] a = new double[]{0, 0, 0}; private static double timeLastSensorEvent;
private double [] aOffset; private static double [] a = new double[]{0, 0, 0};
private double [] v = new double[]{0, 0, 0}; private static double [][] a_ori = new double[][]{{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}};
private double [] p = new double[]{0, 0, 0}; private static double [] aOffset;
private double [] delta = new double[]{0, 0, 0}; private static double [] v = new double[]{0, 0, 0};
private double [] vLast = new double[]{0, 0, 0}; private static double [] p = new double[]{0, 0, 0};
private double [] pLast = new double[]{0, 0, 0}; private static double [] delta = new double[]{0, 0, 0};
private double NS2S = 1/10000.0; private static double [] vLast = new double[]{0, 0, 0};
private boolean calibrated = false; private static double [] pLast = new double[]{0, 0, 0};
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"); private static double NS2S = 1/1000.0;
private static boolean calibrated = false;
private static int index_ori = 0;
static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
public Odometry(String agentId) { //public Odometry(String agentId) {
this.agentId = agentId; // this.agentId = agentId;
//}
private static Date convertToDateViaInstant(LocalDateTime dateToConvert) {
return Date.from(dateToConvert.atZone(ZoneId.systemDefault()).toInstant());
}
private static LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert){
return dateToConvert.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
} }
public static double average(double[] data) {
double sum = 0;
double average;
public void calibrate (PositionEvent event) throws ParseException { for(int i=0; i < data.length; i++){
this.vLast[0] = 0; sum = sum + data[i];
this.vLast[1] = 0; }
this.vLast[2] = 0; average = sum/data.length;
aOffset =a; return average;
this.accuracy = 0.03; }
public static void calibrate (PositionEvent event) throws ParseException {
v = new double[] {0,0,0};
vLast = new double[] {0,0,0};
aOffset = new double[] {average(a_ori[0]),average(a_ori[1]),average(a_ori[2])};
accuracy = 0.03;
if (event.getPlacing().getPosition().getPoint() instanceof Point3D) { if (event.getPlacing().getPosition().getPoint() instanceof Point3D) {
this.pLast[0] = ((Point3D) event.getPlacing().getPosition().getPoint()).getX(); pLast[0] = ((Point3D) event.getPlacing().getPosition().getPoint()).getX();
this.pLast[1] = ((Point3D) event.getPlacing().getPosition().getPoint()).getY(); pLast[1] = ((Point3D) event.getPlacing().getPosition().getPoint()).getY();
this.pLast[2] = ((Point3D) event.getPlacing().getPosition().getPoint()).getZ(); pLast[2] = ((Point3D) event.getPlacing().getPosition().getPoint()).getZ();
p = pLast;
} }
Date date = sdf2.parse(event.getTimeStamp()); LOG.info("Odometry: timestamp" + event.getTimeStamp());
LocalDateTime eventTime = LocalDateTime.parse(event.getTimeStamp(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
Date date = convertToDateViaInstant(eventTime);
timeOfLastCalibration = date.getTime(); timeOfLastCalibration = date.getTime();
calibrated = true;
} }
public PositionEvent Update (IMU imuRawdataEvent) throws ParseException { public static PositionEvent update(IMU imuRawdataEvent) throws ParseException {
IPos_DatamodelFactory datamodelFactory = IPos_DatamodelFactory.eINSTANCE; IPos_DatamodelFactory datamodelFactory = IPos_DatamodelFactory.eINSTANCE;
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Date date = sdf2.parse(imuRawdataEvent.getTimeStamp()); Date date = sdf2.parse(imuRawdataEvent.getTimeStamp());
timeLastSensorEvent = date.getTime();
if (calibrated) { if (calibrated) {
double dt = (date.getTime() - timeLastSensorEvent) * NS2S; double dt = (date.getTime() - timeLastSensorEvent) * NS2S;
a_ori[0][index_ori] = imuRawdataEvent.getAcceleration().getX();
a_ori[1][index_ori] = imuRawdataEvent.getAcceleration().getY();
a_ori[2][index_ori] = imuRawdataEvent.getAcceleration().getZ();
index_ori += 1;
if (index_ori>9) {
index_ori =0;
}
a[0] = imuRawdataEvent.getAcceleration().getX() - aOffset[0]; a[0] = imuRawdataEvent.getAcceleration().getX() - aOffset[0];
a[1] = imuRawdataEvent.getAcceleration().getY() - aOffset[1]; a[1] = imuRawdataEvent.getAcceleration().getY() - aOffset[1];
a[2] = imuRawdataEvent.getAcceleration().getZ() - aOffset[2]; a[2] = imuRawdataEvent.getAcceleration().getZ() - aOffset[2];
for (int index = 0; index < 3; ++index) { for (int index = 0; index < 3; ++index) {
double last_velocity = vLast[index]; v[index] = a[index] * dt + vLast[index];
v[index] += (a[index] + vLast[index]) / 2 * dt; delta[index] = (v[index] + vLast[index]) / 2 * dt;
delta[index] += (v[index] + last_velocity) / 2 * dt;
p[index] = pLast[index] + delta[index]; p[index] = pLast[index] + delta[index];
pLast[index] = p[index];
vLast[index] = v[index];
} }
accuracy = (date.getTime() - timeOfLastCalibration) * 0.01; accuracy = (date.getTime() - timeOfLastCalibration + 500) * 0.0001;
timeLastSensorEvent = date.getTime();
Quaternion calOrientation = datamodelFactory.createQuaternion();
PositionEvent calPositionEvent = datamodelFactory.createPositionEvent(); PositionEvent calPositionEvent = datamodelFactory.createPositionEvent();
Placing calPlacing = datamodelFactory.createPlacing(); Placing calPlacing = datamodelFactory.createPlacing();
LocalizableObject calLObject = datamodelFactory.createLocalizableObject(); LocalizableObject calLObject = datamodelFactory.createLocalizableObject();
...@@ -78,18 +111,33 @@ public class Odometry { ...@@ -78,18 +111,33 @@ public class Odometry {
calPoint3D.setX((float) p[0]); calPoint3D.setX((float) p[0]);
calPoint3D.setY((float) p[1]); calPoint3D.setY((float) p[1]);
calPoint3D.setZ((float) p[2]); calPoint3D.setZ((float) p[2]);
calOrientation.setX(0);
calOrientation.setY(0);
calOrientation.setZ(0);
calOrientation.setW(0);
calPosition.setPoint(calPoint3D); calPosition.setPoint(calPoint3D);
calPosition.setAccuracy(calGaussian); calPosition.setAccuracy(calGaussian);
calPosition.setReferenceSystem(DataServices.getReferenceSystemByIdOrNull("ROOT"));
calPlacing.setPosition(calPosition); calPlacing.setPosition(calPosition);
calLObject.setId(imuRawdataEvent.getSensorId()); calPlacing.setOrientation(calOrientation);
calPositionEvent.setTimeStamp(imuRawdataEvent.getTimeStamp()); //calLObject.setId(imuRawdataEvent.getSensorId());
LocalDateTime timestamp_LDT=convertToLocalDateTimeViaInstant(date);
calPositionEvent.setLObjectId(imuRawdataEvent.getSensorId());
calPositionEvent.setTimeStamp(timestamp_LDT.toString() + "+00:00");
calPositionEvent.setPlacing(calPlacing); calPositionEvent.setPlacing(calPlacing);
return calPositionEvent; return calPositionEvent;
} }
else { else {
a[0] = imuRawdataEvent.getAcceleration().getX(); timeLastSensorEvent = date.getTime();
a[1] = imuRawdataEvent.getAcceleration().getY(); a_ori[0][index_ori] = imuRawdataEvent.getAcceleration().getX();
a[2] = imuRawdataEvent.getAcceleration().getZ(); a_ori[1][index_ori] = imuRawdataEvent.getAcceleration().getY();
a_ori[2][index_ori] = imuRawdataEvent.getAcceleration().getZ();
index_ori += 1;
if (index_ori>9) {
index_ori =0;
}
throw new MissingResourceException("Odometry not calibrated!", "Odometry", "calibration"); throw new MissingResourceException("Odometry not calibrated!", "Odometry", "calibration");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment