MATLAB GUI and SERVO ULTRASONIC CONTROL

Hi,

I worked on a project to control servo and ultrasonıc sensor with MATLAB. Also, I designed GUI to plot real time data. Lets analyze the codes, Mbed part and MATLAB part.

Servo & Ultrasonic Mbed Code

There are ultrasonic sensor HCSR04, servo motor and LCD codes. Sensor is placed onto servo motor to rotate as in the Figure 1. /media/uploads/Birkan/12.png

main.cpp

#include "mbed.h"
#include "Servo.h"
#include "hcsr04.h"
#include "TextLCD.h"

Serial pc(USBTX, USBRX);         
TextLCD lcd(p15, p16, p17, p18, p19, p20);
Servo myservo(p21);  
DigitalOut hedef_var(LED1);
DigitalOut working(LED2);
DigitalOut open(LED3);
DigitalOut close(LED4);
HCSR04  usensor(p25,p9);  

int dist;  
float p;

int main() 
{

working=1;  

while(1)
  {     
    
    
      for(p=0.0; p<=1.0; p += 0.05)  
         { 
           usensor.start();                               
           wait_ms(500);      
           dist=usensor.get_dist_cm();                   
           lcd.cls();                   
           lcd.locate(0,0);             
           lcd.printf("Obs Range cm:%ld",dist);            
           printf("%d\n ",dist); 
           printf("%f\n ",p);    
                                          
         
              if(dist<50)                                 
               { 
                 lcd.cls();
                 lcd.locate(0,0);
                 lcd.printf("Obs Range cm:%ld",dist);
                 lcd.locate(0,1);
                 lcd.printf("location :%f",p);
                 hedef_var=1; 
                 }
                 
                 else if(dist>50)
                 {
                  hedef_var=0;
                     
                 }
                
   
           myservo = p;
           wait(0.2); 
           

        }
          
       for(p=1.0; p>0.0; p -=0.05)
         {
           usensor.start();
           wait_ms(500);     
           dist=usensor.get_dist_cm(); 
           lcd.cls();                   
           lcd.locate(0,0);             
           lcd.printf("Obs Range cm:%ld",dist); 
           printf(" %d\n ",dist); 
           printf("%f\n ",p);    
                                          
              if(dist<=50)
               { 

     
                 lcd.cls();
                 lcd.locate(0,0);
                 lcd.printf("Obs Range cm:%ld",dist);
                 lcd.locate(0,1);
                 lcd.printf("location :%f",p);
                 hedef_var=1;
         
                 }
                 else if(dist>50)
                 {
                  hedef_var=0;
                 }
                
  
           myservo = p;
           wait(0.2); 


           }

          }
       
 
    
    
     }
  

Serial Communication Between MATLAB & Mbed

Serial Com.

There is a MATLAB RPC Library but i did not use it. I used default serial port settings. However, you can change them according to your designes.

 mbed = serial('COM5', ...
                  'BaudRate', 9600, ...
                  'Parity', 'none', ...
                  'DataBits', 8, ...
                  'StopBits', 1);       %change depending on mbed configuration
 TIMEOUT = 1;                           %time to wait for data before aborting
 set(mbed,'Timeout',TIMEOUT);           %adjust timeout to ensure fast response when mbed disconnected
 fopen(mbed);                           %open serial connection
 

OTHER FILES WILL BE UPDATED SOON...


Please log in to post comments.