Asked — Edited

Script Help Needed

I am having a little bit of trouble with this script. Everything works until I get to the "if" statement at the left and right sensor. So what it is supposed to do is not allow movement if tracks are disabled which upon startup they are. Once track motors are enabled and the command robot turn left is given the head is to turn left and down, again which it does. But then it never gets to the send serial commands. There is something missing, endif, elseif, end repeat? BTW I troubleshot by commenting out lines of code until figuring out where the problem began. I appreciate the help. Also how do I use code tags on this website?


if($movement = "false")
print("movement prohibited")
SayEzb("my track motors are disabled")
Else 
print("moving")

ControlCommand("Script Manager ", ScriptStart, "Head_Left")
ControlCommand("Script Manager ", ScriptStart, "Head Down")
sleep(500)

if ($leftsensor <60)And ($rightsensor <60)
sleep(500)
SendSerial(D0,38400,1)
SendSerial(D0,38400,255)
Sleep(5000)
SendSerial(D0,38400,0)
ElseIf 
SendSerial(D0,38400,0) 
SayEZB("i have detected something in my path") 
EndIf


ARC Pro

Upgrade to ARC Pro

ARC Pro will give you immediate updates and new features needed to unleash your robot's potential!

PRO
Synthiam
#1  

ARC is giving you a clear error when attempting to format the code..

User-inserted image

*HINT: Count the ENDIF's

#3  

Yes I get the error but Im not sure where to put the end if's. Im sure I am overthinking this as usual. Thanks again!

#4  

if you meant for the ELSE clause to really contain the IF clause, put another ENDIF at the end of the program

If you really meant for the ELSE clause to end before the next IF, put the ENDIF there

Hint, indents will help you see the nesting

#5  

I'm still missing something here....

if($movement = "false") print("movement prohibited") SayEzb("my track motors are disabled") Else print("moving")

ControlCommand("Script Manager ", ScriptStart, "Head_Left") ControlCommand("Script Manager ", ScriptStart, "Head Down") sleep(1500)

$leftsensor = getadc(adc5) $topsensor = getadc(adc7) $rightsensor = getadc(adc6)

if ($leftsensor <60)And ($rightsensor <60) sleep(2500) SendSerial(D0,38400,1) SendSerial(D0,38400,255) Sleep(5000) SendSerial(D0,38400,0) Elseif ($leftsensor >60 And ($rightsensor >60) SendSerial(D0,38400,0)
SayEZB("i have detected something in my path") EndIf

The way I am reading this code is IF the left sensor and the right sensor are less than 60 wait 2.5 seconds and start the serial commands for 5 seconds and then stop. Otherwise if the sensors are greater than 60 immediately send serial stop command and say I have detected something in my path.

endif.?

Still don't know where to put the end if's?

#6  

Try this;


if($movement = &quot;false&quot;)
print(&quot;movement prohibited&quot;)
SayEzb(&quot;my track motors are disabled&quot;)
Else 
print(&quot;moving&quot;)
#I think you need an Endif here
Endif
ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head_Left&quot;)
ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head Down&quot;)
sleep(500)

if ($leftsensor &lt;60)And ($rightsensor &lt;60)
sleep(500)
SendSerial(D0,38400,1)
SendSerial(D0,38400,255)
Sleep(5000)
SendSerial(D0,38400,0)
ElseIf 
SendSerial(D0,38400,0) 
SayEZB(&quot;i have detected something in my path&quot;) 
EndIf

You need to have an Endif for every If statement

#7  

@bhouston

Shouldn't that ELSEIF be an ELSE ?

#8  

Thank you very much. Makes sense now! Now I can build upon this. I didn't realize the problem was so far up in the code. Thanks again!

                                Chris
#9  

@faengelm, yes, it should be but it will work the way he has it. The key though is to have an Endif for every If statement.

#10  

How are you guys formatting the posted code?

Thanks Chris

PRO
Synthiam
#11  

Use the tags to format code.

Maybe this is what you want? I "assume" the ENDIF is to be at the end of code because the next IF is comparing to see an object in the path - which i suspect means the robot is moving.

You added an ENDIF for one of the IF conditions, but not the other.


if ($movement = &quot;false&quot;)
  
  print(&quot;movement prohibited&quot;)
  
  SayEzb(&quot;my track motors are disabled&quot;)
  
ELSE

  print(&quot;moving&quot;)

  ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head_Left&quot;)
  ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head Down&quot;)
  
  sleep(500)

  if ($leftsensor &lt;60)And ($rightsensor &lt;60)
  
    sleep(500)
    SendSerial(D0,38400,1)
    SendSerial(D0,38400,255)
    Sleep(5000)
    SendSerial(D0,38400,0)
    
  ELSEif
  
    SendSerial(D0,38400,0)
    SayEZB(&quot;i have detected something in my path&quot;)
    
  endif

endif

#12  

Guys after further testing this script today I found further issues. Im hoping someone can shed some light on it. What I want is if the "movement = false" I don't want any more of the script to run. What happens is if I say "robot turn left" and the movement is false, it will say "my track motors are disabled" and then the head lowers and looks in appropriate direction and if nothing detected it starts to move. It should NOT move if the track motors are disabled. Thanks guys major headache tonight.

                     Chris  

IF ($movement = "false") print("movement prohibited") SayEzb("my track motors are disabled")

ELSE print("moving") ENDIF

ControlCommand("Script Manager ", ScriptStart, "Head Down") sleep(1500)

$leftsensor = getadc(adc5) $topsensor = getadc(adc7) $rightsensor = getadc(adc6)

