GUI controlled bot

Well in this post I’ll be covering how to create a GUI for controlling a bot using MATLAB. Now this concept can be readily extended to various other stuff. As after learning alphabets one can make words and sentences and essays so can one make various applications using basics. So let’s start, shall we?

To begin with you need to read this post on creating GUI because I’ll be assuming you have read that. So we know how to make buttons now. So this tutorial will focus on using this GUI along with serial class of MATLAB.

Serial Communication in MATLAB

Well we all know man is a social animal. We need someone or the other to communicate with others. We may use words or gestures or expressions etc. Similarly MATLAB needs to communicate with the outside world to get data in order to manipulate the data and send the processed result back. The way to communicate is via RS232 protocol.

There is a serial class in MATLAB that lets you to create objects and manipulate its properties.

Syntax:

obj = serial(‘port’)

Here port is the COM name and obj is the object name. You can change the baud rate and other properties like number of party bits and stop bits etc. I won’t be covering that in this post. If you have further interest you may refer to their website.

Now that we have a object for the serial COM port we need to open that port for serial communication. Now the syntax for the same is as follows.

fopen(obj)

This will open the COM port for communication. Similarly there is fclose(obj) that will close the port.

Now that we know how to open and close the port might as well learn how to write data to the port.

fwrite(obj,data)

Here obj is the object created and data is 1 byte data that you want to send/write on the port.

For example :

fwrite(s,’a’)

Well that takes care of the serial part that is required for our task. Remember that internet is a universe of knowledge and you can travel it to unravel new mysteries whenever you like.

Now many of you out there who are programmers are already aware of the concept of global and local variables. We’ll require this concept as well. For creating a global object just type global keyword before the object. Now whenever you want to access the global version of the variable just type global before the object or variable.

Now in the GUI code you need to create a global object, open the port. Then the logic we’ll be using for making this GUI work is as follows:

1) Write code in the button callback function to send a specific character. For example send ‘W’ for forward and so on.

2) Now the controller comes into picture wherein you have to receive the character and take some action depending on the character received.

So basically you need to use the UART module of your controller whether it be msp430 or PIC or 51 or arduino or atmel or and processor for that matter. The logic is simple if you are using embedded c or assembly language you can write a simple ISR that will check the character received serially and send some data to port pins that will drive the motor via the motor driver.

I think this much explanation is enough. If you have any queries you can always mail me or comment, I’ll get back to you as soon as possible.

Code

/*
 * serial_reception.c
 *
 *  Created on: 30-Mar-2014
 *      Author: MANPREET
 *     Website: https://learningmsp430.wordpress.com/
 */

#include <msp430g2553.h>
#include "serial.h"

void main(void)
{
	WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
	uart_init(); // Refer the UART post for the functions
	P1DIR |= BIT3|BIT4|BIT5|BIT6;
	P1OUT = 0;
	 IE2 |= UCA0RXIE;     // Enable USCI_A0 RX interrupt
	 __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}

#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
 if(UCA0RXBUF=='W' | UCA0RXBUF == 'w')
 {
	 P1OUT |= BIT3|BIT5;
	 P1OUT &= ~(BIT4|BIT6);// Send 1010 for forward
 }
 if(UCA0RXBUF=='S' | UCA0RXBUF == 's')
 {
	 P1OUT |= BIT4|BIT6;
	 P1OUT &= ~(BIT3|BIT5);// Send 0101 for backward
 }
 if(UCA0RXBUF=='A' | UCA0RXBUF == 'a')
 {
	 P1OUT |= BIT4|BIT5;
	 P1OUT &= ~(BIT3|BIT6);// Send 0110 for left
 }
 if(UCA0RXBUF=='D' | UCA0RXBUF == 'd')
 {
	 P1OUT |= BIT3|BIT6;
	 P1OUT &= ~(BIT4|BIT5);// Send 1001 for right
 }
 if(UCA0RXBUF=='F' | UCA0RXBUF == 'f')
 {
	 P1OUT &= ~(BIT3|BIT6|BIT4|BIT5);// Send 1001 for right
 }
}

MATLAB Code link:

https://www.dropbox.com/s/iyddas4ynf93rz5/project_bot.fig

https://www.dropbox.com/s/78fzdopyci2g5km/project_bot.m

GUI:

GUI

 

Note :

The connections are obvious if you read the code. You have to use a driver like L293d of l298. If you face any difficulties doing this feel free to contact me. I’m happy to help.