Introduction‎ > ‎

Basic or Python?

I consider that the two best and easiest options for programming the EV3 textually are EV3 Basic and EV3 Python. Whether you opt for EV3 Basic or EV3 Python you should get a good grounding in standard Basic (MS Small Basic in the case of EV3 Basic) or standard Python before you progress to the EV3 versions which are like the 'icing on the cake'.

EV3 Basic
I am convinced that the EV3 textual programming language that is the most easy to install and to learn is EV3 Basic. I have spent hundreds of hours working with EV3 Basic and like it so much that I made this site to promote it. Basic was originally designed for beginners but evolved over the years into a sophisticated form called Microsoft Visual Basic that is less easy for beginners to learn. Fortunately Microsoft recognised this problem and released in 2008 a version of Basic that is once again simple and easy to learn: Microsoft Small Basic. Small Basic is designed to appeal to young people, with an emphasis on games and graphics. Adding the 'EV3 extension' converts Small Basic into EV3 Basic and allows it to work with the EV3 robot. Basic was one of the first programming languages to be created and some people feel that it has become outdated and that beginners should learn a more modern language. But Basic has always been very popular and even today (September 2016) Visual Basic.Net and its older variant Visual Basic are among the most popular programming languages in the world - see for yourself at Tiobe.com which tracks the popularity of more than 100 languages. In fact if you lump together the two main kinds of Basic (Visual Basic and Visual Basic.net) then we can say Basic is still about as popular as the Python language! Another reason to consider using EV3 Basic is that it is easier to install than other EV3 textual programming languages since it works directly with the unmodified EV3 brick whereas other textual languages require you either to modify the brick's firmware or prepare a microSD card that will contain an alternative operating system that will be used in place of the brick's firmware. Note that EV3 Basic runs only on Windows computers.

EV3 Python
Python is a modern, very powerful programming language that is easier to learn than most others such as C++ or Java and is therefore very popular in schools and universities. Knowledge of Python is much more likely to help you get a career in computing than knowledge of Basic. One thing that helps to make Python easy to learn is that it creates very concise, easy-to-read code. I have seen an article that says that a program written in Java might typically be nearly four times longer than a program written in Python that does the same thing! Also, an advanced, rather difficult feature called 'object oriented programming' (OOP) is available in Python but is not obligatory, so you can avoid it initially and then start using it when you're ready. See this article Why Python might just be all you need and also this article.

EV3 Python needs to run on top of an operating system called EV3dev. You need to install EV3dev and EV3 Python onto a microSD card - a quick and straightforward process. When you insert the card into the EV3 and turn on the EV3 it will boot from the card rather than from the usual Lego operating system - you will see a different interface called BrickmanSince you will be writing your programs on the computer you need a way to communicate between your computer and the brick  - the recommended way to do this is to use a Secure SHell (SSH) connection. In order to manage that connection you need to know (or learn) a bit about Linux - this could be considered an annoyance or a bonus - Linux is certainly an important operating system (it's quite possible your phone is running a version of Linux called Android).

To learn more about EV3dev visit ev3dev.org. To get started working with EV3dev and EV3 Python, visit my new site ev3python.com.

Sample Programs
Let's see how a given problem can be solved by EV3 Basic and by EV3 Python, as well as by the standard EV3 Lego software (EV3-G). We'll use one of the challenges from the Robot Educator section of the educational version of the Lego EV3 software. You can compare the conciseness and readability of the EV3 Basic and EV3 Python solutions, but your choice of which language to study should also take into account the power of each language and the value that it could bring to your future professional life.

Objective: The robot is assumed to have already moved along two perpendicular arms of a triangle, each with length 25 cm, and to have turned around 180° so it is now in the right location to begin tracing the hypotenuse but it is not pointing in the right direction. The robot should now turn slowly on the spot until the gyro sensor detects that the robot has turned at least 45°, then the motors should be turned off.

Then the robot should calculate the length of the hypotenuse using the actual angle turned by the robot (as measured by the gyro sensor) rather than the 45° angle that the robot should have turned. The calculation will be:

                       hypotenuse length = adjacent arm length / cos(turn angle)

Note that Small Basic (and therefore also EV3 Basic) trigonometric functions (Sin(), Cos() etc) work in radians, not degrees.   1 radian = 57.3° (approx)

Then the robot should calculate the corresponding number of wheel rotations needed, given that the circumference of the standard Lego wheel is 17.6 cm. Then the robot should move at speed 30 in the correct direction and for the correct distance in order to trace out the hypotenuse of the triangle.

EV3 Basic Solution:

Sensor.SetMode(2,0)   'Set gyro sensor on port 2 to mode 0

Motor.StartSync("BC",10,-10)    'Robot will slowly turn to the right on the spot

While Sensor.ReadRawValue(2,0) < 45
EndWhile

Motor.Stop("BC", "True")

'Small Basic trig functions like cos() work in RADIANS
Radians=Sensor.ReadRawValue(2,0)/57.3    'convert to radians.  1 rad = 57.3°
Length = 25/Math.Cos(Radians)   'calculate length of hypotenuse
Rots=Length/17.6 'calculate wheel rotations (wheel circumference=17.6cm)

Motor.Move("BC",30, Rots*360, "True")   'convert wheel rotations to degrees

Notes:

  1. Once again, know that Small Basic trig functions work in radians, not degrees, and that 1 rad = 57.3°.
  2. The above solution is an awkward mix of degrees and radians, but this could not be avoided.
  3. Don't confuse the angle that the robot turns with the angle that the wheels turn.
  4. If you want to see other examples of how EV3 Basic can solve the official EV3 challenges click HERE.

EV3 Python Solution:

The Python solution is almost twice as long as the EV3 Basic solution (not counting comments and empty lines) but it's still pretty easy to read except for the first line which is a special line (a 'shebang') that makes it possible to run the program from the EV3. 

#!/usr/bin/env python3

from ev3dev.ev3 import *  # make the ev3 functions available
from time import sleep  # make the sleep() function available
import math  # needed for cos() and radians()

# Connect gyro sensor to any sensor port and set mode
gy = GyroSensor() 
gy.mode='GYRO-ANG' # Put the gyro sensor into ANGLE mode.

# Attach large motors to ports B and C
mB = LargeMotor('outB')
mC = LargeMotor('outC')

# Make robot slowly turn to the right on the spot
mB.run_forever(speed_sp=250)
mC.run_forever(speed_sp=-250)

while gy.value()<45:
    sleep(0.01) # Continue looping while turn angle less than 45 deg

# calculate length of hypotenuse
length = 25/math.cos(math.radians(gy.value()))
rots=length/17.6
# calculate wheel rotations (wheel circumference= 17.6 cm)

mB.run_to_rel_pos(position_sp=rots*360, speed_sp=500)
mC.run_to_rel_pos(position_sp=rots*360, speed_sp=500)

sleep(5)   # Give the motor time to move

EV3-G solution:

Note that EV3-G trig functions use degrees, not radians.

Comments