Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quickfix #39

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
import asyncio
from target_zone_estimation import handle_data_streaming

app = FastAPI()
connectedClients = set()
Expand All @@ -15,10 +16,11 @@ async def websocket_endpoint(websocket: WebSocket):

while True:
try:
data = await websocket.receive_text()
print(f"Received from client: {data}")
for client in connectedClients:
await client.send_text(f"Message Received: {data}")
await handle_data_streaming(websocket)
# data = await websocket.receive_text()
# print(f"Received from client: {data}")
# for client in connectedClients:
# await client.send_text(f"Message Received: {data}")
except WebSocketDisconnect:
print("Client disconnected")
break
Expand All @@ -34,5 +36,4 @@ async def websocket_endpoint(websocket: WebSocket):
finally:
connectedClients.remove(websocket)
await websocket.close()
print("Connection closed")

print("Connection closed")
48 changes: 28 additions & 20 deletions backend/target_zone_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,45 @@
import asyncio
from Step_Time_Calculation import calculate_step_time


real_force_data = [
(0.00000, 159.548187),
(0.00093, 159.304047),
(0.01296, 158.327484),
(0.01620, 158.327484),
(0.05370, 157.106781),
(0.05972, 156.862640)
(0.01296, 10.327484),
(0.01620, 159.327484),
(0.02000, 5.106781),
(0.03000, 158.862640),
(0.04000, 4.862640),
(0.05000, 160.000000),
(0.06000, 6.000000),
]

threshold = 20.0

async def handle_data_streaming(websocket):
"""Handle data streaming from sample data to the WebSocket."""
"""Handle data streaming from sample data and print it for testing."""
accumulated_data = [] # To accumulate force data over time

for force_data in real_force_data:
try:
# Check if all force values are below the threshold
if force_data[1] < threshold:
step_times = [0.0]
else:
step_times = calculate_step_time([force_data], threshold)
print(f"Force Data: {force_data}") # Debug print to inspect the input data

# Accumulate force data over time
accumulated_data.append(force_data)

# Only process once we have multiple data points
if len(accumulated_data) > 1:
step_times = calculate_step_time(accumulated_data, threshold)
print(f"Calculated step times: {step_times}")

# Estimate the target zone based on step times
target_zone = estimate_target_zone(step_times)
message = {
"step_times": step_times,
"target_zone": target_zone
}
# Send the data to the frontend through WebSocket
await websocket.send_text(json.dumps(message))
await asyncio.sleep(1)
# Estimate the target zone based on step times
target_zone = estimate_target_zone(step_times)
message = {
"step_times": step_times,
"target_zone": target_zone
}
await websocket.send_text(json.dumps(message))
await asyncio.sleep(1)
await asyncio.sleep(1)
except Exception as e:
print(f"Error occurred during data handling: {e}")

Expand Down
4 changes: 2 additions & 2 deletions backend/websocketConnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ async def websocket_endpoint(websocket: WebSocket):

while True:
try:
data = await websocket.receive_text()
await websocket.send_text(data)
data = await websocket.receive_text()
await websocket.send_text()
print(f"Received and sent back: {data}")
except WebSocketDisconnect:
print("Client disconnected")
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ function App() {
useEffect(() => {
websocket.current = new WebSocket("ws://localhost:8000/ws");

websocket.current.onopen = () => {
websocket.current.onopen = () => {
console.log("WebSocket Connected to React");
websocket.current.send("Websocket Connected to React")
// websocketConnected = true;
};

websocket.current.onmessage = function(event) {
console.log("Data received from backend: ", event.data);
console.log("Data received from backend: ", event.data);
};

websocket.current.onclose = (event) => {
console.log("WebSocket connection closed: ", event);
console.log("WebSocket connection closed: ", event);
// websocketConnected = false;
};

return () => {
if (websocket.current) {
websocket.current.close();
console.log("WebSocket connection closed during cleanup");
console.log("WebSocket connection closed during cleanup");
}
};
}, []);
Expand All @@ -52,8 +52,8 @@ function App() {
{views[currentView]}
<div>
<h2>Target Zones</h2>
<p>Left Foot: {stepTime.targetZones.left.min} - {stepTimeData.targetZones.left.max}</p>
<p>Right Foot: {stepTime.targetZones.right.min} - {stepTimeData.targetZones.right.max}</p>
<p>Left Foot: {stepTime.targetZones.left.min} - {stepTime.targetZones.left.max}</p>
<p>Right Foot: {stepTime.targetZones.right.min} - {stepTime.targetZones.right.max}</p>
<p>Average Target Zone: {stepTime.targetZones.average}</p>
</div>
</header>
Expand Down
13 changes: 6 additions & 7 deletions frontend/src/useStepTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ function useStepTime() {
};

websocket.onmessage = (event) => {
const data = JSON.parse(event.data);
// Update step time and target zones
const data = JSON.parse(event.data);
setStepTime({
left: data.step_times[0] || 0, // Assuming data has step_times array
right: data.step_times[1] || 0, // Adjust indices as per your data structure
left: data.step_times[0] ?? 0, // Assuming data has step_times array
right: data.step_times[1] ?? 0, // Adjust indices as per your data structure
targetZones: {
left: data.target_zone || { min: 0, max: 0 }, // Adjust as necessary
right: data.target_zone || { min: 0, max: 0 } // Adjust as necessary
left: data.target_zone ?? { min: 0, max: 0 }, // Adjust as necessary
right: data.target_zone ?? { min: 0, max: 0 } // Adjust as necessary
}
});
};

websocket.onclose = () => {
console.log("WebSocket connection closed");
console.log("WebSocket connection closed");
};

// Cleanup function to close the WebSocket connection when the component unmounts
Expand Down
Loading