Close

Step 6. Creating assistant.js

A project log for GUILLENGAP Cobot

Collaborative Robot Arm with Artificial Intelligence, and Used as Assistant in: 1) Voice Command System & 2) Sorting Objects System

guillermo-perez-guillenGuillermo Perez Guillen 06/28/2021 at 02:150 Comments

This step will go show you how to setup a MATRIX Lite project with Snips.

First, MQTT must be installed and used to listen in on events from Snips.

npm install mqtt --save

assistant.js will be used to listen and respond to events from your Snips assistant. This file combines all the code from everloop.js & relay.js to control the everloop and relay switch through voice.

///////////////////////////////////
//AUTHOR: GUILLERMO PEREZ GUILLEN//
///////////////////////////////////

/////////////
//VARIABLES//
/////////////
var mqtt = require('mqtt');
var client  = mqtt.connect('mqtt://localhost', { port: 1883 });
var relay = require('./relay.js');
var everloop = require('./everloop.js');
var snipsUserName = 'guillengap';
var wakeword = 'hermes/hotword/default/detected';
var sessionEnd = 'hermes/dialogueManager/sessionEnded';
var lightState = 'hermes/intent/'+snipsUserName+':roboticArmState';

//////////////
//ON CONNECT//
//////////////
client.on('connect', function() {
   console.log('Connected to Snips MQTT server\n');
   client.subscribe('hermes/hotword/default/detected');
   client.subscribe('hermes/dialogueManager/sessionEnded');
   client.subscribe(lightState);
});
//////////////
//ON MESSAGE//
//////////////
client.on('message', function(topic,message) {
   var message = JSON.parse(message);
   switch(topic) {
       // * On Wakeword
       case wakeword:
           everloop.startWaiting();
           console.log('Wakeword Detected');
       break;
       // * Robotic Arm Change
       case lightState:
           // Turn robotic arm Here/There
           try{
               if (message.slots[0].rawValue === 'here'){
                   relay.roboticarmHere();
                   everloop.stopWaiting();
                   console.log('Robotic Arm Here');
               }
               else if(message.slots[0].rawValue === 'there'){
                   relay.roboticarmThere();
                   everloop.stopWaiting();
                   console.log('Robotic Arm There');
               }
           }
           // Expect error if `here` or `there` is not heard
           catch(e){
               console.log('Did not receive an Here/There state')
           }
       break;
       // * On Conversation End
       case sessionEnd:
           everloop.stopWaiting();
           console.log('Session Ended\n');
       break;
   }
});

 everloop.js incorporates the code required to control the LEDs on the MATRIX Creator.

///////////////////////////////////
//AUTHOR: GUILLERMO PEREZ GUILLEN//
///////////////////////////////////

/////////////
//VARIABLES//
/////////////
const matrix = require('@matrix-io/matrix-lite');
var matrix_device_leds = 0;// Holds amount of LEDs on MATRIX device
var methods = {};// Declaration of method controls at the end
var waitingToggle = false;
var counter = 0;

setInterval(function(){
   // Turns off all LEDs
   if (waitingToggle == false) {
           // Set individual LED value
           matrix.led.set({});
   }
   // Creates pulsing LED effect
   else if (waitingToggle == true) {
           // Set individual LED value
           matrix.led.set({
               b: (Math.round((Math.sin(counter) + 1) * 100) + 10),// Math used to make pulsing effect
           });
       
   };
   counter = counter + 0.2;
   // Store the Everloop image in MATRIX configuration
},50);
///////////////////
//WAITING METHODS//
///////////////////
methods.startWaiting = function() {
   waitingToggle = true;
};
methods.stopWaiting = function() {
   waitingToggle = false;
};
module.exports = methods;// Export methods in order to make them avaialble to other files 

relay.js includes the code required to turn the robotic arm here and there. This relay will control power to the robotic arm.

///////////////////////////////////
//AUTHOR: GUILLERMO PEREZ GUILLEN//
///////////////////////////////////

/////////////
//VARIABLES//
/////////////
const matrix = require('@matrix-io/matrix-lite');
var methods = {};// Declaration of method controls at the end

matrix.gpio.setFunction(0, 'PWM');
matrix.gpio.setFunction(1, 'PWM');
matrix.gpio.setFunction(2, 'PWM');
matrix.gpio.setFunction(3, 'PWM');
matrix.gpio.setFunction(4, 'PWM');
matrix.gpio.setFunction(5, 'PWM');

matrix.gpio.setMode(0,'output');
matrix.gpio.setMode(1,'output');
matrix.gpio.setMode(2,'output');
matrix.gpio.setMode(3,'output');
matrix.gpio.setMode(4,'output');
matrix.gpio.setMode(5,'output');