IF ($leftsensor <=60)And ($rightsensor <=60) sleep(2500) sendserial(d0,38400,235) sendserial(d0,38400,107) sleep(3000) sendserial(d0,38400,0) ELSE ($leftsensor >=60 Or ($rightsensor >=60) SendSerial(D0,38400,0) SayEZB("i have detected something in my path") ENDIF

ControlCommand("Script Manager ", ScriptStart, "Head_Center")

*** EDIT I need to try DJ's above script. I will try that and get back***

#13  

This doesn't answer your script question, just your question about formatting code.

Unfortunately, I can't type the UBB code, because the forum will format it, so I'll describe it...

Look in the box to the right of the text input window labeled UBB code (it will be under the window if you are on a mobile device or your screen is small).

Next to the word Code is an example...

To format code, type a left open bracket, the word "code" and a right close bracket.

Enter all of your code, then type a left open bracket, a slash, the word code, and a right close bracket.

Quoting is similar, but use the word quote instead of code.

Alan

#14  

Thanks Alan I will try that next time.

Now onto another question regarding DJ's code. What in there is defining $left sensor and $right sensor to there respective adc ports? Thanks again

   Chris
#15  

I believe DJ's code will do what you are looking for

#16  

@kamaroman68 To further what Alan said

Below is an example on how to use code tags

[code #use another closing square bracket after the word code]

#everything between these code tags will be black #example code can be anything say("I am a robot")

[/code #use another closing square bracket after /code]

DJ's code altered a little...


if ($movement = &quot;false&quot;)
  
  print(&quot;movement prohibited&quot;)
  
  SayEzb(&quot;my track motors are disabled&quot;)
  
ELSE

  print(&quot;moving&quot;)

  ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head_Left&quot;)
  ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head Down&quot;)

  
 sleep(250) #play with the value here

$leftsensor = getadc(adc5)
$topsensor = getadc(adc7)
$rightsensor = getadc(adc6)

sleep(250) #a sleep here may not be needed

  if ($leftsensor &lt;60)And ($rightsensor &lt;60)
  
    sleep(500)
    SendSerial(D0,38400,1)
    SendSerial(D0,38400,255)
    Sleep(5000)
    SendSerial(D0,38400,0)
    
  ELSE
  
    SendSerial(D0,38400,0)
    SayEZB(&quot;i have detected something in my path&quot;)
    
  endif

endif

#17  

This script still does not work completely. If I block the sensors it does not move which is correct, but it never says " I have detected something in my path".


IF ($movement = &quot;false&quot;)

  print(&quot;movement prohibited&quot;)

  SayEzb(&quot;my track motors are disabled&quot;)

ELSE

  print(&quot;moving&quot;)

  ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head_Left&quot;)
  ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head Down&quot;)

  sleep(500)

  $leftsensor = getadc(adc5)
  $topsensor = getadc(adc7)
  $rightsensor = getadc(adc6)


  IF ($leftsensor &lt;60)And ($rightsensor &lt;60)

    sleep(1500)
    SendSerial(D0,38400,1)
    SendSerial(D0,38400,255)
    Sleep(3000)
    SendSerial(D0,38400,0)

  ELSEIF

    SendSerial(D0,38400,0)
    SayEZB(&quot;i have detected something in my path&quot;)

  ENDIF

ENDIF



ControlCommand(&quot;Script Manager &quot;, ScriptStart, &quot;Head_Center&quot;) 

#18  

Richard thanks for the lesson on the tags that was a big help. Other forums I visit don't require that. Thanks again.

#19  

In your following statement something looks odd to me.

 
  IF ($leftsensor &lt;60)And ($rightsensor &lt;60)

    sleep(1500)
    SendSerial(D0,38400,1)
    SendSerial(D0,38400,255)
    Sleep(3000)
    SendSerial(D0,38400,0)

I could be wrong but It may be moving too fast and skipping a needed command. Try placing a Sleep() command between:


 SendSerial(D0,38400,1)
 SendSerial(D0,38400,255)

#20  

Thinking about this more I'm sure my above post may be part of your problem. You need to give things time to complete operation before moving on to the next command. This is even more important when you're sending serial commands to other devices. Too fast and you're going to flood the channel or send a command faster then the device you're trying to control can accept and read it. I find this to be true when sending serial commands to the Kangaroo X2 I control through ARC and EZB.

To troubleshoot timing issues I sometime give Sleep() commands a stupid amount of time. This really slows down the script and lets things complete. Then I trim down the sleep() times till everything runs fast and smooth. :)

#21  

Try changing that ELSEIF to ELSE

#22  

Dave the reason those two serial commands are like that are to start the 2 tracks in opposite directions. I cant start 1 and then start the other after a sleep period. Thanks for the response though, I appreciate it and may try a sleep at other points to troubleshoot.

Faengelm I will try that later tonight when I return home this evening. Hopefully that works.

          Chris
#23  

Yes... get rid of the elseif and use else.... You only use elseif if you are checking on an additional condition that may exist....

#24  

Thanks guys will try tomorrow, some life changes prevented me from trying tonight. It was my night with the kids! thanks again

 Chris
#25  

One trick to try when testing script logic, is instead of actually moving the robot or its parts, replace those commands with Print() commands that tell you where in the script you are. Then execute the script while watching the script object window.

If using variables you can also print those so you can easily see that they are getting set the way you expect.

Once you get the logic right, then replace (or add to) the print commands with the actual Robot movement commands. This prevents uncontrolled movement when the logic is wrong, and helps isolate the logic from possible control issues.

Alan

#26  

Yes Yes Yes! A couple of further tweeks to that script after the "else" changed proved exactly what I am looking for. Very very nice. Thanks for all the help with this one.

                  Chris
PRO
Synthiam
#27  

Great to hear!

Also it's very useful to know that running the script while editing in the debug window will display the executing instruction of each line.

Running a script in the editor is a fantastic way to debug the execution. It's super useful