eel 4924 senior design final design report april 20, 2009 ...motor hacking compass software gps...

33
EEL 4924 Senior Design Final Design Report April 20, 2009 HydroGator Team Name: Barely Intelligent Design Submitted by: Gram Slingbaum gram at ufl.edu Erik MacLean emaclean at ufl.edu Project Abstract: HydroGator is an autonomous (water) surface vehicle (ASV) capable of navigating a course on water while avoiding obstacles. It is loosely based on the specifications of the AUVSI ASV competition and could be used as a testbed for better designs. There is a growing need for ASV’s in the fields of oceanography, remote sensing, search and rescue, and reconnaissance.

Upload: others

Post on 08-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

EEL 4924 Senior DesignFinal Design Report

April 20, 2009HydroGator

Team Name: Barely Intelligent Design

Submitted by:Gram Slingbaumgram at ufl.eduErik MacLeanemaclean at ufl.edu

Project Abstract:HydroGator is an autonomous (water) surface vehicle (ASV) capable of navigating a course on

water while avoiding obstacles. It is loosely based on the specifications of the AUVSI ASV competition and could be used as a testbed for better designs. There is a growing need for ASV’s in the fields of oceanography, remote sensing, search and rescue, and reconnaissance.

Page 2: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Table of Contents

List of Tables and Figures............................................................................................................. 3Project Features/Objectives.......................................................................................................... 4 Analysis of Competitive Products.................................................................................................. 4Concept/Technology Selection...................................................................................................... 5Project Architecture....................................................................................................................... 9Division of Labor..........................................................................................................................10User Manual.................................................................................................................................10Bill of Materials............................................................................................................................11 Project Schedule....................... .................................................................................................. 11 Acknowledgements...................................................................................................................... 11Appendix: Code.............. .............................................................................................................12

Page 3: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

List of Tables and FiguresFigure 1: HydroGator.................................................................................................................... 4Figure 2: UCF's Son-of-a-Boatman ............................................................................................. 5Figure 3: HydroGator Microprocessor Board Schematic............................................................. 5Figure 4: HydroGator Microprocessor Board Layout ................................................................. 6Figure 5: Webcam detecting a yellow balloon.............................................................................. 6Figure 6: Xbee board for PC via USB designed by Joshua Childs............................................... 7Figure 7: GPS and Compass connected to Microprocessor Board.............................................. 7Figure 8: Toy RC boat purchased from eBay................................................................................ 8Figure 9: DuraTrax Intellispeed 16T Speed Controller.................................................................8Figure 10: System Block Diagram for HydroGator...................................................................... 9Table 11: Responsibility Table......................................................................................................10Figure 12: Bill of Materials......................................................................................................... 11Figure 13: Schedule of Development for HydroGator.................................................................11

Page 3

Page 4: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Project Features/ObjectivesHydroGator (Figure 1) has several major objectives. These are to be able to locate specifically

colored targets, to navigate using a GPS and compass, and to combine all this into an autonomous routine. Color tracking is accomplished using a wireless camera on the boat which sends images back to an external laptop for image processing. The navigation is done with an algorithm that processes the GPS data and uses the compass direction to know if the boat is facing in the proper direction. Finally the autonomous routine combines the sensor data and utilizes the control code stored on the microprocessor on the boat to accomplish a main task.

The current autonomous routine's mission is to locate several yellow colored balloons using the camera and store their GPS coordinates. The boat then returns home and proceeds directly to the balloons using the GPS to navigate instead of the camera, returning home again once the mission is completed. This autonomous routine can be reprogrammed to meet any need that can be accomplished using these same sensors. Additionally the HydroGator can be run using a laptop as a remote control for steering.

Figure 1: HydroGator.

Analysis of Competitive ProductsMany similar products were created to compete in the AUVSI ASV competition and more teams

are in the works for this Summer's competition. Last year's winner was the University of Central Florida (UCF). UCF's boat (Figure 2) had a camera, GPS, and computer on a catamaran platform weighing 30 pounds. The HydroGator is currently less complex than UCF's boat but is much lighter and is just the start to a potential competition ready surface vehicle. Once our project is expanded upon we can have higher quality parts that come with a higher budget, but for now the HydroGator at least accomplishes similar goals with less cost and weight.