////////////////////////////////////THERE METHOD
methods.roboticarmThere = function() {
{
    for(i_0 = 75; i_0 <= 155; i_0++) {    // Rotating base servo
        matrix.gpio.setServoAngle({
            pin: 0,
            angle: i_0,
            min_pulse_ms: 0.2
        });
        for(i_delay0 = 0; i_delay0 < 2000000; i_delay0++) {
            ///////////////// delay servo 
        }
    };
}
{    
    for(i_2 = 100; i_2 <= 135; i_2++) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };    
}
{
    for(i_5 = 105; i_5 <= 135; i_5++) {    // Mechanical claw servo
        matrix.gpio.setServoAngle({
            pin: 5,
            angle: i_5,
            min_pulse_ms: 0.2
        });
        for(i_delay5 = 0; i_delay5 < 2000000; i_delay5++) {
            ///////////////// delay servo pinza
        }
    };
}
{
    for(i_2 = 135; i_2 >= 100; i_2--) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };
}
{    
        for(i_0 = 155; i_0 >= 45; i_0--) {    // Rotating base servo
        matrix.gpio.setServoAngle({
            pin: 0,
            angle: i_0,
            min_pulse_ms: 0.2
        });
        for(i_delay0 = 0; i_delay0 < 2000000; i_delay0++) {
            ///////////////// delay servo base
        }
    };    
}
{
    for(i_2 = 100; i_2 <= 135; i_2++) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };
}
{
    for(i_5 = 135; i_5 >= 105; i_5--) {    // Mechanical claw servo 
        matrix.gpio.setServoAngle({
            pin: 5,
            angle: i_5,
            min_pulse_ms: 0.2
        });
        for(i_delay5 = 0; i_delay5 < 2000000; i_delay5++) {
            ///////////////// delay servo pinza 
        }
    };
}
{
    for(i_2 = 135; i_2 >= 100; i_2--) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };
}
{    
        for(i_0 = 45; i_0 <= 75; i_0++) {    // Rotating base servo
        matrix.gpio.setServoAngle({
            pin: 0,
            angle: i_0,
            min_pulse_ms: 0.2
        });
        for(i_delay0 = 0; i_delay0 < 2000000; i_delay0++) {
            ///////////////// delay 
        }
    };    
}
   console.log("Robotic Arm Have Been Turned There");
};
////////////////////////////////////HERE METHOD
methods.roboticarmHere = function() {
{
    for(i_0 = 125; i_0 >= 75; i_0--) {    // Rotating base servo
        matrix.gpio.setServoAngle({
            pin: 0,
            angle: i_0,
            // minimum pulse width for a PWM wave (in milliseconds)
            min_pulse_ms: 0.2
        });
        for(i_delay0 = 0; i_delay0 < 2000000; i_delay0++) {
            ///////////////// delay servo base 
        }
    };
}
{    
    for(i_1 = 90; i_1 >= 85; i_1--) {    // Second axis servo
        matrix.gpio.setServoAngle({
            pin: 1,
            angle: i_1,
            min_pulse_ms: 0.2
        });
        for(i_delay1 = 0; i_delay1 < 2000000; i_delay1++) {
            ///////////////// delay
        }
    };
}
{
    for(i_2 = 120; i_2 >= 100; i_2--) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };
}
{
    for(i_3 = 135; i_3 >= 120; i_3--) {    // Fourth axis servo
        matrix.gpio.setServoAngle({
            pin: 3,
            angle: i_3,
            min_pulse_ms: 0.2
        });
        for(i_delay3 = 0; i_delay3 < 2000000; i_delay3++) {
            ///////////////// delay
        }
    };
}
{
    for(i_0 = 75; i_0 >= 45; i_0--) {    // Rotating base servo
        matrix.gpio.setServoAngle({
            pin: 0,
            angle: i_0,
            min_pulse_ms: 0.2
        });
        for(i_delay0 = 0; i_delay0 < 2000000; i_delay0++) {
            ///////////////// delay  
        }
    };
}
{
    for(i_2 = 100; i_2 <= 135; i_2++) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };
}
{
    for(i_5 = 105; i_5 <= 135; i_5++) {    // Mechanical claw servo
        matrix.gpio.setServoAngle({
            pin: 5,
            angle: i_5,
            min_pulse_ms: 0.2
        });
        for(i_delay5 = 0; i_delay5 < 2000000; i_delay5++) {
            ///////////////// delay servo pinza 
        }
    };
}
{
    for(i_2 = 135; i_2 >= 100; i_2--) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };
}
{    
        for(i_0 = 30; i_0 <= 155; i_0++) {    // Rotating base servo
        matrix.gpio.setServoAngle({
            pin: 0,
            angle: i_0,
            min_pulse_ms: 0.2
        });
        for(i_delay0 = 0; i_delay0 < 2000000; i_delay0++) {
            ///////////////// delay servo base
        }
    };    
}
{
    for(i_2 = 100; i_2 <= 135; i_2++) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };
}
{
    for(i_5 = 135; i_5 >= 105; i_5--) {    // Mechanical claw servo
        matrix.gpio.setServoAngle({
            pin: 5,
            angle: i_5,
            min_pulse_ms: 0.2
        });
        for(i_delay5 = 0; i_delay5 < 2000000; i_delay5++) {
            ///////////////// delay servo pinza
        }
    };
}
{    
    for(i_2 = 135; i_2 >= 100; i_2--) {    // Third axis servo
        matrix.gpio.setServoAngle({
            pin: 2,
            angle: i_2,
            min_pulse_ms: 0.2
        });
        for(i_delay2 = 0; i_delay2 < 2000000; i_delay2++) {
            ///////////////// delay
        }
    };    
}
{    
    for(i_0 = 155; i_0 >= 75; i_0--) {    // Rotating base servo
        matrix.gpio.setServoAngle({
            pin: 0,
            angle: i_0,
            min_pulse_ms: 0.2
        });
        for(i_delay0 = 0; i_delay0 < 2000000; i_delay0++) {
            ///////////////// delay servo base
        }
    };    
}
   console.log("Robotic Arm Have Been Turned Here");
};
module.exports = methods;// Export methods in order to make them avaialble to other files

 Copy and paste this files into the /home/pi/lite_js folder of your Raspberry Pi board. You can download this files on the "Code" section.

Notes:

Discussions