United Kingdom
Asked — Edited

Ir Object Avoidance

Another script I've been writing is for the IR object avoidance as the built in control for IR doesn't work correctly with my IR sensor. I'm also trying to get to grips with EZ-Script so playing around is always a great idea in my book.

Adjust and/or comment on as you see fit. I've written it so it can be easily adjusted to suit your settings (and to show how variables can be used to make for easy adjustment over different projects/people)


# IR Detector Script
# Object avoidance using IR sensor on ADC port

# Adjust values below for configuration
$iradcport = ADC6 # Change for ADC Port with sensor attached
$maxirdistance = 35 # Change for maximum distance from object before avoiding in units
$irreverseturn = 0 # Reverse before turn? 0 = no, 1 = yes
$irturndirection = 0 # Change 0 = left or 1 = right
$irturnamount = 500 # Change for how long to turn for in ms
$irreverseamount = 500 # Change for how long to reverse for in ms (if applicable)
$irmovementspeed = 255 # Change for movement speed


# -- Do not modify below this line --
Goto(detect)

:avoid
IF ($irreverseturn = 1)
  reverse($irmovementspeed,$irreverseamount)
ELSEIF ($irturndirection = 0)
  Left($irmovementspeed,$irturnamount)
ELSE 
  Right($irmovementspeed,$irturnamount)
ENDIF 
Return()

:detect
$currirdistance = GetADC($iradcport)
IF ($currirdistance >= $maxirdistance)
  Goto(avoid)
ENDIF 
Sleep (50)
Goto(detect)

Also available on the EZ-Cloud here


ARC Pro

Upgrade to ARC Pro

ARC Pro is more than a tool; it's a creative playground for robot enthusiasts, where you can turn your wildest ideas into reality.

United Kingdom
#1  

The :Detect should be a label called detect but I guess the forum thought putting the d next to a : meant I was grinning;)

PRO
Synthiam
#2  

Hahahaha! That's funny... And fixed:)

Now i have to fix the real smilies

Edit: there good to go!

United Kingdom
#3  

DJ always on the ball even with the smallest of issues. Although I kinda liked the :D in the code, it gave character.

Updated the code (although not the EZ-Cloud version yet as I am at work... don't tell the boss), it had problems with the reverse before turn, as in I had an ELSEIF after it not an ENDIF, so it would back up when detecting an object but didn't turn.

This is why I like sections and posts specifically for scripting, hopefully this will help show others how things work:) Anyway, updated code is as follows (changed in original post also changed back in original post so others can follow the changes);


# IR Detector Script
# Object avoidance using IR sensor on ADC port

# Adjust values below for configuration
$iradcport = ADC6 # Change for ADC Port with sensor attached
$maxirdistance = 35 # Change for maximum distance from object before avoiding in units
$irreverseturn = 0 # Reverse before turn? 0 = no, 1 = yes
$irturndirection = 0 # Change 0 = left or 1 = right
$irturnamount = 500 # Change for how long to turn for in ms
$irreverseamount = 500 # Change for how long to reverse for in ms (if applicable)
$irmovementspeed = 255 # Change for movement speed


# -- Do not modify below this line --
Goto(detect)

:avoid
IF ($irreverseturn = 1)
  reverse($irmovementspeed,$irreverseamount)
ENDIF
IF ($irturndirection = 0)
  Left($irmovementspeed,$irturnamount)
ELSE 
  Right($irmovementspeed,$irturnamount)
ENDIF 
Return()

:detect
$currirdistance = GetADC($iradcport)
IF ($currirdistance >= $maxirdistance)
  Goto(avoid)
ENDIF 
Sleep (50)
Goto(detect)

PRO
Synthiam
#4  

Nice:)

I would recommend putting the :Detect code above the :Avoid... That way it jumps right into :Detect and you won't need the Goto(Detect) anymore. Your way works, of course... It's just one less command

United Kingdom
#5  

For the life of me I don't know why I structured it around that way, I can only think I was tired when I wrote it or had other intentions (very possibly as part of my roaming script I plan to write that this will be a part of). Although it does demonstrate the Goto and Return commands.

