Author Topic: Odd behavior  (Read 6862 times)

Animar

  • Newbie
  • *
  • Posts: 4
    • View Profile
Odd behavior
« 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.

Enigma

  • Administrator
  • Jr. Member
  • *****
  • Posts: 68
    • View Profile
Re: Odd behavior
« Reply #1 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 :

  • Rethink your program using some "state machine" like the example here : https://www.blackvoxel.com/view.php?node=1306 so you'll make only one action per voxel_step.
  • Always call any PickVoxel() before moving the robot.
  • Use the newest "Remote Programmable Robot" along with the Scratch programming language. It have a much simpler behavior and use a continuously running program (http://www.blackvoxel.com/view.php?node=1705). But Squirrel robot remains more powerful and faster.
  • If you want to use some loops with Move() and make it like it's running continuously, you can use a trick : the squirrel generators with the yield() function after each Move() . The yield() will return execution to the Voxel_Step and resume at the same point on the next call. Here is an example.
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
« Last Edit: May 15, 2019, 07:34:16 pm by Enigma »