Page 4

Page 5: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Figure 2: UCF's Son-of-a-Boatman.

Concept/Technology SelectionHydroGator is made up of several modules combined together to provide some level of

automation. The main components are the microprocessor and its board, the camera, the PC interface, the GPS/Compass, and the motors with speed controllers.

First, the microprocessor is on a custom printed circuit board (PCB) designed and populated by Barely Intelligent Design. This is shown in figures 3 and 4.

Figure 3: HydroGator Microprocessor Board Schematic.

Page 5

Page 6: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Figure 4: HydroGator Microprocessor Board Layout.

The microprocessor used for HydroGator is the Atmel Atmega324p. This processor was chosen because of its low cost and because it has two USARTs (two serial interface ports). It can handle all the processing we need it to do since the image processing is sent to the external laptop. On the microprocessor board are headers for connecting the Atmega324p to the XBee wireless, GPS, compass via I2C bus, and the left and right motors. Also are headers for programming the Atmega324p via ISP and JTAG.

The next module of the HydroGator is the camera. A wireless webcam is used to send images back to an external laptop where image processing software can determine colors and other such useful information. For the HydroGator's webcam a Linksys WVC54GC camera is used. A sample yellow detection screenshot is shown in Figure 5.

Figure 5: Webcam detecting a yellow balloon.

Page 6

Page 7: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Once these images are processed, commands are sent back to the microprocessor on the HydroGator which can control which direction the boat turns and how it behaves in general. The communication between the external laptop PC and the microprocessor is handled by the Xbee modules. There are many ways to communicate between a microprocessor and a PC wirelessly but we chose the Xbee since it is easy to use, affordable, and has long enough range for our uses. The Xbee acts as a wireless serial port with one end based on the Microprocessor Board and one end on a board which converts the serial signal to USB for use with a PC (Figure 6).

Figure 6: Xbee board for PC via USB designed by Joshua Childs.

The other sensors of HydroGator are the compass and GPS which are interfaced directly with the microprocessor on the main board (Figure 7 shows the brown GPS module and compass attached via red wires to the main board).

Figure 7: GPS and Compass connected to Microprocessor Board.

Page 7

Page 8: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

We chose this GPS module because it was provided to us for free by Mike Stapleton by way of Joshua Childs. It communicates with the Atmega324p via serial communication. The compass is a low-cost module that utilizes the I2C interface making it easy to connect to the Atmega324p.

Finally, the motors were part of a toy Remote Control (RC) boat we purchased from eBay. Besides the actual motors, we disposed of all other electronics and just used the shell (Figure 8).

Figure 8: Toy RC boat purchased from eBay.To control the motors we used DuraTrax Intellispeed 16T (Figure 9) speed controllers because

we already were in posession of one and so needed to purchase only one more. These controllers are given power and a Pulse Width Modulation (PWM) signal from the microprocessor which is used to speed up, slow down, and reverse the motors.

Figure 9: DuraTrax Intellispeed 16T Speed Controller.

Page 8

Page 9: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Project ArchitectureThe block diagram of how all the parts described above work together can be seen below in

Figure 10.

Figure 10: System Block Diagram for HydroGator.

Page 9

Page 10: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Most of the components of HydroGator are on-board the boat. These parts are interfaced to the microprocessor in various ways. The Xbee and GPS are connected via serial interface, the compass is connected via the I2C bus, and the speed controllers are running off a separate power source but receive PWM signals from the microprocessor to control the motors. The camera, while on the boat is not connected to the microprocessor. It is, however, connected to the laptop via standard 802.11g wireless communication. On the laptop, the camera's image data is processed using OpenCV and commands are sent back to the microprocessor on the boat using the Xbee as a wireless serial interface. Division of Labor

The division of labor for Barely Intelligent Design can be seen in Table 11.

Table 11: Responsibility Table.

User ManualIf the HydroGator is programmed to run autonomously, it needs to only be switched on and put

in the water facing the general direction of its goal. If it is programmed to be remotely controlled by an external laptop, it also needs to be placed in the water facing the general direction of its goal then it can be controlled via X-CTU, a software for sending and receiving data with the Xbee from a PC. It is recommended that HydroGator be connected via a line of some kinda so it can be retrieved manually if anything goes wrong like a dead battery or going out of range of the Xbee. Make sure all batteries are charged before use.

