Week 4 - Section 1 - Ed Py

Site: ΕΛ/ΛΑΚ Moodle
Course: Advanced Coding - June 2017
Book: Week 4 - Section 1 - Ed Py
Printed by: Guest user
Date: Friday, 19 April 2024, 12:03 PM

Description


Chapter 11.1 - Edpy

Until now we have used block icons to program the edison robot. Now we will combine Robotics and Python.

Click on the Edpy link here and open the Edpy application

 

edpy

Documentation

In this window you can search the documentation in the search box and example commands matching your search will appear in the window below.

Recently Opened

This window contains a list of recently opened programs.

Examples

This window contains a list of example programs which you can select and open in the programming area.

Programming Area

This is where you enter the Python code to control the Edison robot.

Check Code

When you select this, the code is checked for errors. If errors are detected, a message will appear in the Compiler Output window at the bottom of the screen providing details on the errors.

Program Edison

When you select this, the current program is downloaded to the Edison provided the Edison is ready.

Compiler Output

This is where errors appear if they are detected when you select “Check Code”. If there are no errors you will see: “There are no errors in your code.” When Python translates the written Python code to actual commands readable by the Edison robot this is called “compiling the code”.

Line help

This window displays help about the command under your cursor.

Chapter 11.2 - Setup Code

Setup Code

All Edison programs must contain the Setup code that you see every time you open the EdPy App.

#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below-----------


As you already learned lines starting with # are comments.

import Ed

The above code imports a library with commands that control edison robot.

Ed.DistanceUnits = Ed.CM

The above code sets the units to cm in driving commands

Ed.Tempo = Ed.TEMPO_MEDIUM

The above code sets the speed of playing music to normal speed.

Chapter 11.3 - Test Program

Select the test program from the Examples window on the left.

The test program looks like this:

#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.TIME Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- while True: Ed.PlayBeep() Ed.LeftLed(Ed.OFF) Ed.RightLed(Ed.ON) Ed.Drive(Ed.SPIN_RIGHT, 5, 350) Ed.TimeWait(20, Ed.TIME_MILLISECONDS) Ed.PlayBeep() Ed.LeftLed(Ed.ON) Ed.RightLed(Ed.OFF) Ed.Drive(Ed.SPIN_LEFT, 5, 350) Ed.TimeWait(20, Ed.TIME_MILLISECONDS)

Edison looks at each line at a time and does what the line says. Blank lines or comment lines are ignored.

Each line here is calling the built-in Edison commands. You can explore each of the commands by searching in the Documentation window.

In Python, commands may have parameters that are passed into them to specify inputs for the command to use. For example, the Ed.RightLed() command takes one input which is telling the LED whether it should turn ON (Ed.ON) or OFF (Ed.OFF).

The Ed.PlayBeep() command has no input parameters. In the Ed.TimeWait() command there are 2 input parameters – the first for the amount of seconds or milliseconds to wait, and the second parameter for the unit of time (milliseconds or seconds).

For an exercise find how many parameter inputs do each of the following commands have:

  • Ed.PlayBeep()
  • Ed.TimeWait()
  • Ed.LeftLed()
  • Ed.DriveRightMotor()

Chapter 11.4 - Driving

Click on Menu and start a New program

Write the following program to drive the Edison robot forward.

#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- Ed.Drive(Ed.FORWARD, Ed.SPEED_5, 8)


As you type in the “Ed”  notice that a box pops up prompting you for what your next command might be. This is a feature of the EdPy App called “command line completion” and it makes it quicker for you to program.

Once you have typed in the above code, press the “Check Code” button to make sure you haven’t made any errors typing. These types of errors are called ‘syntax errors’ because the Python compiler can’t understand words that aren’t part of its language or ‘syntax’.

Ed.Drive() is a function in Python from the Edison Ed library module. Functions are pieces of code that perform a particular ‘function’ depending on which parameters are input. The Drive() function has three input parameters:

  • direction – the direction the Edison will drive
  • speed – the speed the Edison will travel at from 1 to 10 where 10 is the maximum
  • distance – the distance the Edison will travel according to Ed.DistanceUnits (either centimetres as specified by the constant Ed.CM, or inches as specified by the constant Ed.INCH, or time as specified by Ed.TIME.)

Use Activity sheet 2.1 as start and stop markers. 

Measure the distance between the start line and finish line and try and modify your program to make the Edison finish right before the finish line.

Chapter 11.5 - Unlimited

So what if you want to drive your robot without stopping or stop it after an event or after some time. The you have to use the parameter Ed.DISTANCE_UNLIMITED

Copy the following code to edison

#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- Ed.Drive(Ed.FORWARD, Ed.SPEED_6, Ed.DISTANCE_UNLIMITED) Ed.TimeWait(500, Ed.TIME_MILLISECONDS) Ed.Drive(Ed.STOP, Ed.SPEED_10, 0)


With these set of commands edison drives forward with speed 6 for 500 milliseconds or half a second.

Chapter 11.6 - Spin and turn

Copy the following code to edpy

#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- degresToTurn = 90 Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_6, degresToTurn)


Notice that instead of entering the raw value for the third distance parameter, there is the word “degreesToTurn”. This is a variable in Python and it represents a value that has been defined as the value 90. This is called “assigning a value” to a variable.

Variables in Python are places in memory for storing values that are common throughout a program. An advantage of using variables is that if their value changes then it only has to be changed in one line in the code.

Add the following line to your program after the last line and download and run the program.

Ed.Drive(Ed.SPIN_RIGHT, Ed.SPEED_6, degreesToTurn)


Chapter 11.7 - Mini maze

Can you solve the mini maze now using edpy?


minimaze


A solution can be found here