Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
57 changes: 49 additions & 8 deletions src/components/BreadCrumbTrail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React, { useState, useEffect } from 'react';
import { Polyline, Marker, Popup } from 'react-leaflet';
import { useROS } from '@/ros/ROSContext';
import { useWaypoints } from '@/contexts/WaypointContext';
import { useWaypoints, LatLngTuple } from '@/contexts/WaypointContext';
import ROSLIB from 'roslib';
import L from 'leaflet'

Expand All @@ -18,6 +18,8 @@ const BreadcrumbTrail: React.FC = () => {
const [breadcrumbs, setBreadcrumbs] = useState<Breadcrumb[]>([]);
const [paused, setPaused] = useState<boolean>(false);
const [lastFix, setLastFix] = useState<Breadcrumb | null>(null);
const [antennaLoc, setAntennaLoc] = useState<LatLngTuple>([0, 0]);
const [antennaHead, setAntennaHead] = useState<number>(0);

useEffect(() => {
if (!ros) return;
Expand All @@ -28,6 +30,18 @@ const BreadcrumbTrail: React.FC = () => {
messageType: 'sensor_msgs/NavSatFix',
});

const antennaFixTopic = new ROSLIB.Topic({
ros,
name: '/base_station/fix',
messageType: 'sensor_msgs/NavSatFix',
});

const antennaBearingTopic = new ROSLIB.Topic({
ros,
name: '/antenna/tracker_bearing',
messageType: 'std_msgs/Float32',
});

const handleFix = (message: any) => {
if (paused) return;
// Assuming the /fix message contains 'latitude' and 'longitude'
Expand All @@ -40,9 +54,25 @@ const BreadcrumbTrail: React.FC = () => {
setLastFix(newFix);
};

const handleAntennaFix = (message: any) => {
// Assuming the /fix message contains 'latitude' and 'longitude'
const { latitude, longitude } = message;
setAntennaLoc([latitude, longitude]);
};

const handleAntennaBearing = (message: any) => {
// Assuming the message contains float32
const angle = message.data * 360;
setAntennaHead(angle);
};

fixTopic.subscribe(handleFix);
antennaFixTopic.subscribe(handleAntennaFix);
antennaBearingTopic.subscribe(handleAntennaBearing);
return () => {
fixTopic.unsubscribe(handleFix);
antennaFixTopic.unsubscribe(handleAntennaFix);
antennaBearingTopic.unsubscribe(handleAntennaBearing);
};
}, [ros, paused]);

Expand Down Expand Up @@ -107,17 +137,17 @@ const BreadcrumbTrail: React.FC = () => {
maxWidth: '300px',
}}
>
<div style={{ marginBottom: '0.5rem' }}>
<div style={{ marginBottom: '0.125rem' }}>
<strong>GPS Fix Status</strong>
</div>
<div style={{ marginBottom: '0.5rem' }}>
<div style={{ marginBottom: '0.125rem' }}>
<strong>ROS Connection:</strong>{' '}
<span style={{ color: connectionStatus === 'connected' ? 'green' : 'red' }}>
{connectionStatus}
</span>
</div>
{lastFix ? (
<div style={{ marginBottom: '0.5rem' }}>
<div style={{ marginBottom: '0.125rem' }}>
<strong>Last Fix:</strong>
<br />
{/* TODO: Is this enough percision? */}
Expand All @@ -128,15 +158,26 @@ const BreadcrumbTrail: React.FC = () => {
Time: {new Date(lastFix.timestamp).toLocaleTimeString()}
</div>
) : (
<div style={{ marginBottom: '0.5rem' }}>No fix data received yet.</div>
<div style={{ marginBottom: '0.125rem' }}>No fix data received yet.</div>
)}
<div style={{ marginBottom: '0.5rem' }}>
<div style={{ marginBottom: '0.125rem' }}>
<strong>Total Fixes:</strong> {breadcrumbs.length}
</div>
<div style={{ marginBottom: '0.5rem' }}>
<div style={{ marginBottom: '0.125rem' }}>
<strong>Total Distance:</strong> {totalDistance.toFixed(2)} km
</div>
<div style={{ marginBottom: '0.5rem' }}>
<div style={{ marginBottom: '0.125rem' }}>
<strong>Antenna Location:</strong>
<br/>
Comment thread
Copilot marked this conversation as resolved.
Outdated
{/* TODO: Is this enough percision? */}
Lat: {antennaLoc[0].toFixed(6)}
<br />
Lon: {antennaLoc[1].toFixed(6)}
</div>
<div style={{ marginBottom: '0.125rem' }}>
<strong>Antenna Heading:</strong> {antennaHead.toFixed(1)}°
</div>
<div style={{ marginBottom: '0.125rem' }}>
<button
onClick={() => setPaused(!paused)}
style={{
Expand Down
49 changes: 30 additions & 19 deletions src/components/panels/AntennaControlPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,56 @@ import { useROS } from '@/ros/ROSContext';
const AntennaControlPanel: React.FC = () => {
const { ros } = useROS();

const [disabled, setDisabled] = useState(false);
const [enabled, setEnabled] = useState(false);
const [leftHeld, setLeftHeld] = useState(false);
const [rightHeld, setRightHeld] = useState(false);

const topicRef = useRef<ROSLIB.Topic | null>(null);
const antStatusTopicRef = useRef<ROSLIB.Topic | null>(null);
const antValTopicRef = useRef<ROSLIB.Topic | null>(null);
const intervalRef = useRef<number | null>(null);

// Create/cleanup topic when ROS connection changes
useEffect(() => {
if (!ros) {
topicRef.current = null;
antValTopicRef.current = null;
return;
}

topicRef.current = new ROSLIB.Topic({
antValTopicRef.current = new ROSLIB.Topic({
ros,
name: '/antenna_control',
name: '/antenna/manual_value',
messageType: 'std_msgs/Float32',
});

antStatusTopicRef.current = new ROSLIB.Topic({
ros,
name: '/antenna/manual',
messageType: 'std_msgs/Bool',
});

return () => {
try {
topicRef.current?.unadvertise();
antStatusTopicRef.current?.unadvertise();
antValTopicRef.current?.unadvertise();
} catch {
// ignore
}
topicRef.current = null;
antStatusTopicRef.current = null;
antValTopicRef.current = null;
};
}, [ros]);

// Determine what value should be published right now
const computeValue = () => {
if (disabled) return 0.0;
if (leftHeld && !rightHeld) return -0.5;
if (rightHeld && !leftHeld) return 0.5;
if (enabled) return 0.0;
if (leftHeld && !rightHeld) return -0.125;
if (rightHeld && !leftHeld) return 0.125;
return 0.0; // neither held OR both held
};

// Start/stop the 100ms publish loop
useEffect(() => {
if (!ros || !topicRef.current) return;
if (!ros || !antValTopicRef.current || !antStatusTopicRef.current) return;

// Clear any previous loop
if (intervalRef.current) {
Expand All @@ -57,7 +66,9 @@ const AntennaControlPanel: React.FC = () => {
// Publish immediately, then every 100ms
const publishNow = () => {
const value = computeValue();
topicRef.current?.publish(new ROSLIB.Message({ data: value }));
const status = enabled;
antValTopicRef.current?.publish(new ROSLIB.Message({ data: value }));
antStatusTopicRef.current?.publish(new ROSLIB.Message({ data: status }));
};

publishNow();
Expand All @@ -70,23 +81,23 @@ const AntennaControlPanel: React.FC = () => {
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ros, disabled, leftHeld, rightHeld]);
}, [ros, enabled, leftHeld, rightHeld]);

// If user disables controls, clear held state so it goes to 0 cleanly
useEffect(() => {
if (disabled) {
if (enabled) {
setLeftHeld(false);
setRightHeld(false);
}
}, [disabled]);
}, [enabled]);

const setHeld = (side: 'left' | 'right', held: boolean) => {
if (disabled) return;
if (enabled) return;
if (side === 'left') setLeftHeld(held);
else setRightHeld(held);
};

const btnDisabled = disabled || !ros;
const btnDisabled = enabled || !ros;

return (
<div className="antenna-panel">
Expand Down Expand Up @@ -121,8 +132,8 @@ const AntennaControlPanel: React.FC = () => {
<label className="checkbox">
<input
type="checkbox"
checked={disabled}
onChange={(e) => setDisabled(e.target.checked)}
checked={enabled}
onChange={(e) => setEnabled(e.target.checked)}
/>
Auto tracking
</label>
Expand Down