Page 10

Team R espons ibility TableBarely Intelligent Des ignHydroGator

TasksCamera S oftwareMotor HackingCompas s S oftwareGPS S oftwarePC B Des ign

Page 11: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Bill of MaterialsFigure 12 shows a list of the major components and their approximate cost. Please note that

some of components were given to us free of charge and this table reflects that.

Figure 12: Bill of Materials.

Project ScheduleFigure 13 shows the schedule of events for the development of HydroGator.

Figure 13: Schedule of Development for HydroGator.

AcknowledgementsSpecial thanks to TA's Joshua Childs, Josh Hartman, and Jared Bevis as well as Dr. Schwartz,

Mike Stapleton, and Eric Liebner for all of their help and support.

Page 11

ComponentXbee Series 2.5Atmega324pRC Boat from eBayLinksys Wireless Webcam (Refurbished)GPS SANAV FV-M8DuraTrax Intellispeed 16T Speed ControllerCompass CMPS03Various Parts for Completing PCBMilled PCB

Page 12: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

Appendix: Code// Code for Remote Control Functionality

// written by Erik MacLean and Gram Slingbaum// Senior Design Spring 2009

//uart 1 is connected to the gps

#include <avr/io.h>#include <avr/interrupt.h>#include <avr/pgmspace.h>#include <util/delay.h>#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <string.h>#include <inttypes.h>

//#include "environment.h"

//#define CPU_Speed 14745600L

#define ATMEGA128 0#define ATMEGA32 1

#define chipset ATMEGA32#define CPU_Speed 8000000L#define UART1_Baud 38400L#define UART1_RR ((CPU_Speed/(16L*UART1_Baud))-1)

#define UART0_Baud 4800L#define UART0_RR ((CPU_Speed/(16L*UART0_Baud))-1)

char gpsUARTbuffer[35];char gpsdatabuffer[35];char latbuffer[35];char longbuffer[35];char speedbuffer[35];char headingbuffer[35];

char rdylatbuffer[35];char rdylongbuffer[35];char rdyspeedbuffer[35];

Page 12

Page 13: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

char rdyheadingbuffer[35];

#define motordriverfreq 10000//pwm frequency for motors

#define LEFT_MOTOR OCR1A#define RIGHT_MOTOR OCR1B

#define STOP 750

int gpsflag; //gps data readyint motorselect;

char gpssendbuffer[35];

volatile char gpsfieldcounter = 0;unsigned char gpsUARTcounter=0;

char gpsUARTnew=0;int angle; //Compass direction

char xbeeEchoBuffer[40];volatile int xbeeEchoCounter = 0;

void UART0_string(unsigned char data[]);

void initMotors()// this function called at bootup to enable motors// generates PWM to motor drivers// uses OC1A and OC1B

{motorselect=0;

//TCCR0=0b00000001;TCCR1A=0b10100000;TCCR1B=0b00010010;//TIMSK=0b00000000;

//PWMICR1=0x4E20;//CPU_Speed/(2*motordriverfreq); // freqout= CLK/(2*prescaler) prescaler=8 clk=8 mhz

//prescaler = CLK/(2*freqout)

Page 13

Page 14: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

LEFT_MOTOR=STOP;RIGHT_MOTOR=STOP;

sei();//output compare 1A on port d needs to be set as output

if(chipset == ATMEGA128) DDRB |= 0b01100000; //this one for bdmicro

else if(chipset == ATMEGA32) DDRD |= 0b00110000; //for atmega32

//for bdmicro this is port b pins 5 and 6//for atmega324p this is port d pins 4 and 5//ddr should be set constant in program will be in final version

}

char i2c_read(char address, char reg){ char read_data = 0;

TWCR = 0xA4; // send a start bit on i2c bus while(!(TWCR & 0x80)); //UART0_string(gpssendbuffer); // wait for confirmation of transmit TWDR = address; // load address of i2c device TWCR = 0x84; // transmit while(!(TWCR & 0x80)); // wait for confirmation of transmit TWDR = reg; // send register number to read from TWCR = 0x84; // transmit while(!(TWCR & 0x80)); // wait for confirmation of transmit

TWCR = 0xA4; // send repeated start bit while(!(TWCR & 0x80)); // wait for confirmation of transmit TWDR = address+1; // transmit address of i2c device with readbit set TWCR = 0xC4; // clear transmit interupt flag while(!(TWCR & 0x80)); // wait for confirmation of transmit TWCR = 0x84; // transmit, nack (last byte request) while(!(TWCR & 0x80)); // wait for confirmation of transmit read_data = TWDR; // and grab the target data TWCR = 0x94; // send a stop bit on i2c bus return read_data;

}

void i2c_transmit(char address, char reg, char data){

Page 14

Page 15: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

TWCR = 0xA4; // send a start bit on i2c bus while(!(TWCR & 0x80)); // wait for confirmation of transmit TWDR = address; // load address of i2c device TWCR = 0x84; // transmit while(!(TWCR & 0x80)); // wait for confirmation of transmit TWDR = reg; TWCR = 0x84; // transmit while(!(TWCR & 0x80)); // wait for confirmation of transmit TWDR = data; TWCR = 0x84; // transmit while(!(TWCR & 0x80)); // wait for confirmation of transmit TWCR = 0x94; // stop bit}

void UART0_init(){

UCSR0A = 0x00; // Enable normal UART transmission speed// UCSR0B |= 0x18; // Enable Transmit and receive Rx Interrupt// UCSR0B = 0x18;

UCSR0B = 0b10011000;

// UCSR0C |= 0x06; // Enable 8 bit mode, 1 stop bit, no parityUCSR0C = 0x06;UBRR0H = 0; UBRR0L = UART0_RR;

sei();}

void CMPS_DIR(void){ // read cmps03 angle, high byte angle = i2c_read(0xC0,2) <<8; // read cmps03 angle, high byte angle += i2c_read(0xC0,3); // read cmps03 angle, low byte}void UART0_char(unsigned char data){ while(!(UCSR0A & 0b00100000));

UDR0 = data;}void UART0_string(unsigned char data[]){

int i = 0;

Page 15

Page 16: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

while(data[i] != '\0') UART0_char(data[i++]);UART0_char(0x0D); // carriage return

}

void UART1_init(){

UCSR1A &= 0x00; // Enable normal UART transmission speedUCSR1B = 0b10011000; //enable rx interrupts too

UCSR1C |= 0x06; // Enable 8 bit mode, 1 stop bit, no parityUCSR1C &= 0x06;UBRR1H = 0; // baud = 9600UBRR1L = UART1_RR; //

sei();}void UART1_char(unsigned char data){ while(!(UCSR1A & 0b00100000));

UDR1 = data;}void UART1_string(unsigned char data[]){

int i = 0;while(data[i] != '\0') UART1_char(data[i++]);UART1_char(0x0A); UART1_char(0x0D); // carriage return

}

ISR(USART1_RX_vect)//received new data from gps{

char a = UDR1;

if( a == '$' ) //begin new line of data{

gpsUARTnew = 1;gpsfieldcounter=0;gpsUARTcounter=0;gpsflag=0;

}

Page 16

Page 17: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

if( a == 0x0d || a == 0x0a) //new line{

gpsUARTcounter=0;gpsUARTnew=0;gpsfieldcounter=0;

}if(a == ',') //delimiter increment field counter{

gpsfieldcounter++;}else if( gpsUARTnew == 1 ) {

if((gpsfieldcounter == 2)){if(a!=',') {

latbuffer[gpsUARTcounter] = a; // Copy the received char to buffergpsUARTcounter++;

}else{

latbuffer[++gpsUARTcounter] = '\0';gpsUARTcounter=0;

}}if(gpsfieldcounter == 4){

if(a!=',') {longbuffer[gpsUARTcounter] = a; // Copy the received char to buffergpsUARTcounter++;}else{

longbuffer[++gpsUARTcounter] = '\0';gpsUARTcounter=0;

}}

/* if(gpsfieldcounter == 7){if(a!=',') {speedbuffer[gpsUARTcounter] = a; // Copy the received char to buffergpsUARTcounter++;}else{

speedbuffer[++gpsUARTcounter] = '\0';gpsUARTcounter=0;

}

Page 17

Page 18: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

} if(gpsfieldcounter == 8){

if(a!=',') {headingbuffer[gpsUARTcounter] = a; // Copy the received char to buffergpsUARTcounter++;}else{

headingbuffer[++gpsUARTcounter] = '\0';gpsUARTcounter=0;

}}*/

}

if(a == '*' && gpsUARTnew == 1) //reached delimiter and wait until next line{

strcpy(gpsdatabuffer,gpsUARTbuffer);

gpsUARTnew = 0; gpsfieldcounter=0; gpsUARTcounter=0; gpsflag=1;

strcpy(rdylatbuffer,latbuffer); strcpy(rdylongbuffer,longbuffer);

strcpy(rdyspeedbuffer,speedbuffer);strcpy(rdyheadingbuffer,headingbuffer);

}

}

void GPS_Send(unsigned char data[]){

//sends packet to GPS

//data contains packet type and data

//compute checksum// checksum is xor of all bytes between $ and *

Page 18

Page 19: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

//checksum for "PMTK" is 02, always start with this and add to it

unsigned short int chksm = 0x02;unsigned char datacount = 0;

//xor each element in data until get to end of stringwhile ( data[datacount] != '\0')

chksm ^= (unsigned short int)(data[datacount++]);

//convert checksum int type to characters

char chksumtopbyte = (chksm >> 8);char chksumlowerbyte = chksm & 0x00FF;

//transmitUART1_string("$PMTK"); //preamble and TalkerIDUART1_string(data); //PktType and DataUART1_char('*'); //delimiterUART1_char(chksumtopbyte); //2 bytes checksumUART1_char(chksumlowerbyte);UART1_char(0x0A); //carriage returnUART1_char(0x0D); //line feed

}

ISR(USART0_RX_vect) //received new data from xbee { //for testing xbee, echo received data char receivedata = UDR0; if(receivedata == 0x0D){ //carriage return, finished line, reset counter, echo back xbeeEchoBuffer[xbeeEchoCounter]= '\0'; //UART1_string(xbeeEchoBuffer); UART0_string(xbeeEchoBuffer);

int temp; temp=atoi(xbeeEchoBuffer); char *which; if(motorselect==0){ LEFT_MOTOR=temp;

motorselect=1;which="Left\0";UART0_string(which);}

Page 19

Page 20: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

else{RIGHT_MOTOR=temp;motorselect=0;which="Right\0";UART0_string(which);

} xbeeEchoCounter=0; } else { xbeeEchoBuffer[xbeeEchoCounter] = receivedata; xbeeEchoCounter++; } }

int main(){

gpsflag=0;TWBR=32; //Set i2c bus to 100 kHzangle=8;char *data="314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";

_delay_ms(500);_delay_ms(500);_delay_ms(500);_delay_ms(500);_delay_ms(500);_delay_ms(500);

UART0_init();UART1_init();GPS_Send(data);_delay_ms(500);_delay_ms(500);

char teststring[5];teststring[0]='a';teststring[1]='b';teststring[2]='c';teststring[3]='d';teststring[4]='\0';int six=6;

Page 20

Page 21: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

itoa(six, teststring, 10);

strcpy(gpssendbuffer,teststring);UART0_string(gpssendbuffer);

initMotors();UART0_string(gpssendbuffer);char *name;

while (1){

_delay_ms(200);}

}

Page 21

Page 22: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

// main.cpp : Defines the entry point for the console application.//

//this is where the main code for the computer of the hydro gator will be

//right now it is still being used to debug and is still under development

//

//The output of the camera is captured as a HBITMAP using windows GDI and converted to an //opencv iplimage and then processed//the image is converted to HSV space and filtered for colors with a specific hue

// Hydro Gator

//Written by Erik MacLean

#include "stdafx.h"#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <conio.h>//#include <stdbool.h>

//opencv#include <cv.h>#include <cxcore.h>#include <highgui.h>#include <cvcam.h>

//directx#include <d3d9.h>#include <d3dx9.h>//#include <windows.h>

//#include <fstream.h>

#include "serial.h"

//#define _AFXDLL

Page 22

Page 23: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

//#include <afxwin.h> //#include <comdef.h> #include <ctype.h>

using namespace std;

#define imgheight 128#define imgwidth 160

#define offsetx 621#define offsety 330

void setspeed(int leftmotor,int rightmotor, CSerial serialport)//sets OCR1A and OCR1B (motor speed){

char* start= "!";char* left;char* right;itoa(leftmotor,left,10);itoa(rightmotor,right,10);serialport.SendData(start, 1);serialport.SendData(left,5);serialport.SendData(right,5);

}

void CvFindColorHSV(const IplImage* src, IplImage* dst, int H1Min, int S1Min, int V1Min,int H1Max, int S1Max, int V1Max) {

//this function goes through every pixel in an HSV image and if the pixel is within the threshold

//it colors the pixel white otherwise it colors it black//this code was modified from an open source project called flow designer

int x, y;bool h;for (x=0; x<src->height; x++)

for (y=0; y<src->width; y++) {CvScalar s = cvGet2D(src, x, y);if (H1Max > H1Min)

h = (s.val[0]*2 <= H1Max) && (s.val[0]*2 >= H1Min);

Page 23

Page 24: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

elseh = (s.val[0]*2 <= H1Max) || (s.val[0]*2 >= H1Min);

if (h &&(s.val[1]<=((S1Max*255.0)/100.0) &&

s.val[1]>=((S1Min*255.0)/100.0)) &&(s.val[2]<=((V1Max*255.0)/100.0) &&

s.val[2]>=((V1Min*255.0)/100.0))) {s.val[0]=255;s.val[1]=255;s.val[2]=255;cvSet2D(dst, x, y, s);

} else {s.val[0]=0;s.val[1]=0;s.val[2]=0;cvSet2D(dst, x, y, s);

}}

}

IplImage* hBitmap2Ipl()

{

//this function modified from code online http://tinyurl.com/dkhk7c to use with windows GDI//samples the screen for the camera video and then converts the camera output to an iplimage to

be used//by opencv

// add code

//windows gdi stuff

LPVOID pBits; //pointer to image data, used in createDIBsection

BITMAPINFO bmpInfo;ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);bmpInfo.bmiHeader.biBitCount=32; //bits per pixelbmpInfo.bmiHeader.biCompression = BI_RGB;bmpInfo.bmiHeader.biWidth=imgwidth;bmpInfo.bmiHeader.biHeight=(-1*imgheight); //bitmap data is stored upside

down, use negative height here for right side up for opencvbmpInfo.bmiHeader.biPlanes=1;

Page 24

Page 25: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

bmpInfo.bmiHeader.biSizeImage=abs(bmpInfo.bmiHeader.biHeight)*bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biBitCount/8;

HWND hDesktopWnd=GetDesktopWindow();HDC hDesktopDC=GetDC(hDesktopWnd);HDC hDesktopCompatibleDC=CreateCompatibleDC(hDesktopDC);HBITMAP

hDesktopCompatibleBitmap=CreateDIBSection(hDesktopDC,&bmpInfo,DIB_RGB_COLORS,&pBits,NULL,0);

//copy image from desktop dcSelectObject(hDesktopCompatibleDC,hDesktopCompatibleBitmap);

BitBlt(hDesktopCompatibleDC,0,0,imgwidth,imgheight,hDesktopDC,offsetx,offsety,SRCCOPY|CAPTUREBLT);

//offsetx and offset y are coordinates of top left pixel of the camera image on the desktop

// BitBlt(hDesktopCompatibleDC,0,0,1280,800,hDesktopDC,0,0,DSTINVERT|CAPTUREBLT);

//end add code

BITMAP bmp;::GetObject(hDesktopCompatibleBitmap,sizeof(BITMAP),&bmp);//getobject only works if used createdibsection, use getdibits for bmp.bmBits

//GetDIBits(hdc,hBmp,start,clines,bmp.bmBits,lpbmpheader,DIB_RGB_COLORS);

int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;IplImage* img = cvCreateImageHeader( cvSize(bmp.bmWidth, bmp.bmHeight), depth, nChannels );img->imageData =(char*)malloc(bmp.bmHeight*bmp.bmWidth*nChannels*sizeof(char));memcpy(img->imageData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*nChannels);

//clean up added codeReleaseDC(hDesktopWnd,hDesktopDC);

DeleteDC(hDesktopCompatibleDC);

Page 25

Page 26: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

DeleteObject(hDesktopCompatibleBitmap);

//::delete(bmp);

//free(pBits);

//free(bmp.bmBits);

//end clean up added code

return img;}

int _tmain(int argc, _TCHAR* argv[]){

char* window1="window1";char* window2="window2";char* window3="window3";char* window4="window4";cvNamedWindow( window1, CV_WINDOW_AUTOSIZE );

CSerial serialport;

IplImage* img;//IplImage* img2= cvCreateImage(cvSize(imgwidth,imgheight),IPL_DEPTH_8U,3);

cvNamedWindow( window2, CV_WINDOW_AUTOSIZE );cvNamedWindow( window3, CV_WINDOW_AUTOSIZE );cvNamedWindow( window4, CV_WINDOW_AUTOSIZE );//SaveBitmap("tmp.bmp",hDesktopCompatibleBitmap);for(int countr = 0; countr<100000; countr++){img= hBitmap2Ipl();

IplImage* img2= cvCloneImage(img);IplImage* img3= cvCreateImage(cvSize(imgwidth,imgheight),IPL_DEPTH_8U,3); cvCvtColor(img2,img3,CV_BGRA2BGR); cvCvtColor(img3,img3,CV_BGR2HSV);

IplImage* filteredimg= cvCloneImage(img3);

Page 26

Page 27: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

//180,50,80,, 280,255,255 worked well for blue\

//starts picking up dark colors//CvFindColorHSV(img3,filteredimg, 180, 30, 30, 280, 255, 255);

//CvFindColorHSV(img3,filteredimg, 50, 30, 30,130, 255, 255);

//good for yellow//CvFindColorHSV(img3,filteredimg, 50, 30, 30,110, 255, 255);

CvFindColorHSV(img3,filteredimg, 50, 40, 40,120, 255, 255);

//CvFindColorHSV(img3,filteredimg, 180, 40, 40,// 280, 255, 255);

//s and v from 0 - 255, h is degrees//CvFindColorHSV(img3,filteredimg, 180, 50, 50,280, 255, 255);

//cvNamedWindow( window1, CV_WINDOW_AUTOSIZE ); cvShowImage(window1,img); cvShowImage(window2,img2); cvShowImage(window3,img3); cvShowImage(window4,filteredimg);cvWaitKey(1);free(img->imageData);cvReleaseImage(&img);cvReleaseImage(&img2);cvReleaseImage(&img3);cvReleaseImage(&filteredimg);//cvDestroyWindow(window1);

}

return 0;}

Page 27

Page 28: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

// Serial.cpp//CSerial//commonly used class for serial for windows//wrapper for win32 api

//downloaded from http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503

//must include windows.h in stdafx.h//compiles when using multicharacter instead of unicode in project settings

/*This is adapted from http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503

This version Copyright (c) 2008 by James Bremner All rights reserved.

Use license: Modified from standard BSD license.

Redistribution and use in source and binary forms are permitted provided that the above copyright notice and this paragraph are duplicated in all such forms and that any documentation, advertising materials, Web server pages, and other materials related to such distribution and use acknowledge that the software was developed by James Bremner. The name "James Bremner" may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */

#include "stdafx.h"#include "Serial.h"

CSerial::CSerial(){

memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) ); memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

m_hIDComDev = NULL;m_bOpened = FALSE;

}

CSerial::~CSerial(){

Close();

}

bool CSerial::Open( int nPort, int nBaud ){

if( m_bOpened ) return( TRUE );

char szPort[15];

Page 28

Page 29: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

char szComParams[50];DCB dcb;

wsprintf( szPort, "COM%d", nPort );m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,

FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );if( m_hIDComDev == NULL ) return( FALSE );

memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) ); memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

COMMTIMEOUTS CommTimeOuts;CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;CommTimeOuts.ReadTotalTimeoutMultiplier = 0;CommTimeOuts.ReadTotalTimeoutConstant = 0;CommTimeOuts.WriteTotalTimeoutMultiplier = 0;CommTimeOuts.WriteTotalTimeoutConstant = 5000;SetCommTimeouts( m_hIDComDev, &CommTimeOuts );

wsprintf( szComParams, "COM%d:%d,n,8,1", nPort, nBaud );

m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

dcb.DCBlength = sizeof( DCB );GetCommState( m_hIDComDev, &dcb );dcb.BaudRate = nBaud;dcb.ByteSize = 8;unsigned char ucSet;ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );if( !SetCommState( m_hIDComDev, &dcb ) ||

!SetupComm( m_hIDComDev, 10000, 10000 ) ||m_OverlappedRead.hEvent == NULL ||m_OverlappedWrite.hEvent == NULL ){DWORD dwError = GetLastError();if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );CloseHandle( m_hIDComDev );return( FALSE );}

m_bOpened = TRUE;

return( m_bOpened );

}

bool CSerial::Close( void ){

if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );

if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );

Page 29

Page 30: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

CloseHandle( m_hIDComDev );m_bOpened = FALSE;m_hIDComDev = NULL;

return( TRUE );

}

bool CSerial::WriteCommByte( unsigned char ucByte ){

bool bWriteStat;DWORD dwBytesWritten;

bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten, &m_OverlappedWrite );if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){

if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) ) dwBytesWritten = 0;else{

GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten, FALSE );m_OverlappedWrite.Offset += dwBytesWritten;}

}

return( TRUE );

}

int CSerial::SendData( const char *buffer, int size ){

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwBytesWritten = 0;int i;for( i=0; i<size; i++ ){

WriteCommByte( buffer[i] );dwBytesWritten++;}

return( (int) dwBytesWritten );

}

int CSerial::ReadDataWaiting( void ){

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwErrorFlags;COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );

return( (int) ComStat.cbInQue );

}

Page 30

Page 31: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

int CSerial::ReadData( void *buffer, int limit ){

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

bool bReadStatus;DWORD dwBytesRead, dwErrorFlags;COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );if( !ComStat.cbInQue ) return( 0 );

dwBytesRead = (DWORD) ComStat.cbInQue;if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );if( !bReadStatus ){

if( GetLastError() == ERROR_IO_PENDING ){WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );return( (int) dwBytesRead );}

return( 0 );}

return( (int) dwBytesRead );

}

//serial.h

// Serial.h

//#ifndef __SERIAL_H__#define __SERIAL_H__

#define FC_DTRDSR 0x01#define FC_RTSCTS 0x02#define FC_XONXOFF 0x04#define ASCII_BEL 0x07#define ASCII_BS 0x08#define ASCII_LF 0x0A#define ASCII_CR 0x0D#define ASCII_XON 0x11#define ASCII_XOFF 0x13

class CSerial{

public:CSerial();~CSerial();

Page 31

Page 32: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

//bool Open( int nPort = 2, int nBaud = 9600 );bool Open( int nPort, int nBaud = 9600 );//Open connection to serial port.//Parameters:

//[in] port name of port, e.g COM1//[in] nBaud port speed, e.g 9800

//Returns://TRUE if successful

bool Close( void );//Close the serial connection.//Returns:

//always true

int ReadData( void *, int );//Read data from COM port.//Parameters:

//[in] buffer pointer to location to store data//[in] limit maximum number of bytes to read

//Returns://0 if error

int SendData( const char *, int );//Send data to the COM port.

//Parameters://[in] buffer pointer to data to be written//[in] size number of bytes to write

//Returns://0 if error

int ReadDataWaiting( void );//Check for data waiting to be read.//Returns:

//the number of bytes waiting to be read

bool IsOpened( void ){ return( m_bOpened ); }

protected:bool WriteCommByte( unsigned char );

HANDLE m_hIDComDev;OVERLAPPED m_OverlappedRead, m_OverlappedWrite;bool m_bOpened;

};

//#endif

//stdafx.h// stdafx.h : include file for standard system include files,// or project specific include files that are used frequently, but// are changed infrequently

Page 32

Page 33: EEL 4924 Senior Design Final Design Report April 20, 2009 ...Motor Hacking Compass Software GPS Software PCB Design. Bill of Materials Figure 12 shows a list of the major components

//

#pragma once

#include "targetver.h"

#include <stdio.h>#include <tchar.h>#include <windows.h>

// TODO: reference additional headers your program requires here

Page 33