Saturday, February 13, 2016

Day 15 to 18 - Fire Fight Challenge


  • Our objective was to autonomously program a Vex Squarebot robot to navigate throughout a maze and blow out a candle. There were four rooms and the candle was randomly placed in one of the rooms. In order to have the candle detect the candle we used an IR detector to sense the heat from the candle and we used encoders on the wheels to make sure that the robot could have been commanded to go through the maze.
Here is the code for our robot.#pragma config(Sensor, in1,    rightEncoder,   sensorRotation)
#pragma config(Sensor, in3,    leftEncoder,    sensorRotation)
#pragma config(Sensor, in4,    fan,            sensorDigitalOut)
#pragma config(Sensor, in6,    frontIRSensor,  sensorAnalog)
#pragma config(Sensor, in14,   frontSonarSensor, sensorSONAR, int1)
#pragma config(Motor,  port2,           rightMotor,    tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorServoContinuousRotation, openLoop)

void clearEncoders();
void forward(int x);
void tleft(int x);
void tright(int x);
void backwards(int x);
void stop(int x);
void search();
void flame();

task main()
{
wait1Msec(2000);
bMotorReflected[port2] = true;
while(true)
{
//approach room 1//
clearEncoders();
stop(200);
forward(200);
stop(800);
tright(85);
stop(800);
forward(190);
stop(800);
tright(70);
stop(800);

//enter room 1 //
forward(130);
stop(800);
flame();
stop(800);
tleft(115);
stop(100);

//exit room 1 and approach room 2//
backwards(290);
stop(800);
forward(15);
stop(800);
tright(90);
stop(800);
backwards(110);
stop(800);
forward(205);
stop(800);
tright(80);
stop(800);
forward(290);
stop(800);
tright(65);
stop(800);

//enter room 2//
forward(120);
stop(800);
flame();
stop(800);
tleft(115);
stop(800);

//exit room 2 approach room 3//
backwards(150);
stop(800);
tleft(55);
stop(800);
backwards(360);
stop(800);
forward(25);
stop(800);
tright(85);
stop(800);
backwards(180);
stop(800);

//enter room 3//
forward(150);
stop(800);
tright(80);
stop(800);
forward(300);
stop(800);
flame();
stop(800);
tleft(115);
stop(800);

//exit room 3 and approach room 4//
backwards(450);
stop(800);
forward(50);
stop(800);
tleft(55);
stop(800);
forward(100);
stop(800);
tright(85);
stop(800);
forward(200);
stop(800);
tleft(75);
stop(800);

//enter room 4//
forward(150);
stop(800);
flame();
tleft(145);
stop(100);



}


}


void clearEncoders()
{
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0; //function for encoders to reset their count; therefore, to ensure that encoders are accurate at keeping consistent movements.
}

void forward(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] < x) //function ensures that both encoders are working 
{
motor[leftMotor] = 65;
motor[rightMotor] = 60;
}
}
void tleft(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] < x)
{
motor[leftMotor] = -50;
motor[rightMotor] = 50;
}
}

void tright(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] < x)
{
motor[leftMotor] = 50;
motor[rightMotor] = -40;
}
}

void backwards(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] < x)
{
motor[leftMotor] = -60;
motor[rightMotor] = -60;
}
}

void stop(int x)
{
clearEncoders();
{
motor[leftMotor] = 0;
motor[rightMotor] = 0;
wait1Msec(x);
}
}


void flame() ******
{
int count=0;
while(SensorValue[frontIRSensor]==0 && count<30)//was this while function makes the IR sensor search for a flame
{
tright(5); // turns right 30 times at (5) power or in other words it is a tright(150); command if there is no IR value greater than one
stop(5);
count++;
}
if(SensorValue[frontIRSensor]>1)//loop until sonar finds an object <20 inches.  // when the IR sensor detects a flame it should register a value greater than one.
{
while(SensorValue[frontIRSensor]<7) // The while function will not end until the IR sensor registers a value less than 7
{
forward(5); // keep going forward near the flame
}
stop(200);
SensorValue[fan]=true;
wait10Msec(1000);
SensorValue[fan]=false; // after the while statement above is fulfilled and the squarebot is close enough to the flame it will turn on the fan
wait10Msec(500000000);

}
}
  • Here is the video of this code in action with our sqaurebot

Wednesday, February 3, 2016

Day 13 - Ultra Sonic sensors on VEX squarebot

