Saturday, 29 March 2014

Arduino and Matlab interfacing via Bluetooth module

The post is about interfacing Arduino with MatLab via Bluetooth module

1). First of all, connect the Arduino with Bluetooth module as per the diagram given below.
Vcc to 5V
GND to GND
RXD to RX   
TXD to TX    

2). Now you need to upload code to the Arduino board. The code depends on which values you need to send from Arduino or receive to Arduino through MatLab.

Example code for receiving the values from Arduino is

    int a1=0;
    int a2=0;
    int val=0;
   
    void setup()
    {
    Serial.begin(9600);
    }
    
    void loop()
    {
         a1=analogRead(0);
         a2=analogRead(1);
         if (Serial.available() >0) {

    /* whatever is available from the serial is read here    */
    val = Serial.read();
   
    {
     
      if(val==10)
      {
        Serial.println(a1);
      }
      else
            if(val==20)
      {
        Serial.println(a2);
      }
    }
  }



Burn this code to the Arduino board.

3). Then pair your Bluetooth device to the computer.

4). Open MatLab and find your Bluetooth device detected as follow
>> instrhwinfo('Bluetooth')

for more information about your device use this
>> instrhwinfo('Bluetooth','HC-05')



create a Bluetooth variable and open it as follow
    >>b = Bluetooth('HC-05',1);
    >>fopen(b);


After this for getting the two values regularly from Arduino use this code

>>while(1)   
    fwrite(b,10);
    q=fscanf(b,'%d');
    fwrite(b,20);
    z=fscanf(b,'%d');

end

yes that's all, you will get regular values from Arduino

Remove infinite while loop if you want these values once

>>fwrite(b,10);
>>q=fscanf(b,'%d');
>>fwrite(b,20);
>>z=fscanf(b,'%d');

 
You can modify this code or make your own code according to your needs.

***if Bluetooth module is not sending and receiving data from Arduino then exchange the tx,rx connections
RXD to RX  -->  RX to TX
TXD to TX   -->  TX to RX
 ***

Thank you for reading.
Ask if any questions in the comments.

//////////////////////////////////////////////////////////////////
My free android apps.
https://play.google.com/store/apps/developer?id=Gaur+Tavm
Thank you.

Tuesday, 11 March 2014

3 sensor Line follower Arduino code(without PID) but just like PID

Line follower bot is basically a robot that follows a black line. If you have already made a simple line follower robot and looking forward to improve its performance than you are at the right place. To make an improvement to the performance of anything feedback is the best choice. In this post, a new method of using feedback is introduced which you can use to improve your line follower bot.
 
You will find most people use PID feedback to improve the performance of a system. PID controller has its own challenges. It is very hard to code and tune.  But here we are looking for something more simple and easy. The method discussed in this post is best suited with at least three sensors but you can try it two sensors but just little adjustments in code.
Let us start discussing the method. The basic idea is to memorize the current readings of the line follower bot at the end of the loop. In the next loop, If the bot finds itself out of line it trusts on reading taken in the previous loop. So our line follower bot becomes more intelligent by feedback and can deal with such a situation by taking suitable actions.

Example of three sensor bot: Let's say there are three sensors giving binary readings. Here 0 means the sensor is on the line and 1 means sensor is not on the line.

The following actions are taken according to sensors reading:
* Go forward if current readings of sensors are 101.
* Go right if current readings of sensors are 110.
* Go left if current readings of sensors are 011.
* Go right if current readings of sensors are 111 and previous are 110.
* Go left if current readings of sensors are 111 and previous are 011.

Since we are using readings taken only one step before we can call it first-order feedback. You can also use the readings from the last 2 readings. In that case, the complexity of the algorithm will go high.


//The code is

int rightmotor1=12;
int leftmotor1=10;
int rightmotor2=13;
int leftmotor2=11;


int sensorleft=14;
int sensorcenter=15;
int sensorright=16;


int l1=1;
int l2=1;
int l3=1;

int f1=0;
int f2=0;
int f3=0;

//////////////////////////////////////

void setup()
{

 pinMode(rightmotor1,OUTPUT);
 pinMode(leftmotor1,OUTPUT);
 pinMode(rightmotor2,OUTPUT);
 pinMode(leftmotor2,OUTPUT);

 pinMode(sensorleft,INPUT);
 pinMode(sensorcenter,INPUT);
 pinMode(sensorright,INPUT);

}
///////////////////////////////////////////////////

void loop()
{
l1=digitalRead(sensorleft);
l2=digitalRead(sensorcenter);
l3=digitalRead(sensorright);

/////////////////////////////////////////////////////////////////////
if(l1==1&&l2==1&&l3==1)   //feedback when comes all sensor on white
{
l1=f1;
l2=f2;
l3=f3;
}

////////////////////////////////////////////////////////////////////////////
if(l1==0&&l2==1&&l3==1||l1==0&&l2==0&&l3==1)   //left turn
{
  digitalWrite(rightmotor1,1);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,0);
  digitalWrite(leftmotor2,0);
}
else
if(l1==1&&l2==1&&l3==0||l1==1&&l2==0&&l3==0)  //right turn
{
  digitalWrite(rightmotor1,0);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,1);
  digitalWrite(leftmotor2,0);
}
else
if(l1==1&&l2==0&&l3==1)  //go forward straight
{
  digitalWrite(rightmotor1,1);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,1);
  digitalWrite(leftmotor2,0);
}
else
if(l1==0&&l2==0&&l3==0) //stop
{
  digitalWrite(rightmotor1,0);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,0);
  digitalWrite(leftmotor2,0);
}

///////////////////////////////////////////////////////////////////
f1=l1;
f2=l2;
f3=l3;        //memory variables
////////////////////////////////////////////////////////////////////
}




Here f1, f2, and f3 are our feedback variable or we can say memory variables.
This is a simple code you can change the code according to our needs.

This idea is also possible with two sensors but you will see the bot oscillating on the line. This idea best works with three sensors.
Check out this post for two sensor version. https://crackeconcept.blogspot.com/2020/01/two-sensor-code-for-line-follower-using.html
You can see the video is given below in which line follower works on the same algorithm but with four sensors. That is how a three IR sensor line follower will also work.
This line follower robot is also available in the Virtual line follower app given link below. Try yourself and see its performance there.
Hope this post will help you.
 /////////////////////////////////////////////////////////////////////////////////////
My free android apps.
https://play.google.com/store/apps/developer?id=Gaur+Tavm
Thank you.