# CS 101: September 2012

## August 27 - September 3, 2012

This week was basically spent with me pulling my hair out about how Mountain Lion on my Mac and Shelby just wouldn't get along. But luckily, they've made amends since then.

## September 5, 2012

So after I FINALLY got my robot connected, we spent our lecture figuring out how to use the robot.motors() command to draw things. You can see my amazing artistic skills at work in drawing a backwards S right here.

## September 7, 2012

The goal of this lab was to write a program to get the sensor readings on all of the robots in our group, and figure out how to manipulate them when they're on different surfaces.

Lights (Left, Center, Right): (65322, 65192, 65284)
Line Sensor: 1, 0
IR Sensors (Left, Right): 1, 1
Fluke (Obstacle) Sensors: (0,0,0)
Battery: 7.19688

Lights (Left, Center, Right): (65403, 65283, 65281)
Line Sensor: 1, 0
IR Sensors (Left, Right): 1, 1
Fluke (Obstacle) Sensors: (0,0,0)
Battery: 7.33987

Lights (Left, Center, Right): (65181, 65288, 65056)
Line Sensor: 1, 0
IR Sensors (Left, Right): 1, 1
Fluke (Obstacle) Sensors: (0,0,0)
Battery: 7.33987

Lights (Left, Center, Right): (65257, 65257, 65257)
Line Sensor: 0, 0
IR Sensors (Left, Right): 1, 1
Fluke (Obstacle) Sensors: (640,0,0)
Battery: 7.29221

## September 10, 2012

The goal of today's lecture was to learn how to use an "if statement." The way that I tested it out was that if the robot was on a dark surface, it would execute the command to beep and go forward. Here's my code:

``````#include <Myro.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main(int argc, char ** argv)
{
connect("/dev/tty.Fluke2-021E-Fluke2");
cout << "Waiting..." << endl;
wait(2);
if (robot.getLine()[0]==0)
{
cout << "Beep and go forward..." << endl;
robot.beep(1,880);
robot.forward(1,30);
wait(2);
cout<<"Thank you master!"<< endl;
cout<<"Disconnecting..."<< endl;
return 0;
}
}
``````

## September 12 & 14, 2012

The goal of this lecture was to create a program that would use all of the sensors in the robot (Battery, Light, IR, Line, Obstacle) along with all of the control structures that we've learned (for, if, while). I created a program that would make the robot move forward as long as there was nothing in the way, and would turn right 90 degrees if there was something. I also made it so that the robot would start beeping if there was a light shined into the left light sensor. Here's the code:

``````
#include <Myro.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;

void LeftAngle(double degrees)
{
robot.motors(-1.0,1.0);
wait(0.0088888888888*degrees);
}

void RightAngle(double degrees)
{
robot.motors(1.0,-1.0);
wait(0.009*degrees);
}

int main(int argc, char ** argv)
{
connect("/dev/tty.Fluke2-021E-Fluke2");
cout << "Connecting..." << endl;
wait(2);

for (int n=0; n<=40; n++)
{

if (robot.getObstacle("left") ==0 &&
robot.getObstacle("center") ==0 &&
robot.getObstacle("right")==0)
{
robot.motors(1.0,1.0);
int b = robot.getBattery();
}
else
{
RightAngle(90);
}

cout << "Cycle Number: " << n << endl;
cout << "Left Light Sensor: " << robot.getLight("left") << endl;

while (robot.getLight("left") <= 50000 ){
robot.beep(0.3,880);
}

}
cout <<"Disconnecting..."<< endl;
disconnect();
}

``````

## September 17 & 19, 2012

