Cool Cap Engineer

Engineering by an anime nerd


7 Comments

Projects: Face Tracking Turrent

facetracking_overview
Hey guys! School is going okay-ish. I have fewer classes to take this semester, but I have a part- well contractual- time job, a lab assistant position, and a private tutor positions this semester. Not to mention an undergraduate research project to do. Speaking of which, I figured out my next project!

So the research I mentioned before is a mechatronics research. My friend and I are responsible for testing the new Mechtronics kits this semester. What’s cool about these kits is the fact that they use primarily Arduino, a Lego NXT motor and Matlab. Although I primarily use PIC microcontrollers, my experience with Arduino is much stronger than my experience with PIC microcontrollers. Lately, I came across a lot of tutorials which people are able to track their face using Matlab. Then I saw this beauty.

To make a long story short, I WANT TO CREATE THAT! I want to create a face tracking turret. Although my default choice is to use a PIC microcontroller, I might open the project to Arduino. One of the reasons I want to work on this project is not only the fact it’s wicked, but I have a final project to do for my Mechatronics class. You read that right. I’m doing Mechatronics research and taking a Mechatronics class at the same time. Ironic isn’t it (if not cheating according to my multiple friends)?

Keep in mind; I have no experience with image processing. In other words, I need to learn how to use the image processing library in Matlab. I already know how to communicate microcontrollers yo Matlab using serial communication, so I can just skip that step. What I need to figure out is whether I want my device to shoot out water, or to shoot nerf darts. This is going to be a fun project! I’ll see you guys later!


Leave a comment

Mini Projects: XBees Wireless Module and 74HC4052

20121229_130303
First off, I would like to say Happy New Year everyone! Apparently we all survived after all. Anyway, for the past couple of weeks, I started to mess around with X-Bees wireless modules. First off, what are X-Bees? X-Bees are a special type of chip that allows you communicate microcontrollers and computers together using serial communication.
Xbeeresults
There was a great deal of frustration when using these chips. First off, I soldered the X-Bees to the wrong side of the breakout board. It took me quite some time for me to realize the soldering mistake. Then I tried running the modules at an 115200 baudrate, which proved to be too much for the wireless module. Finally I got the modules communicating with each other when I programmed the PIC18F4553 to send “Hello World” to the computer. Although I managed to transmit the message wirelessly, the propagation distance was terrible. In fact, I could only get a transmission distance of a 1 foot. I began to wonder why the propagation distance was so low. Then I realized that the X-Bees I brought did not include the necessary antenna needed. After realizing this fluke, I quickly went to Sparkfun and brought the necessary antennas, which will not come in until next week.
20121229_171905
The final object I brought was a 74HC4052. What’s so amazing about this chip? It’s actually a multiplexer/de-multiplexer chip designed especially for analog and serial devices. In other words, you can connect up to 8 serial/analog devices using one microcontroller. I did an initial test which I connected my serial LCD to one pin, and the serial port of the computer to the other serial port. It took me some time make the right connections and debugged the hardware, but it worked marvelously afterwards. Overall, I think this chip is amazing! Previously, I was trying to think of a way to connect three of my serial devices using one microcontroller. However, after using chip, this will become a default option for me. In fact, you guys can expect a tutorial on how to use the chip this Friday.


Leave a comment

Projects: Pygames Controller Update #1

broken_trigger
So last Monday, my parts for all of my Pygames controller project, which was previously my N64 emulator controller project,came in. The parts included two analog sticks taken from a PS2 controller, 12 large tactile buttons, 2 triggers taken from an X-Box 360 controller, and breakout boards for the analog sticks and the X-Box 360 triggers.However, I had two very unpleasant surprises when I opened my package. The first problem was the absence of solder for the breakout boards. I had to wait until Wednesday to get the right solder for the breakout board. The last nasty surprise was one of the X-Box 360 controller triggers were broken. Since one of the triggers was broken, I’ll just make due without them. The triggers will be added to a future version of the controller.
debug_window
On Wednesday, I finally got some cheap solder from Radioshack. After going to Radioshack, I immediately soldered the analog sticks to their respective breakout board. Once I finished soldering, I quickly started to write test code for one of the analog sticks. Normally, I would use an USART terminal window like the serial window from the Arduino IDE to see the values from the analog stick, but I wanted to do something different. Instead, I used my 160×120 serial LCD as a debug window. As you see in the figure above, the debug window shows digital values associated with the horizontal and vertical pots of the joystick.

