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,304 +5,386 @@
<title>Exercise Bike Display</title> <title>Exercise Bike Display</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet" /> <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> </head>
<body class="bg-gray-200"> <body class="bg-gray-200">
<div class="mx-auto max-w-md p-6 bg-white rounded-md shadow-md md:mt-5"> <nav class="bg-white shadow-lg">
<div class="mt-6"> <div class="container mx-auto px-4">
<div class="flex items-center justify-between"> <div class="flex justify-between">
<div> <div class="flex items-center">
<p class="text-lg font-medium">RPM</p> <a class="text-gray-800 text-xl font-bold" href="/">
<p id="rpm-display" class="text-3xl font-bold">0</p> <div class="flex py-3">
</div> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
<div> stroke="currentColor" class="w-6 h-6 mr-2">
<p class="text-lg font-medium">Duration</p> <path stroke-linecap="round" stroke-linejoin="round"
<p id="duration-display" class="text-3xl font-bold">00:00</p> d="M9 15L3 9m0 0l6-6M3 9h12a6 6 0 010 12h-3" />
</div> </svg>
</div>
<div class="mt-8"> <div>
<button id="toggle-button" {{ user.name }}
class="px-4 py-2 text-white bg-blue-500 rounded-md hover:bg-blue-600">Start</button> </div>
</div> </div>
<div class="mt-8"> </a>
</div>
<div class="flex items-center">
<div class="flex justify-center items-center bg-gray-200">
<svg id="graph" class="bg-white shadow-md"></svg>
</div> </div>
</div> </div>
</div> </div>
</div> </nav>
<script src="https://d3js.org/d3.v7.min.js"></script> <div class="mx-auto max-w-md p-6 bg-white rounded-md shadow-md md:mt-5">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script> <div class="">
<script>
// Get DOM elements
const rpmDisplay = document.getElementById('rpm-display');
const durationDisplay = document.getElementById('duration-display');
const toggleButton = document.getElementById('toggle-button');
// Initialize variables
let rpm = 0;
let power = 0;
let isRunning = false;
let startTime = 0;
let intervalId = null;
let workout = [];
const integerNumber = (num) => parseInt(num); <div class="">
const decimalNumber = (num) => parseFloat(num.toFixed(1)); <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>
// Update RPM and Power <div class="flex mb-4">
function updateRpm(rpm) { <div class="w-1/3 bg-gray-200 text-center mx-2 rounded-md">
rpmDisplay.textContent = integerNumber(rpm); <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>
// Update Duration <div class="mt-4">
function updateDuration() { <div class="flex justify-center items-center bg-gray-200">
const now = new Date(); <svg id="graph" class="bg-white shadow-xl"></svg>
const diff = Math.abs(now - startTime) / 1000; // get difference in seconds </div>
let hours = Math.floor(diff / (60 * 60)); </div>
let minutes = Math.floor(diff / 60) % 60;
let seconds = Math.floor(diff);
let timeSegments = []; <div class="mt-4 sm:w-full">
if (hours > 0) { <button id="toggle-button"
timeSegments.push(hours); class="px-4 py-2 text-white bg-blue-500 rounded-md hover:bg-blue-600 w-full">Start</button>
</div>
</div>
</div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
// 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 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));
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);
} }
timeSegments.push(minutes.toString().padStart(2, '0'));
timeSegments.push(seconds.toString().padStart(2, '0'));
durationDisplay.textContent = timeSegments.join(':'); // format as "1:32" // Update Duration
} function updateDuration() {
const now = new Date();
const diff = Math.abs(now - startTime) / 1000; // get difference in seconds
let hours = Math.floor(diff / (60 * 60));
let minutes = Math.floor(diff / 60) % 60;
let seconds = Math.floor(diff);
// Start workout let timeSegments = [];
function startWorkout() { if (hours > 0) {
// Connect to cadence sensor timeSegments.push(hours);
connect({ }
onChange: parseCSC, timeSegments.push(minutes.toString().padStart(2, '0'));
}).then(() => { timeSegments.push(seconds.toString().padStart(2, '0'));
isRunning = true;
startTime = Date.now();
toggleButton.textContent = 'Stop';
intervalId = setInterval(() => {
updateDuration();
}, 1000);
})
.catch((err) => {
swal("Oops", err.message, "error");
});
}
// Stop workout durationDisplay.textContent = timeSegments.join(':'); // format as "1:32"
function stopWorkout() { }
isRunning = false;
clearInterval(intervalId);
toggleButton.textContent = 'Start';
disconnect();
fetch("{{ url_for('workouts', user_id=user.id) }}", { // Start workout
method: "POST", function startWorkout() {
headers: { // Connect to cadence sensor
Accept: "application/json", connect({
"Content-Type": "application/json", onChange: parseCSC,
}, }).then(() => {
body: JSON.stringify({ workout: workout }), isRunning = true;
}).then(res => res.json()) startTime = Date.now();
.then(res => { toggleButton.textContent = 'Stop';
workout = []; intervalId = setInterval(() => {
swal("Submitted", JSON.stringify(res), "success").then(isConfirm => window.location.href = '/'); updateDuration();
}, 1000);
}) })
.catch(err => swal("Failed to submit workout", err.message, "error")); .catch((err) => {
swal("Oops", err.message, "error");
} });
// Toggle workout
function toggleWorkout() {
if (isRunning) {
stopWorkout();
} else {
startWorkout();
} }
}
// Attach event listener to toggle button // Stop workout
toggleButton.addEventListener('click', toggleWorkout); function stopWorkout() {
isRunning = false;
clearInterval(intervalId);
toggleButton.textContent = 'Start';
disconnect();
// Set up the live plot fetch("{{ url_for('workouts', user_id=user.id) }}", {
const svg = d3.select('#graph'); method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ workout: workout }),
}).then(res => res.json())
.then(res => {
workout = [];
swal("Submitted", JSON.stringify(res), "success").then(isConfirm => window.location.href = '/');
})
.catch(err => swal("Failed to submit workout", err.message, "error"));
// Set the SVG viewBox and dimensions }
svg.attr('viewBox', '0 0 600 400');
svg.attr('width', '100%');
svg.attr('height', '100%');
// Set up the x-axis and y-axis scales // Toggle workout
const xScale = d3.scaleLinear().range([50, 550]); function toggleWorkout() {
const yScale = d3.scaleLinear().range([350, 50]); if (isRunning) {
stopWorkout();
// Set up the x-axis and y-axis } else {
const xAxis = d3.axisBottom(xScale).tickSize(0).tickFormat(''); startWorkout();
const yAxis = d3.axisLeft(yScale).tickSize(0).tickFormat('');
// Add the x-axis and y-axis to the SVG
svg.append('g')
.attr('transform', 'translate(0, 350)')
.call(xAxis);
svg.append('g')
.attr('transform', 'translate(50, 0)')
.call(yAxis);
// Set up the line function
const line = d3.line()
.x((d) => xScale(d.x))
.y((d) => yScale(d.y))
.curve(d3.curveCardinal.tension(0.25));
// Create an empty data array
let data = [];
// Set up the graph update function
const updateGraph = () => {
// Update the x-axis and y-axis scales
xScale.domain([0, data.length]);
yScale.domain([0, d3.max(data, d => d.y)]);
// Remove the old line path from the SVG
svg.selectAll('path').remove();
// Add a new line path to the SVG with the updated data
svg.append('path')
.datum(data)
.attr('d', line)
.attr('fill', 'none')
.attr('stroke', 'blue')
.attr('stroke-width', '2');
};
// Call the updateGraph function to start the live updates
updateGraph();
// BLE code
let characteristic = null;
let prevRes = null;
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function connect(props) {
const device = await navigator.bluetooth.requestDevice({
filters: [{ services: ["cycling_speed_and_cadence"] }],
acceptAllDevices: false,
});
console.log(`%c\n👩🏼‍⚕️`, "font-size: 82px;", "Starting CSC...\n\n");
const server = await device.gatt.connect();
await delay(500);
console.log("Getting Service...");
const service = await server.getPrimaryService("cycling_speed_and_cadence");
console.log("Getting Characteristic...");
characteristic = await service.getCharacteristic("csc_measurement");
await characteristic.startNotifications();
console.log("> Notifications started");
characteristic.addEventListener("characteristicvaluechanged", props.onChange);
console.log("> Characteristic value changed event listener added");
}
async function disconnect() {
if (characteristic) {
try {
await characteristic.stopNotifications();
log("> Notifications stopped");
characteristic.removeEventListener(
"characteristicvaluechanged",
handleNotifications
);
} catch (error) {
console.log("Argh! " + error);
swal("Oops", error, "error");
} }
} }
}
function parseCSC(e) { // Attach event listener to toggle button
const value = e.target.value; toggleButton.addEventListener('click', toggleWorkout);
const flags = value.getUint8(0, true);
const hasWheel = !!(flags & 0x01); // Set up the live plot
const hasCrank = !!(flags & 0x02); const svg = d3.select('#graph');
let index = 1;
const res = { // Set the SVG viewBox and dimensions
wheelRevs: null, svg.attr('viewBox', '0 0 600 400');
wheelTime: null, svg.attr('width', '100%');
crankRevs: null, svg.attr('height', '100%');
crankTime: null,
// Set up the x-axis and y-axis scales
const xScale = d3.scaleLinear().range([50, 550]);
const yScale = d3.scaleLinear().range([350, 50]);
// Set up the x-axis and y-axis
const xAxis = d3.axisBottom(xScale).tickSize(0).tickFormat('');
const yAxis = d3.axisLeft(yScale).tickSize(0).tickFormat('');
// Add the x-axis and y-axis to the SVG
svg.append('g')
.attr('transform', 'translate(0, 350)')
.call(xAxis);
svg.append('g')
.attr('transform', 'translate(50, 0)')
.call(yAxis);
// Set up the line function
const line = d3.line()
.x((d) => xScale(d.x))
.y((d) => yScale(d.y))
.curve(d3.curveCardinal.tension(0.25));
// Create an empty data array
let data = [];
// Set up the graph update function
const updateGraph = () => {
// Update the x-axis and y-axis scales
xScale.domain([0, data.length]);
yScale.domain([0, d3.max(data, d => d.y)]);
// Remove the old line path from the SVG
svg.selectAll('path').remove();
// Add a new line path to the SVG with the updated data
svg.append('path')
.datum(data)
.attr('d', line)
.attr('fill', 'none')
.attr('stroke', 'blue')
.attr('stroke-width', '2');
}; };
if (hasWheel) {
res.wheelRevs = value.getUint32(index, true); // Call the updateGraph function to start the live updates
index += 4; updateGraph();
res.wheelTime = value.getUint16(index, true);
index += 2;
// BLE code
let characteristic = null;
let prevRes = null;
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
} }
if (hasCrank) {
res.crankRevs = value.getUint16(index, true); async function connect(props) {
index += 2; const device = await navigator.bluetooth.requestDevice({
res.crankTime = value.getUint16(index, true); filters: [{ services: ["cycling_speed_and_cadence"] }],
index += 2; acceptAllDevices: false,
});
console.log(`%c\n👩🏼‍⚕️`, "font-size: 82px;", "Starting CSC...\n\n");
const server = await device.gatt.connect();
await delay(500);
console.log("Getting Service...");
const service = await server.getPrimaryService("cycling_speed_and_cadence");
console.log("Getting Characteristic...");
characteristic = await service.getCharacteristic("csc_measurement");
await characteristic.startNotifications();
console.log("> Notifications started");
characteristic.addEventListener("characteristicvaluechanged", props.onChange);
console.log("> Characteristic value changed event listener added");
} }
console.log("CSC", res);
if (prevRes) {
let rpm = revsToRPM(prevRes, res);
if (rpm > 0) {
const newData = {
x: data.length,
y: decimalNumber(rpm),
};
// Add the new data point to the data array async function disconnect() {
data.push(newData); if (characteristic) {
try {
updateRpm(rpm); await characteristic.stopNotifications();
updateGraph(); log("> Notifications stopped");
characteristic.removeEventListener(
workout.push({ rpm, timestamp: new Date() }) "characteristicvaluechanged",
handleNotifications
);
} catch (error) {
console.log("Argh! " + error);
swal("Oops", error, "error");
}
} }
} }
prevRes = res;
return res;
}
function revsToRPM(prevRes, res) { function parseCSC(e) {
const deltaRevs = res.crankRevs - prevRes.crankRevs; const value = e.target.value;
if (deltaRevs === 0) { const flags = value.getUint8(0, true);
// no rotation const hasWheel = !!(flags & 0x01);
return 0; const hasCrank = !!(flags & 0x02);
let index = 1;
const res = {
wheelRevs: null,
wheelTime: null,
crankRevs: null,
crankTime: null,
};
if (hasWheel) {
res.wheelRevs = value.getUint32(index, true);
index += 4;
res.wheelTime = value.getUint16(index, true);
index += 2;
}
if (hasCrank) {
res.crankRevs = value.getUint16(index, true);
index += 2;
res.crankTime = value.getUint16(index, true);
index += 2;
}
console.log("CSC", res);
if (prevRes) {
let rpm = revsToRPM(prevRes, res);
if (rpm > 0) {
const newData = {
x: data.length,
y: decimalNumber(rpm),
};
// Add the new data point to the data array
data.push(newData);
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() })
}
}
prevRes = res;
return res;
} }
let deltaTime = (res.crankTime - prevRes.crankTime) / 1024; function revsToRPM(prevRes, res) {
if (deltaTime < 0) { const deltaRevs = res.crankRevs - prevRes.crankRevs;
// time counter wraparound if (deltaRevs === 0) {
deltaTime += Math.pow(2, 16) / 1024; // no rotation
return 0;
}
let deltaTime = (res.crankTime - prevRes.crankTime) / 1024;
if (deltaTime < 0) {
// time counter wraparound
deltaTime += Math.pow(2, 16) / 1024;
}
deltaTime /= 60; // seconds to minutes
const rpm = deltaRevs / deltaTime;
return rpm;
} }
deltaTime /= 60; // seconds to minutes
const rpm = deltaRevs / deltaTime; function calculateDistance(startDate, endDate, speed) {
return rpm; // Convert speed from km/h to km/ms
} const speedPerMs = speed / (60 * 60 * 1000);
</script> // 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> </body>