Asked
— Edited
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)
Yeah, sorry that's an issue with the forum formatting for emoticons.
The
should be : with a D next to it (no space)
The
should be " with ) after it (no space) - the forums leave the " so just change the
for a )
The
I don't think the space matters but the
does.
This is why I usually upload to pastebin too but didn't get chance last night.
Here are some pieces of code that was in servo Mag. You could do it smartly. But, this is all written in RobotBasic and would have to be converted to EZ-Script.
******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.
@Mel, what does that code do? What does it do better than the code posted (or the ping roam code which is a more advanced method than the older IR code posted). And what is "Robot basic"? Any links to the language?
This code came from RobotBasic and the Robot Programmer's Bonanza, and servo Magazine.
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.
:)
I still don't see why or how it would need to learn. The Ping Roam code, which is the advanced version of this IR code, detects obstructions and moves 100% correctly 100% of the time. There is no learning required, there is no guessing required. Either the obstruction is there or it isn't and the sensor(s) are enough for it to know which direction the obstruction is in.
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.
Hey Rich. Any of your ping roam scripts have the ping sensor stationary ? I like the IR code. And used your ping roam for a test bot. I will use the ping roam for my omnibot. But the IR was used in my wall-e. I will attach a picture of my wall-e and wear the sensors are. Need help with the scripts.
Well, Rich, they say if it 'aint Broke, don't fix it. If it works good we will leave it alone. I am always trying to get a machine to Learn and demonstrate artificial intelligence. Evidently, this little program doesn't need it. It was just a thought. You did a good job. I am sorry about the confusion.
The link you asked for:
robotBasic
:)
Don't get me wrong Mel, it wasn't a complaint or anything along those lines, all input is always welcome
And thanks for the link, I'll have a look at 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)