Afterwards, I started to write the actual code of the PIC MCU side of the controller. As of right now, the controller will have four listed functions:
IsControllerConnected (): Checks to see if the controller is connected to the computer. Returns 1 for true and 0 for false.
ReadJoystickX(): Read the state of the joystick’s x direction for the controller. Returns 0 for the neutral position, 1 for right and -1 for left.
ReadJoystickY (): Read the state of the joystick’s y direction for the controller. Returns 0 for neutral position, 1 for up and -1 for down.
IsButtonPressed () : Checks to see if the button is pressed. Returns 1 for true, and 0 for false.

I spent all of Thursday to write for the PIC MCU side of the controller. To make coding a little bit more organized, I decided to create 3 structures: joystick, button, and controller. Here’s the initial controller.h file.

#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON MY_BUTTON); 

I finished 3/4 of the code, until I ran into a stumbling block with creating the header files and the c files for the controller. Since my controller.h and controller.c needed to use the provided pic18f4553.h library to work, I had some difficulty figuring out how to include the file for not only my controller.c but also my main.c.However, I solved the problem by using ifndef, define, and endif.

#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON MY_BUTTON); 
#endif

From reading a couple tutorials on creating .h files, this piece of code made sure the header files included in the other .h file gets read. Although the problem with creating the .h and .c files was solved, a new problem occurred. Apparently, the function I wrote to set the ADC channel for the horizontal and vertical components of the joystick was not set, which caused a huge mess in my ADC readings. I simply solved this problem by passing my structures by reference whenever I need to use them for a function. I forgot that to edit the values from a structure, they must be passed by reference. Here’s the current version of my controller.h file.

#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER * MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK * MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK * MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON * MY_BUTTON);
#endif

I’m still trying to finish up my controller.c code, but I got it working to a point where the code can tell if the joystick is pointing left, pointing right, or in the neutral position.
20121227_231535
Hopefully I should finish the PIC MCU side of the Pygames controller sometime this weekend if not next week. Anyway, hope you guys have a good weekend!


3 Comments

Tutorials: USART Communication For PIC18 Microcontroller

In my last post, I mentioned two awesome devices I’ve been messing around with lately. The cool thing about those devices is that it only needed one actual pin from the PIC microcontroller to properly work. This pin is one of the pins needed to achieve proper USART communication. Today’s tutorial will serve as a guide in order to achieve proper USART communication. Although I mentioned that USART is used to communicate with the devices I mentioned in the last post, today’s focus will be on achieving communication between PIC18F2550 and the computer using USART.

Circuity
USART (Universal Synchronous Asynchronous Receiver Transmitter)/ UART (Universal Asynchronous Receiver Transmitter) requires only two pins from any microcontroller to send and receive serial data: TX and RX. You can properly guess that TX is the pin which the microcontroller data is transmitted through, and RX is responsible for data received by the microcontroller. The figure below shows the TX and RX pins on the PIC18F2550.
usartpic
Keep in mind; we need one more device to have the PIC18F2550 communicate with the computer. If you have an old computer, then you could use a MAX232 and connect it directly to the serial port of that computer. For this tutorial, we’re going to use a laptop to talk to the PIC. In order to do this, we’re going to use a FTDI to USB board from Sparkfun. We’re just going to use 4 pins of the FTDI to USB board: VCC, GND, TX and RX. Look at the figure below to learn how to set up the circuit. Please note that JP2 is connected to the PICKIT3.
Capture23
Code
Now, create a new project in MPLab X, create a new C file, and code and paste the following code. If you forgot how to create a project in MPlab X, visit my tutorial on starting with PIC18 microcontrollers.

#include <p18f4553.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <stdio.h>
#include <stdlib.h>

/*	PIC Configuratings */
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG= OFF

/* UART Global variables */
char message;						//	Used for receiving commands from the computer



void main(void)
{
	//	Set all of PORTC as outputs. TX and RX must be set as outputs first
	TRISC=0x00;
	
	//	Configure UART
	OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10);
        Delay1KTCYx(4);
        while(1)
        {
            while(!DataRdyUSART());
            message=ReadUSART();
            putcUSART(message);
            Delay1KTCYx(4);
        }
}

So open a Serial Terminal program. For this tutorial, I will use the Arduino IDE. Open the serial monitor in the Arduino IDE and set the baudrate to 115200. Finally, type a single character and hit send. You should see the same character you entered appear in the window.
USART_2
Important! Please Read!

Make sure you enter the right baudrate. If you do not enter the correct baudrate, you will lose data. The figure below shows what happens if we set the serial program to 9600 baud rate even though the PIC is set to a baud rate of 115200.
usart_1

But how do you know if you set the USART to the right baud rate? Let’s look at the following piece of code.

OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10);

As you can tell by the name, this is responsible for setting up the USART on the PIC. We’re going to look at the last two parameters of this command. The last two parameters correspond to the baudrate of the USART. USART_BRGH_HIGH sets the USART to a high baudrate, but we can set it to a low baudrate by replacing USART_BRGH_HIGH with USART_BRGH_LOW. The final parameter is a spbrg value. Although there are a set of equations you can use to find this value, as long as you’re using a 1Mhz, 2Mhz, 4Mhz, 10Mhz, 20Mhz, or 40Mhz crystal, you can find the SPBRG value in the datasheet. The figure below shows where we can find the spbrg value.
tableofusart
Well, I hope this helped you guys! If you have any questions, or comments, please feel free to post a comment!


6 Comments

Tutorials: Beginner’s Guide To PIC18 Microcontrollers

download

Starting today, I will focus on tutorials for  Microchip’s PIC18F series microcontrollers. Why am I shifting my focus from Atmega’s to PIC18F microcontrollers? As I stated in the first post of the N64 emulator controller, which is now Pygames Controller, I spent a semester at RPI learning how to use these microcontrollers and completely forgot how to use Atmegas. Not to mention, a lot of PIC microcontrollers are used for more commercial applications than Atmegas. If I remember correctly, Atmegas are mostly used for automotive applications while PICs are used for more commercial devices (e.g. medical devices). But enough about that! Let’s talk about how to start your first PIC18 microcontroller project.

Necessary Parts

  1. 1x PIC18F4553
  2. 2x 22pf capacitors
  3. 1 20Mhz crystal
  4. 1 PICKIT 3 programmer
    1. It’s highly recommended that you read this user guide for the programmer.
    2. Lots of male headers
    3. 1x Breadboard
    4. Lots of wire
    5. 1x 330ohm resistor
    6. 1x LED (any color)
    7. 1x 10k resistor

Here’s the schematic of the circuit. Although the picture shows a PIC18F4455, they have the same pinout

BASICCIRCUIT

Necessary Software

  1. MPlab X
  2. C18 Complier

Under the C18 Complier link, select MPlab C for PIC18.So once you guys install the necessary software, follow the next steps for setting up your project with MPlab X. First select file->New Project. Select Standalone project and hit next.

step1

Type in “PIC18F4553” under Devices drop in menu. Hit next.
STEP2

Select PICKit3 under tools. Press next.

STEP3

Select C18 as your complier. Hit next.

STEP4

Now we have create a project name and directory. Normally the project location and folder will be placed under my documents. Before this tutorial, I set the project location under my PIC folder. Remember to select “Set as main project.” This will save you the frustration later on. Finally hit finish.

STEP5

Now we have to create a main file. Right click on Source files and select New-> C Main file. Type in main.c and hit finish.

STEP6

STEP7

Finally copy and paste this code into your main.c

#include &lt;p18f4553.h&gt;
#include &lt;delays.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

/*	PIC Configuratings */
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG= OFF

