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)Code:
# 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
Asked
— Edited
Code:
Code:
That should do it I think, however it is untested (and I really need to revisit this code to change a few things I dislike about how I wrote it - which don't change how it works, it's just me being picky)
Edit: Or use Robot-Docs script (that'll teach me to post and eat dinner at the same time, I missed the updates...)
Give it a go, see what happens, I'm usually around to guide you if you get stuck.
Is the yellow face supposed to be
yellow is " )
The
The
The
This is why I usually upload to pastebin too but didn't get chance last night.
******a subroutine
Action5: // hard left
for i=1 to 10
rForward 1
if rFeel()then break
next
for i=1 to 70
rTurn -1
if rSense() then break
next
return
Action6: // hard right
for i=1 to 10
rForward 1
if rFeel()then break
next
for i=1 to 70
rTurn 1
if rSense() then break
next
return
**************************another sub
Action1: // easy left
rForward 1
rTurn -1
return
Action2: // easy right
rForward 1
rTurn 1
return
Action3: // medium left
rForward 1
rTurn -3
return
Action4: // medium right
rForward 1
rTurn 3
return
Action5: // Hard left
rForward 1
rTurn -6
return
Action6: // Hard right
rForward 1
rTurn 6
return
***************************another sub
main:
gosub Init
while 1
gosub RoamAndObserve
wend
end
RoamAndObserve:
// genetically turn away from walls
if rFeel() then rTurn (140+Random(80))
if rSense()
// something got our attention
gosub ReactAndAnalyze
else
if !rFeel() then rForward 1
endif
return
*******************Next program below
ReactAndAnalyze:
CurState = rSense()
// see if CurState is in memory
InMemory=false
if NumStates>0
for p=0 to NumStates-1
if GoodState[p]=CurState
InMemory = True
break
endif
next
endif
if InMemory
// react as expected based on past experience
// perform the action associated with the current state
gosub "Action"+ActionToTake[p]
// decide if the action still produces good outcome
NewState = rSense()
Reward=false
if NewState&2 then Reward=True
// Also reward if this NewState is in memory
if !Reward and NumStates>0
for i=0 to NumStates-1
if NewState=GoodState[i]
Reward=True
break
endif
next
endif
if !Reward
// old memory does not seem to be valid
ConfidenceLevel[p]--
if ConfidenceLevel[p]=0
for i=p to NumStates-1 // delete memory
GoodState[i]=GoodState[i+1]
ActionToTake[i]=ActionToTake[i+1]
ConfidenceLevel[i]=ConfidenceLevel[i+1]
next
NumStates--
endif
else
// increase confidence of memory (up to 2)
if ConfidenceLevel[p]<3 then ConfidenceLevel[p]++
endif
else
// try something new
NewAction = Random(6)+1
gosub "Action"+NewAction
// now see if the New Action produced good results
NewState = rSense()
Reward=false
if NewState&2 then Reward=True // gave direct reward
// Also reward if this NewState is in memory
if !Reward and NumStates>0
for i=0 to NumStates-1
if NewState=GoodState[i]
Reward=True
break
endif
next
endif
if Reward
// good result so add to memory if not there already
// CurState is what initiated this action so add to memory
// NewAction is what we did to get rewarding results
GoodState[NumStates]=CurState
ActionToTake[NumStates]=NewAction
ConfidenceLevel[NumStates]=1
NumStates++
endif
endif
return
Hope that this heled.
Now, parts of it are just printing stufff on the screen. But, some of it are sniplets of A.I. programming where the robot has a confidense level and stores good moves in memory. Evently, it will become a self-programming robot programmed by it's environment. I would write the code myself, but I am not there yet. Read the article in Oct. 2013 servo magazine. Look at some of the code and you will see what i am talking about. Making intelligent decisions based on past mistakes. It is written in Basic, but could be transferred into EZ-Script, I am guessing. (That's where you or some top notch programmer comes in.) I am not saying that the other programs you wrote were bad. I just put this here to assist the robot to go further with A.I.
When I said "Smartly" I wasn't referring to you. I was referring to the robot. Your code is good and I respect you.
You can download RobotBasic Free and play with it. It does lots of good stuff on the screen, but I want the robot to do it, him or herself.
The currently released version of the Ping Roam is intelligent enough to avoid objects, escape from a boxed in situation by itself and avoid getting stuck in loops bouncing backwards and forwards over the same area. The next version (which is still waiting to be tested properly) has "smart avoidance" where the turns are based on the sensor readings rather than the timings.
In other words, avoidance code shouldn't make a bad decision therefore learning is not required (unless you purposely code it to make bad decisions, which there is no benefit in at all), hence the confusion over why the code was posted as I cannot see it being required.
The link you asked for:
robotBasic
@vhs896, This original IR code was for a single fixed sensor. The Ping one hasn't been for fixed since it needs to "look" left and right too. You could adjust it for a fixed ping sensor pretty easily, rather than GetADC() use GetPing() and reverse the If statement (IR works backwards to Ping so any less thans need to be greater than and vice versa)
@Rich, you are the man!