*The Lab today consisted of designing a squarebot that could navigate autonomously using functions and avoid walls forever or approach a can. The setup for this lab utilized encoders to consistently have the same movements; in addition, there were sonar sensors attached to the bot so it could "see" the walls and can to avoid or approach the items.
  • Autonomous robot that avoids the walls
    • Here is the code
#pragma config(Sensor, in2,    rightEncoder,   sensorRotation)
#pragma config(Sensor, in3,    leftEncoder,    sensorRotation)
#pragma config(Sensor, in9,    sonarSensor,    sensorSONAR, int1)
#pragma config(Motor,  port2,           rightMotor,    tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorServoContinuousRotation, openLoop)

void clearEncoders();
void forward(int x);
void tleft(int x);
void tright(int x);
void backwards(int x);
void stop(int x);



task main()

{ wait1Msec(2000);
bMotorReflected[port2]=1;
while(1)
{

if (SensorValue[sonarSensor]>3 ||SensorValue[sonarSensor]==-1)//loop until sonar finds an object less than 3 inches.
{
forward(20);//go forward

}
else
{
backwards(20);
stop(1);
tleft(70);
}
}
}

void clearEncoders() //must clear encoders every time so that way their measurements are accurate
{
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
}

void forward(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x) // read both the left && right encoders
{
motor[leftMotor] = 60;
motor[rightMotor] = 50;
}
}
void tleft(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x)
{
motor[leftMotor] = -40;
motor[rightMotor] = 40;
}
}

void tright(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x)
{
motor[leftMotor] = 60;
motor[rightMotor] = -40;
}
}

void backwards(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x)
{
motor[leftMotor] = -40;
motor[rightMotor] = -40;
}
}

void stop(int x)
{
clearEncoders();
while(SensorValue[leftEncoder] < x && SensorValue[rightEncoder] <x)
{
motor[leftMotor] = 0;
motor[rightMotor] = 0;
}
}

    • Picture of avoiding walls

Day 11 - Vex Programing of squarebot without encoders


  • Today we assembled and programmed the squarebot to drive through a maze. We used commands to tell the robot where to go during the maze. This code is not autonomous because it is not dependent on any sensors.
    • Here is the code for our sqaure bot


/*************************************************************************
VEX - Point Turns

Description: This program instructs your robot turn right for .75 seconds
and then turn left for .75 seconds. There is a two second pause at the
beginning of the program.

Configuration: This program is written to work with the Squarebot model.
Right Motor - port2
Left Motor - port3

Additional Notes:
- The "bMotorReflected[port2] = 1;" is needed with the Squarebot model,
but may not be needed for all robot configurations.
- Point Turns, or turns in place, are achieved by having the motors spin
in opposite directions.
*************************************************************************/
void stop(int x);

task main()
{
wait1Msec(3000); //Robot waits for 2000 milliseconds before executing program
bMotorReflected[port2] = 1; //Reflects the direction of the motor on port2

motor[port2] = 70; //Motor forward 1
motor[port3] = 70; //Motor on port3 is run at full (127) power forward
wait1Msec(3000);
stop(500);

//Turn LEFT
motor[port2] = 78; //Motor on port2 is run at full (-127) power reverse 68
motor[port3] = -83; //Motor on port3 is run at full (127) power forward
wait1Msec(400);//Robot runs previous code for 750 milliseconds before moving on
stop(500);

motor[port2] = 95; //FORWARD 2
motor[port3] = 95; //Motor on port3 is run at full (127) power forward
wait1Msec(1000);
stop(500);

motor[port2] = 130; //Turn 2nd LEFT
motor[port3] = -100; //Motor on port3 is run at full (127) power forward
wait1Msec(300);
stop(500);

motor[port2] = 85; //Forward 3
motor[port3] = 85; //Motor on port3 is run at full (127) power forward
wait1Msec(1700);
stop(500);

motor[port2] = -70; //Turn RIGHT
motor[port3] = 70; //Motor on port3 is run at full (127) power forward 68-70
wait1Msec(500);
stop(500);

motor[port2] = 60; //Foward 4
motor[port3] = 60; //Motor on port3 is run at full (127) power forward
wait1Msec(1000);
stop(500);

motor[port2] = -88; //Turn RIGHT
motor[port3] = 88; //Motor on port3 is run at full (127) power forward
wait1Msec(400);
stop(500);

motor[port2] = 80; //Final Forwa
motor[port3] = 80; //Motor on port3 is run at full (127) power forward
wait1Msec(2000);

} //Program ends, and the robot stops

void stop(int x)
{
motor[port2] = 0;
motor[port3] = 0;
wait1Msec(x);
}
    • In addition here is a video that shows our square bot in action