I'll reorganise it later I expect as I want to add in the option to only check when moving forwards, which I think should be a simple IF command using the $Direction global variable.

I may also add in options for additional detectors, servo mounting, capturing images and video (and anything else anyone can think of to throw in the mix)... more for educational purposes than function - it makes it fun learning that way:)

United Kingdom
#6  

version 1.4 Added in option to only check while moving forwards using the global variable $Direction. Restructured. Commands in CAPS, variables and labels in lowercase.


# IR Detector Script
# Object avoidance using IR sensor on ADC port

# Adjust values below for configuration
$iradcport = ADC6 # Change for ADC Port with sensor attached
$maxirdistance = 35 # Change for maximum distance from object before avoiding in units
$ironlyforward = 0 # Only if moving forwards? 0 = no, 1 = yes
$irreverseturn = 0 # Reverse before turn? 0 = no, 1 = yes
$irturndirection = 0 # Change 0 = left or 1 = right
$irturnamount = 500 # Change for how long to turn for in ms
$irreverseamount = 500 # Change for how long to reverse for in ms (if applicable)
$irmovementspeed = 255 # Change for movement speed


# -- Do not modify below this line --

# The detection script
:detect
IF ($ironlyforward = 1)
  IF ($Direction = "Forward")
    GOTO (detectstart)
  ELSE 
    GOTO (detect)
  ENDIF 
ELSE 
  :detectstart
  $currirdistance = GETADC($iradcport)
  IF ($currirdistance >= $maxirdistance)
    GOTO(avoid)
  ENDIF 
  SLEEP (50)
  GOTO(detect)
ENDIF 

# The avoidance script
:avoid
IF ($irreverseturn = 1)
  reverse($irmovementspeed,$irreverseamount)
ENDIF 
IF ($irturndirection = 0)
  LEFT($irmovementspeed,$irturnamount)
ELSE 
  RIGHT($irmovementspeed,$irturnamount)
ENDIF 
RETURN()

Updated on the cloud too:)

I'm not sure on the best way around for the GOTO(detect), ENDIF after SLEEP(50) in the detectstart script, as far as I can tell it doesn't matter but would be interested in knowing which way around is considered the best practice, if there is such a thing...

Canada
#7  

Rich, is it much work to make this script for two IR, one for each side, on a 45 facing in for a full view? Thanks. I have been trying. and im still new. lol, Thanks

#8  

Here's an example for 3 IR units and a front bump switch with thanks from Rich:

# Three IR units, Left_IR, Center_IR, and Right_IR will
# be monitored for possible collision from objects in 
# front of a moving robot.  If those IR units fail to
# detect then the single bump sensor will activate on
# a collision.

# Once detected then drive motor control will be adjusted 
# to avoid collision

# Normal values for an ADC port are from 0 to 255
# Collision value should be 100 or less
$Collision = 100

# Any value below 100 will indicate a possible collision
# Also the robot is set to move Forward at a speed of 60
# Speed can be 0 - 255

# Read the port values of the 3 IR units as well as the
# one bumper sensor.

:Read_ADC
Forward(60)

$Read_Center_IR = GetADC(ADC0)
$Read_Left_IR = GetADC(ADC1)
$Read_Right_IR = GetADC(ADC2)
$Read_Bump = GetDigital(D8)

print(GetDigital(D8))
print(GetADC(ADC0))
print(GetADC(ADC1))
print(GetADC(ADC2))

IF ($Read_Left_IR <= $Collision)
Stop()
Goto(Avoid)

Elseif ($Read_Center_IR <= $Collision)
Stop()
Goto(Avoid)

Elseif ($Read_Right_IR <= $Collision)
Stop()
Goto(Avoid)

Elseif ($Read_Bump = 0)
Stop()
Goto(Avoid)
EndIf

GoTo(Read_ADC)

#Avoid subroutine
:Avoid
Reverse(60,500)
Left(60, 500)
#print(GetADC(ADC0))
#print(GetADC(ADC1))
#print(GetADC(ADC2))
#Sleep(200)
Return()