Blackvoxel Forum

Blackvoxel => Programming with Blackvoxel => Topic started by: Animar on May 15, 2019, 10:13:00 am

Title: Odd behavior
Post by: Animar on May 15, 2019, 10:13:00 am
Hi There,

I have some expirience in C and Lua but this Squirrel language keeps me confused.
So this bit of code troubles me.

Code: [Select]
if(GetX() == (Start_X - DimX + 1) && NextRow){
  PickVoxel(2);
  Move(2);
  NextRow = false;
  Display( "I Should have moved", 5000, 2);
}

how can it be that the specific display text "I Should have moved" is shown but the Pick(2) and Move(2) is NOT executed.


This is the whole thig so far. The whole digging and movement will be replaced with loops. But first, i need to figure this problem out. As far as this code goes. It should check for a Storage device then go one down, two to the left and finaly one back. But i does not execute the last part ?

Code: [Select]
// Test

BotInit <- true; //        X  Y  Z
Workmode <- false; // 0 =  { 0, 0, 1 } = Front of the robot.
Start_X <- 0; // 1 =  { 1, 0, 0 } = Right of the robot.
Start_Y <- 0; // 2 =  { 0, 0, -1} = Back of the robot.
Start_Z <- 0; // 3 =  {-1, 0, 0 } = Left of the robot.
MaxDepth <- -64; // 4 =  { 0, 1, 0 } = Above the robot.
DimX <- 3; // 5 =  { 0,-1, 0 } = Under the robot.
DimZ <- 3;
NextRow <- false;

function Voxel_Step(){
  local VoxType;
if (BotInit == true){
VoxType = Look(0);
Start_X = GetX();
Start_Y = GetY();
Start_Z = GetZ();
if(VoxType == 49){
BotInit = false
Workmode = true;
PickVoxel(5);
Move(5);
}
else{
Display( "Error, no Storage Found at X = " + Start_X + " - Y " + Start_Y + " - Z " + Start_Z, 5000, 2);
}
}
if(Workmode == true){
if(GetX() > (Start_X - DimX + 1)){
PickVoxel(3);
Move(3);
NextRow = true;
}
if(GetX() == (Start_X - DimX + 1) && NextRow){
PickVoxel(2);
Move(2);
NextRow = false;
Display( "I Should have moved ", 5000, 2);
}
}
}

Thanks for your help.
Title: Re: Odd behavior
Post by: Enigma on May 15, 2019, 07:22:15 pm
Hello Animar,

I see what is the problem. The functions you mentioned are executed... but in your case,  they are doing nothing. That's because in some cases, your program is running out of "action ticket".

To make the things simple, you have the right to do only one Move() per Voxel_Step() call. Then, your Voxel_Step() function must terminate before you have the right to do another Move(). That's for limiting the power of the programmable robot to avoid being faster than extraction robots. If you call the Move() function another time, it will do nothing but return "false" to say the move action was refused. In the documentation, the functions consuming the "action ticket" are documented with the sentence like "You can move the robot only once per cycle (per Voxel_Step() call)" http://www.blackvoxel.com/view.php?node=1303

To understand more deeply, an important precision : The Squirell Robot is not running a continuously running program, but a function called cyclically. The function Voxel_Step() is called each time the voxels of the world are moving (Approx 5 time by seconds). It's working synchronously with the Molecular Voxel Interaction Engine. So it's synchronous with other machines. If you want to use a program behaving a more continuous way, see the trick below.

So, there is what you can do :

Code: [Select]
// Continous program called by a function.

// The generator

Rep<-0;

// My continuously running program

function MyStuff()
{
  local x,y;
 
  for (y=0;y<8;y++)
  {
    for (x=0;x<8;x++)
    {
      PickVoxel(5);
      if (y & 1) Move(1);
      else       Move(3);
      yield(true);
    }
    PickVoxel(5);
    Move(2); yield(true);
  }
 
  return(null);
}

// Calling the continuous program using a generator.

function Voxel_Step()
{
  // Creating the generator.
  if (Rep==0) Rep = MyStuff();
 
  // Calling the generator... or resuming where it was stopped.
  if (Rep==1) return(0);
  if (!resume Rep) {Rep=1;}
}


In the hope we have answered your questions, we wish you a good play.

The Blackvoxel Team