C:\> Rostislav Persion's Projects

.:: Roomba Trajectory Simulator ::.
Roomba robot vacuum cleaner simulation










This project is a simulation of the popular Roomba Vacuum Cleaner. When I first saw the Roomba in action, I was a bit disappointed. It turned out that there is no algorithm for cleaning. The rooma calculates the size of the room and based on that sets a timer. Within that time, random traversing will eventually clean the room, however it takes a very long time and drains the battery. The LG Hombot 3.0 on the other hand uses zig zags as well as two cameras to track its location and avoids going to the previous locations.







SOFTWARE WRITTEN IN C#
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Threading;
   6:  using System.Drawing; 
   7:   
   8:  namespace RoombaWalls
   9:  {
  10:      class Program
  11:      {
  12:          static void Main(string[] args)
  13:          {
  14:              double width = 80;
  15:              double height = 24;
  16:   
  17:              bool[,] RoombaWalls = new bool[(int)width, (int)height];
  18:              int[,] RoombaHistory = new int[(int)width, (int)height];
  19:   
  20:   
  21:              double drawX = 0;
  22:              double drawY = 0;
  23:              bool drawing = true;
  24:              while (drawing)
  25:              {
  26:   
  27:   
  28:                  ConsoleKeyInfo key = Console.ReadKey();
  29:                  if (key.Key == ConsoleKey.Enter)
  30:                  {
  31:                      drawing = false;
  32:                  }
  33:   
  34:                  if (key.Key == ConsoleKey.UpArrow)
  35:                  {
  36:                      if (drawY > 0)
  37:                      {
  38:                          drawY--;
  39:                          RoombaWalls[(int)drawX, (int)drawY] = true;
  40:                          PaintRoomba(RoombaWalls, width, height, "#", ConsoleColor.Blue);
  41:                      }
  42:                  }
  43:                  if (key.Key == ConsoleKey.DownArrow)
  44:                  {
  45:                      if (drawY < ((int)height - 1))
  46:                      {
  47:                          drawY++;
  48:                          RoombaWalls[(int)drawX, (int)drawY] = true;
  49:                          PaintRoomba(RoombaWalls, width, height, "#", ConsoleColor.Blue);
  50:                      }
  51:                  }
  52:                  if (key.Key == ConsoleKey.LeftArrow)
  53:                  {
  54:                      if (drawX > 0)
  55:                      {
  56:                          drawX--;
  57:                          RoombaWalls[(int)drawX, (int)drawY] = true;
  58:                          PaintRoomba(RoombaWalls, width, height, "#", ConsoleColor.Blue);
  59:                      }
  60:                  }
  61:                  if (key.Key == ConsoleKey.RightArrow)
  62:                  {
  63:                      if (drawX < ((int)width - 1))
  64:                      {
  65:                          drawX++;
  66:                          RoombaWalls[(int)drawX, (int)drawY] = true;
  67:                          PaintRoomba(RoombaWalls, width, height, "#", ConsoleColor.Blue);
  68:                      }
  69:                  }
  70:              }
  71:   
  72:   
  73:              Random rnd1 = new Random(Environment.TickCount);
  74:              double angle = rnd1.Next(0,360);
  75:              double vx = Math.Cos(angle * (Math.PI / 180));
  76:              double vy = Math.Sin(angle * (Math.PI / 180));
  77:              
  78:              int steps = 0;
  79:              double area = width * height;
  80:              double botX = (int)(width / 2);
  81:              double botY = (int)(height / 2);
  82:   
  83:   
  84:              double minX = width / 2;
  85:              double maxX = width / 2;
  86:              double minY = height / 2;
  87:              double maxY = height / 2;
  88:   
  89:   
  90:      
  91:               while (true)
  92:               {
  93:   
  94:                  steps++;
  95:   
  96:   
  97:                  if (botX > maxX)
  98:                  {
  99:                      maxX = botX;
 100:                      area = Math.Abs(maxX - minX) * Math.Abs(maxY - minY); 
 101:                  }
 102:                  if (botX < minX)
 103:                  {
 104:                      minX = botX;
 105:                      area = Math.Abs(maxX - minX) * Math.Abs(maxY - minY);
 106:                  }
 107:                  if (botY > maxY)
 108:                  {
 109:                      maxY = botY;
 110:                      area = Math.Abs(maxX - minX) * Math.Abs(maxY - minY);
 111:                  }
 112:                  if (botY < minY) 
 113:                  { 
 114:                      minY = botY;
 115:                      area = Math.Abs(maxX - minX) * Math.Abs(maxY - minY);
 116:                  }
 117:   
 118:                  area = Math.Round(area);
 119:   
 120:   
 121:   
 122:                  if (RoombaHistory[(int)botX, (int)botY] < 10)
 123:                  {
 124:                      RoombaHistory[(int)botX, (int)botY]++;
 125:                  }
 126:   
 127:                  while (RoombaWalls[(int)(botX + vx), (int)(botY)] == true)
 128:                  {
 129:                      angle = rnd1.Next(0, 360);
 130:                      vx = Math.Cos(angle * (Math.PI / 180));
 131:                      vy = Math.Sin(angle * (Math.PI / 180));
 132:                  }
 133:                  while (RoombaWalls[(int)(botX), (int)(botY + vy)] == true)
 134:                  {
 135:                      angle = rnd1.Next(0, 360);
 136:                      vx = Math.Cos(angle * (Math.PI / 180));
 137:                      vy = Math.Sin(angle * (Math.PI / 180));
 138:                  }
 139:   
 140:                  while (RoombaWalls[(int)(botX + vx), (int)(botY + vy)] == true)
 141:                  {
 142:                      angle = rnd1.Next(0, 360);
 143:                      vx = Math.Cos(angle * (Math.PI / 180));
 144:                      vy = Math.Sin(angle * (Math.PI / 180));
 145:                  }
 146:   
 147:                  
 148:                  
 149:                  botX += vx;
 150:                  botY += vy;
 151:   
 152:                  PaintRoomba(RoombaWalls, width, height, "#",ConsoleColor.Blue);
 153:                  PaintRoomba(RoombaHistory, width, height, "@");
 154:                  Console.SetCursorPosition(0, 24);
 155:                  Console.BackgroundColor = ConsoleColor.Red;
 156:                  Console.ForegroundColor = ConsoleColor.Black;
 157:                  double percent = Math.Round((steps / (area * 5)) * 100,2);
 158:                  Console.Write("CODED BY: ROSTISLAV PERSION (seeplusplus@gmail.com) STEPS: " + steps.ToString() + " AREA: " + area.ToString());
 159:   
 160:   
 161:              }
 162:   
 163:   
 164:              Console.ReadLine();
 165:   
 166:   
 167:          }
 168:   
 169:   
 170:          static void PaintRoomba(bool[,] area,double width,double height, string letter, ConsoleColor col)
 171:          {
 172:              for (int y = 0; y < (int)height; y++)
 173:              {
 174:                  for (int x = 0; x < (int)width; x++)
 175:                  {
 176:                      if (area[x, y] == true)
 177:                      {
 178:                          Console.BackgroundColor = col;
 179:                          Console.SetCursorPosition(x, y);
 180:                          Console.Write(letter);
 181:                      }
 182:                  }
 183:              }
 184:          }
 185:          
 186:          static void PaintRoomba(int[,] area,double width,double height, string letter)
 187:          {
 188:              for (int y = 0; y < (int)height; y++)
 189:              {
 190:                  for (int x = 0; x < (int)width; x++)
 191:                  {
 192:   
 193:                      if (area[x, y] > 0)
 194:                      {
 195:                          
 196:                          Console.SetCursorPosition(x, y);
 197:   
 198:                          if ((area[x, y] > 0) && (area[x, y] < 3))
 199:                          {
 200:                              Console.BackgroundColor = ConsoleColor.Black;
 201:                              Console.ForegroundColor = ConsoleColor.DarkGreen;
 202:                              Console.Write("@");
 203:                          }
 204:                          if ((area[x, y] > 3) && (area[x, y] < 6))
 205:                          {
 206:                              Console.BackgroundColor = ConsoleColor.Black;
 207:                              Console.ForegroundColor = ConsoleColor.Green;
 208:                              Console.Write("@");
 209:                          }
 210:                          if ((area[x, y] > 6) && (area[x, y] < 9))
 211:                          {
 212:                              Console.BackgroundColor = ConsoleColor.Black;
 213:                              Console.ForegroundColor = ConsoleColor.White;
 214:                              Console.Write("@");
 215:                          }
 216:   
 217:                      }
 218:                  }
 219:              }
 220:          }
 221:   
 222:      }
 223:  }