2024-06-25 11:21:25 +02:00
|
|
|
<!DOCTYPE html>
|
2024-06-25 11:19:53 +02:00
|
|
|
<html lang="fr">
|
2024-06-25 11:21:25 +02:00
|
|
|
<head>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
|
<title>Contrôle du robot</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
2024-06-25 14:55:30 +02:00
|
|
|
<main>
|
|
|
|
<h1>Contrôle du robot sans fils</h1>
|
|
|
|
<p>
|
|
|
|
Cette page permet de contrôler le robot depuis un PC portable
|
|
|
|
connecté au WiFi du robot. Elle est destinée aux personnes
|
|
|
|
n'ayant pas de smartphone.
|
|
|
|
</p>
|
|
|
|
<section class="infos">
|
|
|
|
<div>
|
|
|
|
<h2>Paramètres de contrôle</h2>
|
|
|
|
<p>
|
|
|
|
Ici vous pourrez paramétrer les contrôles du robot,
|
|
|
|
rentrer son adresse IP ainsi que lui indiquer la
|
|
|
|
distance à parcourir ou l'angle de rotation désiré.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<h2>Contrôle</h2>
|
|
|
|
<p>
|
|
|
|
Amusez vous ! Pour contrôler le robot, utilisez les
|
|
|
|
flèches directionnelles pour le diriger et la barre
|
|
|
|
espace pour le stopper.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
<section class="inputs">
|
|
|
|
<form class="settings" id="settings">
|
|
|
|
<label>
|
|
|
|
<span>Adresse IP du robot</span>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
name="ip-address"
|
|
|
|
title="Adresse IP du robot (ex: 192.168.0.1)"
|
|
|
|
pattern="\d+\.\d+\.\d+\.\d+(:\d+)?"
|
|
|
|
placeholder="192.168.0.0"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<button>Se connecter</button>
|
|
|
|
</form>
|
|
|
|
<div class="input-controls">
|
|
|
|
<label>
|
|
|
|
<span>Rotation en degrés</span>
|
|
|
|
<input
|
|
|
|
id="rotate-input"
|
|
|
|
type="number"
|
|
|
|
title="Rotation en degrés (-360° à 360°)"
|
|
|
|
min="-360"
|
|
|
|
max="360"
|
|
|
|
step="1"
|
|
|
|
placeholder="90"
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
<span>Distance en cm</span>
|
|
|
|
<input
|
|
|
|
id="move-input"
|
|
|
|
type="number"
|
|
|
|
title="Distance en cm (-2^31 à 2^31)"
|
|
|
|
min="-2147483647"
|
|
|
|
step="1"
|
|
|
|
max="2147483647"
|
|
|
|
placeholder="10"
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
<section class="touch-controls" id="touch-controls">
|
|
|
|
<span class="placeholder"></span>
|
|
|
|
<button title="Avancer" data-command="forward" data-value="2">
|
|
|
|
⬆️
|
|
|
|
</button>
|
|
|
|
<span class="placeholder"></span>
|
|
|
|
<button
|
|
|
|
title="Tourner à gauche"
|
|
|
|
data-command="left"
|
|
|
|
data-value="5"
|
|
|
|
>
|
|
|
|
⬅️
|
|
|
|
</button>
|
|
|
|
<button title="Arrêter" data-command="stop" data-value="0">
|
|
|
|
⏸️
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
title="Tourner à droite"
|
|
|
|
data-command="right"
|
|
|
|
data-value="5"
|
|
|
|
>
|
|
|
|
➡️
|
|
|
|
</button>
|
|
|
|
<span class="placeholder"></span>
|
|
|
|
<button title="Reculer" data-command="backward" data-value="2">
|
|
|
|
⬇️
|
|
|
|
</button>
|
|
|
|
<span class="placeholder"></span>
|
|
|
|
</section>
|
|
|
|
</main>
|
2024-06-25 11:21:25 +02:00
|
|
|
</body>
|
2024-06-25 12:27:16 +02:00
|
|
|
<script type="module">
|
2024-06-25 13:19:07 +02:00
|
|
|
// Input handlers
|
|
|
|
document
|
2024-06-25 13:26:35 +02:00
|
|
|
.querySelector('#move-input')
|
2024-06-25 13:19:07 +02:00
|
|
|
.addEventListener('change', (event) =>
|
|
|
|
assignCommands('backward', 'forward', event)
|
|
|
|
)
|
|
|
|
document
|
2024-06-25 13:26:35 +02:00
|
|
|
.querySelector('#rotate-input')
|
2024-06-25 13:19:07 +02:00
|
|
|
.addEventListener('change', (event) =>
|
|
|
|
assignCommands('left', 'right', event)
|
|
|
|
)
|
|
|
|
|
2024-06-25 14:02:59 +02:00
|
|
|
// Buttons handlers
|
|
|
|
for (const button of document
|
|
|
|
.querySelector('#touch-controls')
|
|
|
|
.querySelectorAll('button')) {
|
|
|
|
button.addEventListener('click', () => {
|
|
|
|
const { command, value } = button.dataset
|
|
|
|
|
|
|
|
// Check datas are set for button
|
|
|
|
if (command === undefined || value === undefined) {
|
|
|
|
alert(
|
|
|
|
`Pas de command ou de valeur assigné au bouton "${button.title}"`
|
|
|
|
)
|
|
|
|
throw new Error(
|
|
|
|
`no command or value assigned to ${button.title}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check command is valid
|
|
|
|
if (
|
|
|
|
!['forward', 'backward', 'left', 'right', 'stop'].includes(
|
|
|
|
command
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
alert(
|
|
|
|
`La command "${command}" n'est pas valide en tant que ('forward', 'backward', 'left', 'right', 'stop')`
|
|
|
|
)
|
|
|
|
throw new Error(
|
|
|
|
`specified command "${command}" is not in ['forward', 'backward', 'left', 'right', 'stop']`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check value is valid finite number
|
|
|
|
if (!Number.isFinite(Number(value))) {
|
|
|
|
alert(
|
|
|
|
`La valeur "${value}" n'est convertible en entier fini`
|
|
|
|
)
|
|
|
|
throw new Error(
|
|
|
|
`value "${value}" cannot be casted to finite integer`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
sendCommand(command, Number(value))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-25 13:19:07 +02:00
|
|
|
// Keyboard handler
|
2024-06-25 13:31:27 +02:00
|
|
|
document.addEventListener('keydown', ({ code }) => {
|
|
|
|
if (code === 'ArrowUp') {
|
2024-06-25 13:41:17 +02:00
|
|
|
return sendCommand('forward', 2)
|
2024-06-25 13:31:27 +02:00
|
|
|
}
|
|
|
|
if (code === 'ArrowDown') {
|
2024-06-25 13:41:17 +02:00
|
|
|
return sendCommand('backward', 2)
|
2024-06-25 13:31:27 +02:00
|
|
|
}
|
|
|
|
if (code === 'ArrowLeft') {
|
2024-06-25 13:41:17 +02:00
|
|
|
return sendCommand('left', 5)
|
2024-06-25 13:31:27 +02:00
|
|
|
}
|
|
|
|
if (code === 'ArrowRight') {
|
2024-06-25 13:41:17 +02:00
|
|
|
return sendCommand('right', 5)
|
2024-06-25 13:31:27 +02:00
|
|
|
}
|
|
|
|
if (code === 'Space') {
|
2024-06-25 13:41:17 +02:00
|
|
|
return sendCommand('stop', 0)
|
2024-06-25 13:31:27 +02:00
|
|
|
}
|
2024-06-25 13:19:07 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Settings handler
|
|
|
|
document
|
|
|
|
.querySelector('#settings')
|
|
|
|
.addEventListener('submit', async (event) => {
|
|
|
|
// Don't interrupt event if not form submitting
|
|
|
|
if (!(event instanceof SubmitEvent)) return true
|
|
|
|
if (event.target === null) return true
|
|
|
|
// Disable form sending
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
const form = new FormData(event.target)
|
|
|
|
const ipAddress = form.get('ip-address')
|
|
|
|
|
|
|
|
if (ipAddress === null) return
|
|
|
|
|
|
|
|
const endpoint = `http://${ipAddress}`
|
|
|
|
await testEndpoint(endpoint)
|
|
|
|
setEndpoint(endpoint)
|
|
|
|
})
|
|
|
|
|
2024-06-25 12:27:16 +02:00
|
|
|
/**
|
|
|
|
* A command string.
|
|
|
|
* @typedef {('forward' | 'backward' | 'left' | 'right' | 'stop')} Command
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send command to the robot.
|
|
|
|
*
|
|
|
|
* @param {Command} command Command to send.
|
|
|
|
* @param {number} value Value of the command if needed.
|
2024-06-25 12:49:21 +02:00
|
|
|
*
|
|
|
|
* @returns {Promise<void>}
|
2024-06-25 12:27:16 +02:00
|
|
|
*/
|
2024-06-25 13:41:17 +02:00
|
|
|
async function sendCommand(command, value) {
|
|
|
|
const endpoint = getEndpoint()
|
2024-06-25 12:49:21 +02:00
|
|
|
const url = new URL(
|
|
|
|
`/get?command=${command}&value=${value}`,
|
|
|
|
endpoint
|
2024-06-25 12:27:16 +02:00
|
|
|
)
|
2024-06-25 12:49:21 +02:00
|
|
|
const response = await fetch(url)
|
2024-06-25 12:27:16 +02:00
|
|
|
const text = await response.text()
|
|
|
|
console.log(text)
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:46:11 +02:00
|
|
|
/**
|
|
|
|
* Get robot endpoint address.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function getEndpoint() {
|
|
|
|
const endpoint = sessionStorage.getItem('robot-endpoint')
|
|
|
|
if (endpoint === null) {
|
|
|
|
alert("Aucune adresse IP n'a été renseignée !")
|
|
|
|
throw new Error('no given ip address')
|
|
|
|
}
|
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set robot endpoint address.
|
|
|
|
*
|
|
|
|
* @param {string} ip Robot ip.
|
|
|
|
*
|
|
|
|
* @returns void
|
|
|
|
*/
|
|
|
|
function setEndpoint(ip) {
|
2024-06-25 14:01:37 +02:00
|
|
|
sessionStorage.setItem('robot-endpoint', ip)
|
2024-06-25 12:46:11 +02:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:58:11 +02:00
|
|
|
/**
|
|
|
|
* Test connection to endpoint.
|
2024-06-25 13:15:04 +02:00
|
|
|
*
|
2024-06-25 12:58:11 +02:00
|
|
|
* @param {string} endpoint Endpoint to test for.
|
2024-06-25 13:15:04 +02:00
|
|
|
*
|
2024-06-25 12:58:11 +02:00
|
|
|
* @returns {void}
|
|
|
|
* @throws {Error} Endpoint unreachable.
|
|
|
|
*/
|
|
|
|
async function testEndpoint(endpoint) {
|
|
|
|
try {
|
|
|
|
const response = await fetch(endpoint)
|
|
|
|
if (response.ok) return
|
|
|
|
} catch (cause) {
|
|
|
|
alert(`Impossible de joindre l'adresse "${endpoint}"`)
|
|
|
|
throw new Error(`unable to connect to robot at ${endpoint}`, {
|
|
|
|
cause,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 13:15:04 +02:00
|
|
|
/**
|
|
|
|
* Assign a command to an input.
|
|
|
|
* Do negative command if value < 0 else do positive command.
|
|
|
|
*
|
|
|
|
* @param {Command} negativeCommand Command if value < 0.
|
|
|
|
* @param {Command} positiveCommand Command if value >= 0.
|
|
|
|
* @param {event} event Event of the input.
|
|
|
|
*
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function assignCommands(negativeCommand, positiveCommand, event) {
|
|
|
|
if (event.target === null) return
|
|
|
|
|
|
|
|
/** @type {HTMLInputElement} */
|
|
|
|
const target = event.target
|
|
|
|
const value = target.valueAsNumber
|
|
|
|
|
|
|
|
if (value < 0) {
|
2024-06-25 13:41:17 +02:00
|
|
|
return sendCommand(negativeCommand, Math.abs(value))
|
2024-06-25 13:15:04 +02:00
|
|
|
}
|
2024-06-25 13:41:17 +02:00
|
|
|
return sendCommand(positiveCommand, value)
|
2024-06-25 13:15:04 +02:00
|
|
|
}
|
2024-06-25 11:21:25 +02:00
|
|
|
</script>
|
2024-06-25 14:02:59 +02:00
|
|
|
<style>
|
2024-06-25 14:55:30 +02:00
|
|
|
:root {
|
|
|
|
--translucent-high: rgba(255, 255, 255, 0.8);
|
|
|
|
--translucent-low: rgba(255, 255, 255, 0.4);
|
|
|
|
--padding: 0.5rem;
|
|
|
|
--padding-half: calc(var(--padding) / 2);
|
|
|
|
--border-size: 0.2rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
* {
|
|
|
|
box-sizing: border-box;
|
|
|
|
}
|
|
|
|
|
|
|
|
html {
|
|
|
|
scroll-behavior: smooth;
|
|
|
|
}
|
|
|
|
|
|
|
|
body {
|
|
|
|
background-color: rgb(247, 234, 219);
|
|
|
|
height: 100dvh;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
|
|
|
|
font-family: system-ui, -apple-system, BlinkMacSystemFont,
|
|
|
|
'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans',
|
|
|
|
'Helvetica Neue', sans-serif;
|
|
|
|
}
|
|
|
|
|
|
|
|
h1,
|
|
|
|
h2 {
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
h1 {
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
main {
|
|
|
|
padding: var(--padding);
|
|
|
|
height: 100%;
|
|
|
|
text-wrap: pretty;
|
|
|
|
}
|
|
|
|
|
2024-06-25 14:02:59 +02:00
|
|
|
.touch-controls {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: repeat(3, 1fr);
|
2024-06-25 14:55:30 +02:00
|
|
|
gap: var(--padding);
|
|
|
|
padding: var(--padding);
|
|
|
|
|
|
|
|
button {
|
|
|
|
height: 5.5rem;
|
|
|
|
font-size: 3rem;
|
|
|
|
border-radius: var(--padding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
button {
|
|
|
|
border: solid var(--border-size) var(--translucent-high);
|
|
|
|
background: var(--translucent-high);
|
|
|
|
border-radius: var(--padding-half);
|
|
|
|
transition: all 0.2s ease;
|
|
|
|
height: 100%;
|
|
|
|
font-size: 110%;
|
|
|
|
|
|
|
|
&:active {
|
|
|
|
border-color: var(--translucent-low);
|
|
|
|
background-color: var(--translucent-low);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.input-controls,
|
|
|
|
.settings {
|
|
|
|
padding: var(--padding);
|
|
|
|
display: flex;
|
|
|
|
gap: var(--padding);
|
|
|
|
border: solid var(--border-size) var(--translucent-high);
|
|
|
|
border-radius: var(--padding-half);
|
|
|
|
align-items: center;
|
|
|
|
justify-content: space-around;
|
|
|
|
}
|
|
|
|
|
|
|
|
.inputs {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: repeat(auto-fill, minmax(700px, 1fr));
|
|
|
|
gap: var(--padding);
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.infos {
|
|
|
|
display: grid;
|
|
|
|
grid-template-columns: repeat(auto-fill, minmax(800px, 1fr));
|
|
|
|
flex-wrap: wrap;
|
|
|
|
gap: var(--padding);
|
|
|
|
justify-content: space-between;
|
|
|
|
}
|
|
|
|
|
|
|
|
label {
|
|
|
|
display: grid;
|
|
|
|
gap: var(--padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
input {
|
|
|
|
border: solid var(--border-size) var(--translucent-high);
|
|
|
|
background: var(--translucent-low);
|
|
|
|
border-radius: var(--padding-half);
|
|
|
|
transition: all 0.2s ease;
|
|
|
|
height: 100%;
|
|
|
|
padding: var(--padding-half);
|
|
|
|
font-family: 'Courier New', Courier, monospace;
|
|
|
|
outline: none;
|
|
|
|
|
|
|
|
&:focus {
|
|
|
|
background-color: var(--translucent-high);
|
|
|
|
}
|
2024-06-25 14:02:59 +02:00
|
|
|
}
|
|
|
|
</style>
|
2024-06-25 11:19:53 +02:00
|
|
|
</html>
|