Now things get a little tricky. We had to try to get our robots to draw a Fibonacci spiral (the thing in nature that's usually seen in shells and stuff like that). We had to come up with an algorithm that had the robot move and draw us the spiral. In the end, we came up with this:

Not too shabby.

## September 21, 2012

Robot's Got Talent

## September 24, 2012

Today we tried to get our robots to take on different personalities. Our first personality, "Alive," makes the robot act like a cockroach - it detects the current ambient light, sets that as the threshold, and then tries to move away as quickly as it can from any new light source to get back to whatever that set threshold was.

Here's the code:

``````/* Braitenberg Vehicle #1: "Alive"
* The purpose of this program is to have a robot that will move quicker
* when exposed to brighter lights.
*
* Program written on September 24, 2012.
*/

#include <Myro.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;

double Ambient;
double normalize(int v)
{
if (v > Ambient)
{v = Ambient;}
return 1.0 - v/Ambient;
}

int main ()
{
connect("/dev/tty.Fluke2-021E-Fluke2");
cout << "Connecting..." << endl;
Ambient = robot.getLight("center");
cout << "Are you ready to continue? (y/n): "; cin >> answer;
{
int L = robot.getLight("center");
robot.forward(normalize(L));
}
disconnect();
cout <<"Disconnecting..."<< endl;
return 0;

}
``````

Our second robot personality was called "Coward." This robot type tries to turn away from any bright light source.

``````/* Braitenberg Vehicle #2: "Coward"
* The purpose of this program is to have a robot that will turn away
* from any bright light source.
*
* Program written on September 24, 2012.
*/

#include <Myro.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;

double Ambient;
double normalize(int v)
{
if (v > Ambient)
{v = Ambient;}
return 1.0 - v/Ambient;
}

int main ()
{
connect("/dev/tty.Fluke2-021E-Fluke2");
cout << "Connecting..." << endl;
Ambient = (robot.getLight("left") + robot.getLight("center") + robot.getLight("right")) / 3.0;
cout << "Are you ready to continue? (y/n): "; cin >> answer;
{
int L = robot.getLight("left");
int R = robot.getLight("right");
robot.motors(normalize(L), normalize(R));
}
disconnect();
cout <<"Disconnecting..."<< endl;
return 0;

}
``````

Flipping the normalize(L) and normalize(R) commands will cause the robot to take on the "Aggressive" personality, where it attacks the light.

## September 26, 2012

Today we combined all of the personalities that we learned into one program with a menu so that we could choose whichever one we wanted to use. Here's the code:
``````
#include <Myro.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;

double Ambient;
double normalize(int v)
{
if (v > Ambient)
{v = Ambient;}
return 1.0 - v/Ambient;
}

void alive()
{
connect("/dev/tty.Fluke2-021E-Fluke2");
cout << "Connecting..." << endl;
Ambient = (robot.getLight("center"));
while (true)
{
int L = robot.getLight("left");
robot.forward(normalize(L));
}
disconnect();
cout <<"Disconnecting..."<< endl;
}

void coward()
{
connect("/dev/tty.Fluke2-021E-Fluke2");
cout << "Connecting..." << endl;
Ambient = (robot.getLight("left") + robot.getLight("center") + robot.getLight("right")) / 3.0;
while (true)
{
int L = robot.getLight("left");
int R = robot.getLight("right");
robot.motors(normalize(L), normalize(R));
}
disconnect();
cout <<"Disconnecting..."<< endl;
}

void aggressive()
{
connect("/dev/tty.Fluke2-021E-Fluke2");
cout << "Connecting..." << endl;
Ambient = (robot.getLight("left") + robot.getLight("center") + robot.getLight("right")) / 3.0;
while (true)
{
int L = robot.getLight("left");
int R = robot.getLight("right");
robot.motors(normalize(R), normalize(L));
}
disconnect();
cout <<"Disconnecting..."<< endl;
}

void indecisive()
{
connect("/dev/tty.Fluke2-021E-Fluke2");
cout << "Connecting..." << endl;
Ambient = (robot.getLight("center"));

while (true) {
if (Ambient <= 55000)
{
robot.motors(1.0,1.0);
cout<< Ambient<< endl;
}
else
{
robot.motors(-1.0,-1.0);
cout<< Ambient<< endl;
}
}
}

int main ()
{
cout << "Which program would you like to use?" << endl;
cout << "Enter 1 for Alive, 2 for Coward, 3 for Aggressive, 4 for Indecisive: "; cin >> Answer;

{
alive();

}

{
coward();

}

{
aggressive();

}