-
Notifications
You must be signed in to change notification settings - Fork 23
/
t2.py
executable file
·80 lines (61 loc) · 2.37 KB
/
t2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
import dbus
import dbus.mainloop.glib
import gobject
from optparse import OptionParser
proxSensorsVal=[0,0,0,0,0]
def Braitenberg():
#get the values of the sensors
network.GetVariable("thymio-II", "prox.horizontal",reply_handler=get_variables_reply,error_handler=get_variables_error)
#print the proximity sensors value in the terminal
print proxSensorsVal[0],proxSensorsVal[1],proxSensorsVal[2],proxSensorsVal[3],proxSensorsVal[4]
#Parameters of the Braitenberg, to give weight to each wheels
leftWheel=[-0.01,-0.005,-0.0001,0.006,0.015]
rightWheel=[0.012,+0.007,-0.0002,-0.0055,-0.011]
#Braitenberg algorithm
totalLeft=0
totalRight=0
for i in range(5):
totalLeft=totalLeft+(proxSensorsVal[i]*leftWheel[i])
for j in range(5):
totalRight=totalRight+(proxSensorsVal[j]*rightWheel[j])
#add a constant speed to each wheels so the robot always forward if object detected
if (totalRight + totalLeft) != 0:
totalRight=totalRight+50
totalLeft=totalLeft+50
#print in terminal the values that is sent to each motor
print "totalLeft"
print totalLeft
print "totalRight"
print totalRight
#send motor value to the robot
network.SetVariable("thymio-II", "motor.left.target", [totalLeft])
network.SetVariable("thymio-II", "motor.right.target", [totalRight])
return True
def get_variables_reply(r):
global proxSensorsVal
print "r",r
proxSensorsVal=r
def get_variables_error(e):
print 'error:'
print str(e)
loop.quit()
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-s", "--system", action="store_true", dest="system", default=False,help="use the system bus instead of the session bus")
(options, args) = parser.parse_args()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
if options.system:
bus = dbus.SystemBus()
else:
bus = dbus.SessionBus()
#Create Aseba network
network = dbus.Interface(bus.get_object('ch.epfl.mobots.Aseba', '/'), dbus_interface='ch.epfl.mobots.AsebaNetwork')
#print in the terminal the name of each Aseba NOde
print network.GetNodesList()
#GObject loop
print 'starting loop'
loop = gobject.MainLoop()
#call the callback of Braitenberg algorithm
handle = gobject.timeout_add (100, Braitenberg) #every 0.1 sec
loop.run()