Using rpm->speed/power functions display distance, calories, speed and power (Not yet sending to backend,, need to confirm this is working first)

This commit is contained in:
Peter Stockings
2023-03-11 21:51:33 +11:00
parent a0210ec9a5
commit dd76d01e3d

View File

@@ -5,44 +5,84 @@
<title>Exercise Bike Display</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet" />
<script src="{{ url_for('static', filename='js/bikes.js') }}"></script>
<style>
.graph-container {
width: 100%;
height: 200px;
}
#graph {
background-color: #ffffff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body class="bg-gray-200">
<div class="mx-auto max-w-md p-6 bg-white rounded-md shadow-md md:mt-5">
<div class="mt-6">
<div class="flex items-center justify-between">
<div>
<p class="text-lg font-medium">RPM</p>
<p id="rpm-display" class="text-3xl font-bold">0</p>
</div>
<div>
<p class="text-lg font-medium">Duration</p>
<p id="duration-display" class="text-3xl font-bold">00:00</p>
</div>
</div>
<div class="mt-8">
<button id="toggle-button"
class="px-4 py-2 text-white bg-blue-500 rounded-md hover:bg-blue-600">Start</button>
</div>
<div class="mt-8">
<nav class="bg-white shadow-lg">
<div class="container mx-auto px-4">
<div class="flex justify-between">
<div class="flex items-center">
<a class="text-gray-800 text-xl font-bold" href="/">
<div class="flex py-3">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-6 h-6 mr-2">
<path stroke-linecap="round" stroke-linejoin="round"
d="M9 15L3 9m0 0l6-6M3 9h12a6 6 0 010 12h-3" />
</svg>
<div class="flex justify-center items-center bg-gray-200">
<svg id="graph" class="bg-white shadow-md"></svg>
<div>
{{ user.name }}
</div>
</div>
</a>
</div>
<div class="flex items-center">
</div>
</div>
</div>
</nav>
<div class="mx-auto max-w-md p-6 bg-white rounded-md shadow-md md:mt-5">
<div class="">
<div class="">
<div class="flex mb-4 h-12 leading-10">
<div class="w-1/3 text-lg font-medium">Time</div>
<div class="w-3/4 bg-gray-200 text-3xl font-bold pl-3 rounded-md" id="duration-display">00:00</div>
</div>
<div class="flex mb-4 h-12 leading-10">
<div class="w-1/3 text-lg font-medium">Distance</div>
<div class="w-3/4 bg-gray-200 text-3xl font-bold pl-3 rounded-md" id="distance-display">0.0</div>
</div>
<div class="flex mb-4 h-12 leading-10">
<div class="w-1/3 text-lg font-medium">Calories</div>
<div class="w-3/4 bg-gray-200 text-3xl font-bold pl-3 rounded-md" id="calories-display">0.0</div>
</div>
<div class="flex mb-4">
<div class="w-1/3 bg-gray-200 text-center mx-2 rounded-md">
<div class="flex flex-col justify-between">
<div class="pb-2 text-lg font-medium">Watts</div>
<div class="text-3xl font-bold pb-2" id="watts-display">0</div>
</div>
</div>
<div class="w-1/3 bg-gray-200 text-center mx-2 rounded-md">
<div class="flex flex-col justify-between">
<div class="pb-2 text-lg font-medium">Speed</div>
<div class="text-3xl font-bold pb-2" id="speed-display">0.0</div>
</div>
</div>
<div class="w-1/3 bg-gray-200 text-center mx-2 rounded-md">
<div class="flex flex-col justify-between">
<div class="pb-2 text-lg font-medium">RPM</div>
<div class="text-3xl font-bold pb-2" id="rpm-display">0</div>
</div>
</div>
</div>
<div class="mt-4">
<div class="flex justify-center items-center bg-gray-200">
<svg id="graph" class="bg-white shadow-xl"></svg>
</div>
</div>
<div class="mt-4 sm:w-full">
<button id="toggle-button"
class="px-4 py-2 text-white bg-blue-500 rounded-md hover:bg-blue-600 w-full">Start</button>
</div>
</div>
</div>
@@ -52,20 +92,30 @@
// Get DOM elements
const rpmDisplay = document.getElementById('rpm-display');
const durationDisplay = document.getElementById('duration-display');
const distanceDisplay = document.getElementById('distance-display');
const caloriesDisplay = document.getElementById('calories-display');
const wattsDisplay = document.getElementById('watts-display');
const speedDisplay = document.getElementById('speed-display');
const toggleButton = document.getElementById('toggle-button');
// Initialize variables
let rpm = 0;
let power = 0;
let distance = 0;
let calories = 0;
let isRunning = false;
let startTime = 0;
let intervalId = null;
let workout = [];
let previousReadingTime = null;
const integerNumber = (num) => parseInt(num);
const decimalNumber = (num) => parseFloat(num.toFixed(1));
// Update RPM and Power
function updateRpm(rpm) {
function updateBikeDisplay(distance, calories, watts, speed, rpm) {
distanceDisplay.textContent = decimalNumber(distance);
caloriesDisplay.textContent = decimalNumber(calories);
wattsDisplay.textContent = integerNumber(watts);
speedDisplay.textContent = decimalNumber(speed);
rpmDisplay.textContent = integerNumber(rpm);
}
@@ -274,7 +324,15 @@
// Add the new data point to the data array
data.push(newData);
updateRpm(rpm);
let watts = generators.aab.rpm.power(rpm)
let speed = generators.aab.rpm.speed(rpm)
if (previousReadingTime) {
distance += calculateDistance(previousReadingTime, new Date(), speed)
calories += calculateCalories(previousReadingTime, new Date(), watts)
}
previousReadingTime = new Date();
updateBikeDisplay(distance, calories, watts, speed, rpm);
updateGraph();
workout.push({ rpm, timestamp: new Date() })
@@ -302,6 +360,30 @@
return rpm;
}
function calculateDistance(startDate, endDate, speed) {
// Convert speed from km/h to km/ms
const speedPerMs = speed / (60 * 60 * 1000);
// Calculate the time difference between the start and end dates in milliseconds
const timeDiffMs = endDate.getTime() - startDate.getTime();
// Calculate the distance traveled based on speed and time difference
const distance = speedPerMs * timeDiffMs;
return distance;
}
function calculateCalories(startDate, endDate, power) {
// Calculate the time difference between the start and end dates in seconds
const timeDiffSec = (endDate.getTime() - startDate.getTime()) / 1000;
// Calculate the energy(joules) expended based on power and time difference
const energy = powerPerSec * timeDiffSec;
//Convert joules to calories
return energy * 0.238902957619;
}
</script>
</body>