0

With the following node server I drive the motor speeds with a slider on a web page using socket.io. I would like to stop the motors when the robot touches a wall:

var express = require('express');
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server);
var Gpio = require('pigpio').Gpio, //include pigpio to interact with the GPIO
ledRed = new Gpio(17, {mode: Gpio.OUTPUT}),
ledGreen = new Gpio(18, {mode: Gpio.OUTPUT}); //use GPIO pin 4 as output for RED
ledBlue = new Gpio(22, {mode: Gpio.OUTPUT}), //use GPIO pin 27 as output for BLUE
ledYel = new Gpio(23, {mode: Gpio.OUTPUT}),
redRGB = 0, //set starting value of RED variable to off (0 for common cathode)
greenRGB = 0, //set starting value of GREEN variable to off (0 for common cathode)
blueRGB = 0, //set starting value of BLUE variable to off (0 for common cathode)
yelRGB = 0;
contact1 = new Gpio(2, {mode: Gpio.INPUT}),
contact2 = new Gpio(3, {mode: Gpio.INPUT}),
ir = new Gpio(4, {mode: Gpio.OUTPUT}),
touche1 = contact1.digitalRead();
touche2 = contact2.digitalRead();

//RESET RGB LED
ledRed.digitalWrite(0); // Turn RED LED off
ledGreen.digitalWrite(0); // Turn GREEN LED off
ledBlue.digitalWrite(0); // Turn BLUE LED off
ledYel.digitalWrite(0);

 server.listen(8080);
app.use(express.static('public'));             

io.sockets.on('connection', function (socket) {
        socket.on('led', function (data) {
                redRGB=parseInt(data.value);
touche1 = contact1.digitalRead();
touche2 = contact2.digitalRead();

console.log("socket: ",redRGB,'touche1: ',touche1,'touche2: ',touche2);
if(touche1 == 0 && touche2 == 0)
{
console.log("if: ",redRGB,'touche1: ',touche1,'touche2: ',touche2);
touche1 = contact1.digitalRead();
touche2 = contact2.digitalRead();

ir.digitalWrite(0);
if (redRGB < 125) {
        ledRed.pwmWrite(0); 
        ledGreen.pwmWrite(127); 
        ledBlue.pwmWrite(0); 
        ledYel.pwmWrite(127);
}else if (redRGB >= 125) {
        ledRed.pwmWrite(127); 
        ledGreen.pwmWrite(0); 
        ledBlue.pwmWrite(127); 
        ledYel.pwmWrite(0);
}
}else{
ir.digitalWrite(1);
        ledRed.pwmWrite(0); 
        ledGreen.pwmWrite(0); 
        ledBlue.pwmWrite(0); 
        ledYel.pwmWrite(0); 
}
 });

});

console.log("fin: ",redRGB,'touche1: ',touche1,'touche2: ',touche2);

process.on('SIGINT', function () { //on ctrl+c
  ledRed.digitalWrite(0); // Turn RED LED off
  ledGreen.digitalWrite(0); // Turn GREEN LED off
  ledBlue.digitalWrite(0); // Turn BLUE LED off
  ledYel.digitalWrite(0);
process.exit(); //exit completely
});

console.log("running");`

My question is: The "contact" pins are read only if I move the slider, that is if a new value for redRGB is provided to the socket. Is there a way to get around this? I tried while loops and having the socket inside the if statement, to no avail!

1
  • Please format your code. As it is it's too much effort for me to decipher with only a few hours of sleep. I'll try to edit it if I can. One uses the "{}" command to format code.
    – NomadMaker
    May 13, 2018 at 11:28

1 Answer 1

1

You talk about motors, and yet your code looks like it's controlling an RGBY LED.

To have a robot stop when it hits an obstacle requires some sort of sensor to be triggered when the wall is hit.

I generally use switches as such a sensor. If the robot is small, then a simple contact switch is fine. If the robot is larger then I use a piece of plastic (3d printed now) to spread the force so I can use two switches per side of the robot. I mount the plastic piece either on the switches, or so the switches will be triggered when a wall is hit. Since one switch is on each side of the plastic, I can use the information from which switch is hit or the order that the switch is hit in. Or you can wire the switches in parallel to be read with only a single pin.

If the robot can do some damage if it hits a wall, or a dog, or a person, then you will want to have some other sensors that work at a longer distance, such as IR, sonar, or LIDAR. None are perfect, but it's best to slow a larger robot down before it hits the wall.

At the time the robot stops, the communication with the server should be interrupted. If the communication is two-way, then the robot should inform the server that it has stopped due to contact with an obstacle. After this there are several possibilities to continue.

The person running the client could maneuver the robot away from the wall.

Somebody could manually walk over to the robot, pick it up, and press a button which will communicate to the server to resume.

The robot could autonomously move itself away from the wall and signal the server.

To actually advise on the code I'd need details on the languages and processors involved. For example, a Raspberry Pi could be programmed with multiple threads, whereas an Arduino has to be programmed with a single thread to simulate listening to multiple things at once (the communication and the sensor(s)).

Once piece of advice I have is that contact sensors should be read by interrupt, and in a larger robot should turn the motors off by themselves (and require the main processor to turn the motors on itself). Large robots can be dangerous.

I tend to cheat: I use a single-board computer (like a Raspberry Pi) and a microcontroller like an Arduino.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.