Wednesday, 25 February 2015

AUV Arduino Code




Due to not having a variety of sensors and only one ultrasound, the AUV is coded so when it encounters a wall it will reverse and then turn right. Below is the code which was developed for the Arduino UNO. Resources used to develop code has been listed in the references at the bottom of the post.

--------------------------------------------------------------------------------------


#define LogicR1 2 // Defining the h-bridge pins to digital outputs on the arduino
#define LogicR2 3
#define LogicL1 4
#define LogicL2 5
#define trigger 13 // defining the sensor's trigger and echo pin
#define echo 12
#define LogicX1 7
#define LogicX2 8

void setup()
{
Serial.begin(9600); //9600 bits per seconds.A default setting for arduino
pinMode(echo,INPUT); // configuring the echo pin to act as an input 
pinMode(LogicR1,OUTPUT); // configuring all the other defined pins to act as outputs.
pinMode(LogicR2,OUTPUT);
pinMode(LogicL1,OUTPUT);
pinMode(LogicL2,OUTPUT);
pinMode(trigger,OUTPUT);
pinMode(LogicX1,OUTPUT);
pinMode(LogicX2,OUTPUT);
}


void loop()
{
 
long Time;
long distance; // defining the terms distance and time as long integer
  digitalWrite(trigger, LOW);  //sets the trigger pin off
  delayMicroseconds(4); // delays for 4 microseconds
 
  digitalWrite(trigger, HIGH); // http://arduino.cc/en/Reference/DelayMicroseconds
  delayMicroseconds(9);
 
  digitalWrite(trigger, LOW); // This process configures the trigger pin to work as an output. Reference websit above.
 
  Time = pulseIn(echo, HIGH); //pulseIn reads the pulse when the echo pin is high and sets the value as Time. 
 
  distance = (Time/2) / 29.1; // 29.1 is the speed of sound in a kilometer
  if (distance < 6) { 
    STOP();
    Back(3000); // the AUV goes back for 3 seconds before turning right for seconds.
    Turn_Right(2000);
    Go_Forward();
   
}
  else {
    Go_Forward();
  }
  if (distance >= 220 || distance <= 0){ // if the distance is far, the AUV is set to turn left and then go straight.
    Serial.println("The AUV can't detect anything");
   
   Turn_Left(2000);
   Go_Forward();
  
  }
  else {
    Serial.print(distance);
    Serial.println(" cm"); // prints the distance to the object in centimeters.
  }
  delay(400); // delays for 4 milliseconds



}




void Go_Forward()
{
Serial.println("The AUV is going forward!");
digitalWrite(LogicR1,LOW);  // H-bridge configuration for the AUV to go forward
digitalWrite(LogicR2,HIGH);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,HIGH);

}


void STOP() 
{
Serial.println ("STOPPPPP!!!"); // H-bridge configuration for the AUV to go stop

digitalWrite(LogicR1,LOW);
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,LOW);

}


void Turn_Right(int Enter_Time)
{
Serial.println ("The AUV turns right"); // H-bridge configuration for the AUV to go right

digitalWrite(LogicR1,LOW);
digitalWrite(LogicR2,HIGH);
digitalWrite(LogicL1,HIGH);
digitalWrite(LogicL2,LOW);

delay(Enter_Time);

digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);

}


void Turn_Left(int Enter_Time)
{
Serial.println ("The AUV turns left");
digitalWrite(LogicR1,HIGH); // h-bridge pin configuration for turning left
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,LOW);
digitalWrite(LogicL2,HIGH);

delay(Enter_Time); // once the time is entered, the AUV turns left for that many seconds.

digitalWrite(LogicR1,LOW); // Stops the motor after turning left.
digitalWrite(LogicL2,LOW);

}



void Back(int Enter_Time)
{
Serial.println ("The AUV is going back");  // H-bridge configuration for the AUV to go backwards
digitalWrite(LogicR1,HIGH);
digitalWrite(LogicR2,LOW);
digitalWrite(LogicL1,HIGH);
digitalWrite(LogicL2,LOW);

delay(Enter_Time);

digitalWrite(LogicR1,LOW);
digitalWrite(LogicL1,LOW);
}

----------------------------------------------------------------------------------------------

In our recent laboratory session we spent time troubleshooting the circuit due to it not working. We have now realised the 5V output of the Arduino was struggling to power the ultrasound, h-bridge and motors. Therefore we will need to include another battery supply in circuit, a switch will also be added. Below are the new circuit diagrams:






In other news the motors have now been waterproofed, work on the shell/case has begun and we have retrieved the tank to test out motor.





A two page sustainability report has also been created talking about the regulatory information that we would need to follow, the implications of the manufacturing the AUV at a large scale, and the ethical implications of the market and follow-on products.
Additionally we have made a start on the final report, this is as we have decided that if we start now we can continuously add to it and improve it, instead of having to do all the work at the end. So far we have done an abstract and discussed our original research and hope to expand on it over the coming weeks.


Tasks for next meeting:

Collect and test Perspex sheet
Continue work on shell/case
Complete circuit testing
Modify water tank for testing
Start planning bench inspection poster design and content


References:
[1] Arduino and HC-SR04 Example, http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/
[2] Arduino Libraries, http://arduino.cc/en/reference/libraries
[3]Arduino Programming Notebook, http://playground.arduino.cc/uploads/Main/arduino_notebook_v1-1.pdf
[4]Measuring a watertank level, http://www.makechronicles.com/2012/06/09/arduino-project-11-2d3d-pictures-measuring-a-water-tank-level-v2-hc-sr04-ultrasonic-rangefinderarduino-uno1-0/
[5]Obstacle Avoidance System for Unmanned Underwater Vehicle Using Fin System, http://www.ijisme.org/attachments/File/v1i9/I0407081913.pdf

No comments:

Post a Comment