My first hackathon project
This was the first hackathon that I participated in - a small local hackathon that lasted only around 12 hours in total.
During my first year at university most of my time I was programming, but I’ve never had any experience with robotics. So, even such a simple robot as Lego EV3 seemed an interesting project.
Lego provides software and apps to control their robots, but I wanted to make something a bit different.
My idea was to control the robot through my laptop keyboard and make it react to obstacles (ultrasonic sensor) by playing a custom sound.
The robot was operated through SSH connection and the program was run from there. Surprisingly this simplistic way of controlling the robot was more responsive then dedicated Lego app at the time.
The simple code I used:
from ev3dev.ev3 import * # library for controling ev3
from time import sleep
import sys, termios, tty, os # keyboard listening
# listening for keys
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
# initialising ultrasonic sensor
us = UltrasonicSensor()
# Put the sensor into distance mode
us.mode='US-DIST-CM'
units = us.units
# motors initialisation
m = LargeMotor('outB')
a = LargeMotor('outC')
control=''
Sound.play('/home/robot/Beginning.wav').wait() # starting sound
print('Lets begin') # starting script takes few seconds. This just indicates when it is ready.
while control != 'q':
distance = us.value()/10
char = getch()
if char == 'q':
exit(0)
elif char == 'e':
Leds.set_color(Leds.LEFT, Leds.RED)
Leds.set_color(Leds.RIGHT, Leds.RED)
Sound.play('/home/robot/Exterminate.wav').wait()
Leds.set_color(Leds.LEFT, Leds.YELLOW)
Leds.set_color(Leds.RIGHT, Leds.YELLOW)
elif char == 'm':
Sound.play('/home/robot/MustSurvive.wav').wait()
elif char == ' ' and distance < 20:
print(str(distance) + " " + units)
Sound.play('/home/robot/Exterminate.wav').wait()
elif char == 'w':
a.run_timed(time_sp=500, speed_sp=900)
m.run_timed(time_sp=500, speed_sp=910)
elif char == 's':
a.run_timed(time_sp=500, speed_sp=-900)
m.run_timed(time_sp=500, speed_sp=-900)
elif char == 'a':
a.run_timed(time_sp=140, speed_sp=200)
m.run_timed(time_sp=140, speed_sp=-200)
elif char == 'd':
a.run_timed(time_sp=140, speed_sp=-200)
m.run_timed(time_sp=140, speed_sp=200)