Mini Project

In this Project I plan on remotely controlling a two-wheeled car I built via bluetooth.

I firstly wanted to get familiar with changing the state of the robot by serial, so I built a simple circuit with three LED’s and code that has three different states; one state for each LED to be turned on, then a python script to send different characters via a USB to serial port.

Here is a circuit diagram of the system:

LEDserial

Here is a picture of the circuit:

IMG_3431

IMG_3429

Here is my code:


//
// Testing serial control with USB serial to dsPIC30F4011
// Written by Aron Horan
// 10 Dec 2013
//
 
#include <xc.h>
#include <libpic30.h>
 
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

 
int main(void)
{
	int state = 0;
    char g, h, j, c; 
 
    TRISB = 0;
    
    // Configure UART
	U1BRG = 48;            // 38400 baud @ 30 MIPS
	U1MODEbits.UARTEN = 1; // Enable UART
	

	while(1)
	{
		// Check if any characters were received via UART
        if (U1STAbits.URXDA == 1)     
        {
            // Check that first character was 'f,g or h'
            c = U1RXREG;
            if (c == 'g') state = 1;
            else if (c == 'h') state = 2;
            else if (c == 'j') state = 3;
        }

		//LED on RD0
		if (state == 1) 
		{
			LATB = 0b001;
		}
		
		//LED on RD1
		if (state == 2)
		{
			LATB = 0b010;
		}
		
		//LED on RD2
		if (state == 3)
		{
			LATB = 0b100;
		}
	}
	return 0;
}

I firstly wanted to setup the python to send a char every three seconds, after I get that going I will change it so that it will take inputs from my keyboard.
I also am using a USB to serial port for the moment, once I get that up and running I will setup the bluetooth serial port and control remotely.

Here is my python code that sends a different char every three seconds:


import time
import serial

# configure the serial connections
ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=38400,
   
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.close()
ser.open()
	
while True:

	print "g"
	ser.write("g")
	time.sleep(3)
	
	print "h"
	ser.write("h")
	time.sleep(3)
	
	print "j"
	ser.write("j")
	time.sleep(3)


Here is a video of it in action:

Now that I have that working I have edited the python script to recieve inputs from the keyboard to change state.

Here is the new python code:


import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=38400,
   
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.close()
ser.open()
	
while True:

#print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
	
	# get keyboard input
	input = raw_input(">> ")
        # Python 3 users
        # input = input(">> ")
	if input == 'exit':
		ser.close()
		exit()
	else:
		# send the character to the device
		# (note that I happend a \r\n carriage return and line feed to the 			characters - this is requested by my device)
		ser.write(input + '\r\n')
		out = ''
		
		# wait half a second before reading output (give device time 			 	     # to answer)
		time.sleep(0.5)
		while ser.inWaiting() > 0:
			out += ser.read(1)
			
		if out != '':
			print ">>" + out

The only thing is the I must press enter (return) for the char to be sent. My next stage I will overcome this problem. Also instead of using the USB to serial port I will be using a serial Bluetooth device so I can transmit wirelessly.

So I finally got it working.

Its wirelessly switching a few LEDs (wow!) but now I have the power to do a lot more with it.

I ended up using Tkinter to create a canvas with 3 buttons. Each button sends a different character to the PIC, changing it between three different states.

My C programm is exactly the same as above the only difference is the python script.

Here is my Python script:


import time
import serial
import Tkinter
 
# Define a keyboard callback function. This will be called
# every time a key is pressed anywhere in the window.
def key(event):
    print "Sending", repr(event.char)
    ser.write(event.char)
 
# Callback function for the 'A' button
def a_press():
    print "Sending 'g'"
    ser.write('g')
 
# Callback function for the 'B' button
def b_press():
    print "Sending 'h'"
    ser.write('h')
 
# Callback function for the 'C' button
def c_press():
    print "Sending 'j'"
    ser.write('j')
 
# Open first serial port
ser = serial.Serial(port='/dev/rfcomm0', baudrate = 9600, timeout=1)
 
# Create a Tk GUI window
root = Tkinter.Tk()

# create canvas with a width and height
Tkinter.Canvas(root, width=600, height=600)

# Add title to window
root.title("ARONS BLUETOOTH RC CAR")
 
# Bind all keypresses to the "key" function (define above)
root.bind("<Key>", key)
 
# Create a few buttons and connect each one to a function
a = Tkinter.Button(root, text='A', command=a_press)
b = Tkinter.Button(root, text='B', command=b_press)
c = Tkinter.Button(root, text='C', command=c_press)
 
# Lay out buttons in the GUI
a.grid(row=0, column=0, sticky='EW')
b.grid(row=1, column=0, sticky='EW')
c.grid(row=2, column=0, sticky='EW')
root.columnconfigure(0, weight=1)
 
# Enter the Tkinter main event loop. The program will stay
# in this event loop until it's time to exit.
root.mainloop()
 
# Close serial port
ser.close()
	

Here is a video of it in action:

Advertisements

3 thoughts on “Mini Project

  1. This is brilliant! You’ve tackled a few different problems here really, so there’s a lot of work in this. I know you had a few struggles along the way, but it’s great to see you combining Linux, Python, Tkinter, Bluetooth, dsPIC, etc in one demo. All these things you’ve learned are sure to come in very useful in the future.

    I really like how you’ve done your final video. The ability to document technical work this way is becoming more and more important. Being able to describe your work like this is almost as important as being able to do the work in the first place. This is a great example of how to do it. I’ll be sure to show this to next year’s students to help them see how important narration is!

    Overall, really excellent work! Well done.

    Ted

  2. Hi Bro,

    Can you share the whole circuit that connect PIC and HC-05,

    Thank bro

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s