Skip to content

Commit

Permalink
Halt joystick if unused & add send (0,0) after exiting page
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-gorecki committed Jan 14, 2025
1 parent f240de9 commit a400ea2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 76 deletions.
26 changes: 13 additions & 13 deletions demo/panther-layout.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
"minLevel": 0,
"pinnedIds": [],
"hardwareIdFilter": "",
"topicToRender": "{{env "ROBOT_NAMESPACE"}}/diagnostics",
"topicToRender": "/panther/diagnostics",
"sortByLevel": true
},
"Plot!dg5ynj": {
"paths": [
{
"timestampMethod": "receiveTime",
"value": "{{env "ROBOT_NAMESPACE"}}/imu/data.linear_acceleration.x",
"value": "/panther/imu/data.linear_acceleration.x",
"enabled": true,
"label": "x",
"showLine": true
},
{
"timestampMethod": "receiveTime",
"value": "{{env "ROBOT_NAMESPACE"}}/imu/data.linear_acceleration.y",
"value": "/panther/imu/data.linear_acceleration.y",
"enabled": true,
"label": "y"
},
{
"timestampMethod": "receiveTime",
"value": "{{env "ROBOT_NAMESPACE"}}/imu/data.linear_acceleration.z",
"value": "/panther/imu/data.linear_acceleration.z",
"enabled": true,
"label": "z"
}
Expand All @@ -41,39 +41,39 @@
"followingViewWidth": 60
},
"Bar!3t52ye7": {
"path": "{{env "ROBOT_NAMESPACE"}}/joint_states.effort[0]",
"path": "/panther/joint_states.effort[0]",
"maxValue": 34.52,
"colorMode": "colormap",
"gradient": ["#0000ff", "#ff00ff"],
"reverse": false,
"foxglovePanelTitle": "FL"
},
"Bar!461hl59": {
"path": "{{env "ROBOT_NAMESPACE"}}/joint_states.effort[1]",
"path": "/panther/joint_states.effort[1]",
"maxValue": 34.52,
"colorMode": "colormap",
"gradient": ["#0000ff", "#ff00ff"],
"reverse": true,
"foxglovePanelTitle": "FR"
},
"Bar!1fzrnqw": {
"path": "{{env "ROBOT_NAMESPACE"}}/joint_states.effort[2]",
"path": "/panther/joint_states.effort[2]",
"maxValue": 34.52,
"colorMode": "colormap",
"gradient": ["#0000ff", "#ff00ff"],
"reverse": false,
"foxglovePanelTitle": "RL"
},
"Bar!1q5qffy": {
"path": "{{env "ROBOT_NAMESPACE"}}/joint_states.effort[3]",
"path": "/panther/joint_states.effort[3]",
"maxValue": 34.52,
"colorMode": "colormap",
"gradient": ["#0000ff", "#ff00ff"],
"reverse": true,
"foxglovePanelTitle": "RR"
},
"Battery!wppv5y": {
"path": "{{env "ROBOT_NAMESPACE"}}/battery/battery_status.percentage",
"path": "/panther/battery/battery_status.percentage",
"minValue": 0,
"maxValue": 1,
"colorMap": "red-yellow-green",
Expand All @@ -87,13 +87,13 @@
"layout": "vertical",
"advancedView": false,
"serviceName": "",
"goServiceName": "{{env "ROBOT_NAMESPACE"}}/hardware/e_stop_reset",
"stopServiceName": "{{env "ROBOT_NAMESPACE"}}/hardware/e_stop_trigger",
"statusTopicName": "{{env "ROBOT_NAMESPACE"}}/hardware/e_stop.data",
"goServiceName": "/panther/hardware/e_stop_reset",
"stopServiceName": "/panther/hardware/e_stop_trigger",
"statusTopicName": "/panther/hardware/e_stop.data",
"foxglovePanelTitle": "E-stop"
},
"Joy!3fmstz6": {
"topic": "{{env "ROBOT_NAMESPACE"}}/cmd_vel",
"topic": "/panther/cmd_vel",
"publishRate": 5,
"upButton": {
"field": "linear-x",
Expand Down
42 changes: 23 additions & 19 deletions packages/studio-base/src/panels/Joy/Joy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ function Joy(props: JoyProps): JSX.Element {
const { context } = props;
const { saveState } = context;

const [speed, setVelocity] = useState<{ x: number; y: number } | undefined>();
const [speed, setSpeed] = useState<{ x: number; y: number } | undefined>();
const [topics, setTopics] = useState<readonly Topic[]>([]);
const [halt, setHalt] = useState<boolean>(false);

// Resolve an initial config which may have some missing fields into a full config
const [config, setConfig] = useState<Config>(() => {
Expand Down Expand Up @@ -212,7 +213,7 @@ function Joy(props: JoyProps): JSX.Element {
};
};

const createMessage = () => {
const createMessage = useCallback(() => {
if (config.stamped) {
return {
header: {
Expand All @@ -230,39 +231,43 @@ function Joy(props: JoyProps): JSX.Element {
angular: { x: 0, y: 0, z: 0 },
};
}
};
}, [config.stamped]);

const setTwistValue = (message: any, axis: Axis, value: number) => {
const setTwistValue = useCallback((message: any, axis: Axis, value: number) => {
const target = config.stamped ? message.twist : message;
const [category, direction] = axis.field.split("-");
if (category && direction) {
target[category][direction] = value;
}
};
}, [config.stamped]);

useLayoutEffect(() => {
if (speed == undefined || !currentTopic) {
if (speed == undefined || !currentTopic || config.publishRate <= 0) {
return;
}

const isStoped = speed.x === 0 && speed.y === 0;

if (isStoped && halt) {
return;
}

setHalt(isStoped);

const publishMessage = () => {
const message = createMessage();
setTwistValue(message, config.xAxis, speed.x);
setTwistValue(message, config.yAxis, speed.y);
context.publish?.(currentTopic, message);
};

if (config.publishRate > 0) {
const intervalMs = (1000 * 1) / config.publishRate;
publishMessage();
const intervalHandle = setInterval(publishMessage, intervalMs);
return () => {
clearInterval(intervalHandle);
};
} else {
return;
}
}, [context, config, currentTopic, speed]);
const intervalMs = (1000 * 1) / config.publishRate;
publishMessage();
const intervalHandle = setInterval(publishMessage, intervalMs);
return () => {
clearInterval(intervalHandle);
};
}, [context, config, currentTopic, speed, halt, createMessage, setTwistValue]);

useLayoutEffect(() => {
renderDone();
Expand All @@ -280,10 +285,9 @@ function Joy(props: JoyProps): JSX.Element {
)}
{enabled && (
<JoyVisual
disabled={!enabled}
advanced={config.advanced}
onSpeedChange={(value) => {
setVelocity(value);
setSpeed(value);
}}
xLimit={config.xAxis.limit}
yLimit={config.yAxis.limit}
Expand Down
Loading

0 comments on commit a400ea2

Please sign in to comment.