WiFi Tank

HINT!
To all those who try it: Do not drive it into a dark room, it is dark in there, and there are a pretty good chance that you can't find the way out again.

My WiFi tank is now complete enough to be driven around by visitors, but there are still improvements to come. Here is how far I am with it.

#include <WiServer.h>
#define WIRELESS_MODE_INFRA      1
#define WIRELESS_MODE_ADHOC      2



//PWM output to each motor
int lM = 120;
int rM = 120;

//H-Bridge pins
int EA = 3;
int EB = 4;
int I1 = 41;
int I2 = 40;
int I3 = 39;
int I4 = 38;

//Proximity sensor pins
int pFl = 37; //Front left
int pFr = 36; //Front right

//Battery info
int bV = 15; //Battery voltage
int bA = 13; //Battery voltage

//Relays
int kS = 35; //Kill switch

//Input pins
int iPins[] = {
  pFr, pFl};

//Output pins
int oPins[] = {
  kS, I1, I2, I3, I4};

//Used to make the tank deny all movements
boolean online = true;

//Used for the proximity sensors, to be able to move away from the obstacle
int lastMove = 0; //1 = forwards - 2 = backwards

//Millis to calculate when to call the stopS() function
unsigned long stopMove = 0;

//Milliseconds it should move
int stopTime = 0;



// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {
  192,168,1,15};      // IP address of WiShield
unsigned char gateway_ip[] = {
  192,168,1,1};      // router or gateway IP address
unsigned char subnet_mask[] = {
  255,255,255,0};      // subnet mask for the local network
const prog_char ssid[] PROGMEM = {
  "Captain Slow"};            // max 32 bytes

unsigned char security_type = 3;      // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {
  "xxxxxxxxxxxxxx"};      // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 
  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,      // Key 0
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Key 1
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Key 2
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00      // Key 3
};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------


// This is our page serving function that generates web pages and handles requests
boolean sendMyPage(char* URL)
{
  if (strcmp(URL, "/F1") == 0)
  {
    stopTime = 750;
    stopMove = millis();
    moveF();
  }

  if (strcmp(URL, "/F2") == 0)
  {
    stopTime = 375;
    stopMove = millis();
    moveF();
  }

  else if (strcmp(URL, "/B1") == 0)
  {
    stopTime = 750;
    stopMove = millis();
    moveB();
  }

  else if (strcmp(URL, "/B2") == 0)
  {
    stopTime = 375;
    stopMove = millis();
    moveB();
  }

  else if (strcmp(URL, "/L3") == 0)
  {
    stopTime = 300;
    stopMove = millis();
    moveL();
  }

  else if (strcmp(URL, "/L2") == 0)
  {
    stopTime = 200;
    stopMove = millis();
    moveL();
  }

  else if (strcmp(URL, "/L1") == 0)
  {
    stopTime = 100;
    stopMove = millis();
    moveL();
  }

  else if (strcmp(URL, "/R3") == 0)
  {
    stopTime = 300;
    stopMove = millis();
    moveR();
  }

  else if (strcmp(URL, "/R2") == 0)
  {
    stopTime = 200;
    stopMove = millis();
    moveR();
  }

  else if (strcmp(URL, "/R1") == 0)
  {
    stopTime = 100;
    stopMove = millis();
    moveR();
  }

  double volts = map(analogRead(bV), 0, 1023, 0, 1933) / 100.0;
  double amps = map(analogRead(bA), 512, 1023, 0, 500) / 100.0;
  if (amps < 0.01)
  {
    amps = 0.01;
  }

  // Use WiServer's print and println functions to write out the page content
  WiServer.print(volts);
  WiServer.print("|");
  WiServer.print(amps);

  return true;
}

void moveF() //Forwards
{
  if (digitalRead(pFl) == HIGH && digitalRead(pFr) == HIGH)
  {
    lastMove = 1;
    analogWrite(EA, lM);
    analogWrite(EB, rM);
    digitalWrite(I1, LOW);
    digitalWrite(I2, HIGH);
    digitalWrite(I3, HIGH);
    digitalWrite(I4, LOW);
  }
}

void moveB() //Backwards
{
  lastMove = 2;
  analogWrite(EA, lM);
  analogWrite(EB, rM);
  digitalWrite(I1, HIGH);
  digitalWrite(I2, LOW);
  digitalWrite(I3, LOW);
  digitalWrite(I4, HIGH);
}

void moveL() //Left
{
  if (digitalRead(pFl) == HIGH)
  {
    analogWrite(EA, lM);
    analogWrite(EB, rM);
    digitalWrite(I1, HIGH);
    digitalWrite(I2, LOW);
    digitalWrite(I3, HIGH);
    digitalWrite(I4, LOW);
  }
}

