WiFi Tank

This post will be updated along with the progress on the tank

A short intro to what this is

The code for the arduino controlling all of it.

//Volts needed to be functional
int nV = 7;

//PWM amount output to each motor (0-255)
int lM = 120;
int rM = 120;

//H-Bridge pins
int EA = 3; //PWM
int EB = 4; //PWM
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;

//Milliseconds from when cV was over nV
unsigned long vD = 0;

//Current Voltage
int cV = 0;

//Current Amps
int cA = 0;

#include <WiServer.h>
#define WIRELESS_MODE_INFRA	1
#define WIRELESS_MODE_ADHOC	2
// 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 = {
  "xxxxxxxxxxxxxxxxxxxxxxxxxx"};	// 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)
{
  // Use WiServer's print and println functions to write out the page content
  WiServer.print(cV);
  WiServer.print("|");
  WiServer.print(cA);

  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();
  }

  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(I1, HIGH);
  digitalWrite(I2, HIGH);
  digitalWrite(I3, HIGH);
  digitalWrite(I4, 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()
{
  cV = map(analogRead(bV), 0, 1023, 0, 1933) / 100.0;
  cA = map(analogRead(bA), 512, 1023, 0, 500) / 100.0;

  if (cA < 0.01)
  {
    cA = 0.01;
  }

  WiServer.server_task();

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

  if ((millis() - stopMove >= stopTime || cV <= nV) && 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 (cV <= nV)
  {
    if (millis() - vD >= 2000 && vD != 0)
    {
      getServer.setURL("/arduino/control/call.php?status=nopower");
      getServer.submit();

      //Set to offline, to no more commands will be possible
      online = false;
    }
    else
    {
      vD = millis();
    }
    return;
  }
}

2 comments

  • hey, whats the current status with the tank? Is it still running? what is the link to it?

    • The tank is offline for now, I have ordered a new wheel base for it, one that is more silent (the one with tracks is very loud), and it will also be easier for me to mount the sensors and servo’s for the PT camera mount.

Leave a Reply