Microbot

General FreeBASIC programming questions.
Post Reply
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Microbot

Post by neil »

I am trying to make a microbot figure out how to solve a simple maze. So far I have to use data statements that steers the microbot to the goal. Fixed more bugs.

Code: Select all

'' Microbot by Neil
Screenres 544,556,32
setmouse 0,0,0
Type Coord
x As Ushort
y as Ushort
End Type

Dim C(16) As Coord
Dim As Long fps
Dim As UShort m,z
Dim As Byte d,u,v,speed

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
      Static As Double timervalue,_lastsleeptime,t3,frames
      frames+=1
      If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
      Var sleeptime=_lastsleeptime+((1/myfps)-Timer+timervalue)*1000
      If sleeptime<1 Then sleeptime=1
      _lastsleeptime=sleeptime
      timervalue=Timer
      Return sleeptime
End Function

sub microbot(x as ushort,y as ulong,c as ulong)
line(x,y)-(x+31,y+31),c,bf
end sub
Do
Cls
'' Simple Maze
line(16 ,0)-(542,16),rgb(225,0,0),bf
line(16 ,540)-(542,556),rgb(225,0,0),bf
line(0 ,556)-(16,0),rgb(225,0,0),bf
line(100 ,124)-(316,140),rgb(225,0,0),bf
line(100 ,16)-(116,124),rgb(225,0,0),bf
line(16 ,224)-(116,240),rgb(225,0,0),bf
line(100 ,330)-(116,556),rgb(225,0,0),bf
line(200 ,224)-(216,344),rgb(225,0,0),bf
line(116 ,330)-(200,344),rgb(225,0,0),bf
line(216 ,224)-(316,240),rgb(225,0,0),bf
line(116 ,434)-(324,450),rgb(225,0,0),bf
line(310 ,224)-(326,340),rgb(225,0,0),bf
line(424 ,434)-(524,450),rgb(225,0,0),bf
line(424 ,124)-(440,434),rgb(225,0,0),bf
line(526 ,540)-(542,0),rgb(225,0,0),bf

m = 0:z = 1:d = 0: v = 0:u = 0
speed = 8
c(0).y = 31
c(0).x = 2

Locate 67,5:print "START"
Locate 32,31:PRINT "FINISH"
restore goal
Do

Screenlock
microbot C(M).X * 16, C(M).Y * 16,rgb(0,0,0)  ''erase microbot
m = 1 '' fixes bug erasing upper left corner 
c(z) = c(z - 1)

microbot c(z).x * 16, c(z).y * 16,rgb(0,225,0) '' microbot color

c(0).x = c(0).x + U
c(0).y = c(0).y + V
read d
if d = 5 Then u = 0:v = -1 '' up
if d = 4 Then u = 1:v = 0 '' right
if d = 3 Then u = 0:v = 1 '' down
if d = 2 Then u = -1:v = 0 '' left
microbot C(0).X * 16, C(0).Y * 16,rgb(0,225,0) ''microbot color
Screenunlock

if d = 0 Then exit do

Sleep regulate(speed,fps),1

Loop until inkey = chr(27)
Locate 10,28:Print "FINISHED MAZE"
sleep
Loop until inkey = chr(27)

'' steers microbot to goal
goal: 
Data 5,5,5,5,5,5,5,5,5,5,5,5,5,5,4,4,4,4,4,4,4,5,5,5,5,5,5,5
Data 4,4,4,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,3
Data 2,2,2,2,2,2,2,5,5,5,5,5,5,5,0
Last edited by neil on May 27, 2023 19:40, edited 4 times in total.
David Watson
Posts: 56
Joined: May 15, 2013 16:48
Location: England

Re: Microbot

Post by David Watson »

When this line

Code: Select all

microbot C(M).X * 16, C(M).Y * 16,rgb(0,0,0) ''erase microbot
is first executed it erases the top left of the maze.
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Re: Microbot

Post by neil »

@David Watson
Thanks the bug has been fixed.
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Re: Microbot

Post by neil »

This microbot version simulates what AI would do. It simulates scanning the maze and is suppose to be mapping it.
It's only doing what I told it to. Real AI is not that easy to do,

Code: Select all