Day 10 - Hack a toy


  • The objective of the Hack-a-Toy project was to buy an old toy and dismantle it then re configure it and program it to do similar functions with the arduino.
  1. Here is the toy before it was was dismantled. It is the bunny on the left.
  2. The first step to hacking the toy was to take it apart. Here is a descriptive video that shows the toy's electronic components after it was taken apart.  An interesting part about the toy's anatomy is that it is that the system used transistors, motors, resistors, speakers, and other components.
  • The plan to reconfigure the toy involved making the toy so that the temperature of a human body could trigger the motor of the bunny. 
  1. Here is a picture of how, mechanically, this was achieved.  

Day 9 - Intro to Infrared Detectors and Ultrasonic Sensors


  • For this lab we used the arduino, along with IR Detector and Ultrasonic Sensors to view heat and distance values using the Arduino. 
    • Here is the code for the IR sensor portion of the lab.

#define sensorPin A0 // select the input pin (pin2)
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600);//9600
}
void loop() {
  sensorValue = analogRead(sensorPin); // read the value fromt he sensor:
  Serial.println(sensorValue);
  delay(300);
 // while (Serial.available()){
   // int inChar = Serial.read();
   // Serial.print(inChar);
   // delay(300);
  //}
}
    • Picture of IR Sensor in action
    • Code for Sonar Sensor.
const int trigPin = 2;
const int echoPin = 4;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

    • Video for Sonar Sensor

Day 6 - Intro to Sensors and Microcontroller inputs and outputs


  • Microcontrollers, input and outputs
    •  Demonstrate Push button LED using if/else statement
      • Here is how it was built in the video and what the end result was.


      • In addition here is the code that helped achieve this


#define buttonPin 8 //# of pushbutton pin
#define ledPin 6    //# of LED pin
int buttonState = 0; //variable for reading the pushbutton status
void setup() {
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as output:
  pinMode(buttonPin, INPUT);  //initialize push button as input:
}
void loop() {
buttonState = digitalRead(buttonPin); //read the state of the pushbutton value:
// check if push buton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH); // turn LED on:
}
else {
digitalWrite(ledPin, LOW); // turn LED off
}
}

Day 5 - Intro to c code and hbridge


  • Driving Motors and Other Output Devices
  1. Demonstrate microcontroller controlling signal lamp
    1. Here is the picture of the contraption In addition here is the code that allowed the microcontroller to control the signal lamp.
void setup() {
  pinMode(9, OUTPUT); //Initialize Digital Pin 9 as an output
}

void loop() {
  digitalWrite(9, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(5000);              // wait for a second
  digitalWrite(9, LOW);    // turn the LED off by making the voltage LOW
  delay(3000);              // wait for a second
}
2. Demonstrate Darlington controlling signal lamp
3. Demonstrate microcontroller controlling motor

void setup() {
  pinMode(9, OUTPUT); //Initialize Digital Pin 9 as an output
}

void loop() {
  digitalWrite(9, HIGH);   // turn the motor on (HIGH is the voltage level)
  delay(5000);              // wait for a second
  digitalWrite(9, LOW);    // turn the motor off by making the voltage LOW
  delay(3000);              // wait for a second
}
4. Demonstrate PWM signal driving motor
#define motorPin 9 //Motor connected to dig. pin 9
void setup() {
  // nothing happens in set up

}

void loop() {
analogWrite (motorPin, 90);//set motor to 20%
delay (1000);
analogWrite (motorPin, 50);//set motor to 90%
delay (1000);

}
  • Bi-directional Motor Control
  1. This first video demonstrates DPDT relay controlled the motor in both directions.
  2. In addition, this next picture and video shows a motor being controlled in opposing directions because of the arduino programming.Here is the code for this achievement 
    void setup() {
      pinMode (13, OUTPUT) ; //Initialize Digital Pin 13 as an output
        pinMode (12, OUTPUT) ; //Initialize Digital Pin 12 as an output
    }

    void loop () {
      digitalWrite (13,HIGH) ; //Set the motor On ClockWise
      delay (100);           //Wait for 1000 ms (1 Second)
      digitalWrite(13,LOW);  //Set the LED off
      delay (1000);   //Wait for 1 second
     
      digitalWrite (12,HIGH) ; //Set the motor On CounterClockWise
      delay (100);           //Wait for 1000 ms (1 Second)
      digitalWrite(12,LOW);  //Set the LED off
      delay (1000);   //Wait for 1 second
    }