Another user, or myself, will place a colored card behind a wall,
the user will tell the robot to go around the wall, take a picture
of the card, and the robot will return to the user. The robot will
then play a different series of notes, depending on what color was
seen. The User will then select which color was seen through an on
screen menu. The robot will play a song depending on the color and if
the user was correct or not.
My Code (decipher that!):
The last third is songs!
#include
#include
#include
#include
#include
using namespace std;
class window //class for the numerous windows that will be displayed
{
public:
int choice();
void show(int);
void setpass(string);
window(string, int); //constructor, no need for destructor
private:
string password;
int numch;
PicturePtr face;
};
struct colors //relic of a bygone part of the program, name no longer needed
{
string name;
Pixel code;
};
void robotgo(int);
int acolor();
void playmorse(int);
void playsong(int);
void robotreturn(int);
void writeout(int, int, int);
int str2int(string);
int main()
{
int uses = 0;
int correct = 0;
int replays = 0;
window interface("interface.jpg", 3); //sets up the 5 interfaces that will be used
window colorchoose("colors.jpg", 9);
window checkready("ready.jpg", 1);
window success("good.jpg", 1);
window failure("fail.jpg", 1);
cout << "Enter the commport: "; //straight forward
string port;
cin >> port;
connect(port.c_str());
robot.setForwardness("fluke-forward");
string password; //sets a password so the
cout << "Set admin password: "; //kids don't accidentally
cin >> password; //close the program
interface.setpass(password);
while (true)
{
int gate;
gate = interface.choice(); //gets user choice of direction
if (gate > 2) //exits the program if desired
{
writeout(uses, correct, replays);//writes out the data
disconnect();
return 0;
}
else
{
robotgo(gate); //goes to user chosen gate
}
int tcolor = acolor(); //takes a picture and analyzes it
robotreturn(gate); //returns the robot to the start
//it returns before playing the code so that users can hear it better
int gate2;
gate2 = checkready.choice(); //waits for the user to be ready
int looper = 1;
while(looper)
{
replays++;
playmorse(tcolor); //plays the code
gate2 = colorchoose.choice(); //user interprets code
if (gate2 != 8) //replays code if choice is pressed
{ //else it goes on
looper = 0;
replays--;
}
}
if (gate2 == tcolor) //if the code was correctly interpretted
{
success.show(10); //congradulates
playsong(tcolor); //and plays a song
correct++;
}
else
{
failure.show(10); //else
playsong(8); //plays fail song and message
}
uses++;
}
writeout(uses, correct, replays); //unnecessary, never hit
disconnect();
return 0;
}
//sends the robot to the user specified area to gather data
void robotgo(int gate)
{
if (gate == 0 || gate == 2) //table was too small
{
cout << "Too bad I'm lazy." << endl;
}
gate = 1;
cout << "Gathering Data..." << endl;
switch (gate)
{
case 0:
robot.forward(0.8, 3);
robot.motors(0.2, 0.8);
wait(1);
robot.forward(0.8, 1);
robot.turnLeft(0.6, 2.2);
robot.forward(0.8, 0.8);
robot.turnLeft(0.6, 2.5);
break;
case 1: //updated and only had one wall
robot.forward(0.8, 2.5);
robot.turnRight(0.6, 1.3);
robot.forward(0.8, 1.5);
robot.turnRight(0.6, 1.2);
break;
case 2:
robot.motors(0.8, 0.2);
wait(1);
robot.forward(0.8, 1);
robot.turnLeft(0.6, 2.2);
robot.forward(0.8, 0.8);
robot.turnLeft(0.6, 2.5);
break;
}
}
int acolor()
{
colors colorwheel[8]; //creates an array of eight colors
colorwheel[0].code.R = 0;
colorwheel[0].code.G = 0;
colorwheel[0].code.B = 0;
colorwheel[0].name = "black";
colorwheel[1].code.R = 255;
colorwheel[1].code.G = 0;
colorwheel[1].code.B = 0;
colorwheel[1].name = "red";
colorwheel[2].code.R = 255;
colorwheel[2].code.G = 255;
colorwheel[2].code.B = 0;
colorwheel[2].name = "yellow";
colorwheel[3].code.R = 0;
colorwheel[3].code.G = 255;
colorwheel[3].code.B = 0;
colorwheel[3].name = "green";
colorwheel[4].code.R = 0;
colorwheel[4].code.G = 255;
colorwheel[4].code.B = 255;
colorwheel[4].name = "cyan";
colorwheel[5].code.R = 0;
colorwheel[5].code.G = 0;
colorwheel[5].code.B = 255;
colorwheel[5].name = "blue";
colorwheel[6].code.R = 255;
colorwheel[6].code.G = 0;
colorwheel[6].code.B = 255;
colorwheel[6].name = "purple";
colorwheel[7].code.R = 255;
colorwheel[7].code.G = 255;
colorwheel[7].code.B = 255;
colorwheel[7].name = "white";
PicturePtr data = robot.takePicture("color"); //takes photo
double distance;
Pixel datap = getPixel(data,1000,700); //gets a single pixel
double low = sqrt((datap.R - colorwheel[0].code.R)^2+(datap.G-colorwheel[0].code.G)^2+(datap.B - colorwheel[0].code.B)^2);
int top = 0;
for (int i = 1; i < 8; i++)
{
distance = sqrt((datap.R - colorwheel[i].code.R)^2+(datap.G-colorwheel[i].code.G)^2+(datap.B - colorwheel[i].code.B)^2);
if (distance < low) //gets the shortest distance colorwise from the pixel to the eight specified colors
{
top = i;
low = distance;
}
}
return top; //returns closest colors index
}
//straight forward plays the code
//which is different for each color
void playmorse(int morse)
{
switch (morse)
{
case 0:
robot.beep(.75,587.3);
break;
case 1:
robot.beep(.75,587.3);
wait(1);
robot.beep(.75,587.3);
break;
case 2:
robot.beep(.75,587.3);
wait(.5);
robot.beep(.75,587.3);
wait(.5);
robot.beep(.75,587.3);
break;
case 3:
robot.beep(.75,587.3);
wait(.5);
robot.beep(.75,587.3);
wait(.5);
robot.beep(.75,587.3);
wait(.5);
robot.beep(.75,587.3);
break;
case 4:
robot.beep(.75,587.3);
wait(2);
robot.beep(.75,587.3);
break;
case 5:
robot.beep(2.5,587.3);
break;
case 6:
robot.beep(.25,587.3);
wait(.25);
robot.beep(.25,587.3);
wait(.25);
robot.beep(1.5,587.3);
break;
case 7:
robot.beep(1.5,587.3);
wait(.5);
robot.beep(.5,587.3);
wait(.1);
robot.beep(.5,587.3);
break;
}
}
//returns the robot to the starting point
void robotreturn(int gate)
{
gate = 1;
cout << "Returning..." << endl;
switch (gate)
{
case 0:
robot.turnLeft(0.6, 2.5);
robot.forward(0.8, 0.8);
robot.turnRight(0.8, 2.2);
robot.move(0.8, 0.2);
wait(1);
robot.forward(0.8, 2.2);
robot.turnLeft(0.6, 4.5);
break;
case 1://only this was used
robot.turnRight(0.6, 1.3);
robot.forward(0.8, 1.6);
robot.turnLeft(0.8, .9);
robot.forward(0.8, 2.4);
robot.turnLeft(0.6, 2.6);
robot.forward(0.8, 0.1);
break;
case 2:
robot.turnLeft(0.6, 2.5);
robot.forward(0.8, 0.8);
robot.turnRight(0.8, 2.2);
robot.move(0.8, 0.2);
wait(1);
robot.turnRight(0.6, 2.5);
break;
}
}
//writes the user data to a file
//and creates a graph of the user data
void writeout(int uses, int correct, int replays)
{
ofstream writeout;
const char *filename = "demodata.txt";
writeout.open(filename);
writeout << "Data for the demonstration\nNumber of times used: " << uses << endl;
writeout << "Number of correct data matches: " << correct << endl;
writeout << "Average number of times the morse code needed to be replayed: " << replays / uses;
writeout.close();
cout << "The total number of uses was: " << uses << endl;
cout << "The number of correct answers was: " << correct << endl;
cout << "The average number of needed replays was: " << replays / uses << endl;
PicturePtr final = makePicture(300, 500);
for (int i = 50; i < 450; i++) //creates graph of data
{
for (int j = 0; j < 50; j++)
{
if (correct >= uses * (450 - i) / 400)
{
setPixelColor(final, j + 125, i, 0, 255, 0);
}
setPixelColor(final, j + 25, i, 255, 0, 0);
}
}
int limit = replays * 20 / uses;
for (int k = 0; k < limit ; k++)
{
if ( k < 400)
{
for (int m = 0; m < 50; m++)
{
setPixelColor(final, m + 225, 450 - k, 0, 0, 255);
}
}
}
show(final);
}
//no longer used
int str2int(string str)
{
int con;
istringstream convert(str);
convert >> con;
return con;
}
//window class constructor
//gets the picture pointers set, and the number of options in each window
window::window (string img, int num)
{
face = loadPicture(img.c_str());
numch = num;
}
//gets the user choice from the window
int window::choice()
{
Point m;
GraphWin win("Exploration", 1500, 800);
win.setBackground(face); //creates window
int button = 0;
while (true)
{
m = win.getMouse(); //gets mouse location
if (m.getX() < 20 && m.getY() < 20 && numch == 3) //on the first interface is the exit button
{
win.close();
string passtest; //password locked
cout << "Enter the admin password: ";
cin >> passtest;
if (passtest == password)
{
return 3;
}
else
{
cout << "Access Denied\n\n";
GraphWin win("Exploration", 1500, 800);
win.setBackground(face);
}
}
else
{
for (int i = 1; i <= numch; i++)//divides the image into the number of buttons needed
{
if (m.getY() > 299 && m.getX() < 1500 * i / numch)//if mouse is in the range, returns the value
{
win.close();
return i - 1;
}
}
}
}
}
//shows the windows without needing users to click out of them
void window::show(int t)
{
GraphWin finally("Exploration", 1500, 800);
finally.setBackground(face);
wait(t);
finally.close();
}
//sets the password
void window::setpass(string pass)
{
password = pass;
}
//this is the last function
//it contains all of the song data for the different colors
void playsong(int song)
{
int tempo = 176;
double beat = 60.0/180.0;
double a = 220*2;
double as = 233.08*2;
double b = 246.94*2;
double c = 261.63*2;
double cs = 277.18*2;
double d = 293.66*2;
double ds = 311.13*2;
double e = 329.63*2;
double f = 349.23*2;
double fs = 369.99*2;
double g = 392.00*2;
double gs = 415.30*2;
switch (song)
{
case 0:
robot.beep(0.02, 783.99);
robot.beep(0.02, 987.77);
robot.beep(0.02, 1174.66);
robot.beep(0.02, 1567.98);
robot.beep(0.02, 1975.53);
robot.beep(0.02, 830.61);
robot.beep(0.02, 1046.50);
robot.beep(0.02, 1244.51);
robot.beep(0.02, 1661.22);
robot.beep(0.02, 2093);
robot.beep(0.02, 932.33);
robot.beep(0.02, 1174.66);
robot.beep(0.02, 1396.91);
robot.beep(0.02, 1864.66);
robot.beep(0.02, 2349.32);
break;
case 1:
beat = (60.0/tempo);
robot.beep((1.0/3.0)*beat,1046.50);
robot.beep((2.0/3.0)*beat,0.0);
robot.beep((2.0)*beat,1046.50);
robot.beep((2.0/3.0)*beat,0.0);
robot.beep((1.0/3.0)*beat,880.000);
robot.beep((0)*beat,0.0);
robot.beep((1.0/3.0)*beat,932.328);
robot.beep((1.0/3.0)*beat,0.0);
robot.beep((1.0/3.0)*beat,783.991);
robot.beep((0)*beat,0.0);
robot.beep((2)*beat,880.000);
robot.beep((2.0/3.0)*beat,0.0);
robot.beep((1.0/3.0)*beat,698.456);
robot.beep((0)*beat,0.0);
robot.beep((1.0/3.0)*beat,659.255);
robot.beep((1.0/3.0)*beat,0.0);
robot.beep((1.0/3.0)*beat,587.330);
robot.beep((0)*beat,0.0);
robot.beep((2)*beat,523.251);
robot.beep((2.0/3.0)*beat,0.0);
robot.beep((1.0/3.0)*beat,880.000);
robot.beep((0)*beat,0.0);
robot.beep((1.0/3.0)*beat,932.328);
robot.beep((1.0/3.0)*beat,0.0);
robot.beep((1.0/3.0)*beat,659.255);
robot.beep((0)*beat,0.0);
robot.beep((2)*beat,698.456);
robot.beep((1)*beat,0.0);
robot.beep((1.0/3.0)*beat,1046.50);
robot.beep((2.0/3.0)*beat,0.0);
break;
case 2:
for(int x=1;x<=2;x++)
{
robot.beep(0.4,783.99);
robot.beep(0.8,987.77);
}
robot.beep(0.4,783.99);
robot.beep(0.8,1046.50);
robot.beep(0.4,987.77);
robot.beep(0.8,932.33);
robot.beep(0.4,739.99);
robot.beep(0.6,783.99);
robot.beep(0.2,987.77);
robot.beep(0.4,932.33);
robot.beep(0.8,466.16);
robot.beep(0.4,493.88);
robot.beep(2.0,987.77);
for(int y=1;y<=2;y++)
{
robot.beep(0.4,783.99);
robot.beep(0.8,987.77);
}
robot.beep(0.4,783.99);
robot.beep(0.8,1174.66);
robot.beep(0.4,1108.73);
robot.beep(0.8,1046.50);
robot.beep(0.4,830.61);
robot.beep(0.6,1046.50);
robot.beep(0.2,987.77);
robot.beep(0.4,932.33);
robot.beep(0.8,466.16);
robot.beep(0.4,783.99);
robot.beep(2.0,659.26);
break;
case 3:
robot.beep(beat*1.5, d*2.0);
robot.beep(beat*.5, fs);
robot.beep(beat*.5,b*2.0);
robot.beep(beat*.5,d*2.0);
robot.beep(beat*.5,b*2.0);
robot.beep(beat*.5,fs);
robot.beep(beat*1.5,d*2.0);
robot.beep(beat*.5,b);
robot.beep(beat,d);
robot.beep(beat,e);
robot.beep(beat,as*2.0);
robot.beep(beat*.5,cs);
robot.beep(beat*.5,as*2.0);
robot.beep(beat*.5,cs*2.0);
robot.beep(beat*.5,as*2.0);
robot.beep(beat,cs);
robot.beep(beat*4.0,as*2.0);
robot.beep(beat*1.5,a*2.0);
robot.beep(beat*.5,a);
robot.beep(beat*.5,e);
robot.beep(beat*.5,b*2);
robot.beep(beat*.5,e);
robot.beep(beat*.5,a);
robot.beep(beat,a*2.0);
robot.beep(beat*.5,a);
robot.beep(beat*.5,a*2.0);
robot.beep(beat,e);
robot.beep(beat,a);
robot.beep(beat,fs);
robot.beep(beat*.5,gs*.5);
robot.beep(beat,d);
robot.beep(beat*.5,fs);
robot.beep(beat*.5,d);
robot.beep((beat*.5)+(beat*4.0),gs*.5);
robot.beep(beat,g);
robot.beep(beat*.5,g*.5);
robot.beep(beat,d);
robot.beep(beat*.5,g);
robot.beep(beat*.5,d);
robot.beep(beat*.5,g*.5);
robot.beep(beat, b*2.0);
robot.beep(beat*.5,d);
robot.beep(beat,g);
robot.beep(beat,g*.5);
robot.beep(beat,a*2.0);
robot.beep(beat*.5,a);
robot.beep(beat,fs);
robot.beep(beat*.5,a*2.0);
robot.beep(beat*.5,d);
robot.beep(beat*.5,a);
robot.beep(beat*2.0,a*2.0);
robot.beep(beat*2.0,d);
robot.beep(beat, e);
robot.beep(beat*.5, e*.5);
robot.beep(beat,b);
robot.beep(beat*.5,e);
robot.beep(beat*.5,b);
robot.beep(beat*.5, e*.5);
robot.beep(beat,e);
robot.beep(beat*.5,e*.5);
robot.beep(beat*.5,b);
robot.beep(beat,e);
robot.beep(beat,g*.5);
robot.beep(beat,fs);
robot.beep(beat*.5,fs*.5);
robot.beep((beat*.5)+(beat*2.0),as);
robot.beep(beat*2.0, fs);
robot.beep(.1, g*6.0);
wait(beat);
robot.beep(.1, g*6.0);
break;
case 4:
robot.beep(.5,659.3);
robot.beep(.5,659.3);
robot.beep(.5,698.5);
robot.beep(.5,784);
robot.beep(.5,784);
robot.beep(.5,698.5);
robot.beep(.5,659.3);
robot.beep(.5,587.3);
robot.beep(.5,523.3);
robot.beep(.5,523.3);
robot.beep(.5,587.3);
robot.beep(.5,659.3);
robot.beep(.5,659.3);
robot.beep(.25,587.3);
robot.beep(.75,587.3);
wait(.5);
robot.beep(.5,659.3);
robot.beep(.5,659.3);
robot.beep(.5,698.5);
robot.beep(.5,784);
robot.beep(.5,784);
robot.beep(.5,698.5);
robot.beep(.5,659.3);
robot.beep(.5,587.3);
robot.beep(.5,523.3);
robot.beep(.5,523.3);
robot.beep(.5,587.3);
robot.beep(.5,659.3);
robot.beep(.5,587.3);
robot.beep(.3,523.3);
robot.beep(.75,523.3);
break;
case 5:
for (int i = 0; i < 2; i++)
{
robot.beep(1.0,1318.51);
robot.beep(0.4,1318.51);
robot.beep(0.4,1396.913);
robot.beep(0.4,1396.913);
robot.beep(1.0,1174.659);
robot.beep(0.4,1174.659);
robot.beep(0.4,1318.51);
robot.beep(0.4,1318.51);
robot.beep(1.0,1046.502);
robot.beep(0.4,1046.502);
robot.beep(0.4,987.767);
robot.beep(0.4,1046.502);
robot.beep(1.0,880);
robot.beep(0.4,880);
robot.beep(0.4,987.767);
robot.beep(0.4,1046.502);
}
break;
case 6:
robot.beep(0.2,783.99); //G5
robot.beep(0.2,739.99); //F#5
robot.beep(0.2,659.26); //E5
robot.beep(0.2,659.26); //E5
robot.beep(0.8,739.99); //F#5
wait(1.4);
robot.beep(0.2,440.00); //A4
robot.beep(0.2,783.99); //G5
robot.beep(0.2,739.99); //F#5
robot.beep(0.2,659.26); //E5
robot.beep(0.4,659.26); //E5
robot.beep(0.6,739.99); //F#5
robot.beep(0.4,587.33); //D5
robot.beep(0.2,659.26); //E5
robot.beep(1.6,440.00); //A4
robot.beep(0.2,440.00); //A4
robot.beep(0.4,659.26); //E5
robot.beep(0.2,739.99); //F#5
robot.beep(0.6,783.99); //G5
robot.beep(0.2,659.26); //E5
robot.beep(0.2,554.37); //C#5
robot.beep(0.4,554.37); //C#5
robot.beep(0.6,587.33); //D5
robot.beep(0.4,659.26); //E5
robot.beep(0.2,440.00); //A4
robot.beep(0.4,440.00); //A4
robot.beep(1.4,739.99); //F#5
wait(0.8);
robot.beep(0.2,783.99); //G5
robot.beep(0.2,739.99); //F#5
robot.beep(0.2,659.26); //E5
robot.beep(0.2,659.26); //E5
robot.beep(0.8,739.99); //F#5
wait(1.4);
robot.beep(0.2,440.00); //A4
robot.beep(0.2,783.99); //G5
robot.beep(0.2,739.99); //F#5
robot.beep(0.2,659.26); //E5
robot.beep(0.6,659.26); //E5
robot.beep(0.2,739.99); //F#5
robot.beep(0.6,587.33); //D5
robot.beep(0.2,659.26); //E5
robot.beep(1.0,440.00); //A4
break;
case 7:
for(int x = 1;x <= 4;x++)
{
robot.beep(0.2,1174.66);
}
wait(0.4);
robot.beep(0.2,1174.66);
robot.beep(0.4,1046.50);
robot.beep(0.2,932.33);
robot.beep(0.4,698.46);
robot.beep(0.2,698.46);
robot.beep(0.2,587.33);
robot.beep(0.2,932.33);
robot.beep(0.4,1174.66);
robot.beep(0.4,1174.66);
robot.beep(0.2,1046.50);
robot.beep(0.2,932.33);
robot.beep(0.6,1046.50);
robot.beep(0.2,587.33);
robot.beep(0.2,587.33);
robot.beep(0.2,587.33);
robot.beep(0.2,587.33);
break;
case 8:
robot.beep(1, d);
robot.beep(1, e);
robot.beep(2.5, f);
break;
}
}
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees