Hello, Thought I would share this as I find it fairly simple, useful and fun. Using Myro and ffnet I have been able to get impresive object avoidance capabilities from the srv ir sensors... given the limits of ir. Below is a training program for a neural net and a brain for use of a trained network with the srv.
Run the training program to create the trained network, it will save to a file called srv_ir.net in the directory that the trainer is run from. Then use the brain to run the trained network with the srv.
I found if I turn on the srv in the room or area that it is going to be operated in I get better results from the ir sensors.
Beware ffnet has some dependencies and caused me to go in circles for an hour or so... this however was probably just due to my lack of experience with python.
Here are the websites for the software:
http://ffnet.sourceforge.net/ http://wiki.roboteducation.org/Myro_Hardware Example programs:
BEGIN TRAINER CODE:
########################################################################
## Copyright (C) 2006 by Marek Wojciechowski
## <mwojc@p.lodz.pl>
##
## Distributed under the terms of the GNU General Public License (GPL)
##
http://www.gnu.org/copyleft/gpl.html ## Modified for SRV-1 Robot IR sensor navigation 03/31/07 By Jason Copeland
## jason@linux4solutions.com
########################################################################
### SRV-1 IR sensor input ###
from ffnet import ffnet, mlgraph
# Generate standard layered network architecture
conec = mlgraph((4,4,2))
# Create network
net = ffnet(conec)
# Define training data
#All possible ir input combos, can be chaged to increase/decrese sensitivity of srv ir.
input = [[0.,0.,0.,0.], [0.,0.,0.,0.3], [0.,0.,0.3,0.], [0.,0.,0.3,0.3],[0.,0.3,0.,0.],[0.,0.3,0.,0.3],[0.,0.3,0.3,0],[0.,0.3,0.3,0.3],[0.3,0.,0.,0.],[0.3,0.,0.,0.3],[0.3,0.,0.3,0.],[0.3,0.,0.3,0.3],[0.3,0.3,0.3,0.],[0.3,0.3,0.,0.3],[0.3,0.3,0.3,0.],[0.3,0.3,0.3,0.3]]
#These are the reactions I want from the coresponding input, they should in essence avoid all objects in given environment, these can be chaged to get many different behaviors.
target = [[1.,1.], [1.,1.], [1.,1.], [1.,1.], [1.,1.], [1.,1.], [1.,1.], [1.,1.],[0.,0.],[0.,1.],[0.,1.],[0.,1.],[1.,0.],[0.,0.],[1.,0.],[0.,0.]]
# Train network
#first find good starting point with genetic algorithm (not necessary, but may be helpful)
print "FINDING STARTING WEIGHTS WITH GENETIC ALGORITHM..."
net.train_genetic(input, target, individuals=20, generations=500)
#then train with scipy tnc optimizer
print "TRAINING NETWORK..."
net.train_tnc(input, target, maxfun = 1000, messages=1)
# Test network
print
print "TESTING NETWORK..."
output, regression = net.test(input, target, iprint = 2)
# Save/load/export network
from ffnet import savenet, loadnet, exportnet
print "Network is saved..."
savenet(net, "srv_ir.net")
print "loading trained neural net..."
net = loadnet("srv_ir.net")
print "Network is tested again, but nothing is printed..."
output, regression = net.test(input, target, iprint = 0)
print
print output
print
print "Done..."
END TRAINER CODE:
BEGIN BRAIN CODE:
##IR sensor brain for srv-1
##Takes as inputs SRV-1 ir sensors
from ffnet import ffnet, loadnet
from myro import *
#initialize robot
robot = Surveyor("com4")
#Output adjustment.
adjustL = 0.6
adjustR = 0.6
#Load network created with training program.
net = loadnet("srv_ir.net")
while True:
ans = net.call(robot.get("ir")) #Nework input
robot.motors(ans[0]-adjustL, ans[1]-adjustR) #Network output
END BRAIN CODE:
Save the two examples in the same directory as something like:
trainer.py
brain.py
From a command line enter the directory you saved to and execute:
python trainer.py
Make sure the srv is turned on and ready to go, execute:
python brain.py
Keep an eye on srv...
Enjoy,
Jason