void main(void)
{
    TRISCbits.RC0=0;
    PORTCbits.RC0=0;

    while(1)
    {
        PORTCbits.RC0=1;
        Delay10KTCYx(50);
        PORTCbits.RC0=0;
        Delay10KTCYx(50);
    }
}

Now we’re going to build and program our mcu. First select build. You should receive a confirmation that the project was built successfully.

STEP8

Once it finishes building, hit the program button.

STEP9

Congrats! Your LED is flashing at 10hz!

Here how the code works. Out al of the #pragma in the code, pay close attention to #pragma config FOSC =HS. This tells the PIC microcontroller to accept the external cystal we connect. Please note that if we were using the 28pin PIC18 series microcontroller, this would most likely be changed to #pragma config OSC =HS.

Now for the code. Whenever we want to set a particular pin as an output, we have to access the TRISx, where the x stands for the letter of the port we want (in this case, TRISC for PORTC), and write a 0 to that pin. Thus we write TRISCbits.RC0=0 to set PORTC0 as an output. Finally, to either turn on or turn off this particular pin, we just have to write a 0 (off) or 1 (on)  to PORTC. Thus PORTCbits.RC0=0 is setting PORTC0 off.

Finally, the rest of the code is just flashing the LED on and off every 100ms. The while(1) is there to make sure it runs forever.We, that’s all I got for you guys today! Thank you guys for reading! If you have any comments or suggestions, please leave a comment!


2 Comments

Projects: Open Source N64 Emulator Controller #1

So, here I am again. I am going to relax after having another brutal semester at RPI. However, next semester will be one of the toughest semesters I ever had. Although I’m only taking 12-15 credits next semester, I will be working at 5 different jobs. The first job is a part time job at TV Ferret redesigning their sewage inspection systems. The second job is working as a lab TA for the Mercer Lab at RPI. Another job will be working at RPI’s Advising Learning and Assistance Center as a tutor for introduction for Electronics and Electric Circuits. The final two jobs will be working at two research centers, but I doubt I will get both of them. Add in my senior design project, and you get a very stressful semester. So I figured going to have to work on a semi big project before going back to RPI. What’s the project I’m working on? A controller for an N64 Emulator.

oot

Recently I started to get into video game emulators, more specifically the Project64. Lately, I’ve been playing The Legend of Zelda; Ocarina of  Time on the emulator. The biggest problem, which I’m sure a lot of people who played emulators have encountered, is the controls. Controlling your characters using a keyboard is not humanly possible. I want to make playing on the N64 emulator as easy as pie. I envision myself, picking the device, and playing a rousing game of Super Mario 64 without attempting to throw my laptop out of the window.

To make the project more interesting, I will set up a deadline for myself. I want to at least finish the prototype before January 21st, or before I go back to RPI. Whether the final prototype will be built on a breadboard or a pcb is up in the air. If I finished the project before January 21st, I will make it into a project for RPI RCOS, which focuses on open source projects,  next semester.  In other words, expect more posts to come from me December 17th until January 20th….as long as the world does not end by that time. So, the question you guys might be wondering how am I going to accomplish this?

PIC18F4553-pinout

So obviously I’m going to use a microcontroller for this project, but it will be a very different microcontroller than the microcontrollers I used in the past. Instead, I will be using Microchip’s PIC18 series microcontrollers. Why? Simple, my dear Watson. I spent one semester using nothing but PIC18 microcontrollers for my research at RPI’s Center for Automation Technologies and Systems.

Slide1

So the picture above shows the overall system I will be working with for the project. Here’s how it will work. I will have the PIC18F4553 microcontroller to read in the state of the buttons on the controller. Afterwards, it will tell a python serial communication script which button is pressed. Once the serial communication script receives the data from the microcontroller, then it will tell another python script to generate the appropriate keyboard events, which will be automatically seen by the emulator.Depending on the progress of the project, I will create a gui interface for the device. The gui will have an on/off button, and an option to determine the COM port the driver should look at. Again, I will give more details as time progresses. It’s going to be a fun 35 days!