'' Microbot Version 2 by Neil
Screenres 544,556,32
setmouse 0,0,0
Type Coord
x As Ushort
y as Ushort
End Type

Dim C(16) As Coord
Dim As Long fps
Dim As UShort m,z
Dim As Byte d,u,v,speed

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
      Static As Double timervalue,_lastsleeptime,t3,frames
      frames+=1
      If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
      Var sleeptime=_lastsleeptime+((1/myfps)-Timer+timervalue)*1000
      If sleeptime<1 Then sleeptime=1
      _lastsleeptime=sleeptime
      timervalue=Timer
      Return sleeptime
End Function

sub microbot(x as ushort,y as ulong,c as ulong)
line(x,y)-(x+31,y+31),c,bf
end sub
Do
Cls
'' Simple Maze
line(16 ,0)-(542,16),rgb(225,0,0),bf
line(16 ,540)-(542,556),rgb(225,0,0),bf
line(0 ,556)-(16,0),rgb(225,0,0),bf
line(100 ,124)-(316,140),rgb(225,0,0),bf
line(100 ,16)-(116,124),rgb(225,0,0),bf
line(16 ,224)-(116,240),rgb(225,0,0),bf
line(100 ,330)-(116,556),rgb(225,0,0),bf
line(200 ,224)-(216,344),rgb(225,0,0),bf
line(116 ,330)-(200,344),rgb(225,0,0),bf
line(216 ,224)-(316,240),rgb(225,0,0),bf
line(116 ,434)-(324,450),rgb(225,0,0),bf
line(310 ,224)-(326,340),rgb(225,0,0),bf
line(424 ,434)-(524,450),rgb(225,0,0),bf
line(424 ,124)-(440,434),rgb(225,0,0),bf
line(526 ,540)-(542,0),rgb(225,0,0),bf

m = 0:z = 1:d = 0: v = 0:u = 0
speed = 20
c(0).y = 31
c(0).x = 2

Locate 67,5:print "START"
Locate 32,31:PRINT "FINISH"
restore goal
Do

Screenlock
microbot C(M).X * 16, C(M).Y * 16,rgb(0,0,0)  ''erase microbot
m = 1
c(z) = c(z - 1)

microbot c(z).x * 16, c(z).y * 16,rgb(0,225,0) '' microbot color

c(0).x = c(0).x + U
c(0).y = c(0).y + V
read d
if d = 5 Then u = 0:v = -1 '' up
if d = 4 Then u = 1:v = 0 '' right
if d = 3 Then u = 0:v = 1 '' down
if d = 2 Then u = -1:v = 0 '' left
microbot C(0).X * 16, C(0).Y * 16,rgb(0,225,0) ''microbot color
Screenunlock

if d = 0 Then exit do

Sleep regulate(speed,fps),1

Loop until inkey = chr(27)
Locate 10,28:Print "FINISHED MAZE"
sleep
Loop until inkey = chr(27)

'' steers microbot to goal
goal:
Data 5,5,5,5,5,5,5,5,5,5,5,5,5,5,4,4,4,4,4,4,4,5,5,5,5
Data 5,5,5,2,2,2,2,2,2,5,5,5,5,5,5,5,5,3,3,3,3,3,3,3,3
Data 3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5
Data 5,5,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4
Data 4,4,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,3
Data 3,3,3,3,3,3,3,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
Data 5,5,5,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
Data 3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2
Data 2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,2
Data 2,2,2,2,2,5,5,5,5,5,5,5,2,2,2,2,2,2,2,2,2,2,2,2,2
Data 2,2,4,4,4,4,4,4,4,5,5,5,5,5,5,5,0
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Microbot

Post by BasicCoder2 »

@neil
I am trying to make a microbot figure out how to solve a simple maze.
My suggestion is you use an array of walkable and unwalkable tiles. This will make it easier to implement automatic maze generation and path planning.

You don't want to have to store its moves, it can work them out itself!

It moves from tile to tile. In computer games we call each location in the array a tile. At each tile it has to decide if it can turn left, turn right, keep going in the same direction or if it has come to a dead end and has to turn around.

Finding a path from the current position to the goal position can be done using an algorithm like ASTAR.

A simple random mouse in a maze example.

viewtopic.php?t=32229
Post Reply