Skip to main content

Featured Post

Product Design of a 4 wheel differential drive robot

Use Matlab to read serial data

 

Matlab is one big tool which you can play with.. Smile Matlab can be configured to read serial data that can be send through a microcontroller. Here is the code for Matlab to read a single line and print. Assume that you are sending ascii character values through microcontroller separated by new line.

Here is the Matlab code to receive the data.

   1: obj1 = instrfind('Type', 'serial', 'Port', 'COM13', 'Tag', '');
   2: % Create the serial port object if it does not exist
   3: % otherwise use the object that was found.
   4: if isempty(obj1)
   5: obj1 = serial('COM13');
   6: else
   7: fclose(obj1);
   8: obj1 = obj1(1)
   9: end
  10: obj1.BaudRate=4800;
  11: set(obj1, 'Parity', 'even');
  12: set(obj1, 'StopBits', 2.0);
  13: fopen(obj1);
  14: data1=fscanf(obj1, '%s');
  15: data1=fscanf(obj1, '%s'); %Assume that you are sending characters and 8 bit binary no.s
  16: data1;                    %Use a=fread(obj1); if it's not charecter
  17: fclose(obj1);
  18: delete(obj1);  
  19: clear obj1;

 

Here is the code for AVR Microcontroller to send ‘1024’ through serial port.

 


   1: /*
   2:  * Ploting_ADC_Values.c
   3:  *
   4:  * Created: 22-10-2013 09:44:54
   5:  *  Author: ANANTHAKRISHNAN U S
   6:  */ 
   7:  
   8:  
   9:  
  10: #define F_CPU 1000000UL
  11: #define FOSC 1000000 
  12: #define BAUD 4800
  13: #define MYUBRR FOSC/16/BAUD-1
  14:  
  15: #include <avr/io.h>
  16: #include <util/delay.h>
  17: #include <stdio.h>
  18:  
  19: static int uart_putchar(char c, FILE *stream);
  20: static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,_FDEV_SETUP_WRITE);
  21:  
  22:                                              
  23: void USART_Init( unsigned int ubrr);
  24: static int uart_putchar(char c, FILE *stream);
  25: char uart_getchar(FILE *stream);
  26:  
  27: int main(void)
  28: {
  29:     USART_Init(MYUBRR);                  //Initializing USART
  30:     stdout = &mystdout;
  31:     int i =1024;
  32:     while(1)
  33:     {
  34:         printf("%d\n",i);
  35:         _delay_ms(50);
  36:     }
  37: }
  38:  
  39: void USART_Init( unsigned int ubrr)
  40: {
  41:     /*Set baud rate */
  42:     UBRR0H = (unsigned char)(ubrr>>8);
  43:     UBRR0L = (unsigned char)ubrr;
  44:     //Enable receiver and transmitter */
  45:     UCSR0B = (1<<TXEN0) | (1<<RXEN0);
  46:     /* Set frame format: 8data, 2stop bit */
  47:     UCSR0C = (1<<USBS0)|(3<<UCSZ00)|(1<<UPM01);//Asynchronous 8bit data Even parity 2 stop bit
  48: }
  49:  
  50: static int uart_putchar(char c, FILE *stream)
  51: {
  52:      if (c == '\n') {
  53:         uart_putchar('\r', stream);
  54:     }
  55:    // Wait for empty transmit buffer */
  56:    while ((UCSR0A & (1 << UDRE0)) == 0) {};
  57:    // Put data into buffer, sends the data */
  58:    UDR0 = c;
  59: }
  60:  
  61:  
  62: char uart_getchar(FILE *stream) {
  63:     
  64:     while ((UCSR0A & (1 << RXC0)) == 0) {};
  65:      /* Wait until data exists. */
  66:     return UDR0;
  67: }

 

 

Comments

Popular posts from this blog

Plymouth theme for Ubuntu

Bored of having the same boot animation screen again and again? There are plenty of Plymouth themes available out there. This is a theme I created by slicing some cool gif files I found online. You can download the theme and find the installation steps on my github account. ( https://github.com/krishnan793/PlymouthTheme-Cat )  The theme is created for Ubuntu 16.04. But this can be installed on previous versions with slight modification. If you have a slow computer then you can watch the whole animation loops. (The VM I used to record the screen was fast though. :))

Remote access your BeagleBone Black using vnc

Before going into how to share desktop with your BBB I assume you have a proper ssh access to your BBB. (through USB) (if not read this ) Also this tutorial is for BBB with Debian installed (type cat /etc/*-release to know which distro you are currently using. Or refer here ) First install the vnc server in BBB.(After logging into BBB) ssh root@192.168.7.2 apt-get install x11vnc Then go to your local computer and open a terminal window. Install a vnc viewer like vinagre. sudo apt-get install vinagre Now its time to start vnc server in BBB. x11vnc -auth /var/run/lightdm/root/:0 -forever This will start a vnc server at port 5900. Note this server setup is temporary. If you want to remotely connect to your BBB after a reboot type the above command again. Go to your local computer and type  vinagre 192.168.7.2::5900 A remote desktop will be shown if all goes well. Try exploring other options in x11vnc using (in BBB) man x11vnc The se

Product Design of a 4 wheel differential drive robot

I have been thinking of creating a small mobile robot from scratch. From scratch means, go through all the steps of a product development. Design the overall architecture, design the circuits, make 3D model of the parts, 3D print the parts and finally integrate with ROS. The methods I followed may not be the best practice. The only aim is to reach from idea to actual product (prototype). Features: 4 Wheel Differential Drive Camera WiFi IMU GPS(optional) Overview A 4 wheeled differential mobile robot with wheel encoders, camera, WiFi, IMU and GPS(optional). The first thing I did was to come up with an overall architecture. Raspberry pi 3 will be the heart of the robot. Pi Zero was considered in the beginning, but eventually had to  change because of the lack of support for ROS. Even though able to compile ROS from scratch, it became so hard to compile other packages needed for the project. Since I do not want to use pi to directly control motors, I had to design a