Skip to content

Instantly share code, notes, and snippets.

@clarkwinkelmann
Created June 9, 2018 14:17
Show Gist options
  • Save clarkwinkelmann/3e0af54bf140d1124dc4966bdb804c65 to your computer and use it in GitHub Desktop.
Save clarkwinkelmann/3e0af54bf140d1124dc4966bdb804c65 to your computer and use it in GitHub Desktop.
Drive the Thymio with a keyboard
<!DOCTYPE aesl-source>
<network>
<!--list of global events-->
<event size="8" name="utimateLed"/>
<!--list of constants-->
<!--show keywords state-->
<keywords flag="true"/>
<!--node thymio-II-->
<node nodeId="1" name="thymio-II">onevent utimateLed
call leds.circle(event.args[0], event.args[1], event.args[2], event.args[3], event.args[4], event.args[5], event.args[6], event.args[7])
onevent rc5
if rc5.command==32 then
motor.left.target = 200
motor.right.target = 200
end
if rc5.command==33 then
motor.left.target = -200
motor.right.target = -200
end
if rc5.command==37 then
motor.left.target = 0
motor.right.target = 0
end</node>
</network>
import dbus
import dbus.mainloop.glib
import gobject
import sys
import pygame
import pygame.locals
import os
# Use the following to start the serial service:
# asebamedulla "ser:device=/dev/ttyACM0"
proxSensorsVal=[0,0,0,0,0]
ledLoop = 0
direction = 1
prevSpeedLeft = 0
prevSpeedRight = 0
def Braitenberg():
global ledLoop
global direction
global prevSpeedLeft
global prevSpeedRight
leftSpeed = 0
rightSpeed = 0
# get key current state
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.locals.K_UP]:
leftSpeed += 100
rightSpeed += 100
direction = 1
if keys[pygame.locals.K_DOWN]:
leftSpeed -= 150
rightSpeed -= 150
direction = -1
if keys[pygame.locals.K_LEFT]:
rightSpeed += 200 * direction
if keys[pygame.locals.K_RIGHT]:
leftSpeed += 200 * direction
#send motor value to the robot
if leftSpeed != prevSpeedLeft:
network.SetVariable("thymio-II", "motor.left.target", [leftSpeed])
prevSpeedLeft = leftSpeed
if rightSpeed != prevSpeedRight:
network.SetVariable("thymio-II", "motor.right.target", [rightSpeed])
prevSpeedRight = rightSpeed
ledState = [0,0,0,0,0,0,0,0]
for i in range(0, 8):
if ledLoop == i:
ledState[i] = 32
elif (ledLoop - 1) % 8 == i or (ledLoop + 1) % 8 == i:
ledState[i] = 16
elif (ledLoop - 2) % 8 == i or (ledLoop + 2) % 8 == i:
ledState[i] = 5
network.SendEventName('utimateLed', ledState)
ledLoop = (ledLoop + direction) % 8
return True
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
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()
network.LoadScripts(os.getcwd() + '/Pymio.aesl')
pygame.init()
screen = pygame.display.set_mode((200, 200))
#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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment