Wednesday, October 28, 2009

Gnuplot - Simple Tutorial

There are a ton of tutorials on how to use gnuplot, but most can be convoluted. Here is a simple example/tutorial for gnuplot.

1. Put data in a file (e.g. plot1.dat).  X-axis data followed by a space and Y-axis data

1 23.8
2 23.8
3 23.8
4 23.8
5 23.75
6 23.75
6.5 24.15
6.75 24.45
7 24.95
7.25 25.15
7.5 25.30
7.75 25.40
9 25.70
10 25.83
11 25.88
12 25.89
13 25.90
14 25.90

2. Put the setup commands in another file (e.g. plot1.setup)

# Basics
unset log
unset label
unset arrow
set xtic auto 
set ytic auto 
set autoscale

# Title Information
set title "Temperature vs. Time - Trial 1"
set xlabel "Time (min)"
set ylabel "Temperature (celsius)"

# Initial Point (1,23.80)
set label "i" at 2,24.8 font "Arial,25" offset 1
set arrow from 2,24.80 to 1,23.80

# Final Point (14,25.90)
set label "f" at 13,24.90 font "Arial,25" offset 1
set arrow from 13,24.90 to 14,25.90

# Time fired (6,23.75)
set label "a" at 6,24.75 font "Arial,25" offset 1
set arrow from 6,24.75 to 6,23.75

# Temp reaches 60% (7.1,25.04)
set label "b" at 8.1,25.04 font "Arial,25" offset 1
set arrow from 8.1,25.04 to 7.1,25.04

# Max Temp (13,25.90)
set label "c" at 12,24.90 font "Arial,25" offset 1
set arrow from 12,24.90 to 13,25.90

# Make the plot
plot "plot1.dat" with linespoints

3. Run the following command
   a. gnuplot -persist -raise plot1.setup

Thursday, October 15, 2009

C++ number precision

Today I learned a very useful feature that C++ has.  It is the precision of numbers when using streams. Below I will show how the .precision function is important if you want precision when displaying numbers to the screen using the cout function or when writing numbers to a file using ofstream.  Undoubtedly there are other ways to use it, but I am more concerned with those two functions.

COUT
The below code will produce an output that is the full 10 digits in length

double digit = 5.000000001
cout.precision(10);
cout << digit << endl;
visit here for a more in depth explanation/example

OFSTREAM 

The below code will save your value in 10 digit format to a file (appending it to the end)
// Make a value to write to a file called file.txt
double value = 15.12345678;

// Create and open a stream
ofstream myFileWrite; 
myFileWrite.open("file.txt",ios::app); // append to the end of the file

// Make sure we opened it right
if (!myFileWrite.is_open())
   return -1;


// Define the precision to write at 
myFileWrite.precision(10);

// Write the value to the file at the end of the file 
myFileWrite << value << endl;

// Close the file
myFileWrite.close();

Tuesday, October 6, 2009

Eclipse - Go to function

Following in the foot steps of Jon's blog on a very cool Eclipse function (Eclipse C/C++ IDE File/Folder Restore), here is another very useful function of Eclipse.

This function allows you to go to a variable, function, etc. from where you are calling it.  The variable/function that you are interested in is linked to another part of your code and Eclipse of course knows this (assuming you have completed a build).  So how do you do it?

Hold down the Ctrl button and click on the variable/function as if it were a regular link.

Friday, October 2, 2009

Senior Year Project - Robotics


At the University of New Hampshire Electrical Engineering Department there is a requirement to take a final project class for your entire last year. Students from EE and ME combine together and choose projects that they are interested in. Teams then complete a full development cycle on the project they choose. During the class we did such things as team organization, budget creation, criticality analyses, bill of materials, Gantt charts, weekly reports, complete testing for design verification, project design, hands-on creation of project, coding, engineering, and more! The project was very detailed and ended with a final product, a final report over 70 pages in length and a presentation at UNH's Interdisciplinary Science & Engineering Symposium (click here for the webpage).  Also visit the link for a video of the robot in action.  And you can see that we won "Best Project Presentation" for 2007.

(The robot image above is property of my buddy Jeff Nichols who was our team leader and made this for our team)

So what did my team do? We developed an Autonomous Fire Fighting Robot and entered it into the Trinity College Competition. My group, Johnny 5 Associates, Inc., had 5 engineers. We had 2 Software Engineers, 2 Electrical Engineers and a Mechanical Engineer. We designed a robot that had a Motorola Microprocessor, stepper motors, rechargeable batteries, CO2 extinguisher, horn detector, Hamamatsu flame detector, infrared phototransistors, sonar, and white-line detectors.




My main part of the project was sensor research/integration for location information, object detection, horn detection and cabling.

Location Information & Object Detection

In order to know where the robot is and where it should go I found multiple sensors that would follow along with the specifications set forth by Trinity College.  I decided to utilize 2 infrared "white-line" sensors, 4 sonar sensors and dead reckoning.  I looked at other sensors such as accelerometer and compass sensors, but decided against them for cost and other reasons.  The compass for example would not be the best choice because there can be metallic objects in the maze that could mess with the sensor.

Infrared "white-line" sensors
Each room in the maze had a white line across it.  We decided to use this line as a way to reorient the robot as it enters each room (thus decreasing error).  So as the robot approached the line, it would sense the line with one sensor and rotate the opposite wheel until the robot was square with the line.

Sonar sensors
To avoid objects that may be in the maze and to measure distances to the nearest walls we utilized sonar.  Infrared would have worked, but there is the possibility that mirrors may exist in the maze.  The sonar worked great most of the time, but weird readings would occur sometimes.

Dead Reckoning
A great method is one where you count the turns of your wheels.  In this case if you have a stepper motor, you can pulse the motors and count how many pulses you send.  Thus you can get a distance measure.  The problem with this is that you accumulate error over time.  In order to fix this error we would take measurements with the sonar at certain points to fix the error.

Horn Detection

One of the best parts of this project for me was the creation of a horn and a detector.  This was part of the rules as an added bonus.  The idea being that your  fire alarm goes off and the robot begins.  So the rules stated that your detector must be between 3 and 4 kHz and that you must provide a transmitter that a judge, or the user, can start the robot with.

I implemented a frequency transmitter using a 555 timer and a gain circuit that drives a small speaker.


I also implemented a frequency detector... Unfortunately we purchased a  pre-made circuit that can detect a certain frequency range based on your pot. adjustment and then it activates a relay.  It is unfortunate because I found a chip that performs the same task and is easily created.  Either way I had to create a triple gain stage from a microphone and feed it into the relay circuit which when activated flips the voltage of a microcontroller input.

The system worked flawlessly and was an impressive way to start the robot.

Make sure to come back to my blog as I will eventually be describing more of the robot in greater detail (with more images).