Trouble converting Visual Basic program to Picaxe Basic

tonto2k

Member
Hi everyone :)

For some time now I have been trying to write a program in Picaxe basic that would let me map out a room using wavefront mapping. I found someone who had already done it :) but not in Picaxe basic only Visual Basic which am not familiar with.

I have managed to convert most of the code (easy enough actually!) except for one line which I think might be the whole map laid out horizontally? heres the code:

Dim map(,) As Integer = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {255, 255, 255, 255, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}}

Could you please help me and explain if 'map' is an integer, or is it (,) that is the integer? or both ? ! ?

I am guessing that each number is a memory location on the micro-controller?

I was thinking if its possible I might do something like:

symbol map = ($1,$2,$3,$4,$5,$6),($7,$8,$9,$10,$11,$12) etc etc using picaxe memory ? ?

hope someone can help me understand this please? :)
 

papaof2

Senior Member
The line appears to be attempting to create an array, but I don't think it's correct because "," isn't a legal variable name in VB.

The array would consist of 6 elements, each containing 5 integers. Since PICAXE doesn't support arrays, you would need 30 WORD variables and the equivalent code that can handle those variables as if they were an array.

I seriously doubt that the program can be adapted to run on a PICAXE because of the limited memory when compared to a PC.

John
 

tonto2k

Member
Thanks for the reply mate.

Gutted ! :(

I really wanted to use a picaxe chip for my mapping robot because I like programming in picaxe basic, can't get the hang of C !
 

benryves

Senior Member
The line appears to be attempting to create an array, but I don't think it's correct because "," isn't a legal variable name in VB.
It's a multidimensional (two-dimensional, in this case) array.

If you have an (x,y) coordinate you could convert that into an element address in user RAM (or similar memory, eg the I²C EEPROM) using y*6+x.
 

tonto2k

Member
Yes it is a x,y co-ordinate map. If you have a look at the visual basic code it is as you said. None of the code seems too hard to convert apart from the array section.

Code:
Module Module1
'values for special locations
Dim blank As Integer = 0
Dim wall As Integer = 255
Dim goal As Integer = 1
Dim robot As Integer = 254
 
'starting robot/goal locations
Dim robot_x As Integer = 5
Dim robot_y As Integer = 3
Dim goal_x As Integer = 0
Dim goal_y As Integer = 3
 
'map starting pos
Dim x As Integer = 0
Dim y As Integer = 0
 
'temp variables
Dim temp_A As Integer = 0
Dim temp_B As Integer = 0
Dim counter As Integer = 0
Dim steps As Integer = 0
 
'when searching for node with lowest value
Dim minimum_node As Integer = 250
Dim min_node_location As Integer = 250
Dim new_state As Integer = 4
Dim old_state As Integer = 0
Dim trans As Integer = 50
Dim reset_min As Integer = 250
 
'x is vertical, y is horizontal
Dim map(,) As Integer = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {255, 255, 255, 255, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}}
 
Sub Main()
Console.WriteLine("Starting WaveFront")
Console.WriteLine()
 
'WAVEFRONT CODE'
While map(robot_x, robot_y) <> goal
'find new location to go to
new_state = propagate_wavefront(robot_x, robot_y)
 
'update new location of robot
If new_state = 1 Then
robot_x -= 1
End If
If new_state = 2 Then
robot_y += 1
End If
If new_state = 3 Then
robot_x += 1
End If
If new_state = 4 Then
robot_y -= 1
End If
 
'make new state the old state
old_state = new_state
trans -= 1
End While
Console.WriteLine("Steps: " & steps)
Console.Write("Press any key to continue...")
Console.ReadKey()
End Sub
 
Public Function propagate_wavefront(ByVal robot_x, ByVal robot_y)
'clear old wavefront
unpropagate(robot_x, robot_y)
'add goal
map(goal_x, goal_y) = goal
Console.WriteLine("Adding Goal:")
print_map()
 
counter = 0
While counter < 50
x = 0
y = 0
'while the map hasn't been fully scanned
While x < 6 And y < 6
'if this location is a wall or a goal, ignore it
If map(x, y) <> wall And map(x, y) <> goal Then
'a full trail to the robot has been found, finished!
If min_surrounding_node_value(x, y) < reset_min And map(x, y) = robot Then
Console.WriteLine("Finished Wavefront:")
print_map()
'finished! tell robot to start moving down path
Return min_node_location
'record a value into this node
ElseIf minimum_node <> reset_min Then 'if this isn't here, nothing will go in the location
map(x, y) = minimum_node + 1
End If
End If
 
'go to next node and/or row
y += 1
If y = 6 And x <> 6 Then
x += 1
y = 0
End If
End While
Console.WriteLine("Sweep #: " & counter + 1)
print_map()
counter += 1
End While
End Function
 
Public Sub unpropagate(ByVal robot_x, ByVal robot_y)
Console.WriteLine("Old Map:")
print_map()
'go through each location
For x = 0 To 5
For y = 0 To 5
'if it's not wall or goal
If map(x, y) <> wall And map(x, y) <> goal Then
'set it to 0
map(x, y) = blank
End If
Next
Next
' put the robot location back in
map(robot_x, robot_y) = robot
Console.WriteLine("Unpropagation Complete:")
print_map()
End Sub
 
Public Function min_surrounding_node_value(ByVal x, ByVal y)
'reset minimum
minimum_node = reset_min
 
'down
If x < 5 Then
If map(x + 1, y) < minimum_node And map(x + 1, y) <> blank Then
minimum_node = map(x + 1, y)
min_node_location = 3
End If
End If
 
'up
If x > 0 Then
If map(x - 1, y) < minimum_node And map(x - 1, y) <> blank Then
minimum_node = map(x - 1, y)
min_node_location = 1
End If
End If
 
'right
If y < 5 Then
If map(x, y + 1) < minimum_node And map(x, y + 1) <> blank Then
minimum_node = map(x, y + 1)
min_node_location = 2
End If
End If
 
'left
If y > 0 Then
If map(x, y - 1) < minimum_node And map(x, y - 1) <> blank Then
minimum_node = map(x, y - 1)
min_node_location = 4
End If
End If
Return minimum_node
End Function
 
Public Sub print_map()
'go through each location
For temp_B = 0 To 5
For temp_A = 0 To 5
If map(temp_B, temp_A) = wall Then
'if it's a wall, print W
Console.Write("W ")
ElseIf map(temp_B, temp_A) = robot Then
'if it's the robot, print R
Console.Write("R ")
ElseIf map(temp_B, temp_A) = goal Then
'if it's the goal, print G
Console.Write("G ")
Else
'otherwise, print the value
Console.Write(map(temp_B, temp_A) & " ")
End If
Next
'new line
Console.WriteLine()
Next
Console.WriteLine()
steps += 1
End Sub
 
Public Sub Pause(ByVal Milliseconds As Integer)
Dim dTimer As Date
dTimer = Now.AddMilliseconds(Milliseconds)
Do While dTimer > Now
Loop
End Sub
End Module
.....Maybe there is some hope ? :)

Cheers.
 
Last edited by a moderator:

BeanieBots

Moderator
I think that should have read "Dim map(6,6)....".

Should be OK to have an array of that size with a PICAXE.
Your I2C EEPROM won't help. If you use it for data it will soon wear out.
You could use it for program space but only as an extra slot with an X2.
You can get I2C RAM but only 256X8, (which would be enough).

The rest comes down to tricks like that shown by benryves.
 

boriz

Senior Member
All Picaxes have at least 46 bytes of internal RAM, some have more. This would be more than sufficient for a 6*6 byte array. Look up PEEK/POKE in the manual.
 
Top