void moveR() //Right
{
  if (digitalRead(pFr) == HIGH)
  {
    analogWrite(EA, lM);
    analogWrite(EB, rM);
    digitalWrite(I1, LOW);
    digitalWrite(I2, HIGH);
    digitalWrite(I3, LOW);
    digitalWrite(I4, HIGH);
  }
}

void moveS() //Stop
{
  lastMove = 0;
  stopMove = 0;
  analogWrite(EA, 0);
  analogWrite(EB, 0);
  digitalWrite(hI1, HIGH);
  digitalWrite(hI2, HIGH);
  digitalWrite(hI3, HIGH);
  digitalWrite(hI4, HIGH);
}

void moveES() //Emergency Stop
{
  moveB();
  delay(50);
  moveF();
  delay(50);
  moveS();
}

void cutPower() //Pull the latching relay to turn everything off
{
  digitalWrite(kS, HIGH);
  delay(100);
  digitalWrite(kS, LOW);
}

//IP of my NAS
uint8 ip[] = {
  192,168,1,245};

// Function to print response from the web server
void printData(char* data, int len)
{
}

//Define the request
GETrequest getServer(ip, 80, "192.168.1.245", "");

void setup()
{
  pinMode(41, OUTPUT);
  //Set all output pins
  for(int i=0; i < sizeof(oPins) / sizeof(oPins[0]); i++)
  {
    pinMode(oPins[i], OUTPUT);
  }
      
  //Set all input pins
  for(int i=0; i < sizeof(iPins) / sizeof(iPins[0]); i++)
  {
    pinMode(iPins[i], INPUT);
  }

  //Reset H-Bridge
  moveS();

  //Prepare WiShield
  getServer.setReturnFunc(printData);
  WiServer.init(sendMyPage);
}

void loop()
{
  WiServer.server_task();

  double volts = map(analogRead(bV), 0, 1023, 0, 1933) / 100.0;

  if (lastMove == 1 && (digitalRead(pFl) == LOW || digitalRead(pFr) == LOW))
  {
    moveES();
  } 

  if ((millis() - stopMove >= stopTime || volts <= 7) && stopMove != 0)
  {
    moveS();
  }

  //If not online, pull the latching relay to disconnect from battery
  if (!online)
  {
    delay(250);
    cutPower();
    delay(250);
    return;
  }

  //If voltage is too low, call the web server to flag the tank as offline
  if (volts <= 7)
  {
    getServer.setURL("/arduino/control/call.php?status=nopower");
    getServer.submit();

    //Set to offline, to no more commands will be possible
    online = false;
    return;
  }
}

Try it here: http://bld.is-a-geek.com/wifitank/ (only when I am home so far)

Latest picture from it:

And as some already might have noticed... Got plastic for some doors, as they are being painted. :wink:

Awesome project.

Your cat is eating.. and I'm trying to go on your foot

Your cat is eating.. and I'm trying to go on your foot

You can't run into anything, the proximity sensors stop it before hitting anything. :wink:

0:55 in the movie

I know that but it was funny ;D
The data from the webcam uses the same WiFi link of arduino or you are using a wireless cam?

The camera is a separate that got nothing to do with the arduino

Awesome project :slight_smile:

I am pretty pleased with the battery time too...

"Battery: 9.61V" after 5 hours, and it is a 9.6V battery, but starts at 10.2 right after charging.

Putting the battery in the charger now...

:slight_smile: Awesome. Too bad someone else came to disturb me exploring your house.

Someone is driving around right now :slight_smile: Was some earlier (30 mins ago) but I had to take it offline to recharge the battery. That only takes about 30 minutes, and it is then good for another ~5 hours

You should clean up, found an interesting magazine offer laying on the floor? ;D

PS: apparently I parked it under your bed...............
what can I say, its great fun.. wanted to see what it could do, lol
Geuss its not powerful enough to mess around underneath furniture.
Sorry 'bout that..

How did you do the webcam part, was it already wireless, or did you hack it?

Got it out again, was under my bed... as far under as it could be...

It is an analog wireless camera, that I connected to a yoics 9100 camera server.

Smart idea :slight_smile: :slight_smile: :slight_smile:

I nearly jump on a Cola Cola bottle
Nice job bld !!

Actually, you almost ran over my heat gun xD it was balancing on top of it, and if you had moved forwards instead of backwards, it would have driven all the way over it.

Still need to tweak the proximity sensors, but also going to add two distance sensors to it, so it got a proximity sensor at each corner, and a distance sensor front and back.

Just been harassing the coke bottle, well done, I want one!

Took it offline... It have been running around here all day. And now going to make some updates to it. :wink:

Will be back again tomorrow.

How did you do the camera setup/image capture?

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1287921858/13#13