First part of project : Manual control

The first part of project is terminated and now it is time to explain more about the conception of this system. At the begining I must remind you that this project has been programmed completely using node.js tools which enable programmers to make application related to web and networking. It can be also used for local application for real time data transfers using Wi-Fi , Bluetooth , RF modules and differents sensors but the main purpos remains for web activities. As the format of node.js shows , applications are written in Javascript language which can be understood by the common human. It is much easier and more robust than Java and also from diffrenet languages. Node.js applicatipon can be developped and run in different environnement such as Linux , Microsoft Windows

Different technologies for the first part of the project :

Intel Edison Kit forArduino

b2ap3_thumbnail_photo-2.JPG

 

 

It is clear that this programming card is too much complicated for this part of project which consist of control the APMC via game controller using a Bluetooth Connection. But I should say that this card will be useful later for GPS and GSM modules connections and also for diffrerent sensors which will be installed directly on this machine.Other adventage of this card is its flexibility , in fact it offers several important adavantages: It has a preinstalled Bluetooth model based on BLE technology with a lower consumption.It includes also PWM ports which give this possibility to control motors of the APMC.Intel Edison Board is equiped of analog and digital pins which have capacity to be set as input or output. It can be programmed easily by using different softwares like for example Arduino or Intel Iot XDK Edition.I must notice that in this project we programmed this card by entering directly into the kernel of this board. I have not used neither the arduino software for programing nor the usual codes for Bluetooth device detection.

It must be said that for security reasons , codes to handle Bluetooth connection do not work properly. In fact the board is unable to find the serial port channel of Bluetooth and that's why the connection is refused each time. This code is shown below:

var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();
 
btSerial.on('found', function(address, name) {
    btSerial.findSerialPortChannel(address, function(channel) {
        btSerial.connect(address, channel, function() {
            console.log('connected');
 
            btSerial.write(new Buffer('my data', 'utf-8'), function(err, bytesWritten) {
                if (err) console.log(err);
            });
 
            btSerial.on('data', function(buffer) {
                console.log(buffer.toString('utf-8'));
            });
        }, function () {
            console.log('cannot connect');
        });
 
        // close the connection when you're ready 
        btSerial.close();
    }, function() {
        console.log('found nothing');
    });
});
 
btSerial.inquire();

This code which is written in Javascript has been provided by NPM archive package named bluetooth-serial-port from author 'eelco'. Unfortunatly this code for reasons mentionned up can not work.
Consequently , I decided to chose another strategy which consist of accesing the kernel file that is created automatically when a bluetooth device is paired and connected. My goal was to receive instantly all incoming data from Bluetooth module and after be able to analyze it and decide for motor controlling and etc, according to a protocole.
In order to achieve this goal , I decided to create my own library using EVTEST tool which is included in Intel Edison documentation. EVTEST tool is a programme written in C language giving possibility to receive not only all incoming data from Bluetooth but also from any different type of devices. In case you want to use this application , you must have at least GCC compiler to generate binary files.  All files of this programm are open source and are available at next hyper link : http://cgit.freedesktop.org/evtest/ .
After analyzing source code in C language , I tried to adapt these codes to CPP language format in order to make them compilable by G++ compiler. Then I created a full package NPM library enabling to receive all of incoming data from different devices. These libraries are availabe in next link and can be used as shown in this example.

Below , can be seen the code that I used to control the behaviour of the APMC according to orders arrived from the controller :

/* Copy-Right 2016 - Author : Afshin KHALGHDOOST */

/* definition of objects and call for libraries */
var addon = require('./build/Release/addon');
var obj = new addon.DS4("/dev/input/event2");

/* call for cylon */
// Cylon is the name of library which enable you to control motors , wheels and so on.
// for more information , please visit http://cylonjs.com
var Cylon = require('cylon');

Cylon.robot({ connections: { edison: { adaptor: 'intel-iot'} // definition of device in order to use different fucntionalities }, devices: { // definition of differents parameters for different motor control // In this example there are 4 parameters //motor_rear1: to control backward movement //motor_rear2: to control forward movement //motor_front1: to control left direction //motor_front2: to control right direction motor_rear1: { driver: 'motor', pin: 3, connection: 'edison' }, motor_rear2: { driver: 'motor', pin: 6, connection: 'edison' }, motor_front1: { driver: 'motor', pin: 5 ,connection: 'edison' }, motor_front2: { driver: 'motor', pin: 9 ,connection: 'edison' } }, work: function(my) { // function to define right movement function right(){ my.motor_front1.speed(250); my.motor_front2.speed(0); } // function to define left movement function left(){ my.motor_front1.speed(0); my.motor_front2.speed(250); } // function to define mddle position for wheels function middle_position(){ my.motor_front1.speed(250); my.motor_front2.speed(250); } // Initialization fo wheels movement // blocking all motors function init(){ my.motor_rear1.speed(250); my.motor_rear2.speed(250); my.motor_front1.speed(250); my.motor_front2.speed(250); } init(); while(true){ // show_flows is a function of our library which can search permanently for Bluetooth devices // when a connection with Bluetooth device is established , data are immedialtely analyzed obj.show_flows(); // Rz means when user move the right stick of game controller // for these next 3 if conditions , thse speed is linear to the movement // the more user moves the right stick , the more speed increases if (obj.value()<160 && obj.value()>90 && obj.identifier() == 'Rz'){ my.motor_rear1.speed(250); my.motor_rear2.speed(250); } if (obj.value()<90 && obj.identifier() == 'Rz'){ my.motor_rear1.speed(250); my.motor_rear2.speed(obj.value()); } if (obj.value()>160 && obj.identifier() == 'Rz'){ my.motor_rear2.speed(250); if ( obj.value() < 250) my.motor_rear1.speed(250-obj.value()); } // Hat0X means right or left directional buttons on PS4 games controlelr if (obj.value()==1 && obj.identifier() == 'Hat0X') left(); if (obj.value()==-1 && obj.identifier() == 'Hat0X') right(); if (obj.value()==0 && obj.identifier() == 'Hat0X') middle_position(); if (obj.value()==0 && obj.identifier() == 'Hat0X') middle_position(); } } }).start();

 Here is general algorithm shceme for a better understanding of this code :

 rsz screenshot from 2015 07 08 162036

In order to use these libraries , they must be included via Intel IoT XDK Edition software where the main code must be written in main.js file . Package.json must also be precised during upload and build process.
All files are available for downloading at the bottom ot this article.


Game Controller :
For the controller I decied to use a normal game controller available on market. I chosed a PlayStation 4 controller. I insist on the fact that any kind of game controller can be used but in this case I settled for a PS4 game controller.

b2ap3_thumbnail_photo-4.JPG

rsz screenshot from 2015 07 08 165939

These information are provided by http://www.ps4playstation4.com


Motor drive :
The motor drive that has been used is named (L9110) Dual Channel Motor Driver Module. This motro device enable us to control maximum 2 motors. The first one , I used for wheels acceleration and the other for wheels direction. The power is about 12 V and energy pins are connected directly to the batteries.
One of the specificities of this motor device is that there are 4 outputs. For each motor , 2 outputs are dedicated. You have to connect the VCC and GND to these 2 pins. By permuting VCC and GND , you only change the direction of wheels.

rsz screenshot from 2015 07 08 165945

Essential tutorial:
Now the question is to ask how we can execute automatically this programm at system start up ?
Well, to answer this question , we must know the opertaion of Intel Edison. It must be noticed at system start up of this board which has a Linux as operating system , every essentiel software and hardware functionalities are initialized and then set to stand by. Each of this functionality has a file in the kernel and exactly in /lib/systemd/system
You must know that if you want your programm be launched automaticaly at start up , you have to create a kernel file in that path mentionned above.But before starting, you should be aware that the work of your programm depends on activation of Bluetooth functionality. With Intel Edison , the default settings is configured to disable automatically Bluetooth functionality for each reboot. So in this small tutorial we begin by creating a Bluetoth kernel file to launch automatically Bluetooth functionality by avoiding to ente rrfkill unblock bluetooth command manually.
To create this kernel file you have to follow next easy steps :

1) You open gedit or other text editor and you put next code inside:

 [Unit]
 Description=BTLAUNCHER
 After=bluetooth.target
 Before=systemd-user-sessions.service
 [Service]
 Type=simple
 RemainAfterExit=true
 ExecStart=/usr/sbin/rfkill unblock bluetooth
 Environment="HOME=/home/root"
 WorkingDirectory=/home/root/
 [Install]
 WantedBy=multi-user.target


2) You save this code as BTLAUNCHER.service
Don't forget that .service format is reserved to be used by system.

3) You put this file into /lib/systemd/system/ . Be aware that you have to change rights. To avoid all problems I used next line command :

chmod 777  BTLAUNCHER.service


4) You must activate the file that you have just created :

systemctl enable /lib/systemd/system/BTLAUNCHER.service


and then

systemctl start BTLAUNCHER.service


** You can monitor this kernel file activity by using next line command:

 journalctl -f -u BTLAUNCHER.service


5) Reboot

After creating Bluetooth kernel file , you can now create another kernel file to launch automatically this programm by following previous steps and change only the setp 1 code. Use next code :

[Unit]
Description=MyProg
After=bluetooth.service
Requires=bluetooth-rfkill-event.service
Requires=blink-led.service 
[Service]
TimeoutStartSec=0
Type=simple
RemainAfterExit=true
ExecStart=/usr/bin/node /home/root/.node_app_slot/main.js
[Install]
WantedBy=multi-user.target

 
dont't forget that on line 10 , you have to specify the path where node is installed followed by the path where you main.js file and all other files are gathered and uploaded inside your Intel Ediso card.
Please note that you have to change the .service file name and follow exactly steps which are mentionned above.

 

Rate this blog entry:
0
Mobile Biometric Data Monitoring
Autonomous Prototype Model Car
 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Guest
Saturday, 26 May 2018

Captcha Image