Speed control

This function will update the duty cycle of the enable pin of both the motors and vary the speed of rotation:

module.exports.motor_speed = function(pwm,callback){
console.log(pwm)
enable_1.pwmWrite(pwm);
enable_2.pwmWrite(pwm);
console.log ("speed change")
callback("speed change")
}

An indicator function is required which will make indicator LEDs on the left front and right front of the car blink whenever car makes a turn:

indicatorLeft = function(){ leftIndicatorHandler = setInterval(function() {
if(front_left_LED.digitalRead()==1){
front_left_LED.digitalWrite(0)
}
else{
front_left_LED.digitalWrite(1)
}
},250)
}

Now we will create an MQTT client for the car which will receive the commands from the MQTT broker. Here we are using EMQTT broker which is an open source and free of cost broker.

We have already done its installation and setup in an earlier chapter of this book so we will not go through it again.

Create a file with the name car_client.js and include the mqtt module and car_control.js module:

var mqtt = require('mqtt');
var motor_Dir= require ('./car_control.js ');

Now connect to MQTT broker running on server and subscribe to topic:

var options = {
port:'1883',
host: IP_Adress_Of_Mqtt_Broker_Server
}

var client = mqtt.connect(options)
client.on('connect', () => {
client.subscribe('controlPi/cmd');
console.log (" Raspberry Pi mqtt client connected to broker ");
})

Now listen on event 'message' which is emitted when a message is received from broker. Based on the message type we execute the functions of car_control.js module:

client.on('message', (topic, message) => {
message= message.toString();
else if(message=='forward'){
motor_Dir.move_forward((callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}
else if(message=='reverse'){
motor_Dir.move_reverse((callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}

else if(message=='right'){
motor_Dir.move_right((callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}
else if(message=='stop') {
motor_Dir.motor_stop((callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}
else if(message=='left'){
motor_Dir.move_left((callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}
else if(message=='64'){
motor_Dir.motor_speed(message,(callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}
else if(message=='128'){
motor_Dir.motor_speed(message,(callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}
else if(message=='192'){
motor_Dir.motor_speed(message,(callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}
else if(message=='255'){
motor_Dir.motor_speed(message,(callback)=> {
var Acknowledgement=callback;
client.publish('raspbPi/ack',Acknowledgement)
})
}
else {
console.log("wrong command")
}
})

In the previous code we passed 64, 128, 192, and 255 as the values of duty cycle to speed control method motor_speed. Since default value for range of duty cycle is 255, a duty cycle of 64 is equal to 25% of full range and 128 is 50%, 192 is 75%, and 255 is 100%.

As we have completed our coding, let's execute the code. Open the terminal in Raspberry Pi and execute command sudo node car_client.js as shown in the following screenshot:

Figure 8.10

As our robot car is up and ready to run, let's use MQTT.fx client to send commands to the car remotely and see the action. MQTT.fx is a free and open source client tool for MQTT.

MQTT.fx can be downloaded from http://emqtt.io/ as per the OS being used.

After download and installation, open the client, click on the Settings button and provide connection details and then connect. Once connected we will subscribe to topic raspbPi/ack on which we will get acknowledgment from the car.

Now we will Publish commands to make our car move.

Before we start to publish our commands to car we must subscribe to topic raspbPi/ack on which the acknowledgement from car (Raspberry Pi) will be received as shown in the following screenshot:

Figure 8.11
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.135.63