Skip to content

Commit

Permalink
documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgilbers committed Sep 27, 2024
1 parent cbac94d commit 513b772
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 55 deletions.
38 changes: 21 additions & 17 deletions js/Astar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { map } from './map.js'
*/
export default class Astar {
/**
* Create a search object
* @param {Array} graph Array of Nodes
*/
* Create a search object
* @param {Array} graph Array of Nodes
*/
constructor (graph) {
this.graph = [...graph]
for (const node of this.graph) {
Expand All @@ -22,12 +22,11 @@ export default class Astar {
}

/**
* Perform an A* Search on a graph given a start and end node.
* @param {Node} start Start Node
* @param {Number} endIndex End Node Index
*
* @returns {Array} Nodes from start to end
*/
* Perform an A* Search on a graph given a start and end node.
* @param {Node} start Start Node
* @param {Number} endIndex End Node Index
* @returns {Array} Nodes from start to end
*/
search (start, endIndex) {
const end = this.graph[endIndex]
this.#cleanDirty()
Expand Down Expand Up @@ -151,6 +150,11 @@ export default class Astar {
}
}

/**
* Get nearest node to position
* @param {LatLng} position Current position
* @returns Nearest node
*/
nearestNode (position) {
const nearestHeap = new BinaryHeap(function (node) {
return L.point(position.lat, position.lng).distanceTo(L.point(node.latlng.lat, node.latlng.lng))
Expand All @@ -163,34 +167,34 @@ export default class Astar {

getDirection () {
const points = this.polyline.getLatLngs()
// Überprüfen, ob es weniger als drei Punkte gibt
// Check if there are less than three points
if (points[0].length < 3) {
return ['sports_score', 'Ziel erreicht']
}

// Punkte extrahieren
// Extract points
const p1 = points[0][0]
const p2 = points[0][1]
const p3 = points[0][2]

// Vektoren berechnen
// Calculate vectors
const v1 = { x: p2.lng - p1.lng, y: p2.lat - p1.lat }
const v2 = { x: p3.lng - p2.lng, y: p3.lat - p2.lat }

// Längen der Vektoren berechnen
// Calculate lengths of vectors
const v1Length = Math.sqrt(v1.x * v1.x + v1.y * v1.y)
const v2Length = Math.sqrt(v2.x * v2.x + v2.y * v2.y)

// Skalarprodukt berechnen
// Calculate scalar product
const dotProduct = v1.x * v2.x + v1.y * v2.y

// Winkel berechnen (in Grad)
// Calculate angle (in degrees)
const angle = Math.acos(dotProduct / (v1Length * v2Length)) * (180 / Math.PI)

// Kreuzprodukt berechnen (um die Richtung zu bestimmen)
// Calculate cross product (to determine the direction)
const crossProduct = v1.x * v2.y - v1.y * v2.x

// Entscheiden, ob gerade, links oder rechts
// Decide whether straight, left or right
if (angle < 45) {
return ['north', 'Weiter geradeaus']
} else if (crossProduct > 0) {
Expand Down
8 changes: 4 additions & 4 deletions js/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Node extends L.marker {

/**
* Select node as start- or endnode
* @param {*} e
* @param {Event} e
*/
clickOnNode = (e) => {
checkGraphToggle() && checkAB(this)
Expand All @@ -49,7 +49,7 @@ class Node extends L.marker {

/**
* Add node to edge
* @param {*} e
* @param {Event} e
*/
clickOnEdge = (e) => {
if (checkGraphToggle()) {
Expand Down Expand Up @@ -92,7 +92,7 @@ function checkAB (node) {

/**
* Create new node and check for start or end
* @param {*} e
* @param {Event} e
*/
export function clickOnMap (e) {
checkGraphToggle() && checkAB(new Node(e.latlng))
Expand Down Expand Up @@ -138,7 +138,7 @@ let textFile = null

/**
* Create textfile for download
* @param {*} text
* @param {String} text
* @returns {String} URL you can use as a href
*/
export function makeTextFile (text) {
Expand Down
73 changes: 51 additions & 22 deletions js/Position.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,61 @@ const { KalmanFilter } = kalmanFilter
const magnitudeArrayLength = 5
const position = { lat: 0, lng: 0 }
const magnitudeArray = []
const stepLength = 0.7 // Schrittweite in Metern
const stepLength = 0.7 // Step size in metres
const stepThreshold = 2.3

/**
* Konvertiere Winkel in Radiant
* @param {*} degrees Winkel in Grad
* @returns Radiant
* Convert angle to radian
* @param {*} degrees Angle in degrees
* @returns Radian
*/
function toRadians (degrees) {
return degrees * (Math.PI / 180)
}

/**
* Convert relative acceleration direction to global acceleration direction
* @param {Number} x
* @param {Number} y
* @param {Number} z
* @param {Number} yaw
* @param {Number} pitch
* @param {Number} roll
* @returns Global acceleration matrix
*/
function rotateAcceleration (x, y, z, yaw, pitch, roll) {
const theta = toRadians(yaw)
const beta = toRadians(pitch)
const gamma = toRadians(roll)

// Rotationsmatrix Yaw
// Rotation matrix Yaw
const rYaw = [
[Math.cos(theta), -Math.sin(theta), 0],
[Math.sin(theta), Math.cos(theta), 0],
[0, 0, 1]
]

// Rotationsmatrix Pitch
// Rotation matrix Pitch
const rPitch = [
[1, 0, 0],
[0, Math.cos(beta), -Math.sin(beta)],
[0, Math.sin(beta), Math.cos(beta)]
]

// Rotationsmatrix Roll
// Rotation matrix Roll
const rRoll = [
[Math.cos(gamma), 0, Math.sin(gamma)],
[0, 1, 0],
[-Math.sin(gamma), 0, Math.cos(gamma)]
]

// Beschleunigungsvektor [x, y, z]
// Acceleration vector [x, y, z]
const accel = [x, y, z]

// Multipliziere die Rotationsmatrizen
// Multiply the rotation matrices
const R = multiplyMatrices(multiplyMatrices(rYaw, rPitch), rRoll)

// Beschleunigung in globale Koordinaten umrechnen
// Convert acceleration to global coordinates
return multiplyMatrixAndVector(R, accel)
}

Expand Down Expand Up @@ -80,6 +90,11 @@ function multiplyMatrixAndVector (matrix, vector) {
return result
}

/**
* Apply Kalman Filter
* @param {Number[]} arr Array of numbers
* @returns Filtered Array of numbers
*/
export function kFilter (arr) {
const kFilter = new KalmanFilter({
observation: {
Expand All @@ -89,51 +104,65 @@ export function kFilter (arr) {
},
dynamic: {
name: 'constant-speed'
// covariance: [0.1, 0.5]
}
})
const res = kFilter.filterAll(arr)
return res
return kFilter.filterAll(arr)
}

/**
* Check whether there is a peak in the middle of the last three values
* @param {Number[]} data Array of numbers
* @returns true if there is a peak
*/
export function detectPeak (data) {
const len = data.length
if (len < 3) return false

const last = data[len - 1]
const beforeLast = data[len - 2]
const twoBeforeLast = data[len - 3]
const after = data[len - 1]
const middle = data[len - 2]
const before = data[len - 3]

// Prüfe, ob es einen Peak gibt (der mittlere Wert ist größer als die umliegenden)
return (beforeLast > last && beforeLast > twoBeforeLast && beforeLast > stepThreshold)
return (middle > after && middle > before && middle > stepThreshold)
}

/**
* Calculate new position of user if a step has been detected
* @param {Number[]} motionArray Array of motion data
* @param {LatLng} userPosition Current user position
* @param {Number} bias Rotation bias of map
* @returns Updated userposition
*/
export function calculatePosition (motionArray, userPosition, bias) {
const rotationBias = 360 - bias
position.lat = userPosition.lat
position.lng = userPosition.lng
const rotationBias = 360 - bias
const filteredArrays = kFilter(motionArray)

const lastIndex = filteredArrays.length - 1
const [xFiltered, yFiltered, zFiltered] = filteredArrays[lastIndex].slice(0, 3)

const lastOrientation = motionArray[lastIndex][3] + rotationBias
// Calculation of magnitude
const magnitude = Math.sqrt(xFiltered ** 2 + yFiltered ** 2 + zFiltered ** 2)
if (magnitudeArray.length >= magnitudeArrayLength) {
magnitudeArray.shift()
}
magnitudeArray.push(magnitude)

// Update position if step is detected
if (detectPeak(magnitudeArray)) {
return updatePosition(lastOrientation)
}
}

/**
* Update user position based on current orientation
* @param {Number} lastOrientation Last orientation of user
* @returns New position of user
*/
function updatePosition (lastOrientation) {
const directionRad = toRadians(lastOrientation)
const cosDirection = Math.cos(directionRad)
const sinDirection = Math.sin(directionRad)

// Calculate new position
position.lat += stepLength * cosDirection
position.lng += stepLength * sinDirection

Expand Down
1 change: 0 additions & 1 deletion js/Products.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const fuse = new Fuse(products, {

/**
* Load JSON data of products
*
* @returns {Array} Products
*/
async function loadProducts () {
Expand Down
Loading

0 comments on commit 513b772

Please sign in to comment.