Ev3Dev Code, annotated.

Rohan Arni
Path to Polymath
Published in
2 min readMar 6, 2021

Last week, I explained how the Ev3 tournament worked. Now, let’s take a look at the code.

This is the launcher code:

#!/usr/bin/env python3from ev3dev2.motor import *
from ev3dev2.sensor.lego import *
from ev3dev2.button import *
from ev3dev2.wheel import *
from ev3dev2.sound import *
import library
steering = MoveSteering(OUTPUT_C, OUTPUT_B)
motor = MediumMotor()
tank = MoveTank(OUTPUT_B, OUTPUT_C)
STUD_MM = 8
mdiff = MoveDifferential(OUTPUT_B, OUTPUT_C, EV3Tire, 16 * STUD_MM)
tank.gyro = GyroSensor()
mdiff.gyro = GyroSensor()
btn = Button()
sound = Sound()
sound.play_tone(880, 0.25)
sound.play_tone(440, 0.25)
library.choice(tank, SpeedPercent, follow_for_ms, motor, mdiff, steering,btn, sound)

This program is linked with the library code. There is nothing really interesting in this code. Some generic imports, some definitions, and runs the library function. What is the library code? Let’s take a look at that.

import bench
import dance
import bottom_right
import step_slide
import stepper
import slide
import input
import treadmil
import treadmil_gate
def choice(tank, SpeedPercent, follow_for_ms, motor, mdiff, steering, btn, sound):
while True:
print(“ready for next command”)
functionNumber = input.readKeys(btn, sound)
# functionNumber = 3
print(functionNumber)
if functionNumber == 1:
bench.go(tank)
# if functionNumber == 1:
# bottom_right.go(tank, SpeedPercent, follow_for_ms, motor, mdiff)
elif functionNumber == 2:
stepper.go(tank, SpeedPercent, follow_for_ms, motor, mdiff)
elif functionNumber == 3:
treadmil_gate.go(tank, SpeedPercent, follow_for_ms, motor, mdiff)
# elif functionNumber == 5:
# step_slide.go(tank, SpeedPercent, follow_for_ms, motor, mdiff)
# elif functionNumber == 11:
# slide.go(tank, mdiff, SpeedPercent)
# elif functionNumber == 12:
# treadmil.go(tank, SpeedPercent, follow_for_ms, motor, mdiff)
elif functionNumber == 0:
dance.dance_now(tank, steering)
#uncomment below when debugging
#exit(0)

There are 4 missions that we did. We used binary to identify the missions. 00 is 0, 01 is 1, 10 is 2, and 11 is 3. We used the buttons on the robot to make the robot move. Left button was 0, right button was 1. Every time we ran code, it invoked the function for each mission. Let’s take a look at one of the missions: the stepper.

The STEPPER!!!!!

The goal is to push that green tab into the purple-and-black box.

from time import sleep
def go(tank, SpeedPercent, follow_for_ms, motor, mdiff):
#forward fast 63cm
tank.on_for_rotations(50, 50, 5.0)
#forward slow 20cm
for i in range(33):
tank.on_for_rotations(5, 5, 0.05)
sleep(0.03)
#backward fast 32cm
tank.on_for_rotations(-50, -50, 10)

No imports, as you can see. There is no need for them, as library.py has the imports.

So this whole thing is a function that library invokes. First, the robot uses the tank function, which controls the robot’s wheels as a tank. It goes forward for 5 rotations, which is 63 cm. That takes us close to the stepper contraption. We go slower for 20 cm to push the stepper in using the tank function. Then we back out of there super fast, coming back to the home base.

Thanks for reading!

exit(pathToPolymath)

--

--