home

Documentation EV3 Robotics with global functions

Autonomous and Simulation Mode (implemented in simulation mode: [S])

Modul import: from grobot import *

Robot with Gear (2 synchronized motors on MotorPort A and B):

forward() [S] sets the robot in forward motion with predefined speed
forward(time) [S] moves forward during given time (in ms) (blocking method)
backward() [S] sets the robot in back ward motion with predefined speed
backward(time) [S] moves backward during given time (in ms) (blocking method)
left() [S] starts the left turn
left(time) [S] turns left during given time (in ms) (blocking method)
right() [S] starts the right turn
right(ms) [S] turns right during given time (in ms) (blocking method)
leftArc(radius) [S] starts the left curve movement (radius in m, only approximative)
leftArc(radius, time) [S] turns on left arc during given time (in ms) with given radius left (blocking method)
rightArc(radius) [S] starts the right curve movement (radius in m, only approximative)
rightArc(radius, time) [S] turns on right arc during given time (in ms) with given radius left (blocking method)
stop() [S] stops the movement
moveTo(count, blocking = True) [S] starts the rectilinear movement with predefined speed up to the given wheel count and stops. count > 0: forward, count < 0: reverse
turnTo(count, blocking = True) [S] starts the rotation up to the given wheel count and stops. count > 0: clockwise, count < 0: counterclockwise
setSpeed(speed) [S] sets the speed (0..100). Only becomes effective when the next motion function is called. Default speed = 30.
delay(time) [S] stops the program during the given time (in ms)
sleep(time [S] same as delay(ms)
setLED(n) [S] switches LED lights: 0: off, 1: green, 2: red, 3: light red , 4: green flashing, 5: red flashing, 6: light red flashing, 7: green double flashing, 8: red double flashing, 9: light red double flashing
setAlarm(n) [S] switches beep signal: 0: off, 1: on
playTone(freq, duration) [S] plays a tone with a given frequency (in Hertz) and duration (in ms)
setVolume(volume) sets the volume (0..100). This value is stored permanently
clearDisplay() clears the display
drawString(text, x, y) writes single-line text at position (x: horizontal, y: vertical)
getIPAddresses()

returns a list of all active IP addresses

getRobot() returns the instance of the global robot (to be used with OOP classes)
reset() [only S] sets the robot to the starting position/direction


Motor (control motors EV3 or NXT):

Instances Port
motA, motB, motC, motD motor at corresponding MotorPort
motL = motA, motR = motB left and right motor

Methods Action
motX.rotate(speed) [S] turns the motor motX (speed > 0: forward, speed> 0, backward, speed = 0: stop()
motX.isMoving() [S] returns True, if the motor is rotating
motX.getMotorCount() [S] returns the current status of the counter
motX.resetMotorCount() [S] sets the counter to 0
motX.rotateTo(count) [S] moves motor to counter count and stops (blocking). Sets counter to 0
motX.rotateTo(count, blocking) [S] same as rotateTo(count); not blocking, if blocking = False
motX.continueTo(count) [S] same as rotateTo(count), but counter not set to 0
motX.continueTo(count, blocking) [S] same as rotateTo(count); not blocking, if blocking = False. Counter not set to 0
motX.continueRelativeTo(count) [S] same as continueTo(count), but count is increment
motX.continueRelativeTo(count, blocking) [S] same as continueTo(count), but count is increment; not blocking, if blocking = False
motX.setSpeed(speed) sets the speed of motX (default speed=50)

 

Buttons:

Instances Position
button_escape [S] top left
button_enter [S]

center

button_left [S] cursor left
button_right [S] cursor right
button_up [S] cursor up
button_down [S] cursor down

Method Action
was_pressed() [S] returns True if the button has been pressed since the last call.

Functionen [S] Action
isEscapeHit() [S] returns True, if the escape button has been pressed since the last call
isEnterHit() [S] same for the enter button
isLeftHit() [S] same for the left button
isRightHit() [S] same for the right button
isUpHit() [S] same for the up button
isDownHit() [S] same for the down button

 

LightSensor (EV3 Colorsensor as light sensor):

Instances Port
ls1, ls2, ls3, ls4 [S] light sensor at corresponding SensorPort

Method Action
getValue() [S] returns light intensity (0..1023)

 

TouchSensor

Instances Port
ts1, ts2, ts3, ts4 [S] touch sensor at corresponding SensorPort

Method Action
isPressed() [S] returns True, if the touch sensor is pressed

 

Distance sensor (EV3 Ultrasonic sensor):

Instances Port
us1, us2, us3, us4 [S] sensor at corresponding SensorPort


Methods Action
getDistance() [S] returns distance (in cm); 255, if no reflecting object is detected (for simulation mode: -1)
setProximityCircleColor(color) [S] (simulation mode only) sets the color of the proximity circle
setMeshTriangleColor(color) [S] (simulation mode only) sets the color of the mesh triangles
setBeamAreaColor(color) [S] (simulation mode only) sets the color of the beam area boundaries
eraseBeamArea() [S] (simulation mode only) erases the beam area boundaries

Colorsensor:

Instances Port
cs1, cs2, cs3, cs4 [S] Colorsensor at corresponding SensorPort

Methode Aktion
getColor() [S] returns the basic colors as a string (RED, BLUE, YELLOW, GREEN, WHITE, BLACK)
getColorRGB() [S] returns a list of color components [r, g, b] ( numbers 0 ... 255)
calibrateColor(colors, previous) calibrates colors that are not correctly recognized by the color sensor. Parameter colors is a list of colors to be calibrated. If no list is given, the standard colors (black, blue, green, yellow, red and white) are calibrated. If parameter previous is True, a new calibration starts, if False the results of the last calibration are used.
Ex: cs3.calibrateColor(["RED", "BLUE", "GREEN"], True)

Remark:
If a sensor is connected to SensorPort 1, you can also use global functions (e.g. getValue ().

GyroRateSensor:

Instanzen Port
grs1, grs2, grs3, grs4 Gyro Rate Sensor at corresponding SensorPort

Methode Aktion
getValue() returns the rotation speed of the robot in degrees per second

 

GyroAngleSensor:

Instanzen Port
gas1, gas2, gas3, gas4 Gyro Angle Sensor at corresponding SensorPort

Methode Aktion
getValue() provides the rotation angle of the robot in degrees
reset() sets the start position

 

Temperature/Humidity sensor (Sensirion SHT31 at I2C):

Instances Port
sht1, sht2, sht3, sht4 sensor at corresponding SensorPort

Method Action
getValues() returns a tuple of floats with temperature (deg C) and humidity (%)


Environment sensor (Temperature, Humidity, Barometric pressure) (Bosch BME280 at I2C):

Instances Port
bme1, bme2, bme3, bme4 sensor at corresponding SensorPort

Method Action
getValues() returns a tuple of floats with temperature (deg C), humidity (%) and air pressure (hPa)

 

3-axes acceleration sensor (ADXL345 at I2C):

Instances Port
adxl1, adxl2, adxl3, adxl4 sensor at corresponding SensorPort

Method Action
getValues() returns a tuple of floats with accelerations in x-, y- and z-direction (m/s^2 in range -2g .. 2g)

 

Temperature sensor (NXT temperature sensor) :

Instanzen Port
tmp1, tmp2, tmp3, tmp4 sensor at corresponding SensorPort


Method Action
getTemperature() returns the temperature (deg C)

Remark:
This method (with SensorPort.S1) is also available as global function (compatibility to mbrobot)


Infrared sensor (EV3 Infrared sensor with remote module) at SensorPort S1:

Instances Port
irs Infrared sensor at SensorPort S1

Method Action
getCommand()

returns the current command ID for the pressed keys:

0: nothing pressed
1: top-left
2: bottom-left
3: top-right
4: lower-right
5: top-left+top-right
6: top-left+bottom-right
7: bottom-left+top-right
8: bottom-left+bottom-right
9: centre
10: bottom-left+top-left
11: top-right+bottom-right

The red slide switch must be at the top position (selecting SensorPort S1)

 

Internet (EV3 connected via WLAN adapter with an access point):

Function Action
httpGet(url) executes an HTTP GET request and returns the response. url in the form "http://<server>?key=value&key=value&..." or for SSL "https://..."
httpPost(url, content) executes an HTTP POST request and returns the response. url in the form "http://<server>". content in the format "key=value&key=value&..." or for SSL "https://..."
httpDelete(url) executes an HTTP DELETE request with the given resource.
startHTTPServer(handler)

starts an HTTP server (web server on port 80) that listens to HTTP GET requests. For a GET request, the user-defined callback handler(clientIP, filename, params) is called:

 

clientIP: dotted IP address of the client
filename: filename of the URL with the prefix "/". If the filename is missing: "/"
params: dictionary with the GET request parameters {key: value}


Example http://192.168.0.101/on?a=ok&b=3

clientIP = "192.168.0.101"
filename = "/on"
params = {"a" : "ok", "b" : "3"}

The return value consists of comma-separated values (packed into tuple) that are inserted into the %s-format specifications of the saved HTML file before being sent back to the browser. (The number and order must match.) If the return value is missing, the saved HTML file will be returned unchanged.

(The call is not blocking because the server runs in a separate thread.)

saveHTML(text) saves the text as an HTML file, which is sent to the browser after a GET request. It may contain %-format specifications that are replaced with the return values of the callback handler(clientIP, filename, params)

 

Weather data

Function Action
getWeather(city, key)

performs a weather data request on http://openweathermap.org for the selected town. The given authorization key is used. You can get it for free on this host. (If the parameter is omitted, a standard key is used, which allows a maximum of 60 requests / min for all users.)

Return value: Dictionary with the following fields:

"status" "OK" or error string describing the error, e.g. "City not found" (string)
"temp" temperature in °C (float)
"pressure" air pressure in hPa (mbar) (float)
"humidity" air humidity in % (int)
"temp_min" daily minimum temperature in °C (float)
"temp_max" daily maximum temperature in °C (float)
"description" weather conditions (in German) (string)
"sunrise" sunrise in Universal Time (UTC) (string)
"sunset" sunset in Universal Time (UTC) (string)
"datetime" datetime (UTC) of weather recording (string)

 

Local Date/Time

for given city

Function Action
getHTTPDateTime(city, timezone, key)

performs a time/date request to https://api.timezonedb.com for the selected city. The given authorization key is used. You can get it for free from this host. (If the parameter is omitted, a default key is used which allows a maximum of 1 request/sec for all users.) timezone is either the country code or the country name.

Return value: String in format: ww yyyy-nn-dd hh:mm:ss
ww: weekday, yyyy: year, nn: month, dd: day, hh: hours, mm: minutes, ss: seconds
Example: Mo 2018-09-10 20:27:33

If an error occurs, None is returned

getHTTPTime(city, timezone, key) same, but only the time part is returned in the format hh:mm:ss
getCountryCode(country) returns the country code (2 letters) for the given country (written in English)
getCountry(countryCode) returns the country for the given country code
countryCodes Dictionary with code:country

 

home