summaryrefslogtreecommitdiff
path: root/robot.js
blob: e67aba492d1bbe2dc5da6e22e2430641841bd89c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const client = mqtt.connect("ws://marcelus.net:9001");

function wait(ms)
{
    return new Promise((resolve, reject) => {
        let isObstacle = false;

        const timer = setTimeout(() => {
            if(!isObstacle){
                resolve(0);
            } // Le délai s'est écoulé sans détecter d'obstacle
        }, ms);

        // Supposons que `client` est un objet qui écoute les messages
        client.on('message', (topic, message) => {
            if (topic === 'move' && parseInt(message.toString()) < 150) {
                isObstacle = true;
                clearTimeout(timer);
                resolve(1); // Un obstacle a été détecté
            }
        });
    });
}

function getTime(dist)
{
    let res = (dist / 200)*1000
    console.log('time is '+res)
    return res
}

async function moveTo(x,y)
{
    var dist = Math.sqrt(x * x + y * y)
    console.log('dist is '+dist)
    var angle = (Math.atan2(y,x))*180/Math.PI
    if(angle>180)
    {
        angle-=180
    }
    else if(angle<-180)
    {
        angle+=180
    }
    console.log(angle.toString())
    var str = 'T'+(Math.floor(angle)-90).toString()
    client.publish('move',str)
    client.publish('move','D200')
    const time = getTime(dist)
    let tmp = await wait(time);
    if (tmp == 0){
        client.publish('move', 'STOP');
    }
    
}

client.on('connect', () => {
    console.log('Connecté au courtier MQTT');
    client.subscribe('move')
});
// client.publish('move','ULT1')


/* client.on('close', function () {
    console.log('Déconnecté du courtier MQTT');
});*/