Hi guys,
I just want to connect my Raspberry Pi to my Thymio II so that I can control it with a Python script.
I tried to import the gobject package with:
import gobject
But this did not work for me, so I did this instead:
from gi.repository import GObject as gobject
This is now my whole code:
import dbus
import dbus.mainloop.glib
from gi.repository import GObject as 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])
totalRight=totalRight+(proxSensorsVal[i]*rightWheel[i])
#add a constant speed to each wheels so the robot moves always forward
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
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()
When I run the code with the following command:
python3 test.py
I get this error message:
pi@berry:~/Desktop $ python3 test.py
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/dbus/bus.py", line 175, in activate_name_owner
return self.get_name_owner(bus_name)
File "/usr/lib/python3/dist-packages/dbus/bus.py", line 361, in get_name_owner
's', (bus_name,), **keywords)
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NameHasNoOwner: Could not get owner of name 'ch.epfl.mobots.Aseba': no such name
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 65, in <module>
network = dbus.Interface(bus.get_object('ch.epfl.mobots.Aseba', '/'), dbus_interface='ch.epfl.mobots.AsebaNetwork')
File "/usr/lib/python3/dist-packages/dbus/bus.py", line 241, in get_object
follow_name_owner_changes=follow_name_owner_changes)
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 248, in __init__
self._named_service = conn.activate_name_owner(bus_name)
File "/usr/lib/python3/dist-packages/dbus/bus.py", line 180, in activate_name_owner
self.start_service_by_name(bus_name)
File "/usr/lib/python3/dist-packages/dbus/bus.py", line 278, in start_service_by_name
'su', (bus_name, flags)))
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name ch.epfl.mobots.Aseba was not provided by any .service files
My question:
How can I fix this?
Or is there a more simple way to connect and control my Thymio II from my Raspberry Pi?
Edit:
Okey, I don't know why but it's working now with the import. But now I have a new problem.
I get this error:
pi@berry:~/Desktop $ python3 test.py
dbus.Array([], signature=dbus.Signature('s'))
starting loop
0 0 0 0 0
totalLeft
50.0
totalRight
50.0
Traceback (most recent call last):
File "test.py", line 37, in Braitenberg
network.SetVariable("thymio-II", "motor.left.target", [totalLeft])
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.InvalidArgs: node thymio-II does not exists
error:
org.freedesktop.DBus.Error.InvalidArgs: node thymio-II does not exists
pi@berry:~/Desktop $
Thank you in advance.